Skip to content

Commit 5daa313

Browse files
Trotttargos
authored andcommitted
tools: notify user if format-md needs to be run
This will help enforce formatting of markdown files. PR-URL: #40647 Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent ecccf48 commit 5daa313

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

tools/lint-md/lint-md.mjs

+19-4
Original file line numberDiff line numberDiff line change
@@ -29361,11 +29361,26 @@ const linter = unified()
2936129361

2936229362
paths.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
});

tools/lint-md/lint-md.src.mjs

+19-4
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,26 @@ const linter = unified()
2828

2929
paths.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
});

0 commit comments

Comments
 (0)