Skip to content

Commit

Permalink
CLI: Don't use logger to print information (#12477)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Mar 21, 2022
1 parent e67aa19 commit 5ef8212
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 7 deletions.
12 changes: 12 additions & 0 deletions changelog_unreleased/cli/12477.md
@@ -0,0 +1,12 @@
#### Ignore `loglevel` when printing information (#12477 by @fisker)

<!-- prettier-ignore -->
```bash
# Prettier stable
prettier --loglevel silent --find-config-path index.js
# Nothing printed

# Prettier main
prettier --loglevel silent --help no-color
# .prettierrc
```
3 changes: 2 additions & 1 deletion src/cli/file-info.js
Expand Up @@ -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 {
Expand All @@ -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;
3 changes: 2 additions & 1 deletion src/cli/find-config-path.js
Expand Up @@ -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}"`);
}
Expand Down
9 changes: 5 additions & 4 deletions src/cli/index.js
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -83,7 +84,7 @@ async function main(rawArguments, logger) {
}

if (context.argv.supportInfo) {
logger.log(
printToScreen(
prettier.format(stringify(prettier.getSupportInfo()), {
parser: "json",
})
Expand All @@ -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));
}
}

Expand Down
6 changes: 6 additions & 0 deletions 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 };
79 changes: 79 additions & 0 deletions tests/integration/__tests__/loglevel.js
Expand Up @@ -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",
Expand Down
4 changes: 3 additions & 1 deletion tests/integration/run-prettier.js
Expand Up @@ -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;
Expand All @@ -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]);
}
Expand Down

0 comments on commit 5ef8212

Please sign in to comment.