-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathcli-executable.ts
More file actions
85 lines (71 loc) · 2.94 KB
/
cli-executable.ts
File metadata and controls
85 lines (71 loc) · 2.94 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/env node
// hashbang needed to get npm generated ".bin/dmno" to work with esm imports
import './lib/init-process';
/* eslint-disable import/first */
// import first - we add global exception handler here
const startBoot = new Date().getTime();
const cliExecId = new Date().toISOString();
import * as _ from 'lodash-es';
import Debug from 'debug';
import { DmnoCommand } from './lib/dmno-command';
import packageJson from '../../package.json';
import { addDocsCommand } from './lib/cli-schema-generation';
import { customizeHelp } from './lib/help-customizations';
import { getCliRunCtx, initCliRunCtx } from './lib/cli-ctx';
import { CliExitError } from './lib/cli-error';
import { WATCHING_FILES_MESSAGE } from './lib/watch-mode-helpers';
import { ResolveCommand } from './commands/resolve.command';
import { RunCommand } from './commands/run.command';
import { DevCommand } from './commands/dev.command';
import { PluginCommand } from './commands/plugin.command';
import { InitCommand } from './commands/init.command';
import { ClearCacheCommand } from './commands/clear-cache.command';
import { PrintEnvCommand } from './commands/printenv.command';
const debug = Debug('dmno:cli');
const program = new DmnoCommand('dmno')
.description('dmnno cli - https://dmno.dev')
.version(packageJson.version);
program.addCommand(ResolveCommand);
program.addCommand(RunCommand);
program.addCommand(DevCommand);
program.addCommand(InitCommand);
program.addCommand(ClearCacheCommand);
program.addCommand(PluginCommand);
program.addCommand(PrintEnvCommand);
// have to pass through the root program for this one so we can access all the subcommands
addDocsCommand(program);
customizeHelp(program);
program
.hook('preAction', (thisCommand, actionCommand) => {
// init command does not need a dmno server
// might want to opt-in on each command instead of skipping here?
if (actionCommand.name() === 'init') return;
// we need to know up front whether to enable the file watchers when initializing the vite server
initCliRunCtx({
// TODO: a bit awkward how this is being set up
createParentServer: ['dev', 'run'].includes(actionCommand.name()),
enableWebUi: actionCommand.name() === 'dev',
watch: actionCommand.name() === 'dev' || actionCommand.opts().watch,
});
});
process.on('exit', () => {
debug(`cli execution (${cliExecId}) took ${+new Date() - +startBoot}ms`);
});
debug(`finish loading - begin parse ${+new Date() - startBoot}ms`);
try {
await program.parseAsync();
} catch (err) {
// if we are on our first run and we hit an error, our watch-mode post-hook never fires
// so we must recreate that logic... can clean this up if it gets more complicated
if (err instanceof CliExitError) {
console.error(err.getFormattedOutput());
const ctx = getCliRunCtx();
if (ctx.watchEnabled && !err.forceExit) {
console.log(WATCHING_FILES_MESSAGE);
} else {
process.exit(1);
}
} else {
throw err;
}
}