/ script / lib / lint-java-script-paths.js
lint-java-script-paths.js
 1  'use strict';
 2  
 3  const path = require('path');
 4  const { spawn } = require('child_process');
 5  const process = require('process');
 6  
 7  const CONFIG = require('../config');
 8  
 9  module.exports = async function() {
10    return new Promise((resolve, reject) => {
11      const eslintArgs = ['--cache', '--format', 'json'];
12  
13      if (process.argv.includes('--fix')) {
14        eslintArgs.push('--fix');
15      }
16  
17      const eslintBinary = process.platform === 'win32' ? 'eslint.cmd' : 'eslint';
18      const eslint = spawn(
19        path.join('script', 'node_modules', '.bin', eslintBinary),
20        [...eslintArgs, '.'],
21        { cwd: CONFIG.repositoryRootPath }
22      );
23  
24      let output = '';
25      let errorOutput = '';
26      eslint.stdout.on('data', data => {
27        output += data.toString();
28      });
29  
30      eslint.stderr.on('data', data => {
31        errorOutput += data.toString();
32      });
33  
34      eslint.on('error', error => reject(error));
35      eslint.on('close', exitCode => {
36        const errors = [];
37        let files;
38  
39        try {
40          files = JSON.parse(output);
41        } catch (_) {
42          reject(errorOutput);
43          return;
44        }
45  
46        for (const file of files) {
47          for (const error of file.messages) {
48            errors.push({
49              path: file.filePath,
50              message: error.message,
51              lineNumber: error.line,
52              rule: error.ruleId
53            });
54          }
55        }
56  
57        resolve(errors);
58      });
59    });
60  };