1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | const core_1 = require("@boost/core");
|
7 | const camelCase_1 = __importDefault(require("lodash/camelCase"));
|
8 | const trim_1 = __importDefault(require("lodash/trim"));
|
9 | class Context extends core_1.Context {
|
10 | constructor(args) {
|
11 | super();
|
12 | this.argv = [];
|
13 | this.configPaths = [];
|
14 |
|
15 | this.cwd = '';
|
16 |
|
17 | this.moduleRoot = '';
|
18 |
|
19 | this.workspaceRoot = '';
|
20 |
|
21 | this.workspaces = [];
|
22 | this.args = args;
|
23 | }
|
24 | |
25 |
|
26 |
|
27 | addArg(arg) {
|
28 | this.args._.push(arg);
|
29 | this.argv.push(arg);
|
30 | return this;
|
31 | }
|
32 | |
33 |
|
34 |
|
35 | addArgs(args) {
|
36 | args.forEach(arg => {
|
37 | this.addArg(arg);
|
38 | });
|
39 | return this;
|
40 | }
|
41 | |
42 |
|
43 |
|
44 | addConfigPath(driverName, path) {
|
45 | this.configPaths.push({
|
46 | driver: driverName,
|
47 | path,
|
48 | });
|
49 | return this;
|
50 | }
|
51 | |
52 |
|
53 |
|
54 | addOption(arg, defaultValue = true, useEquals = false) {
|
55 | const list = [];
|
56 | let option = arg;
|
57 | let value = defaultValue;
|
58 | if (option.includes('=')) {
|
59 | [option, value] = option.split('=');
|
60 | useEquals = true;
|
61 | }
|
62 | let name = trim_1.default(option, '-');
|
63 | if (name.startsWith('no-')) {
|
64 | name = name.slice(3);
|
65 | value = false;
|
66 | }
|
67 | this.args[name] = value;
|
68 | this.args[camelCase_1.default(name)] = value;
|
69 | if (typeof value === 'boolean' || !value) {
|
70 | list.push(option);
|
71 | }
|
72 | else if (useEquals) {
|
73 | list.push(`${option}=${value}`);
|
74 | }
|
75 | else {
|
76 | list.push(option, String(value));
|
77 | }
|
78 | this.argv.push(...list);
|
79 | return this;
|
80 | }
|
81 | |
82 |
|
83 |
|
84 | addOptions(args) {
|
85 | args.forEach(arg => {
|
86 | this.addOption(arg);
|
87 | });
|
88 | return this;
|
89 | }
|
90 | |
91 |
|
92 |
|
93 | findConfigByName(name) {
|
94 | return this.configPaths.find(config => config.path.endsWith(name) || config.driver === name);
|
95 | }
|
96 | |
97 |
|
98 |
|
99 | getArg(name, fallback = null) {
|
100 | return this.args[name] || fallback;
|
101 | }
|
102 | }
|
103 | exports.default = Context;
|