Minor tweak to CLI (#15366)

This commit is contained in:
fisker Cheung authored and GitHub committed 2023-09-07 16:32:43 +08:00
1 parent b9a3995e3a
commit 98ae581402
2 files changed
+25 -14

No files matched your search

+22 -13
View File
@@ -76,27 +76,36 @@ async function main(context) {
return printSupportInfo();
}
if (context.argv.findConfigPath) {
await logResolvedConfigPathOrDie(context);
return;
}
if (context.argv.fileInfo) {
await logFileInfoOrDie(context);
return;
}
const hasFilePatterns = context.filePatterns.length > 0;
const useStdin =
!hasFilePatterns && (!process.stdin.isTTY || context.argv.filepath);
if (context.argv.findConfigPath) {
await logResolvedConfigPathOrDie(context);
} else if (context.argv.fileInfo) {
await logFileInfoOrDie(context);
} else if (useStdin) {
if (useStdin) {
if (context.argv.cache) {
process.exitCode = 2;
context.logger.error("`--cache` cannot be used with stdin.");
return;
throw new Error("`--cache` cannot be used when formatting stdin.");
}
await formatStdin(context);
} else if (hasFilePatterns) {
await formatFiles(context);
} else {
process.exitCode = 1;
printToScreen(createUsage(context));
return;
}
if (hasFilePatterns) {
await formatFiles(context);
return;
}
process.exitCode = 1;
printToScreen(createUsage(context));
}
export { run };
+3 -1
View File
@@ -75,7 +75,9 @@ describe("--cache option", () => {
["--cache", "--stdin-filepath", "foo.js"],
{ input: "const a = a;" },
);
expect(stderr.trim()).toBe("[error] `--cache` cannot be used with stdin.");
expect(stderr.trim()).toBe(
"[error] `--cache` cannot be used when formatting stdin.",
);
});
it("throws error when use `--cache-strategy` without `--cache`.", async () => {