From 98ae5814027f4ebab8e2d3c20647d2cdad9e6f29 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 7 Sep 2023 16:32:43 +0800 Subject: [PATCH] Minor tweak to CLI (#15366) --- src/cli/index.js | 35 +++++++++++++++++----------- tests/integration/__tests__/cache.js | 4 +++- 2 files changed, 25 insertions(+), 14 deletions(-) diff --git a/src/cli/index.js b/src/cli/index.js index 6d1962260..179762380 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -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 }; diff --git a/tests/integration/__tests__/cache.js b/tests/integration/__tests__/cache.js index 1a9103f55..f02df5439 100644 --- a/tests/integration/__tests__/cache.js +++ b/tests/integration/__tests__/cache.js @@ -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 () => {