From 051e9efe7c9e113dce9a4157e65fe597fdb347c1 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 16 Mar 2022 18:11:34 +0800 Subject: [PATCH 01/10] Don't use `logger` to print information --- .eslintrc.js | 1 + src/cli/file-info.js | 2 +- src/cli/find-config-path.js | 2 +- src/cli/index.js | 8 +-- tests/integration/__tests__/loglevel.js | 74 +++++++++++++++++++++++++ tests/integration/run-prettier.js | 4 +- 6 files changed, 84 insertions(+), 7 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 45ea5747375b..798d8c2a2b65 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -222,6 +222,7 @@ module.exports = { patterns: ["../"], }, ], + "no-console": "off", }, }, { diff --git a/src/cli/file-info.js b/src/cli/file-info.js index 8dc0f2d9ee06..00bbd16387a8 100644 --- a/src/cli/file-info.js +++ b/src/cli/file-info.js @@ -22,7 +22,7 @@ async function logFileInfoOrDie(context) { resolveConfig: config !== false, }); - context.logger.log(prettier.format(stringify(fileInfo), { parser: "json" })); + console.log(prettier.format(stringify(fileInfo), { parser: "json" })); } module.exports = logFileInfoOrDie; diff --git a/src/cli/find-config-path.js b/src/cli/find-config-path.js index 05283fe3b52e..224b04418be3 100644 --- a/src/cli/find-config-path.js +++ b/src/cli/find-config-path.js @@ -9,7 +9,7 @@ async function logResolvedConfigPathOrDie(context) { const file = context.argv.findConfigPath; const configFile = await prettier.resolveConfigFile(file); if (configFile) { - context.logger.log(path.relative(process.cwd(), configFile)); + console.log(path.relative(process.cwd(), configFile)); } else { throw new Error(`Can not find configure file for "${file}"`); } diff --git a/src/cli/index.js b/src/cli/index.js index 8017c615d86a..2f11dbaf52b4 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -69,12 +69,12 @@ async function main(rawArguments, logger) { } if (context.argv.version) { - logger.log(prettier.version); + console.log(prettier.version); return; } if (context.argv.help !== undefined) { - logger.log( + console.log( typeof context.argv.help === "string" && context.argv.help !== "" ? createDetailedUsage(context, context.argv.help) : createUsage(context) @@ -83,7 +83,7 @@ async function main(rawArguments, logger) { } if (context.argv.supportInfo) { - logger.log( + console.log( prettier.format(stringify(prettier.getSupportInfo()), { parser: "json", }) @@ -104,8 +104,8 @@ async function main(rawArguments, logger) { } else if (hasFilePatterns) { await formatFiles(context); } else { - logger.log(createUsage(context)); process.exitCode = 1; + console.log(createUsage(context)); } } diff --git a/tests/integration/__tests__/loglevel.js b/tests/integration/__tests__/loglevel.js index 51d745e38d50..c9e070254ee7 100644 --- a/tests/integration/__tests__/loglevel.js +++ b/tests/integration/__tests__/loglevel.js @@ -37,6 +37,80 @@ describe("Should use default level logger to log `--loglevel` error", () => { }); }); +describe("loglevel should not effect information print", () => { + for (const { argv, runOptions, assertOptions } of [ + { + argv: ["--version"], + assertOptions: { + stdout(value) { + expect(value).not.toBe(""); + }, + }, + }, + { + argv: ["--help"], + assertOptions: { + stdout(value) { + expect(value.includes("-v, --version")).toBe(true); + }, + }, + }, + { + argv: ["--help", "write"], + assertOptions: { + stdout(value) { + expect(value.startsWith("-w, --write")).toBe(true); + }, + }, + }, + { + argv: ["--support-info"], + assertOptions: { + stdout(value) { + expect(JSON.parse(value)).toBeDefined(); + }, + }, + }, + { + argv: ["--find-config-path", "any-file"], + assertOptions: { + stdout: ".prettierrc", + }, + }, + { + argv: ["--file-info", "any-js-file.js"], + assertOptions: { + stdout: { ignored: false, inferredParser: "babel" }, + }, + }, + { + argv: [], + runOptions: { isTTY: true }, + assertOptions: { + status: "non-zero", + stdout(value) { + expect(value.includes("-v, --version")).toBe(true); + }, + }, + }, + { + argv: ["--parser", "babel"], + runOptions: { input: "foo" }, + assertOptions: { stdout: "foo;\n" }, + }, + ]) { + runPrettier("cli/loglevel", ["--loglevel", "silent", ...argv], { + ...runOptions, + title: argv.join(" "), + }).test({ + stderr: "", + status: 0, + write: [], + ...assertOptions, + }); + } +}); + async function runPrettierWithLogLevel(logLevel, patterns) { const result = await runPrettier("cli/loglevel", [ "--loglevel", diff --git a/tests/integration/run-prettier.js b/tests/integration/run-prettier.js index e2e813887a6b..68cde716ea49 100644 --- a/tests/integration/run-prettier.js +++ b/tests/integration/run-prettier.js @@ -178,7 +178,7 @@ function runPrettier(dir, args = [], options = {}) { function testResult(testOptions) { for (const name of ["status", "stdout", "stderr", "write"]) { - test(`(${name})`, async () => { + test(`${options.title || ""}(${name})`, async () => { const result = await runCli(); const value = // \r is trimmed from jest snapshots by default; @@ -192,6 +192,8 @@ function runPrettier(dir, args = [], options = {}) { if (name in testOptions) { if (name === "status" && testOptions[name] === "non-zero") { expect(value).not.toBe(0); + } else if (typeof testOptions[name] === "function") { + testOptions[name](value); } else { expect(value).toEqual(testOptions[name]); } From 342f7b1306b4b942dd9b3cbb3b27583ebb42cc39 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 16 Mar 2022 18:34:09 +0800 Subject: [PATCH 02/10] Add changelog --- changelog_unreleased/cli/12477.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 changelog_unreleased/cli/12477.md diff --git a/changelog_unreleased/cli/12477.md b/changelog_unreleased/cli/12477.md new file mode 100644 index 000000000000..e32a453efca4 --- /dev/null +++ b/changelog_unreleased/cli/12477.md @@ -0,0 +1,14 @@ +#### Ignore `loglevel` when printing information + + +```bash +# Prettier stable +prettier --loglevel silent --help no-color +# Nothing printed + +// Prettier main +prettier --loglevel silent --help no-color +#--no-color +# +# Do not colorize error messages. +``` From b47ca82900daeb0fbfeb8f08fc54f6241dd29c42 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 16 Mar 2022 18:35:54 +0800 Subject: [PATCH 03/10] Update example --- changelog_unreleased/cli/12477.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/changelog_unreleased/cli/12477.md b/changelog_unreleased/cli/12477.md index e32a453efca4..71f0fac73452 100644 --- a/changelog_unreleased/cli/12477.md +++ b/changelog_unreleased/cli/12477.md @@ -3,12 +3,10 @@ ```bash # Prettier stable -prettier --loglevel silent --help no-color +prettier --loglevel silent --find-config-path index.js # Nothing printed // Prettier main prettier --loglevel silent --help no-color -#--no-color -# -# Do not colorize error messages. +# .prettierrc ``` From 724fc5424baa505550d9c174cbb27ab8f8e40842 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 16 Mar 2022 18:56:55 +0800 Subject: [PATCH 04/10] Fix --- changelog_unreleased/cli/12477.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog_unreleased/cli/12477.md b/changelog_unreleased/cli/12477.md index 71f0fac73452..1606da8e7947 100644 --- a/changelog_unreleased/cli/12477.md +++ b/changelog_unreleased/cli/12477.md @@ -1,4 +1,4 @@ -#### Ignore `loglevel` when printing information +#### Ignore `loglevel` when printing information (#12477 by @fisker) ```bash From 927f715f1c6adbab74d392f4db8bfca1b36347b2 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 16 Mar 2022 19:27:26 +0800 Subject: [PATCH 05/10] Fix test --- tests/integration/__tests__/loglevel.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/integration/__tests__/loglevel.js b/tests/integration/__tests__/loglevel.js index c9e070254ee7..ba0c587aab1f 100644 --- a/tests/integration/__tests__/loglevel.js +++ b/tests/integration/__tests__/loglevel.js @@ -74,13 +74,15 @@ describe("loglevel should not effect information print", () => { { argv: ["--find-config-path", "any-file"], assertOptions: { - stdout: ".prettierrc", + stdout: ".prettierrc\n", }, }, { argv: ["--file-info", "any-js-file.js"], assertOptions: { - stdout: { ignored: false, inferredParser: "babel" }, + stdout(value) { + expect(JSON.parse(value)).toEqual({ ignored: false, inferredParser: "babel" }); + }, }, }, { From 4abdff9fe02264f463b88022f2b4a94f791bf607 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 17 Mar 2022 09:27:31 +0800 Subject: [PATCH 06/10] Linting --- tests/integration/__tests__/loglevel.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/integration/__tests__/loglevel.js b/tests/integration/__tests__/loglevel.js index ba0c587aab1f..53ad29937d45 100644 --- a/tests/integration/__tests__/loglevel.js +++ b/tests/integration/__tests__/loglevel.js @@ -81,7 +81,10 @@ describe("loglevel should not effect information print", () => { argv: ["--file-info", "any-js-file.js"], assertOptions: { stdout(value) { - expect(JSON.parse(value)).toEqual({ ignored: false, inferredParser: "babel" }); + expect(JSON.parse(value)).toEqual({ + ignored: false, + inferredParser: "babel", + }); }, }, }, From 14c1976b3d75cccd93116bf8cc7a4ba322538fcc Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sun, 20 Mar 2022 13:49:45 +0800 Subject: [PATCH 07/10] [skip ci] Fix changelog --- changelog_unreleased/cli/12477.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog_unreleased/cli/12477.md b/changelog_unreleased/cli/12477.md index 1606da8e7947..40b537db3c80 100644 --- a/changelog_unreleased/cli/12477.md +++ b/changelog_unreleased/cli/12477.md @@ -6,7 +6,7 @@ prettier --loglevel silent --find-config-path index.js # Nothing printed -// Prettier main +# Prettier main prettier --loglevel silent --help no-color # .prettierrc ``` From d26bfc3e1070b74fcbce22ced6c43a5402a8585e Mon Sep 17 00:00:00 2001 From: fisker Date: Sun, 20 Mar 2022 14:30:16 +0800 Subject: [PATCH 08/10] Add `printToScreen` --- .eslintrc.js | 1 - src/cli/file-info.js | 3 ++- src/cli/find-config-path.js | 3 ++- src/cli/index.js | 9 +++++---- src/cli/utils.js | 7 +++++++ 5 files changed, 16 insertions(+), 7 deletions(-) create mode 100644 src/cli/utils.js diff --git a/.eslintrc.js b/.eslintrc.js index 798d8c2a2b65..45ea5747375b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -222,7 +222,6 @@ module.exports = { patterns: ["../"], }, ], - "no-console": "off", }, }, { diff --git a/src/cli/file-info.js b/src/cli/file-info.js index 00bbd16387a8..c13c8243b1da 100644 --- a/src/cli/file-info.js +++ b/src/cli/file-info.js @@ -3,6 +3,7 @@ const stringify = require("fast-json-stable-stringify"); // eslint-disable-next-line no-restricted-modules const prettier = require("../index.js"); +const { printToScreen } = require("./utils.js"); async function logFileInfoOrDie(context) { const { @@ -22,7 +23,7 @@ async function logFileInfoOrDie(context) { resolveConfig: config !== false, }); - console.log(prettier.format(stringify(fileInfo), { parser: "json" })); + printToScreen(prettier.format(stringify(fileInfo), { parser: "json" })); } module.exports = logFileInfoOrDie; diff --git a/src/cli/find-config-path.js b/src/cli/find-config-path.js index 224b04418be3..7f2ec6b79f2d 100644 --- a/src/cli/find-config-path.js +++ b/src/cli/find-config-path.js @@ -4,12 +4,13 @@ const path = require("path"); // eslint-disable-next-line no-restricted-modules const prettier = require("../index.js"); +const { printToScreen } = require("./utils.js"); async function logResolvedConfigPathOrDie(context) { const file = context.argv.findConfigPath; const configFile = await prettier.resolveConfigFile(file); if (configFile) { - console.log(path.relative(process.cwd(), configFile)); + printToScreen(path.relative(process.cwd(), configFile)); } else { throw new Error(`Can not find configure file for "${file}"`); } diff --git a/src/cli/index.js b/src/cli/index.js index 2f11dbaf52b4..d51e263174c5 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -13,6 +13,7 @@ const logResolvedConfigPathOrDie = require("./find-config-path.js"); const { utils: { isNonEmptyArray }, } = require("./prettier-internal.js"); +const { printToScreen } = require("./utils.js"); async function run(rawArguments) { // Create a default level logger, so we can log errors during `logLevel` parsing @@ -69,12 +70,12 @@ async function main(rawArguments, logger) { } if (context.argv.version) { - console.log(prettier.version); + printToScreen(prettier.version); return; } if (context.argv.help !== undefined) { - console.log( + printToScreen( typeof context.argv.help === "string" && context.argv.help !== "" ? createDetailedUsage(context, context.argv.help) : createUsage(context) @@ -83,7 +84,7 @@ async function main(rawArguments, logger) { } if (context.argv.supportInfo) { - console.log( + printToScreen( prettier.format(stringify(prettier.getSupportInfo()), { parser: "json", }) @@ -105,7 +106,7 @@ async function main(rawArguments, logger) { await formatFiles(context); } else { process.exitCode = 1; - console.log(createUsage(context)); + printToScreen(createUsage(context)); } } diff --git a/src/cli/utils.js b/src/cli/utils.js new file mode 100644 index 000000000000..e546f94bf3a5 --- /dev/null +++ b/src/cli/utils.js @@ -0,0 +1,7 @@ +"use strict"; + +function printToScreen(text) { + process.stdout.write(text); +} + +module.exports = { printToScreen }; From 42bad622173406ebbb3abadc3d73eaf2094a1770 Mon Sep 17 00:00:00 2001 From: fisker Date: Mon, 21 Mar 2022 09:12:45 +0800 Subject: [PATCH 09/10] Fix --- src/cli/utils.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/cli/utils.js b/src/cli/utils.js index e546f94bf3a5..bd74192eb428 100644 --- a/src/cli/utils.js +++ b/src/cli/utils.js @@ -1,7 +1,6 @@ "use strict"; -function printToScreen(text) { - process.stdout.write(text); -} +// eslint-disable-next-line no-console +const printToScreen = console.log.bind(console) module.exports = { printToScreen }; From 95cf99fad2bba464d3e30d4ea21df099d7500b68 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 21 Mar 2022 09:54:26 +0800 Subject: [PATCH 10/10] Update utils.js --- src/cli/utils.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cli/utils.js b/src/cli/utils.js index bd74192eb428..6c0bff9fdabf 100644 --- a/src/cli/utils.js +++ b/src/cli/utils.js @@ -1,6 +1,6 @@ "use strict"; // eslint-disable-next-line no-console -const printToScreen = console.log.bind(console) +const printToScreen = console.log.bind(console); module.exports = { printToScreen };