diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index ebe1748..a199829 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -21,4 +21,4 @@ jobs: with: node-version: ${{ matrix.node-version }} - run: npm install - - run: CLANG_FORMAT_START=refs/remotes/origin/main npm run lint + - run: FORMAT_START=refs/remotes/origin/main npm run lint diff --git a/package.json b/package.json index 03519e9..4e79ae3 100644 --- a/package.json +++ b/package.json @@ -377,8 +377,8 @@ "predev:incremental": "node-gyp configure build -C test --debug", "dev:incremental": "node test", "doc": "doxygen doc/Doxyfile", - "lint": "eslint $(git diff --name-only refs/remotes/origin/main '**/*.js' | xargs) && node tools/clang-format", - "lint:fix": "node tools/clang-format --fix && eslint --fix $(git diff --cached --name-only '**/*.js' | xargs && git diff --name-only '**/*.js' | xargs)" + "lint": "node tools/eslint-format && node tools/clang-format", + "lint:fix": "node tools/clang-format --fix && node tools/eslint-format --fix" }, "pre-commit": "lint", "version": "4.2.0", diff --git a/tools/clang-format.js b/tools/clang-format.js index 997733e..b76d89b 100644 --- a/tools/clang-format.js +++ b/tools/clang-format.js @@ -4,7 +4,7 @@ const spawn = require('child_process').spawnSync; const path = require('path'); const filesToCheck = ['*.h', '*.cc']; -const CLANG_FORMAT_START = process.env.CLANG_FORMAT_START || 'main'; +const FORMAT_START = process.env.FORMAT_START || 'main'; function main (args) { let fix = false; @@ -22,19 +22,17 @@ function main (args) { const clangFormatPath = path.dirname(require.resolve('clang-format')); const options = ['--binary=node_modules/.bin/clang-format', '--style=file']; if (fix) { - options.push(CLANG_FORMAT_START); + options.push(FORMAT_START); } else { - options.push('--diff', CLANG_FORMAT_START); + options.push('--diff', FORMAT_START); } - const gitClangFormatPath = path.join(clangFormatPath, - 'bin/git-clang-format'); - const result = spawn('python', [ - gitClangFormatPath, - ...options, - '--', - ...filesToCheck - ], { encoding: 'utf-8' }); + const gitClangFormatPath = path.join(clangFormatPath, 'bin/git-clang-format'); + const result = spawn( + 'python', + [gitClangFormatPath, ...options, '--', ...filesToCheck], + { encoding: 'utf-8' } + ); if (result.stderr) { console.error('Error running git-clang-format:', result.stderr); @@ -48,9 +46,11 @@ function main (args) { return 0; } // Detect if there is any complains from clang-format - if (clangFormatOutput !== '' && - clangFormatOutput !== ('no modified files to format') && - clangFormatOutput !== ('clang-format did not modify any files')) { + if ( + clangFormatOutput !== '' && + clangFormatOutput !== 'no modified files to format' && + clangFormatOutput !== 'clang-format did not modify any files' + ) { console.error(clangFormatOutput); const fixCmd = 'npm run lint:fix'; console.error(` @@ -58,7 +58,7 @@ function main (args) { Note that when running the command locally, please keep your local main branch and working branch up to date with nodejs/node-addon-api to exclude un-related complains. - Or you can run "env CLANG_FORMAT_START=upstream/main ${fixCmd}".`); + Or you can run "env FORMAT_START=upstream/main ${fixCmd}".`); return 1; } } diff --git a/tools/eslint-format.js b/tools/eslint-format.js new file mode 100644 index 0000000..5938835 --- /dev/null +++ b/tools/eslint-format.js @@ -0,0 +1,71 @@ +#!/usr/bin/env node + +const spawn = require('child_process').spawnSync; + +const filesToCheck = '*.js'; +const FORMAT_START = process.env.FORMAT_START || 'main'; + +function main (args) { + let fix = false; + while (args.length > 0) { + switch (args[0]) { + case '-f': + case '--fix': + fix = true; + break; + default: + } + args.shift(); + } + + // Check js files that change on unstaged file + const fileUnStaged = spawn( + 'git', + ['diff', '--name-only', FORMAT_START, filesToCheck], + { + encoding: 'utf-8' + } + ); + + // Check js files that change on staged file + const fileStaged = spawn( + 'git', + ['diff', '--name-only', '--cached', FORMAT_START, filesToCheck], + { + encoding: 'utf-8' + } + ); + + const options = [ + ...fileStaged.stdout.split('\n').filter((f) => f !== ''), + ...fileUnStaged.stdout.split('\n').filter((f) => f !== '') + ]; + + if (fix) { + options.push('--fix'); + } + const result = spawn('node_modules/.bin/eslint', [...options], { + encoding: 'utf-8' + }); + + if (result.status === 1) { + console.error('Eslint error:', result.stdout); + const fixCmd = 'npm run lint:fix'; + console.error(`ERROR: please run "${fixCmd}" to format changes in your commit + Note that when running the command locally, please keep your local + main branch and working branch up to date with nodejs/node-addon-api + to exclude un-related complains. + Or you can run "env FORMAT_START=upstream/main ${fixCmd}". + Also fix JS files by yourself if necessary.`); + return 1; + } + + if (result.stderr) { + console.error('Error running eslint:', result.stderr); + return 2; + } +} + +if (require.main === module) { + process.exitCode = main(process.argv.slice(2)); +}