-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynamicCommand.js
293 lines (244 loc) · 9.95 KB
/
DynamicCommand.js
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import fs from 'fs';
import path from 'path';
import oclif from '@oclif/core';
import dotenv from 'dotenv';
import { NonFatalError } from '@typhonjs-oclif/errors';
/**
* Provides default handling for TyphonJS dynamic command initialization of flags from Oclif plugins.
*/
class DynamicCommand extends oclif.Command
{
/**
* Performs any final steps before the command execution completes. This is useful for logging any data
* in response to the `--metafile` flag.
*/
async finally()
{
// Write any log metafiles on finalize.
if (globalThis.$$eventbus !== void 0 && typeof this._cliFlags.metafile === 'boolean' &&
this._cliFlags.metafile)
{
await globalThis.$$eventbus.triggerAsync('typhonjs:oclif:system:handler:metafile:write', this);
}
}
/**
* Returns the parsed data.
*
* @returns {*} Any loaded command data.
*/
get commandData()
{
return this._commandData;
}
/**
* Returns the parsed CLI flags.
*
* @returns {object} Parsed CLI flags.
*/
get cliFlags()
{
return this._cliFlags;
}
/**
* Loads all dynamic flags for this command after running any init hook.
*
* @param {object} CommandClass - The DynamicCommand subclass.
*
* @param {object} config - An Oclif config.
*
* @param {boolean} [loadDefault=true] - A boolean indicating whether to load defaults or current environment
* variables.
*
* @returns {Promise<{}>} - Parsed flags.
*/
static async loadDynamicFlags(CommandClass, config, loadDefault = true)
{
const commandData = CommandClass._dynamicCommand;
let flags = {};
if (typeof commandData === 'object')
{
if (Array.isArray(commandData.initHooks))
{
for (const hook of commandData.initHooks)
{
// Run any custom init hook for all Oclif bundle plugins to load respective bundler plugins.
await config.runHook(hook, { id: this.id, flagsModule: '@oclif/core/lib/flags.js' });
}
}
if (globalThis.$$eventbus !== void 0 && Array.isArray(commandData.flagCommands))
{
// Dynamically load flags for the command from oclif-flaghandler.
flags = globalThis.$$eventbus.triggerSync('typhonjs:oclif:system:handler:flag:get', {
commands: commandData.flagCommands
});
}
}
const defaultData = loadDefault ? null : void 0;
// Sanitize default flags. Invoke any default functions taking the value provided.
for (const v of Object.values(flags))
{
if (typeof v.default === 'function')
{
v.default = v.default(defaultData);
if (Array.isArray(v.default) && v.default.length === 0) { delete v.default; }
if (v.default === '') { delete v.default; }
}
}
return flags;
}
/**
* Attempts to load environment variables from a *.env file w/ `dotenv`. Many flags have defaults, but also can be
* set with environment variables and this is a convenient way to load many different configurations.
*
* Note: If an environment file is loaded by `dotenv` the flags are parsed again below via
* `this.parse(BuildCommand)`.
*
* @param {object} existingFlags - parsed flags from command.
*
* @param {object} CommandClass - The actual child command class.
*
* @returns {object} Either the existing flags if there is no .env file to load or the new flags after new
* environment variables have been loaded.
*
* @private
*/
async _loadEnvFile(existingFlags = {}, CommandClass)
{
let output = existingFlags;
// Check to see if the `env` flag has been set; if so attempt to load the *.env file and parse the flags again.
if (typeof existingFlags.env === 'string')
{
// By default the environment variables will always be stored in `./env`
const envFilePath = `${globalThis.$$cli_baseCWD}${path.sep}env${path.sep}${existingFlags.env}.env`;
const logEnvFilePath = `${globalThis.$$cli_logCWD}${path.sep}env${path.sep}${existingFlags.env}.env`;
// Exit gracefully if the environment file could not be found.
if (!fs.existsSync(envFilePath))
{
this.error(`Could not find specified environment file: \n'${logEnvFilePath}'`);
this.exit(1);
}
else
{
globalThis.$$eventbus.trigger('log:verbose', `Loading environment variables from: \n${logEnvFilePath}`);
// Store current environment variable keys.
const beforeEnvKeys = Object.keys(process.env);
// Potentially load environment variables from a *.env file.
const env = dotenv.config({ path: envFilePath });
// Detect which new environment keys were added.
globalThis.$$process_env_key_change = Object.keys(process.env).filter((x) => !beforeEnvKeys.includes(x));
if (env.error)
{
this.error(`An error occurred with 'dotenv' when loading environment file: \n${env.error.message}`);
this.exit(1);
}
// Parse flags again after environment variables have been loaded.
const { flags } = await this.parse(CommandClass);
output = flags;
}
}
return output;
}
/**
* Performs all initialization, loading of flags from *.env file via dotenv and verification of flags.
*/
async init()
{
this._cliFlags = {};
this._commandData = {};
const commandData = this.constructor._dynamicCommand;
if (typeof commandData === 'object')
{
if (Array.isArray(commandData.initHooks))
{
for (const hook of commandData.initHooks)
{
// Run any custom init hooks.
await this.config.runHook(hook, { id: this.id, flagsModule: '@oclif/core/lib/flags.js' });
}
}
if (Array.isArray(commandData.flagCommands))
{
this._cliFlags = await this._initializeFlags(commandData.flagCommands);
}
// If an event path is provided then fire it off to load command data.
if (typeof commandData.eventData === 'string')
{
this._commandData = await globalThis.$$eventbus.triggerAsync(commandData.eventData, this._cliFlags,
globalThis.$$cli_baseCWD, globalThis.$$cli_origCWD);
}
}
// Handle noop / no operation flag / Exit out now!
if (typeof this._cliFlags.noop === 'boolean' && this._cliFlags.noop)
{
let results = `-----------------------------------\n`;
results += `${globalThis.$$cli_name_version} running: '${this.id}' - `;
// Attempt to get abbreviated noop description from command data.
if (this._commandData && typeof this._commandData.toString === 'function')
{
results += this._commandData.toString();
}
const localStringNoop = this.toString();
results += `${localStringNoop !== '' ? '\n' : ''}${localStringNoop}`;
results += `-----------------------------------`;
throw new NonFatalError(results, 'info:raw');
}
}
/**
* Performs all initialization, loading of flags from *.env file via dotenv and verification of flags.
*
* @param {string[]} commands - The actual command names.
*
* @returns {object} Parsed and verified flags.
*
* @private
*/
async _initializeFlags(commands)
{
const CommandClass = this.constructor;
const eventbus = globalThis.$$eventbus;
// Dynamically load flags for the command from oclif-flaghandler.
CommandClass.flags = eventbus.triggerSync('typhonjs:oclif:system:handler:flag:get', { commands });
// Perform the first stage of parsing flags. This is
let { flags } = await this.parse(CommandClass);
// Notify that the current working directory is being changed and verify that the new directory exists.
if (typeof flags.cwd === 'string' && flags.cwd !== '.')
{
const origCWD = globalThis.$$cli_baseCWD;
const newCWD = flags.cwd;
// Perform any initialization after initial flags have been loaded. Handle defining `cwd` and verify.
globalThis.$$cli_baseCWD = path.resolve(globalThis.$$cli_origCWD, newCWD);
// Only log absolute path if the CWD location is outside of the original path.
globalThis.$$cli_logCWD = newCWD.startsWith(origCWD) ? path.relative(origCWD, newCWD) : newCWD;
if (!fs.existsSync(globalThis.$$cli_baseCWD))
{
throw new NonFatalError(`New current working directory does not exist:\n${globalThis.$$cli_logCWD}`);
}
}
// Attempt to parse any environment variables via dotenv if applicable and reload / update flags accordingly.
flags = await this._loadEnvFile(flags, CommandClass);
// Verify flags given any plugin provided verify functions in FlagHandler.
eventbus.triggerSync('typhonjs:oclif:system:handler:flag:verify', { commands, flags });
// Be sure to log after flags are verified and any log level is set for CWD.
if (typeof flags.cwd === 'string' && flags.cwd !== '.')
{
globalThis.$$eventbus.trigger('log:verbose',
`New current working directory set: \n${globalThis.$$cli_logCWD}`);
}
return flags;
}
/**
* Provides the base method to be overridden to provide per command implementation details.
*
* @returns {string} - A string containing a description of the command.
*/
toString()
{
return '';
}
}
DynamicCommand._metaFileData = [
{ key: 'config', filename: 'oclif.config.json' },
{ key: 'cliFlags', filename: 'cli-flags.json' },
{ key: 'commandData', filename: 'command-data.json' }
];
export default DynamicCommand;