@vexify-org/yaggs 3.1.0 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +12 -0
- package/lib/parser.js +39 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -133,6 +133,18 @@ class Yaggs {
|
|
|
133
133
|
return this;
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
+
env(name, envVar) {
|
|
137
|
+
if (this.parser.options[name]) {
|
|
138
|
+
this.parser.options[name].env = envVar;
|
|
139
|
+
}
|
|
140
|
+
return this;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
config(filePath) {
|
|
144
|
+
this.parser.loadConfig(filePath);
|
|
145
|
+
return this;
|
|
146
|
+
}
|
|
147
|
+
|
|
136
148
|
parse(argv = process.argv.slice(2)) {
|
|
137
149
|
const result = this.parser.parse(argv);
|
|
138
150
|
|
package/lib/parser.js
CHANGED
|
@@ -12,6 +12,8 @@ class ArgumentParser {
|
|
|
12
12
|
this.scriptName = options.scriptName || process.argv[1] || 'yaggs';
|
|
13
13
|
this.helpFunction = options.helpFunction || null;
|
|
14
14
|
this.errorHandler = options.errorHandler || null;
|
|
15
|
+
this.configFile = options.configFile || null;
|
|
16
|
+
this.config = {};
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
option(name, config) {
|
|
@@ -93,6 +95,30 @@ class ArgumentParser {
|
|
|
93
95
|
return this;
|
|
94
96
|
}
|
|
95
97
|
|
|
98
|
+
loadConfig(filePath) {
|
|
99
|
+
const fs = require('fs');
|
|
100
|
+
const path = require('path');
|
|
101
|
+
|
|
102
|
+
try {
|
|
103
|
+
const fullPath = path.resolve(filePath);
|
|
104
|
+
if (!fs.existsSync(fullPath)) {
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const ext = path.extname(filePath).toLowerCase();
|
|
109
|
+
if (ext === '.json') {
|
|
110
|
+
this.config = JSON.parse(fs.readFileSync(fullPath, 'utf8'));
|
|
111
|
+
} else if (ext === '.js') {
|
|
112
|
+
this.config = require(fullPath);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
return this;
|
|
116
|
+
} catch (error) {
|
|
117
|
+
this._handleError(`Failed to load config file: ${error.message}`);
|
|
118
|
+
return this;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
96
122
|
parse(argv = process.argv.slice(2)) {
|
|
97
123
|
const result = {
|
|
98
124
|
_: [],
|
|
@@ -276,6 +302,12 @@ class ArgumentParser {
|
|
|
276
302
|
}
|
|
277
303
|
}
|
|
278
304
|
|
|
305
|
+
for (const [key, value] of Object.entries(this.config)) {
|
|
306
|
+
if (result[key] === undefined) {
|
|
307
|
+
result[key] = value;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
|
|
279
311
|
this._applyTypeConversion(result);
|
|
280
312
|
this._validateOptions(result);
|
|
281
313
|
this._validatePositionals(result);
|
|
@@ -296,6 +328,13 @@ class ArgumentParser {
|
|
|
296
328
|
return;
|
|
297
329
|
}
|
|
298
330
|
|
|
331
|
+
if (opt.env && !result[name]) {
|
|
332
|
+
const envValue = process.env[opt.env];
|
|
333
|
+
if (envValue !== undefined) {
|
|
334
|
+
result[name] = envValue;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
299
338
|
if (opt.array) {
|
|
300
339
|
if (!result[name]) result[name] = [];
|
|
301
340
|
result[name].push(...values);
|