@@ -7,17 +7,31 @@ const filesToCheck = ['*.h', '*.cc'];
77const CLANG_FORMAT_START = process . env . CLANG_FORMAT_START || 'main' ;
88
99function 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+
1021 let clangFormatPath = path . dirname ( require . resolve ( 'clang-format' ) ) ;
1122 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+ }
1228
1329 const gitClangFormatPath = path . join ( clangFormatPath ,
1430 'bin/git-clang-format' ) ;
1531 const result = spawn ( 'python' , [
1632 gitClangFormatPath ,
1733 ...options ,
18- '--diff' ,
19- CLANG_FORMAT_START ,
20- 'HEAD' ,
34+ '--' ,
2135 ...filesToCheck
2236 ] , { encoding : 'utf-8' } ) ;
2337
@@ -27,13 +41,19 @@ function main(args) {
2741 }
2842
2943 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
3050 if ( clangFormatOutput !== '' &&
3151 clangFormatOutput !== ( 'no modified files to format' ) &&
3252 clangFormatOutput !== ( 'clang-format did not modify any files' ) ) {
3353 console . error ( clangFormatOutput ) ;
34- const fixCmd = '" npm run lint:fix" ' ;
54+ const fixCmd = 'npm run lint:fix' ;
3555 console . error ( `
36- ERROR: please run ${ fixCmd } to format changes in your commit
56+ ERROR: please run " ${ fixCmd } " to format changes in your commit
3757 Note that when running the command locally, please keep your local
3858 main branch and working branch up to date with nodejs/node-addon-api
3959 to exclude un-related complains.
0 commit comments