diff --git a/changelog_unreleased/cli/12477.md b/changelog_unreleased/cli/12477.md new file mode 100644 index 000000000000..40b537db3c80 --- /dev/null +++ b/changelog_unreleased/cli/12477.md @@ -0,0 +1,12 @@ +#### Ignore `loglevel` when printing information (#12477 by @fisker) + + +```bash +# Prettier stable +prettier --loglevel silent --find-config-path index.js +# Nothing printed + +# Prettier main +prettier --loglevel silent --help no-color +# .prettierrc +``` diff --git a/src/cli/file-info.js b/src/cli/file-info.js index 8dc0f2d9ee06..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, }); - context.logger.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 05283fe3b52e..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) { - context.logger.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 8017c615d86a..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) { - logger.log(prettier.version); + printToScreen(prettier.version); return; } if (context.argv.help !== undefined) { - logger.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) { - logger.log( + printToScreen( prettier.format(stringify(prettier.getSupportInfo()), { parser: "json", }) @@ -104,8 +105,8 @@ async function main(rawArguments, logger) { } else if (hasFilePatterns) { await formatFiles(context); } else { - logger.log(createUsage(context)); process.exitCode = 1; + printToScreen(createUsage(context)); } } diff --git a/src/cli/utils.js b/src/cli/utils.js new file mode 100644 index 000000000000..6c0bff9fdabf --- /dev/null +++ b/src/cli/utils.js @@ -0,0 +1,6 @@ +"use strict"; + +// eslint-disable-next-line no-console +const printToScreen = console.log.bind(console); + +module.exports = { printToScreen }; diff --git a/tests/integration/__tests__/loglevel.js b/tests/integration/__tests__/loglevel.js index 51d745e38d50..53ad29937d45 100644 --- a/tests/integration/__tests__/loglevel.js +++ b/tests/integration/__tests__/loglevel.js @@ -37,6 +37,85 @@ 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\n", + }, + }, + { + argv: ["--file-info", "any-js-file.js"], + assertOptions: { + stdout(value) { + expect(JSON.parse(value)).toEqual({ + 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]); }