Repository navigation
Expand file tree
/
Copy pathcommand.js
More file actions
709 lines (608 loc) · 19.1 KB
/
Copy pathcommand.js
File metadata and controls
709 lines (608 loc) · 19.1 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
'use strict';
const nopt = require('nopt');
const { default: chalk } = require('chalk');
const path = require('path');
const isGitRepo = require('is-git-url');
const camelize = require('ember-cli-string-utils').camelize;
const getCallerFile = require('get-caller-file');
const printCommand = require('@ember-tooling/blueprint-model/utilities/print-command');
const union = require('lodash/union');
const defaults = require('lodash/defaults');
const uniq = require('lodash/uniq');
const uniqBy = require('lodash/uniqBy');
const reject = require('lodash/reject');
const EOL = require('os').EOL;
const CoreObject = require('core-object');
const logger = require('heimdalljs-logger')('ember-cli:command');
const WatchDetector = require('watch-detector');
const SilentError = require('silent-error');
const execSync = require('child_process').execSync;
const fs = require('fs');
const { determineInstallCommand } = require('../utilities/package-managers');
let cache = {};
let allowedWorkOptions = {
insideProject: true,
outsideProject: true,
everywhere: true,
};
path.name = 'Path';
// extend nopt to recognize 'gitUrl' as a type
nopt.typeDefs.gitUrl = {
type: 'gitUrl',
validate(data, k, val) {
if (isGitRepo(val)) {
data[k] = val;
return true;
} else {
return false;
}
},
};
/**
* The base class for all CLI commands.
*
* @module ember-cli
* @class Command
* @constructor
* @extends CoreObject
*/
let Command = CoreObject.extend({
/**
* The description of what this command does.
*
* @final
* @property description
* @type String
*/
description: null,
/**
* Does this command work everywhere or just inside or outside of projects.
*
* Possible values:
*
* - `insideProject`
* - `outsideProject`
* - `everywhere`
*
* @final
* @property works
* @type String
* @default `insideProject`
*/
works: 'insideProject',
_printableProperties: ['name', 'description', 'aliases', 'works', 'availableOptions', 'anonymousOptions'],
init() {
this._super.apply(this, arguments);
/**
* @final
* @property isWithinProject
* @type Boolean
*/
this.isWithinProject = this.project.isEmberCLIProject();
/**
* @final
* @property isViteProject
* @type Boolean
*/
this.isViteProject = this.isWithinProject && this.project.isViteProject?.();
/**
* The name of the command.
*
* @final
* @property name
* @type String
* @example `new` or `generate`
*/
this.name = this.name || path.basename(getCallerFile(), '.js');
logger.info('initialize: name: %s, name: %s', this.name);
/**
* An array of aliases for the command
*
* @final
* @property aliases
* @type Array
* @example `['g']` for the `generate` command
*/
this.aliases = this.aliases || [];
// Works Property
if (!allowedWorkOptions[this.works]) {
throw new Error(
`The "${this.name}" command's works field has to be either "everywhere", "insideProject" or "outsideProject".`
);
}
/**
* An array of available options for the command
*
* @final
* @property availableOptions
* @type Array
* @example
* ```js
* availableOptions: [
* { name: 'dry-run', type: Boolean, default: false, aliases: ['d'] },
* { name: 'verbose', type: Boolean, default: false, aliases: ['v'] },
* { name: 'blueprint', type: String, default: 'app', aliases: ['b'] },
* { name: 'skip-npm', type: Boolean, default: false, aliases: ['sn', 'skip-install', 'si'] },
* { name: 'skip-git', type: Boolean, default: false, aliases: ['sg'] },
* { name: 'directory', type: String , aliases: ['dir'] }
* ],
* ```
*/
this.availableOptions = this.availableOptions || [];
/**
* An array of anonymous options for the command
*
* @final
* @property anonymousOptions
* @type Array
* @example
* ```js
* anonymousOptions: [
* '<blueprint>'
* ],
* ```
*/
this.anonymousOptions = this.anonymousOptions || [];
},
/**
Registers options with command. This method provides the ability to extend or override command options.
Expects an object containing anonymousOptions or availableOptions, which it will then merge with
existing availableOptions before building the optionsAliases which are used to define shorthands.
@method registerOptions
@param {Object} options
*/
registerOptions(options) {
let extendedAvailableOptions = (options && options.availableOptions) || [];
let extendedAnonymousOptions = (options && options.anonymousOptions) || [];
this.anonymousOptions = union(this.anonymousOptions.slice(0), extendedAnonymousOptions);
// merge any availableOptions
this.availableOptions = union(this.availableOptions.slice(0), extendedAvailableOptions);
let optionKeys = uniq(this.availableOptions.map((option) => option.name));
optionKeys.map(this.mergeDuplicateOption.bind(this));
this.optionsAliases = this.optionsAliases || {};
this.availableOptions.map(this.validateOption.bind(this));
},
/**
* Called when command is interrupted from outside, e.g. ctrl+C or process kill
* Can be used to cleanup artifacts produced by command and control process exit code
*
* @method onInterrupt
* @return {Promise|undefined} if rejected promise then result of promise will be used as an exit code
*/
onInterrupt() {
if (this._currentTask) {
return this._currentTask.onInterrupt();
} else {
// interrupt all external commands which don't use `runTask()` with an error
// eslint-disable-next-line n/no-process-exit
process.exit(1);
}
},
_env(/* name */) {
return {
ui: this.ui,
project: this.project,
testing: this.testing,
settings: this.settings,
};
},
/**
* Looks up for the task and runs
* It also keeps the reference for the current active task
* Keeping reference for the current task allows to cleanup task on interruption
*
* @private
* @method runTask
* @throws {Error} when no task found
* @throws {Error} on attempt to run concurrent task
* @param {string} name Task name from the tasks registry. Should be capitalized
* @param {object} options
* @return {Promise} Task run
*/
runTask(name, options) {
if (this._currentTask) {
throw new Error(`Concurrent tasks are not supported`);
}
logger.info(`\`${this.name}\` command running \`${name}\` task`);
let Task = this.tasks[name];
if (!Task) {
throw new Error(`Unknown task "${name}"`);
}
let task = new Task(this._env(name));
this._currentTask = task;
return Promise.resolve()
.then(() => task.run(options))
.catch((error) => {
logger.info(`An error occurred running \`${name}\` from the \`${this.name}\` command.`, error.stack);
throw error;
})
.finally(() => {
delete this._currentTask;
});
},
/**
Hook for extending a command before it is run in the cli.run command.
Most common use case would be to extend availableOptions.
@method beforeRun
@return {Promise|null}
*/
beforeRun() {},
/**
@method validateAndRun
@return {Promise}
*/
async validateAndRun(args) {
let commandOptions = this.parseArgs(args);
// If the `help` option was passed, resolve with `callHelp` to call the `help` command:
if (commandOptions && (commandOptions.options.help || commandOptions.options.h)) {
logger.info(`${this.name} called with help option`);
return 'callHelp';
}
if (commandOptions === null) {
throw new SilentError();
}
if (this.works === 'outsideProject' && this.isWithinProject) {
throw new SilentError(`You cannot use the ${chalk.green(this.name)} command inside an ember-cli project.`);
}
if (this.works === 'insideProject') {
if (!this.project.hasDependencies()) {
if (!this.isWithinProject && !this.project.isEmberCLIAddon()) {
throw new SilentError(
`You have to be inside an ember-cli project to use the ${chalk.green(this.name)} command.`
);
}
let installCommand = await determineInstallCommand(this.project.root);
throw new SilentError(
`Required packages are missing, run \`${installCommand}\` from this directory to install them.`
);
}
}
let detector = new WatchDetector({
ui: this.ui,
childProcess: { execSync },
fs,
watchmanSupportsPlatform: /^win/.test(process.platform),
cache,
root: this.project.root,
});
let options = commandOptions.options;
if (this.hasOption('watcher') || process.env.EMBROIDER_PREBUILD) {
// Do stuff to try and provide a good experience when it comes to file watching:
let watchPreference = detector.findBestWatcherOption(options);
this.project._watchmanInfo = watchPreference.watchmanInfo;
options.watcher = watchPreference.watcher;
}
return this.run(options, commandOptions.args);
},
/**
Reports if the given command has a command line option by a given name
@method hasOption
@param {String} name
@return {Boolean}
*/
hasOption(name) {
for (let i = 0; i < this.availableOptions.length; i++) {
if (this.availableOptions[i].name === name) {
return true;
}
}
return false;
},
/**
Merges any options with duplicate keys in the availableOptions array.
Used primarily by registerOptions.
@method mergeDuplicateOption
@param {String} key
@return {Object}
*/
mergeDuplicateOption(key) {
let duplicateOptions, mergedOption, mergedAliases;
// get duplicates to merge
duplicateOptions = this.availableOptions.filter((option) => option.name === key);
if (duplicateOptions.length > 1) {
// TODO: warn on duplicates and overwriting
mergedAliases = duplicateOptions.flatMap((option) => option.aliases);
// merge duplicate options
mergedOption = Object.assign.apply(null, duplicateOptions);
// replace aliases with unique aliases
mergedOption.aliases = uniqBy(mergedAliases, (alias) => {
if (typeof alias === 'object') {
return alias[Object.keys(alias)[0]];
}
return alias;
});
// remove duplicates from options
this.availableOptions = reject(this.availableOptions, { name: key });
this.availableOptions.push(mergedOption);
}
return this.availableOptions;
},
/**
Normalizes option, filling in implicit values
@method normalizeOption
@param {Object} option
@return {Object}
*/
normalizeOption(option) {
option.key = camelize(option.name);
option.required = option.required || false;
return option;
},
/**
Assigns option
@method assignOption
@param {Object} option
@param {Object} parsedOptions
@param {Object} commandOptions
@return {Boolean}
*/
assignOption(option, parsedOptions, commandOptions) {
let isValid = isValidParsedOption(option, parsedOptions[option.name]);
if (isValid) {
if (parsedOptions[option.name] === undefined) {
if (option.default !== undefined) {
commandOptions[option.key] = option.default;
}
if (this.settings[option.name] !== undefined) {
commandOptions[option.key] = this.settings[option.name];
} else if (this.settings[option.key] !== undefined) {
commandOptions[option.key] = this.settings[option.key];
}
} else {
commandOptions[option.key] = parsedOptions[option.name];
delete parsedOptions[option.name];
}
} else {
this.ui.writeLine(
`The specified command ${chalk.green(this.name)} requires the option ${chalk.green(option.name)}.`
);
}
return isValid;
},
/**
Validates option
@method validateOption
@param {Object} option
@return {Boolean}
*/
validateOption(option) {
let parsedAliases;
if (!option.name || !option.type) {
throw new Error(`The command "${this.name}" has an option without the required type and name fields.`);
}
if (option.name !== option.name.toLowerCase()) {
throw new Error(`The "${option.name}" option's name of the "${this.name}" command contains a capital letter.`);
}
this.normalizeOption(option);
if (option.aliases) {
parsedAliases = option.aliases.map(this.parseAlias.bind(this, option));
return parsedAliases.map(this.assignAlias.bind(this, option)).indexOf(false) === -1;
}
return false;
},
/**
Parses alias for an option and adds it to optionsAliases
@method parseAlias
@param {Object} option
@param {Object|String} alias
@return {Object}
*/
parseAlias(option, alias) {
let aliasType = typeof alias;
let key, value, aliasValue;
if (isValidAlias(alias, option.type)) {
if (aliasType === 'string') {
key = alias;
value = [`--${option.name}`];
} else if (aliasType === 'object') {
key = Object.keys(alias)[0];
value = [`--${option.name}`, alias[key]];
}
} else {
if (Array.isArray(alias)) {
aliasType = 'array';
aliasValue = alias.join(',');
} else {
aliasValue = alias;
try {
aliasValue = JSON.parse(alias);
} catch (e) {
const logger = require('heimdalljs-logger')('ember-cli/models/command');
logger.error(e);
}
}
throw new Error(
`The "${aliasValue}" [type:${aliasType}] alias is not an acceptable value. ` +
`It must be a string or single key object with a string value (for example, "value" or { "key" : "value" }).`
);
}
return {
key,
value,
original: alias,
};
},
/**
* @method assignAlias
* @param option
* @param alias
* @return {Boolean}
*/
assignAlias(option, alias) {
let isValid = this.validateAlias(option, alias);
if (isValid) {
this.optionsAliases[alias.key] = alias.value;
}
return isValid;
},
/**
Validates alias value
@method validateAlias
@param {Object} alias
@return {Boolean}
*/
validateAlias(option, alias) {
let key = alias.key;
let value = alias.value;
if (!this.optionsAliases[key]) {
return true;
} else {
if (value[0] !== this.optionsAliases[key][0]) {
throw new SilentError(
`The "${key}" alias is already in use by the "${this.optionsAliases[key][0]}" option ` +
`and cannot be used by the "${value[0]}" option. Please use a different alias.`
);
} else if (value[1] !== this.optionsAliases[key][1]) {
this.ui.writeLine(chalk.yellow(`The "${key}" alias cannot be overridden. Please use a different alias.`));
// delete offending alias from options
let index = this.availableOptions.indexOf(option);
let aliasIndex = this.availableOptions[index].aliases.indexOf(alias.original);
if (this.availableOptions[index].aliases[aliasIndex]) {
// first one wins
this.availableOptions[index].aliases = [this.availableOptions[index].aliases[0]];
}
}
return false;
}
},
/**
Parses command arguments and processes
@method parseArgs
@param {Object} commandArgs
@return {Object|null}
*/
parseArgs(commandArgs) {
let knownOpts = {}; // Parse options
let commandOptions = {};
let parsedOptions;
this.registerOptions();
let assembleAndValidateOption = function (option) {
return this.assignOption(option, parsedOptions, commandOptions);
};
let validateParsed = function (key) {
// ignore 'argv', 'h', and 'help'
if (!(key in commandOptions) && key !== 'argv' && key !== 'h' && key !== 'help') {
this.ui.writeLine(
chalk.yellow(
`The option '--${key}' is not registered with the '${this.name}' command. ` +
`Run \`ember ${this.name} --help\` for a list of supported options.`
)
);
}
if (typeof parsedOptions[key] !== 'object') {
commandOptions[camelize(key)] = parsedOptions[key];
}
};
this.availableOptions.forEach((option) => {
if (typeof option.type !== 'string') {
knownOpts[option.name] = option.type;
} else if (option.type === 'Path') {
knownOpts[option.name] = path;
} else {
knownOpts[option.name] = String;
}
});
parsedOptions = nopt(knownOpts, this.optionsAliases, commandArgs, 0);
if (!this.availableOptions.every(assembleAndValidateOption.bind(this))) {
return null;
}
Object.keys(parsedOptions).map(validateParsed.bind(this));
return {
options: defaults(commandOptions, this.settings),
args: parsedOptions.argv.remain,
};
},
/**
* @method run
* @param commandArgs
*/
run(commandArgs) {
throw new Error(`command must implement run${commandArgs.toString()}`);
},
_printCommand: printCommand,
/**
Prints basic help for the command.
Basic help looks like this:
ember generate <blueprint> <options...>
Generates new code from blueprints
aliases: g
--dry-run (Default: false)
--verbose (Default: false)
The default implementation is designed to cover all bases
but may be overridden if necessary.
@method printBasicHelp
*/
printBasicHelp() {
// ember command-name
let output;
if (this.isRoot) {
output = `Usage: ${this.name}`;
} else {
output = `ember ${this.name}`;
}
output += this._printCommand();
output += EOL;
return output;
},
/**
Prints detailed help for the command.
The default implementation is no-op and should be overridden
for each command where further help text is required.
@method printDetailedHelp
*/
printDetailedHelp() {},
/**
* @method getJson
* @param {Object} options
* @return {Object}
*/
getJson(options) {
let json = {};
this.registerOptions(options);
this._printableProperties.forEach((key) => (json[key] = this[key]));
if (this.addAdditionalJsonForHelp) {
this.addAdditionalJsonForHelp(json, options);
}
return json;
},
});
/*
Validates options parsed by nopt
*/
function isValidParsedOption(option, parsedOption) {
// option.name didn't parse
if (parsedOption === undefined) {
// no default
if (option.default === undefined) {
if (option.required) {
return false;
}
}
}
return true;
}
/*
Validates alias. Must be a string or single key object
*/
function isValidAlias(alias, expectedType) {
let type = typeof alias;
let value, valueType;
if (type === 'string') {
return true;
} else if (type === 'object') {
// no arrays, no multi-key objects
if (!Array.isArray(alias) && Object.keys(alias).length === 1) {
value = alias[Object.keys(alias)[0]];
valueType = typeof value;
if (!Array.isArray(expectedType)) {
if (valueType === expectedType.name.toLowerCase()) {
return true;
}
} else if (expectedType.indexOf(value) > -1) {
return true;
}
}
}
return false;
}
module.exports = Command;