@@ -15,7 +15,10 @@ const glob = require('glob');
15
15
const path = require ( 'path' ) ;
16
16
const execFileSync = require ( 'child_process' ) . execFileSync ;
17
17
18
- const shouldWrite = process . argv [ 2 ] === 'write' ;
18
+ const mode = process . argv [ 2 ] || 'check' ;
19
+ const shouldWrite = mode === 'write' || mode === 'write-changed' ;
20
+ const onlyChanged = mode === 'check-changed' || mode === 'write-changed' ;
21
+
19
22
const isWindows = process . platform === 'win32' ;
20
23
const prettier = isWindows ? 'prettier.cmd' : 'prettier' ;
21
24
const prettierCmd = path . resolve (
@@ -49,6 +52,17 @@ function exec(command, args) {
49
52
return execFileSync ( command , args , options ) . toString ( ) ;
50
53
}
51
54
55
+ var mergeBase = exec ( 'git' , [ 'merge-base' , 'HEAD' , 'master' ] ) . trim ( ) ;
56
+ var changedFiles = new Set (
57
+ exec ( 'git' , [
58
+ 'diff' ,
59
+ '-z' ,
60
+ '--name-only' ,
61
+ '--diff-filter=ACMRTUB' ,
62
+ mergeBase ,
63
+ ] ) . match ( / [ ^ \0 ] + / g)
64
+ ) ;
65
+
52
66
Object . keys ( config ) . forEach ( key => {
53
67
const patterns = config [ key ] . patterns ;
54
68
const options = config [ key ] . options ;
@@ -57,7 +71,13 @@ Object.keys(config).forEach(key => {
57
71
const globPattern = patterns . length > 1
58
72
? `{${ patterns . join ( ',' ) } }`
59
73
: `${ patterns . join ( ',' ) } ` ;
60
- const files = glob . sync ( globPattern , { ignore} ) ;
74
+ const files = glob
75
+ . sync ( globPattern , { ignore} )
76
+ . filter ( f => ! onlyChanged || changedFiles . has ( f ) ) ;
77
+
78
+ if ( ! files . length ) {
79
+ return ;
80
+ }
61
81
62
82
const args = Object . keys ( defaultOptions ) . map (
63
83
k => `--${ k } =${ ( options && options [ k ] ) || defaultOptions [ k ] } `
0 commit comments