From 5daa31321547f35f773c5bb187a3ae976f318293 Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Thu, 28 Oct 2021 19:20:12 -0700 Subject: [PATCH] tools: notify user if format-md needs to be run MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This will help enforce formatting of markdown files. PR-URL: https://github.com/nodejs/node/pull/40647 Reviewed-By: Antoine du Hamel Reviewed-By: Michaƫl Zasso --- tools/lint-md/lint-md.mjs | 23 +++++++++++++++++++---- tools/lint-md/lint-md.src.mjs | 23 +++++++++++++++++++---- 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/tools/lint-md/lint-md.mjs b/tools/lint-md/lint-md.mjs index baecd0a3e2d447..b1a596d4368dfd 100644 --- a/tools/lint-md/lint-md.mjs +++ b/tools/lint-md/lint-md.mjs @@ -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)); + } } }); diff --git a/tools/lint-md/lint-md.src.mjs b/tools/lint-md/lint-md.src.mjs index 258d341f3b5967..4116d7e803b64e 100644 --- a/tools/lint-md/lint-md.src.mjs +++ b/tools/lint-md/lint-md.src.mjs @@ -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)); + } } });