-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.ts
78 lines (67 loc) · 1.85 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
71
72
73
74
75
76
77
78
import {Command, flags} from '@oclif/command';
import {writeFile} from 'mz/fs';
import {resolve} from 'path';
import {Config, ScriptConfig} from '../lib/config';
import {format as f, makeDebug} from '../lib/debug';
import {findProjectRoot, hasRc} from '../lib/project';
const debug = makeDebug(__filename);
/**
* Create a .clarkrc file in your project root
*/
export default class Init extends Command {
/**
* description
*/
static description = 'Create a .clarkrc file in your project root';
/**
* flags
*/
static flags = {
force: flags.boolean({
char: 'f',
description: 'Overwrite .clarkrc with new config',
}),
script: flags.string({
char: 's',
description: 'Identifies a script to add to the config file',
multiple: true,
}),
};
/**
* implementation
*/
async run() {
const {flags: options} = this.parse(Init);
debug('locating root directory');
const rootDir = await findProjectRoot();
debug('checking if root dir contains a .clarkrc');
if ((await hasRc(rootDir)) && !options.force) {
throw new Error(
'Project already configured for clark. Pass --force to overwrite',
);
}
const extract = (s: ScriptConfig, item: string) => {
const [key, value] = item.split(/=(.+)/);
s[key] = value;
return s;
};
debug('generating scripts hash');
let scripts: ScriptConfig = {};
if (options.script) {
if (Array.isArray(options.script)) {
scripts = options.script.reduce(extract, scripts);
} else {
extract(scripts, options.script);
}
}
const config: Config = {
scripts,
};
debug(f`writing .clarkrc to ${rootDir}`);
await writeFile(
resolve(rootDir, '.clarkrc'),
`${JSON.stringify(config, null, 2)}\n`,
);
debug(f`wrote .clarkrc to ${rootDir}`);
}
}