Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: Don't use logger to print information #12477

Merged
merged 12 commits into from Mar 21, 2022
1 change: 1 addition & 0 deletions .eslintrc.js
Expand Up @@ -222,6 +222,7 @@ module.exports = {
patterns: ["../"],
},
],
"no-console": "off",
},
},
{
Expand Down
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
```
2 changes: 1 addition & 1 deletion src/cli/file-info.js
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion src/cli/find-config-path.js
Expand Up @@ -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}"`);
}
Expand Down
8 changes: 4 additions & 4 deletions src/cli/index.js
Expand Up @@ -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)
Expand All @@ -83,7 +83,7 @@ async function main(rawArguments, logger) {
}

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

Expand Down
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