-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.ts
70 lines (64 loc) · 1.91 KB
/
init.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
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
import { flush } from '@oclif/core'
import { ArgInput } from '@oclif/core/lib/parser'
import FactoryCommand, {
CommonFlags,
FactoryFlags,
} from '../../providers/command'
import { createDefaultConfig, safeLoadConfig } from '../../providers/config'
/**
* Init command configure an ovm.json config file in user's home dir.
*/
export default class Init extends FactoryCommand {
static readonly aliases = ['ci', 'config init']
static override readonly description = `Configure an ovm.json config file in user's home dir.`
static override readonly examples = ['<%= config.bin %> <%= command.id %>']
static override readonly flags = {
...this.commonFlags,
}
/**
* Executes the command.
* Parses the arguments and flags, and calls the action method.
* Handles errors and ensures flushing of logs.
*/
public async run() {
try {
const { args, flags } = await this.parse(Init)
await this.action(args, this.flagsInterceptor(flags))
} catch (error) {
this.handleError(error)
} finally {
flush()
}
}
/**
* Main action method for the command.
* @param {ArgInput} args - The arguments passed to the command.
* @param {FactoryFlags<InitFlags>} flags - The flags passed to the command.
* @returns {Promise<void>}
*/
private async action(
args: ArgInput,
flags: FactoryFlags<CommonFlags>,
): Promise<void> {
try {
const { data: config, error } = await safeLoadConfig(flags.config)
if (config) {
throw new Error('File already exists!')
}
if (error) {
throw error
}
} catch (error) {
const typedError = error as Error
if (typedError && typedError.message === 'Config file not found') {
try {
await createDefaultConfig(flags.config)
} catch (error) {
this.handleError(error)
}
} else {
this.handleError(typedError)
}
}
}
}