All files / api lint.js

66.67% Statements 20/30
38.46% Branches 5/13
71.43% Functions 5/7
70.37% Lines 19/27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74              1x                                     3x       3x   3x 3x   3x 3x 3x         3x 3x       3x 3x 10x 3x   3x 3x 3x                   3x         3x        
import 'colors';
import debug from 'debug';
import { spawn as yarnOrNPMSpawn } from 'yarn-or-npm';
 
import asyncOra from '../util/ora-handler';
import resolveDir from '../util/resolve-dir';
 
const d = debug('electron-forge:lint');
 
/**
 * @typedef {Object} LintOptions
 * @property {string} [dir=process.cwd()] The path to the module to import
 * @property {boolean} [interactive=false] Whether to use sensible defaults or prompt the user visually
 */
 
/**
 * Lint a local Electron application.
 *
 * The promise will be rejected with the stdout+stderr of the linting process if linting fails or
 * will be resolved if it succeeds.
 *
 * @param {LintOptions} providedOptions - Options for the Lint method
 * @return {Promise<null, string>} Will resolve when the lint process is complete
 */
export default async (providedOptions = {}) => {
  // eslint-disable-next-line prefer-const, no-unused-vars
  let { dir, interactive } = Object.assign({
    dir: process.cwd(),
    interactive: false,
  }, providedOptions);
  asyncOra.interactive = interactive;
 
  let success = true;
  let result = null;
 
  await asyncOra('Linting Application', async (lintSpinner) => {
    dir = await resolveDir(dir);
    Iif (!dir) {
      // eslint-disable-next-line no-throw-literal
      throw 'Failed to locate lintable Electron application';
    }
 
    d('executing "run lint -- --color" in dir:', dir);
    const child = yarnOrNPMSpawn(['run', 'lint', '--', '--color'], {
      stdio: process.platform === 'win32' ? 'inherit' : 'pipe',
      cwd: dir,
    });
    const output = [];
    Eif (process.platform !== 'win32') {
      child.stdout.on('data', data => output.push(data.toString()));
      child.stderr.on('data', data => output.push(data.toString().red));
    }
    await new Promise((resolve) => {
      child.on('exit', (code) => {
        Iif (code !== 0) {
          success = false;
          lintSpinner.fail();
          if (interactive) {
            output.forEach(data => process.stdout.write(data));
            process.exit(code);
          } else {
            result = output.join('');
          }
        }
        resolve();
      });
    });
  });
 
  Iif (!success) {
    throw result;
  }
};