File tree Expand file tree Collapse file tree 2 files changed +38
-8
lines changed Expand file tree Collapse file tree 2 files changed +38
-8
lines changed Original file line number Diff line number Diff line change @@ -29361,11 +29361,26 @@ const linter = unified()
2936129361
2936229362paths.forEach(async (path) => {
2936329363 const file = await read(path);
29364+ // We need to calculate `fileContents` before running `linter.process(files)`
29365+ // because `linter.process(files)` mutates `file` and returns it as `result`.
29366+ // So we won't be able to use `file` after that to see if its contents have
29367+ // changed as they will have been altered to the changed version.
29368+ const fileContents = file.toString();
2936429369 const result = await linter.process(file);
29370+ const isDifferent = fileContents !== result.toString();
2936529371 if (format) {
29366- fs.writeFileSync(path, result.toString());
29367- } else if (result.messages.length) {
29368- process.exitCode = 1;
29369- console.error(reporter(result));
29372+ if (isDifferent) {
29373+ fs.writeFileSync(path, result.toString());
29374+ }
29375+ } else {
29376+ if (isDifferent) {
29377+ process.exitCode = 1;
29378+ const cmd = process.platform === 'win32' ? 'vcbuild' : 'make';
29379+ console.error(`${path} is not formatted. Please run '${cmd} format-md'.`);
29380+ }
29381+ if (result.messages.length) {
29382+ process.exitCode = 1;
29383+ console.error(reporter(result));
29384+ }
2937029385 }
2937129386});
Original file line number Diff line number Diff line change @@ -28,11 +28,26 @@ const linter = unified()
2828
2929paths . forEach ( async ( path ) => {
3030 const file = await read ( path ) ;
31+ // We need to calculate `fileContents` before running `linter.process(files)`
32+ // because `linter.process(files)` mutates `file` and returns it as `result`.
33+ // So we won't be able to use `file` after that to see if its contents have
34+ // changed as they will have been altered to the changed version.
35+ const fileContents = file . toString ( ) ;
3136 const result = await linter . process ( file ) ;
37+ const isDifferent = fileContents !== result . toString ( ) ;
3238 if ( format ) {
33- fs . writeFileSync ( path , result . toString ( ) ) ;
34- } else if ( result . messages . length ) {
35- process . exitCode = 1 ;
36- console . error ( reporter ( result ) ) ;
39+ if ( isDifferent ) {
40+ fs . writeFileSync ( path , result . toString ( ) ) ;
41+ }
42+ } else {
43+ if ( isDifferent ) {
44+ process . exitCode = 1 ;
45+ const cmd = process . platform === 'win32' ? 'vcbuild' : 'make' ;
46+ console . error ( `${ path } is not formatted. Please run '${ cmd } format-md'.` ) ;
47+ }
48+ if ( result . messages . length ) {
49+ process . exitCode = 1 ;
50+ console . error ( reporter ( result ) ) ;
51+ }
3752 }
3853} ) ;
You can’t perform that action at this time.
0 commit comments