[0c6b92a] | 1 | #!/usr/bin/env node
|
---|
| 2 |
|
---|
| 3 | const spawn = require('child_process').spawnSync;
|
---|
| 4 |
|
---|
| 5 | const filesToCheck = '*.js';
|
---|
| 6 | const FORMAT_START = process.env.FORMAT_START || 'main';
|
---|
| 7 | const IS_WIN = process.platform === 'win32';
|
---|
| 8 | const ESLINT_PATH = IS_WIN ? 'node_modules\\.bin\\eslint.cmd' : 'node_modules/.bin/eslint';
|
---|
| 9 |
|
---|
| 10 | function main (args) {
|
---|
| 11 | let fix = false;
|
---|
| 12 | while (args.length > 0) {
|
---|
| 13 | switch (args[0]) {
|
---|
| 14 | case '-f':
|
---|
| 15 | case '--fix':
|
---|
| 16 | fix = true;
|
---|
| 17 | break;
|
---|
| 18 | default:
|
---|
| 19 | }
|
---|
| 20 | args.shift();
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | // Check js files that change on unstaged file
|
---|
| 24 | const fileUnStaged = spawn(
|
---|
| 25 | 'git',
|
---|
| 26 | ['diff', '--name-only', '--diff-filter=d', FORMAT_START, filesToCheck],
|
---|
| 27 | {
|
---|
| 28 | encoding: 'utf-8'
|
---|
| 29 | }
|
---|
| 30 | );
|
---|
| 31 |
|
---|
| 32 | // Check js files that change on staged file
|
---|
| 33 | const fileStaged = spawn(
|
---|
| 34 | 'git',
|
---|
| 35 | ['diff', '--name-only', '--cached', '--diff-filter=d', FORMAT_START, filesToCheck],
|
---|
| 36 | {
|
---|
| 37 | encoding: 'utf-8'
|
---|
| 38 | }
|
---|
| 39 | );
|
---|
| 40 |
|
---|
| 41 | const options = [
|
---|
| 42 | ...fileStaged.stdout.split('\n').filter((f) => f !== ''),
|
---|
| 43 | ...fileUnStaged.stdout.split('\n').filter((f) => f !== '')
|
---|
| 44 | ];
|
---|
| 45 |
|
---|
| 46 | if (fix) {
|
---|
| 47 | options.push('--fix');
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | const result = spawn(ESLINT_PATH, [...options], {
|
---|
| 51 | encoding: 'utf-8'
|
---|
| 52 | });
|
---|
| 53 |
|
---|
| 54 | if (result.error && result.error.errno === 'ENOENT') {
|
---|
| 55 | console.error('Eslint not found! Eslint is supposed to be found at ', ESLINT_PATH);
|
---|
| 56 | return 2;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | if (result.status === 1) {
|
---|
| 60 | console.error('Eslint error:', result.stdout);
|
---|
| 61 | const fixCmd = 'npm run lint:fix';
|
---|
| 62 | console.error(`ERROR: please run "${fixCmd}" to format changes in your commit
|
---|
| 63 | Note that when running the command locally, please keep your local
|
---|
| 64 | main branch and working branch up to date with nodejs/node-addon-api
|
---|
| 65 | to exclude un-related complains.
|
---|
| 66 | Or you can run "env FORMAT_START=upstream/main ${fixCmd}".
|
---|
| 67 | Also fix JS files by yourself if necessary.`);
|
---|
| 68 | return 1;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | if (result.stderr) {
|
---|
| 72 | console.error('Error running eslint:', result.stderr);
|
---|
| 73 | return 2;
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | if (require.main === module) {
|
---|
| 78 | process.exitCode = main(process.argv.slice(2));
|
---|
| 79 | }
|
---|