[e29cc2e] | 1 | #!/usr/bin/env node
|
---|
| 2 |
|
---|
| 3 | const spawn = require('child_process').spawnSync;
|
---|
| 4 | const path = require('path');
|
---|
| 5 |
|
---|
| 6 | const filesToCheck = ['*.h', '*.cc'];
|
---|
| 7 | const CLANG_FORMAT_START = process.env.CLANG_FORMAT_START || 'main';
|
---|
| 8 |
|
---|
| 9 | function main(args) {
|
---|
| 10 | let fix = false;
|
---|
| 11 | while (args.length > 0) {
|
---|
| 12 | switch (args[0]) {
|
---|
| 13 | case '-f':
|
---|
| 14 | case '--fix':
|
---|
| 15 | fix = true;
|
---|
| 16 | default:
|
---|
| 17 | }
|
---|
| 18 | args.shift();
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | let clangFormatPath = path.dirname(require.resolve('clang-format'));
|
---|
| 22 | const options = ['--binary=node_modules/.bin/clang-format', '--style=file'];
|
---|
| 23 | if (fix) {
|
---|
| 24 | options.push(CLANG_FORMAT_START);
|
---|
| 25 | } else {
|
---|
| 26 | options.push('--diff', CLANG_FORMAT_START);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | const gitClangFormatPath = path.join(clangFormatPath,
|
---|
| 30 | 'bin/git-clang-format');
|
---|
| 31 | const result = spawn('python', [
|
---|
| 32 | gitClangFormatPath,
|
---|
| 33 | ...options,
|
---|
| 34 | '--',
|
---|
| 35 | ...filesToCheck
|
---|
| 36 | ], { encoding: 'utf-8' });
|
---|
| 37 |
|
---|
| 38 | if (result.stderr) {
|
---|
| 39 | console.error('Error running git-clang-format:', result.stderr);
|
---|
| 40 | return 2;
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | const clangFormatOutput = result.stdout.trim();
|
---|
| 44 | // Bail fast if in fix mode.
|
---|
| 45 | if (fix) {
|
---|
| 46 | console.log(clangFormatOutput);
|
---|
| 47 | return 0;
|
---|
| 48 | }
|
---|
| 49 | // Detect if there is any complains from clang-format
|
---|
| 50 | if (clangFormatOutput !== '' &&
|
---|
| 51 | clangFormatOutput !== ('no modified files to format') &&
|
---|
| 52 | clangFormatOutput !== ('clang-format did not modify any files')) {
|
---|
| 53 | console.error(clangFormatOutput);
|
---|
| 54 | const fixCmd = 'npm run lint:fix';
|
---|
| 55 | console.error(`
|
---|
| 56 | ERROR: please run "${fixCmd}" to format changes in your commit
|
---|
| 57 | Note that when running the command locally, please keep your local
|
---|
| 58 | main branch and working branch up to date with nodejs/node-addon-api
|
---|
| 59 | to exclude un-related complains.
|
---|
| 60 | Or you can run "env CLANG_FORMAT_START=upstream/main ${fixCmd}".`);
|
---|
| 61 | return 1;
|
---|
| 62 | }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | if (require.main === module) {
|
---|
| 66 | process.exitCode = main(process.argv.slice(2));
|
---|
| 67 | }
|
---|