UNPKG

3.01 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const core_1 = require("@boost/core");
7const camelCase_1 = __importDefault(require("lodash/camelCase"));
8const trim_1 = __importDefault(require("lodash/trim"));
9class Context extends core_1.Context {
10 constructor(args) {
11 super();
12 this.argv = [];
13 this.configPaths = [];
14 // Current working directory
15 this.cwd = '';
16 // Absolute path to the configuration module
17 this.moduleRoot = '';
18 // Absolute path to the folder containing `package.json` (Yarn workspaces) or `lerna.json`
19 this.workspaceRoot = '';
20 // List of paths (with trailing glob star) for each defined workspace
21 this.workspaces = [];
22 this.args = args;
23 }
24 /**
25 * Add a positional argument to the argv list.
26 */
27 addArg(arg) {
28 this.args._.push(arg);
29 this.argv.push(arg);
30 return this;
31 }
32 /**
33 * Add multiple positional arguments.
34 */
35 addArgs(args) {
36 args.forEach(arg => {
37 this.addArg(arg);
38 });
39 return this;
40 }
41 /**
42 * Add a config path for the defined driver.
43 */
44 addConfigPath(driverName, path) {
45 this.configPaths.push({
46 driver: driverName,
47 path,
48 });
49 return this;
50 }
51 /**
52 * Add an option argument to both the args object and argv list.
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 * Add multiple boolean option arguments.
83 */
84 addOptions(args) {
85 args.forEach(arg => {
86 this.addOption(arg);
87 });
88 return this;
89 }
90 /**
91 * Find a configuration path by file name.
92 */
93 findConfigByName(name) {
94 return this.configPaths.find(config => config.path.endsWith(name) || config.driver === name);
95 }
96 /**
97 * Return an argument or option value by name, or a fallback value if not found.
98 */
99 getArg(name, fallback = null) {
100 return this.args[name] || fallback;
101 }
102}
103exports.default = Context;