2022-02-11 09:56:58 +08:00
|
|
|
import stringify from "fast-json-stable-stringify";
|
2022-05-05 15:03:08 +08:00
|
|
|
import * as prettier from "../index.js";
|
2022-02-11 09:56:58 +08:00
|
|
|
import createLogger from "./logger.js";
|
|
|
|
|
import Context from "./context.js";
|
|
|
|
|
import { parseArgvWithoutPlugins } from "./options/parse-cli-arguments.js";
|
|
|
|
|
import { createDetailedUsage, createUsage } from "./usage.js";
|
|
|
|
|
import { formatStdin, formatFiles } from "./format.js";
|
|
|
|
|
import logFileInfoOrDie from "./file-info.js";
|
|
|
|
|
import logResolvedConfigPathOrDie from "./find-config-path.js";
|
2022-04-29 13:51:34 +08:00
|
|
|
import { printToScreen, isNonEmptyArray } from "./utils.js";
|
2017-09-03 17:21:06 +08:00
|
|
|
|
2021-05-09 20:43:10 +08:00
|
|
|
async function run(rawArguments) {
|
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-05-28 10:50:43 +08:00
|
|
|
const { loglevel: logLevel } = parseArgvWithoutPlugins(
|
2021-02-07 09:01:28 +08:00
|
|
|
rawArguments,
|
|
|
|
|
logger,
|
|
|
|
|
"loglevel"
|
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
|
|
|
|
2022-02-18 16:36:53 +08:00
|
|
|
if (context.argv.pluginSearch === false) {
|
|
|
|
|
const rawPluginSearchDirs = context.argv.__raw["plugin-search-dir"];
|
|
|
|
|
if (
|
|
|
|
|
typeof rawPluginSearchDirs === "string" ||
|
|
|
|
|
isNonEmptyArray(rawPluginSearchDirs)
|
|
|
|
|
) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
"Cannot use --no-plugin-search and --plugin-search-dir 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");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-05-06 09:27:54 +08:00
|
|
|
const supportInfo = await prettier.getSupportInfo();
|
2022-03-21 10:38:48 +08:00
|
|
|
printToScreen(
|
2022-05-06 09:27:54 +08:00
|
|
|
await prettier.format(stringify(supportInfo), { parser: "json" })
|
2021-02-02 09:25:36 +08:00
|
|
|
);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const hasFilePatterns = context.filePatterns.length > 0;
|
|
|
|
|
const useStdin =
|
2021-12-18 11:11:49 +08:00
|
|
|
!hasFilePatterns && (!process.stdin.isTTY || context.argv.filePath);
|
2021-02-02 09:25:36 +08:00
|
|
|
|
2021-12-18 11:11:49 +08:00
|
|
|
if (context.argv.findConfigPath) {
|
2021-12-16 09:21:15 +08:00
|
|
|
await logResolvedConfigPathOrDie(context);
|
2021-12-18 11:11:49 +08:00
|
|
|
} else if (context.argv.fileInfo) {
|
2021-12-16 09:21:15 +08:00
|
|
|
await logFileInfoOrDie(context);
|
2021-02-02 09:25:36 +08:00
|
|
|
} else if (useStdin) {
|
2021-12-16 09:21:15 +08:00
|
|
|
await formatStdin(context);
|
2021-02-02 09:25:36 +08:00
|
|
|
} else if (hasFilePatterns) {
|
2021-12-16 09:21:15 +08:00
|
|
|
await formatFiles(context);
|
2021-02-02 09:25:36 +08:00
|
|
|
} else {
|
|
|
|
|
process.exitCode = 1;
|
2022-03-21 10:38:48 +08:00
|
|
|
printToScreen(createUsage(context));
|
2017-09-03 17:21:06 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-11 09:56:58 +08:00
|
|
|
export { run };
|