UNPKG

2.85 kBJavaScriptView Raw
1import meow from "meow";
2import { getSupportInfo } from "prettier";
3import { lint } from "./lint.js";
4import { formatFiles } from "./vendor/format.js";
5export const helpMessage = `
6 Usage
7 $ prettylint [options] [file/dir/glob ...]
8
9 Options
10 --no-config Do not load config file.
11 --config <path> Specify the config file.
12 --no-editorconfig Do not load .editorconfig
13 --fix Fix linting errors.
14 --format <path> Specify the module to format output.
15 Defaults to "eslint-formatter-pretty".
16 --ignore-path <path> Specify the .ignore file.
17 Defaults to [".gitignore", ".prettierignore"].
18 --silent Do not print message.
19 --with-node-modules Process files inside 'node_modules' directory.
20 -h, --help Show help.
21 -v, --version Show version.
22
23 Examples
24 $ prettylint .
25 $ prettylint . --fix
26`;
27export async function run(argv) {
28 const { input, flags } = meow(helpMessage, {
29 importMeta: import.meta,
30 argv,
31 flags: {
32 fix: {
33 type: "boolean",
34 default: false,
35 },
36 silent: {
37 type: "boolean",
38 default: false,
39 },
40 config: {
41 type: "string",
42 },
43 editorconfig: {
44 type: "boolean",
45 default: true,
46 },
47 ignorePath: {
48 type: "string",
49 isMultiple: true,
50 default: [".gitignore", ".prettierignore"],
51 },
52 withNodeModules: {
53 type: "boolean",
54 default: false,
55 },
56 format: {
57 type: "string",
58 default: "eslint-formatter-pretty",
59 },
60 help: {
61 shortFlag: "h",
62 },
63 version: {
64 shortFlag: "v",
65 },
66 },
67 });
68 const context = {
69 argv: {
70 filePatterns: input,
71 ignorePath: flags.ignorePath,
72 config: flags.config,
73 editorconfig: flags.editorconfig,
74 withNodeModules: flags.withNodeModules,
75 write: flags.fix,
76 },
77 languages: (await getSupportInfo()).languages,
78 };
79 const results = [];
80 for await (const result of formatFiles(context)) {
81 results.push(lint(result));
82 }
83 if (!flags.silent) {
84 const formatter = (await import(flags.format)).default;
85 const message = formatter(results);
86 if (message.length !== 0) {
87 console.log(message);
88 }
89 }
90 if (results.some((_) => _.errorCount || _.warningCount)) {
91 process.exitCode = 1;
92 }
93 return results;
94}