Skip to content

Commit 5ef8212

Browse files
authored
CLI: Don't use logger to print information (#12477)
1 parent e67aa19 commit 5ef8212

7 files changed

Lines changed: 109 additions & 7 deletions

File tree

changelog_unreleased/cli/12477.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#### Ignore `loglevel` when printing information (#12477 by @fisker)
2+
3+
<!-- prettier-ignore -->
4+
```bash
5+
# Prettier stable
6+
prettier --loglevel silent --find-config-path index.js
7+
# Nothing printed
8+
9+
# Prettier main
10+
prettier --loglevel silent --help no-color
11+
# .prettierrc
12+
```

src/cli/file-info.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
const stringify = require("fast-json-stable-stringify");
44
// eslint-disable-next-line no-restricted-modules
55
const prettier = require("../index.js");
6+
const { printToScreen } = require("./utils.js");
67

78
async function logFileInfoOrDie(context) {
89
const {
@@ -22,7 +23,7 @@ async function logFileInfoOrDie(context) {
2223
resolveConfig: config !== false,
2324
});
2425

25-
context.logger.log(prettier.format(stringify(fileInfo), { parser: "json" }));
26+
printToScreen(prettier.format(stringify(fileInfo), { parser: "json" }));
2627
}
2728

2829
module.exports = logFileInfoOrDie;

src/cli/find-config-path.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ const path = require("path");
44

55
// eslint-disable-next-line no-restricted-modules
66
const prettier = require("../index.js");
7+
const { printToScreen } = require("./utils.js");
78

89
async function logResolvedConfigPathOrDie(context) {
910
const file = context.argv.findConfigPath;
1011
const configFile = await prettier.resolveConfigFile(file);
1112
if (configFile) {
12-
context.logger.log(path.relative(process.cwd(), configFile));
13+
printToScreen(path.relative(process.cwd(), configFile));
1314
} else {
1415
throw new Error(`Can not find configure file for "${file}"`);
1516
}

src/cli/index.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const logResolvedConfigPathOrDie = require("./find-config-path.js");
1313
const {
1414
utils: { isNonEmptyArray },
1515
} = require("./prettier-internal.js");
16+
const { printToScreen } = require("./utils.js");
1617

1718
async function run(rawArguments) {
1819
// Create a default level logger, so we can log errors during `logLevel` parsing
@@ -69,12 +70,12 @@ async function main(rawArguments, logger) {
6970
}
7071

7172
if (context.argv.version) {
72-
logger.log(prettier.version);
73+
printToScreen(prettier.version);
7374
return;
7475
}
7576

7677
if (context.argv.help !== undefined) {
77-
logger.log(
78+
printToScreen(
7879
typeof context.argv.help === "string" && context.argv.help !== ""
7980
? createDetailedUsage(context, context.argv.help)
8081
: createUsage(context)
@@ -83,7 +84,7 @@ async function main(rawArguments, logger) {
8384
}
8485

8586
if (context.argv.supportInfo) {
86-
logger.log(
87+
printToScreen(
8788
prettier.format(stringify(prettier.getSupportInfo()), {
8889
parser: "json",
8990
})
@@ -104,8 +105,8 @@ async function main(rawArguments, logger) {
104105
} else if (hasFilePatterns) {
105106
await formatFiles(context);
106107
} else {
107-
logger.log(createUsage(context));
108108
process.exitCode = 1;
109+
printToScreen(createUsage(context));
109110
}
110111
}
111112

src/cli/utils.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"use strict";
2+
3+
// eslint-disable-next-line no-console
4+
const printToScreen = console.log.bind(console);
5+
6+
module.exports = { printToScreen };

tests/integration/__tests__/loglevel.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,85 @@ describe("Should use default level logger to log `--loglevel` error", () => {
3737
});
3838
});
3939

40+
describe("loglevel should not effect information print", () => {
41+
for (const { argv, runOptions, assertOptions } of [
42+
{
43+
argv: ["--version"],
44+
assertOptions: {
45+
stdout(value) {
46+
expect(value).not.toBe("");
47+
},
48+
},
49+
},
50+
{
51+
argv: ["--help"],
52+
assertOptions: {
53+
stdout(value) {
54+
expect(value.includes("-v, --version")).toBe(true);
55+
},
56+
},
57+
},
58+
{
59+
argv: ["--help", "write"],
60+
assertOptions: {
61+
stdout(value) {
62+
expect(value.startsWith("-w, --write")).toBe(true);
63+
},
64+
},
65+
},
66+
{
67+
argv: ["--support-info"],
68+
assertOptions: {
69+
stdout(value) {
70+
expect(JSON.parse(value)).toBeDefined();
71+
},
72+
},
73+
},
74+
{
75+
argv: ["--find-config-path", "any-file"],
76+
assertOptions: {
77+
stdout: ".prettierrc\n",
78+
},
79+
},
80+
{
81+
argv: ["--file-info", "any-js-file.js"],
82+
assertOptions: {
83+
stdout(value) {
84+
expect(JSON.parse(value)).toEqual({
85+
ignored: false,
86+
inferredParser: "babel",
87+
});
88+
},
89+
},
90+
},
91+
{
92+
argv: [],
93+
runOptions: { isTTY: true },
94+
assertOptions: {
95+
status: "non-zero",
96+
stdout(value) {
97+
expect(value.includes("-v, --version")).toBe(true);
98+
},
99+
},
100+
},
101+
{
102+
argv: ["--parser", "babel"],
103+
runOptions: { input: "foo" },
104+
assertOptions: { stdout: "foo;\n" },
105+
},
106+
]) {
107+
runPrettier("cli/loglevel", ["--loglevel", "silent", ...argv], {
108+
...runOptions,
109+
title: argv.join(" "),
110+
}).test({
111+
stderr: "",
112+
status: 0,
113+
write: [],
114+
...assertOptions,
115+
});
116+
}
117+
});
118+
40119
async function runPrettierWithLogLevel(logLevel, patterns) {
41120
const result = await runPrettier("cli/loglevel", [
42121
"--loglevel",

tests/integration/run-prettier.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ function runPrettier(dir, args = [], options = {}) {
178178

179179
function testResult(testOptions) {
180180
for (const name of ["status", "stdout", "stderr", "write"]) {
181-
test(`(${name})`, async () => {
181+
test(`${options.title || ""}(${name})`, async () => {
182182
const result = await runCli();
183183
const value =
184184
// \r is trimmed from jest snapshots by default;
@@ -192,6 +192,8 @@ function runPrettier(dir, args = [], options = {}) {
192192
if (name in testOptions) {
193193
if (name === "status" && testOptions[name] === "non-zero") {
194194
expect(value).not.toBe(0);
195+
} else if (typeof testOptions[name] === "function") {
196+
testOptions[name](value);
195197
} else {
196198
expect(value).toEqual(testOptions[name]);
197199
}

0 commit comments

Comments
 (0)