1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const command_1 = require("@oclif/command");
|
4 | const fs_1 = require("mz/fs");
|
5 | const path_1 = require("path");
|
6 | const debug_1 = require("../lib/debug");
|
7 | const project_1 = require("../lib/project");
|
8 | const debug = debug_1.makeDebug(__dirname);
|
9 |
|
10 |
|
11 |
|
12 | class Init extends command_1.Command {
|
13 | |
14 |
|
15 |
|
16 | async run() {
|
17 | const { flags: options } = this.parse(Init);
|
18 | debug('locating root directory');
|
19 | const rootDir = await project_1.findProjectRoot();
|
20 | debug('checking if root dir contains a .clarkrc');
|
21 | if ((await project_1.hasRc(rootDir)) && !options.force) {
|
22 | throw new Error('Project already configured for clark. Pass --force to overwrite');
|
23 | }
|
24 | const extract = (s, item) => {
|
25 | const [key, value] = item.split(/=(.+)/);
|
26 | s[key] = value;
|
27 | return s;
|
28 | };
|
29 | debug('generating scripts hash');
|
30 | let scripts = {};
|
31 | if (options.script) {
|
32 | if (Array.isArray(options.script)) {
|
33 | scripts = options.script.reduce(extract, scripts);
|
34 | }
|
35 | else {
|
36 | extract(scripts, options.script);
|
37 | }
|
38 | }
|
39 | const config = {
|
40 | scripts,
|
41 | };
|
42 | debug(debug_1.format `writing .clarkrc to ${rootDir}`);
|
43 | await fs_1.writeFile(path_1.resolve(rootDir, '.clarkrc'), `${JSON.stringify(config, null, 2)}\n`);
|
44 | debug(debug_1.format `wrote .clarkrc to ${rootDir}`);
|
45 | }
|
46 | }
|
47 |
|
48 |
|
49 |
|
50 | Init.description = 'Create a .clarkrc file in your project root';
|
51 |
|
52 |
|
53 |
|
54 | Init.flags = {
|
55 | force: command_1.flags.boolean({
|
56 | char: 'f',
|
57 | description: 'Overwrite .clarkrc with new config',
|
58 | }),
|
59 | script: command_1.flags.string({
|
60 | char: 's',
|
61 | description: 'Identifies a script to add to the config file',
|
62 | multiple: true,
|
63 | }),
|
64 | };
|
65 | exports.default = Init;
|
66 |
|
\ | No newline at end of file |