-
-
Notifications
You must be signed in to change notification settings - Fork 599
/
Copy pathcli.ts
40 lines (35 loc) · 1.4 KB
/
cli.ts
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
import { Command, Option } from 'commander';
import { diagnostics, LogLevel, logLevels } from './commands/diagnostics';
function getVersion(): string {
const { version }: { version: string } = require('../package.json');
return `v${version}`;
}
function validateLogLevel(logLevelInput: unknown): logLevelInput is LogLevel {
return typeof logLevelInput === 'string' && (logLevels as ReadonlyArray<string>).includes(logLevelInput);
}
(async () => {
const program = new Command();
program.name('vti').description('Vetur Terminal Interface').version(getVersion());
program
.command('diagnostics [workspace] [paths...]')
.description('Print all diagnostics')
.addOption(
new Option('-l, --log-level <logLevel>', 'Log level to print')
.default('WARN')
// logLevels is readonly array but .choices need read-write array (because of weak typing)
.choices(logLevels as unknown as string[])
)
.action(async (workspace, paths, options) => {
const logLevelOption: unknown = options.logLevel;
if (!validateLogLevel(logLevelOption)) {
throw new Error(`Invalid log level: ${logLevelOption}`);
}
await diagnostics(workspace, paths, logLevelOption);
});
program.showHelpAfterError();
program.parse(process.argv);
})().catch(err => {
console.error(`VTI operation failed with error`);
console.error((err as Error).stack);
process.exit(1);
});