-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase-command.ts
More file actions
95 lines (76 loc) · 2.38 KB
/
base-command.ts
File metadata and controls
95 lines (76 loc) · 2.38 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
86
87
88
89
90
91
92
93
94
95
import { Command, Flags } from '@oclif/core';
import { OutputFlags } from '@oclif/core/interfaces';
import chalk from 'chalk';
import { SudoRequestData } from 'codify-schemas';
import createDebug from 'debug';
import { Event, ctx } from '../events/context.js';
import { Reporter, ReporterFactory, ReporterType } from '../ui/reporters/reporter.js';
import { prettyPrintError } from './errors.js';
export abstract class BaseCommand extends Command {
static enableJsonFlag = true;
static baseFlags = {
'debug': Flags.boolean(),
'output': Flags.option({
char: 'o',
default: 'default',
options: ['plain', 'default', 'debug', 'json'],
})(),
'secure': Flags.boolean({
char: 's',
default: false,
})
}
protected reporter!: Reporter;
public async init(): Promise<void> {
await super.init();
const { flags } = await this.parse({
baseFlags: (super.ctor as typeof BaseCommand).baseFlags,
strict: false,
});
const reporterType = this.getReporterType(flags);
this.reporter = ReporterFactory.create(reporterType)
if (flags.secure) {
console.log(chalk.blue('Running Codify in secure mode. Sudo will be prompted every time'));
}
ctx.on(Event.SUDO_REQUEST, async (pluginName: string, data: SudoRequestData) => {
try {
const result = await this.reporter.promptSudo(pluginName, data, flags.secure);
ctx.sudoRequestGranted(pluginName, result);
// This listener is outside of the base-command callstack. We have to manually catch the error.
} catch (error) {
this.catch(error as Error);
}
});
}
protected async catch(err: Error): Promise<void> {
prettyPrintError(err);
process.exit(1);
}
private getReporterType(flags: OutputFlags<any>): ReporterType {
const debug = createDebug('codify');
if (debug.enabled || flags.debug) {
createDebug.enable('*');
return ReporterType.DEBUG;
}
if (this.jsonEnabled()) {
return ReporterType.JSON;
}
if (flags.output) {
switch (flags.output) {
case 'debug': {
return ReporterType.DEBUG;
}
case 'json': {
return ReporterType.JSON;
}
case 'plain': {
return ReporterType.PLAIN;
}
case 'default': {
return ReporterType.DEFAULT;
}
}
}
return ReporterType.DEFAULT;
}
}