Skip to content

Commit

Permalink
tools: notify user if format-md needs to be run
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Trott authored and danielleadams committed Feb 1, 2022
1 parent ea75ef5 commit e915cec
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
23 changes: 19 additions & 4 deletions tools/lint-md/lint-md.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29361,11 +29361,26 @@ const linter = unified()

paths.forEach(async (path) => {
const file = await read(path);
// We need to calculate `fileContents` before running `linter.process(files)`
// because `linter.process(files)` mutates `file` and returns it as `result`.
// So we won't be able to use `file` after that to see if its contents have
// changed as they will have been altered to the changed version.
const fileContents = file.toString();
const result = await linter.process(file);
const isDifferent = fileContents !== result.toString();
if (format) {
fs.writeFileSync(path, result.toString());
} else if (result.messages.length) {
process.exitCode = 1;
console.error(reporter(result));
if (isDifferent) {
fs.writeFileSync(path, result.toString());
}
} else {
if (isDifferent) {
process.exitCode = 1;
const cmd = process.platform === 'win32' ? 'vcbuild' : 'make';
console.error(`${path} is not formatted. Please run '${cmd} format-md'.`);
}
if (result.messages.length) {
process.exitCode = 1;
console.error(reporter(result));
}
}
});
23 changes: 19 additions & 4 deletions tools/lint-md/lint-md.src.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,26 @@ const linter = unified()

paths.forEach(async (path) => {
const file = await read(path);
// We need to calculate `fileContents` before running `linter.process(files)`
// because `linter.process(files)` mutates `file` and returns it as `result`.
// So we won't be able to use `file` after that to see if its contents have
// changed as they will have been altered to the changed version.
const fileContents = file.toString();
const result = await linter.process(file);
const isDifferent = fileContents !== result.toString();
if (format) {
fs.writeFileSync(path, result.toString());
} else if (result.messages.length) {
process.exitCode = 1;
console.error(reporter(result));
if (isDifferent) {
fs.writeFileSync(path, result.toString());
}
} else {
if (isDifferent) {
process.exitCode = 1;
const cmd = process.platform === 'win32' ? 'vcbuild' : 'make';
console.error(`${path} is not formatted. Please run '${cmd} format-md'.`);
}
if (result.messages.length) {
process.exitCode = 1;
console.error(reporter(result));
}
}
});

0 comments on commit e915cec

Please sign in to comment.