@@ -7,17 +7,31 @@ const filesToCheck = ['*.h', '*.cc'];
7
7
const CLANG_FORMAT_START = process . env . CLANG_FORMAT_START || 'main' ;
8
8
9
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
+
10
21
let clangFormatPath = path . dirname ( require . resolve ( 'clang-format' ) ) ;
11
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
+ }
12
28
13
29
const gitClangFormatPath = path . join ( clangFormatPath ,
14
30
'bin/git-clang-format' ) ;
15
31
const result = spawn ( 'python' , [
16
32
gitClangFormatPath ,
17
33
...options ,
18
- '--diff' ,
19
- CLANG_FORMAT_START ,
20
- 'HEAD' ,
34
+ '--' ,
21
35
...filesToCheck
22
36
] , { encoding : 'utf-8' } ) ;
23
37
@@ -27,13 +41,19 @@ function main(args) {
27
41
}
28
42
29
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
30
50
if ( clangFormatOutput !== '' &&
31
51
clangFormatOutput !== ( 'no modified files to format' ) &&
32
52
clangFormatOutput !== ( 'clang-format did not modify any files' ) ) {
33
53
console . error ( clangFormatOutput ) ;
34
- const fixCmd = '" npm run lint:fix" ' ;
54
+ const fixCmd = 'npm run lint:fix' ;
35
55
console . error ( `
36
- ERROR: please run ${ fixCmd } to format changes in your commit
56
+ ERROR: please run " ${ fixCmd } " to format changes in your commit
37
57
Note that when running the command locally, please keep your local
38
58
main branch and working branch up to date with nodejs/node-addon-api
39
59
to exclude un-related complains.
0 commit comments