-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcli.ts
More file actions
66 lines (60 loc) · 1.97 KB
/
cli.ts
File metadata and controls
66 lines (60 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { parseArgs } from 'node:util';
import { resolve } from '@css-modules-kit/core';
import packageJson from '../package.json' with { type: 'json' };
import { ParseCLIArgsError } from './error.js';
const helpText = `
Usage: cmk [options]
Options:
--help, -h Show help information
--version, -v Show version number
--project, -p The path to its configuration file, or to a folder with a 'tsconfig.json'.
--pretty Enable color and formatting in output to make errors easier to read.
--clean Remove the output directory before generating files. [default: false]
--watch, -w Watch for changes and regenerate files. [default: false]
`;
export function printHelpText(): void {
// eslint-disable-next-line no-console
console.log(helpText);
}
export function printVersion(): void {
// eslint-disable-next-line no-console
console.log(packageJson.version);
}
export interface ParsedArgs {
help: boolean;
version: boolean;
project: string;
pretty: boolean | undefined;
clean: boolean;
watch: boolean;
}
/**
* Parse command-line arguments.
* @throws {ParseCLIArgsError} If failed to parse CLI arguments.
*/
export function parseCLIArgs(args: string[], cwd: string): ParsedArgs {
try {
const { values } = parseArgs({
args,
options: {
help: { type: 'boolean', short: 'h', default: false },
version: { type: 'boolean', short: 'v', default: false },
project: { type: 'string', short: 'p', default: '.' },
pretty: { type: 'boolean' },
clean: { type: 'boolean', default: false },
watch: { type: 'boolean', short: 'w', default: false },
},
allowNegative: true,
});
return {
help: values.help,
version: values.version,
project: resolve(cwd, values.project),
pretty: values.pretty,
clean: values.clean,
watch: values.watch,
};
} catch (cause) {
throw new ParseCLIArgsError(cause);
}
}