2022-05-05 15:03:08 +08:00
|
|
|
import * as prettier from "../index.js";
|
2022-02-11 09:56:58 +08:00
|
|
|
import Context from "./context.js";
|
|
|
|
|
import logFileInfoOrDie from "./file-info.js";
|
|
|
|
|
import logResolvedConfigPathOrDie from "./find-config-path.js";
|
2023-12-17 19:34:12 +00:00
|
|
|
import { formatFiles, formatStdin } from "./format.js";
|
|
|
|
|
import createLogger from "./logger.js";
|
2025-04-23 23:42:19 +08:00
|
|
|
import mockable from "./mockable.js";
|
2023-12-17 19:34:12 +00:00
|
|
|
import { parseArgvWithoutPlugins } from "./options/parse-cli-arguments.js";
|
2022-06-06 22:46:29 +08:00
|
|
|
import printSupportInfo from "./print-support-info.js";
|
2023-12-17 19:34:12 +00:00
|
|
|
import { createDetailedUsage, createUsage } from "./usage.js";
|
2025-12-01 07:25:25 +08:00
|
|
|
import { printToScreen } from "./utilities.js";
|
2017-09-03 17:21:06 +08:00
|
|
|
|
2023-09-04 19:54:16 +08:00
|
|
|
async function run(rawArguments = process.argv.slice(2)) {
|
2021-02-07 09:01:28 +08:00
|
|
|
// Create a default level logger, so we can log errors during `logLevel` parsing
|
2021-12-16 09:21:15 +08:00
|
|
|
let logger = createLogger();
|
2021-02-07 09:01:28 +08:00
|
|
|
|
2018-01-18 15:26:27 +08:00
|
|
|
try {
|
2022-08-02 09:39:41 +09:00
|
|
|
const { logLevel } = parseArgvWithoutPlugins(
|
2021-02-07 09:01:28 +08:00
|
|
|
rawArguments,
|
|
|
|
|
logger,
|
2022-08-02 09:39:41 +09:00
|
|
|
"log-level",
|
2022-05-06 09:27:54 +08:00
|
|
|
);
|
2021-02-07 09:01:28 +08:00
|
|
|
if (logLevel !== logger.logLevel) {
|
2021-12-16 09:21:15 +08:00
|
|
|
logger = createLogger(logLevel);
|
2021-02-07 09:01:28 +08:00
|
|
|
}
|
2022-04-28 11:24:59 +08:00
|
|
|
const context = new Context({ rawArguments, logger });
|
2022-05-06 09:27:54 +08:00
|
|
|
await context.init();
|
2022-04-28 11:24:59 +08:00
|
|
|
if (logger.logLevel !== "debug" && context.performanceTestFlag) {
|
|
|
|
|
context.logger = createLogger("debug");
|
|
|
|
|
}
|
2021-02-07 09:01:28 +08:00
|
|
|
|
2022-04-28 11:24:59 +08:00
|
|
|
await main(context);
|
2018-01-18 15:26:27 +08:00
|
|
|
} catch (error) {
|
2021-02-02 09:25:36 +08:00
|
|
|
logger.error(error.message);
|
|
|
|
|
process.exitCode = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-28 11:24:59 +08:00
|
|
|
async function main(context) {
|
|
|
|
|
context.logger.debug(`normalized argv: ${JSON.stringify(context.argv)}`);
|
2021-02-02 09:25:36 +08:00
|
|
|
|
2025-05-27 03:16:26 +02:00
|
|
|
if (
|
|
|
|
|
(context.argv.config === false && context.argv.__raw.config !== false) ||
|
|
|
|
|
(context.argv.config && context.rawArguments.includes("--no-config"))
|
|
|
|
|
) {
|
|
|
|
|
throw new Error("Cannot use --no-config and --config together.");
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-18 11:11:49 +08:00
|
|
|
if (context.argv.check && context.argv.listDifferent) {
|
2021-02-02 09:25:36 +08:00
|
|
|
throw new Error("Cannot use --check and --list-different together.");
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-18 11:11:49 +08:00
|
|
|
if (context.argv.write && context.argv.debugCheck) {
|
2021-02-02 09:25:36 +08:00
|
|
|
throw new Error("Cannot use --write and --debug-check together.");
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-18 11:11:49 +08:00
|
|
|
if (context.argv.findConfigPath && context.filePatterns.length > 0) {
|
2021-02-02 09:25:36 +08:00
|
|
|
throw new Error("Cannot use --find-config-path with multiple files");
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-18 11:11:49 +08:00
|
|
|
if (context.argv.fileInfo && context.filePatterns.length > 0) {
|
2021-02-02 09:25:36 +08:00
|
|
|
throw new Error("Cannot use --file-info with multiple files");
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 10:20:46 +08:00
|
|
|
if (!context.argv.cache && context.argv.cacheStrategy) {
|
|
|
|
|
throw new Error("`--cache-strategy` cannot be used without `--cache`.");
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-02 09:25:36 +08:00
|
|
|
if (context.argv.version) {
|
2022-03-21 10:38:48 +08:00
|
|
|
printToScreen(prettier.version);
|
2021-02-02 09:25:36 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (context.argv.help !== undefined) {
|
2022-03-21 10:38:48 +08:00
|
|
|
printToScreen(
|
2021-02-02 09:25:36 +08:00
|
|
|
typeof context.argv.help === "string" && context.argv.help !== ""
|
2021-12-16 09:21:15 +08:00
|
|
|
? createDetailedUsage(context, context.argv.help)
|
|
|
|
|
: createUsage(context),
|
2021-02-02 09:25:36 +08:00
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-18 11:11:49 +08:00
|
|
|
if (context.argv.supportInfo) {
|
2022-06-06 22:46:29 +08:00
|
|
|
return printSupportInfo();
|
2021-02-02 09:25:36 +08:00
|
|
|
}
|
|
|
|
|
|
2023-09-07 16:32:43 +08:00
|
|
|
if (context.argv.findConfigPath) {
|
|
|
|
|
await logResolvedConfigPathOrDie(context);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (context.argv.fileInfo) {
|
|
|
|
|
await logFileInfoOrDie(context);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-02 09:25:36 +08:00
|
|
|
const hasFilePatterns = context.filePatterns.length > 0;
|
|
|
|
|
const useStdin =
|
2025-04-23 23:42:19 +08:00
|
|
|
!hasFilePatterns &&
|
|
|
|
|
(!mockable.isStreamTTY(process.stdin) || context.argv.filepath);
|
2021-02-02 09:25:36 +08:00
|
|
|
|
2023-09-07 16:32:43 +08:00
|
|
|
if (useStdin) {
|
2022-06-11 23:32:32 +09:00
|
|
|
if (context.argv.cache) {
|
2023-09-07 16:32:43 +08:00
|
|
|
throw new Error("`--cache` cannot be used when formatting stdin.");
|
2022-06-11 23:32:32 +09:00
|
|
|
}
|
2023-09-07 16:32:43 +08:00
|
|
|
|
2021-12-16 09:21:15 +08:00
|
|
|
await formatStdin(context);
|
2023-09-07 16:32:43 +08:00
|
|
|
return;
|
2017-09-03 17:21:06 +08:00
|
|
|
}
|
2023-09-07 16:32:43 +08:00
|
|
|
|
|
|
|
|
if (hasFilePatterns) {
|
|
|
|
|
await formatFiles(context);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
process.exitCode = 1;
|
|
|
|
|
printToScreen(createUsage(context));
|
2017-09-03 17:21:06 +08:00
|
|
|
}
|
|
|
|
|
|
2022-02-11 09:56:58 +08:00
|
|
|
export { run };
|
2025-04-23 23:42:19 +08:00
|
|
|
// Exposed for tests
|
|
|
|
|
export { mockable } from "./mockable.js";
|