-
-
Notifications
You must be signed in to change notification settings - Fork 306
/
Copy pathindex.js.html
2057 lines (1845 loc) · 64.7 KB
/
index.js.html
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Source: index.js | Generator</title>
<script src="scripts/prettify/prettify.js"></script>
<script src="scripts/prettify/lang-css.js"></script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/bootstrap.min.css" />
<link type="text/css" rel="stylesheet" href="styles/prettify-jsdoc.css" />
<link
type="text/css"
rel="stylesheet"
href="styles/prettify-tomorrow.css"
/>
<link type="text/css" rel="stylesheet" href="styles/tui-doc.css" />
</head>
<body>
<nav class="lnb" id="lnb">
<div class="logo" style="width: 127px; height: 14px">
<img
src="https://raw.githubusercontent.com/yeoman/yeoman.github.io/source/app/assets/img/logo.png"
width="100%"
height="100%"
/>
</div>
<div class="title">
<h1><a href="index.html" class="link">Generator</a></h1>
<span class="version">v5.8.0</span>
</div>
<div class="search-container" id="search-container">
<input type="text" placeholder="Search" />
<ul></ul>
</div>
<div class="lnb-api hidden">
<h3>Modules</h3>
<ul>
<li>
<a href="module-promptSuggestion.html">promptSuggestion</a>
<button type="button" class="hidden toggle-subnav btn btn-link">
<span class="glyphicon glyphicon-plus"></span>
</button>
<div class="hidden" id="module:promptSuggestion_sub"></div>
</li>
</ul>
</div>
<div class="lnb-api hidden">
<h3>Classes</h3>
<ul>
<li>
<a href="Generator.html">Generator</a>
<button type="button" class="hidden toggle-subnav btn btn-link">
<span class="glyphicon glyphicon-plus"></span>
</button>
<div class="hidden" id="Generator_sub">
<div class="member-type">Members</div>
<ul class="inner">
<li><a href="Generator.html#config">config</a></li>
<li><a href="Generator.html#packageJson">packageJson</a></li>
</ul>
<div class="member-type">Methods</div>
<ul class="inner">
<li>
<a href="Generator.html#_templateData">_templateData</a>
</li>
<li>
<a href="Generator.html#addDependencies">addDependencies</a>
</li>
<li>
<a href="Generator.html#addDevDependencies">
addDevDependencies
</a>
</li>
<li><a href="Generator.html#argument">argument</a></li>
<li>
<a href="Generator.html#argumentsHelp">argumentsHelp</a>
</li>
<li>
<a href="Generator.html#cancelCancellableTasks">
cancelCancellableTasks
</a>
</li>
<li><a href="Generator.html#composeWith">composeWith</a></li>
<li>
<a href="Generator.html#copyDestination">copyDestination</a>
</li>
<li><a href="Generator.html#copyTemplate">copyTemplate</a></li>
<li>
<a href="Generator.html#copyTemplateAsync">
copyTemplateAsync
</a>
</li>
<li>
<a href="Generator.html#createStorage">createStorage</a>
</li>
<li><a href="Generator.html#debug">debug</a></li>
<li>
<a href="Generator.html#deleteDestination">
deleteDestination
</a>
</li>
<li><a href="Generator.html#desc">desc</a></li>
<li>
<a href="Generator.html#destinationPath">destinationPath</a>
</li>
<li>
<a href="Generator.html#destinationRoot">destinationRoot</a>
</li>
<li>
<a href="Generator.html#determineAppname">determineAppname</a>
</li>
<li><a href="Generator.html#git.email">git.email</a></li>
<li>
<a href="Generator.html#existsDestination">
existsDestination
</a>
</li>
<li><a href="Generator.html#getFeatures">getFeatures</a></li>
<li><a href="Generator.html#help">help</a></li>
<li>
<a href="Generator.html#moveDestination">moveDestination</a>
</li>
<li><a href="Generator.html#git.name">git.name</a></li>
<li><a href="Generator.html#option">option</a></li>
<li><a href="Generator.html#optionsHelp">optionsHelp</a></li>
<li><a href="Generator.html#prompt">prompt</a></li>
<li><a href="Generator.html#queueMethod">queueMethod</a></li>
<li><a href="Generator.html#queueTask">queueTask</a></li>
<li>
<a href="Generator.html#queueTaskGroup">queueTaskGroup</a>
</li>
<li><a href="Generator.html#queueTasks">queueTasks</a></li>
<li>
<a href="Generator.html#queueTransformStream">
queueTransformStream
</a>
</li>
<li>
<a href="Generator.html#readDestination">readDestination</a>
</li>
<li>
<a href="Generator.html#readDestinationJSON">
readDestinationJSON
</a>
</li>
<li><a href="Generator.html#readTemplate">readTemplate</a></li>
<li>
<a href="Generator.html#registerConfigPrompts">
registerConfigPrompts
</a>
</li>
<li>
<a href="Generator.html#registerPriorities">
registerPriorities
</a>
</li>
<li>
<a href="Generator.html#renderTemplate">renderTemplate</a>
</li>
<li>
<a href="Generator.html#renderTemplateAsync">
renderTemplateAsync
</a>
</li>
<li>
<a href="Generator.html#renderTemplates">renderTemplates</a>
</li>
<li>
<a href="Generator.html#renderTemplatesAsync">
renderTemplatesAsync
</a>
</li>
<li>
<a href="Generator.html#rootGeneratorName">
rootGeneratorName
</a>
</li>
<li>
<a href="Generator.html#rootGeneratorVersion">
rootGeneratorVersion
</a>
</li>
<li><a href="Generator.html#run">run</a></li>
<li><a href="Generator.html#setFeatures">setFeatures</a></li>
<li><a href="Generator.html#sourceRoot">sourceRoot</a></li>
<li><a href="Generator.html#spawnCommand">spawnCommand</a></li>
<li>
<a href="Generator.html#spawnCommandSync">spawnCommandSync</a>
</li>
<li><a href="Generator.html#startOver">startOver</a></li>
<li><a href="Generator.html#templatePath">templatePath</a></li>
<li><a href="Generator.html#usage">usage</a></li>
<li>
<a href="Generator.html#github.username">github.username</a>
</li>
<li>
<a href="Generator.html#writeDestination">writeDestination</a>
</li>
<li>
<a href="Generator.html#writeDestinationJSON">
writeDestinationJSON
</a>
</li>
</ul>
</div>
</li>
<li>
<a href="Storage.html">Storage</a>
<button type="button" class="hidden toggle-subnav btn btn-link">
<span class="glyphicon glyphicon-plus"></span>
</button>
<div class="hidden" id="Storage_sub">
<div class="member-type">Methods</div>
<ul class="inner">
<li><a href="Storage.html#createProxy">createProxy</a></li>
<li><a href="Storage.html#createStorage">createStorage</a></li>
<li><a href="Storage.html#defaults">defaults</a></li>
<li><a href="Storage.html#delete">delete</a></li>
<li><a href="Storage.html#get">get</a></li>
<li><a href="Storage.html#getAll">getAll</a></li>
<li><a href="Storage.html#getPath">getPath</a></li>
<li><a href="Storage.html#merge">merge</a></li>
<li><a href="Storage.html#save">save</a></li>
<li><a href="Storage.html#set">set</a></li>
<li><a href="Storage.html#setPath">setPath</a></li>
</ul>
</div>
</li>
</ul>
</div>
<div class="lnb-api hidden">
<h3>Mixins</h3>
<ul>
<li>
<a href="actions_fs.html">actions/fs</a>
<button type="button" class="hidden toggle-subnav btn btn-link">
<span class="glyphicon glyphicon-plus"></span>
</button>
<div class="hidden" id="actions/fs_sub">
<div class="member-type">Methods</div>
<ul class="inner">
<li>
<a href="actions_fs.html#._templateData">_templateData</a>
</li>
<li>
<a href="actions_fs.html#.copyDestination">copyDestination</a>
</li>
<li>
<a href="actions_fs.html#.copyTemplate">copyTemplate</a>
</li>
<li>
<a href="actions_fs.html#.copyTemplateAsync">
copyTemplateAsync
</a>
</li>
<li>
<a href="actions_fs.html#.deleteDestination">
deleteDestination
</a>
</li>
<li>
<a href="actions_fs.html#.existsDestination">
existsDestination
</a>
</li>
<li>
<a href="actions_fs.html#.moveDestination">moveDestination</a>
</li>
<li>
<a href="actions_fs.html#.readDestination">readDestination</a>
</li>
<li>
<a href="actions_fs.html#.readDestinationJSON">
readDestinationJSON
</a>
</li>
<li>
<a href="actions_fs.html#.readTemplate">readTemplate</a>
</li>
<li>
<a href="actions_fs.html#.renderTemplate">renderTemplate</a>
</li>
<li>
<a href="actions_fs.html#.renderTemplateAsync">
renderTemplateAsync
</a>
</li>
<li>
<a href="actions_fs.html#.renderTemplates">renderTemplates</a>
</li>
<li>
<a href="actions_fs.html#.renderTemplatesAsync">
renderTemplatesAsync
</a>
</li>
<li>
<a href="actions_fs.html#.writeDestination">
writeDestination
</a>
</li>
<li>
<a href="actions_fs.html#.writeDestinationJSON">
writeDestinationJSON
</a>
</li>
</ul>
</div>
</li>
<li>
<a href="actions_help.html">actions/help</a>
<button type="button" class="hidden toggle-subnav btn btn-link">
<span class="glyphicon glyphicon-plus"></span>
</button>
<div class="hidden" id="actions/help_sub">
<div class="member-type">Methods</div>
<ul class="inner">
<li>
<a href="actions_help.html#.argumentsHelp">argumentsHelp</a>
</li>
<li><a href="actions_help.html#.desc">desc</a></li>
<li><a href="actions_help.html#.help">help</a></li>
<li>
<a href="actions_help.html#.optionsHelp">optionsHelp</a>
</li>
<li><a href="actions_help.html#.usage">usage</a></li>
</ul>
</div>
</li>
<li>
<a href="actions_install.html">actions/install</a>
<button type="button" class="hidden toggle-subnav btn btn-link">
<span class="glyphicon glyphicon-plus"></span>
</button>
<div class="hidden" id="actions/install_sub">
<div class="member-type">Methods</div>
<ul class="inner">
<li>
<a href="actions_install.html#.bowerInstall">bowerInstall</a>
</li>
<li>
<a href="actions_install.html#.installDependencies">
installDependencies
</a>
</li>
<li>
<a href="actions_install.html#.npmInstall">npmInstall</a>
</li>
<li>
<a href="actions_install.html#.scheduleInstallTask">
scheduleInstallTask
</a>
</li>
<li>
<a href="actions_install.html#.yarnInstall">yarnInstall</a>
</li>
</ul>
</div>
</li>
<li>
<a href="actions_package-json.html">actions/package-json</a>
<button type="button" class="hidden toggle-subnav btn btn-link">
<span class="glyphicon glyphicon-plus"></span>
</button>
<div class="hidden" id="actions/package-json_sub">
<div class="member-type">Methods</div>
<ul class="inner">
<li>
<a href="actions_package-json.html#addDependencies">
addDependencies
</a>
</li>
<li>
<a href="actions_package-json.html#addDevDependencies">
addDevDependencies
</a>
</li>
</ul>
</div>
</li>
<li>
<a href="actions_spawn-command.html">actions/spawn-command</a>
<button type="button" class="hidden toggle-subnav btn btn-link">
<span class="glyphicon glyphicon-plus"></span>
</button>
<div class="hidden" id="actions/spawn-command_sub">
<div class="member-type">Methods</div>
<ul class="inner">
<li>
<a href="actions_spawn-command.html#.spawnCommand">
spawnCommand
</a>
</li>
<li>
<a href="actions_spawn-command.html#.spawnCommandSync">
spawnCommandSync
</a>
</li>
</ul>
</div>
</li>
<li>
<a href="actions_user.html">actions/user</a>
<button type="button" class="hidden toggle-subnav btn btn-link">
<span class="glyphicon glyphicon-plus"></span>
</button>
<div class="hidden" id="actions/user_sub">
<div class="member-type">Methods</div>
<ul class="inner">
<li><a href="actions_user.html#.git.email">git.email</a></li>
<li><a href="actions_user.html#.git.name">git.name</a></li>
<li>
<a href="actions_user.html#.github.username">
github.username
</a>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div class="lnb-api hidden">
<h3>Global</h3>
<ul>
<li class="hidden"><a href="global.html#Priority">Priority</a></li>
<li><a href="global.html#proxyHandler">proxyHandler</a></li>
<li class="hidden">
<a href="global.html#QueueOptions">QueueOptions</a>
</li>
<li class="hidden"><a href="global.html#Task">Task</a></li>
<li class="hidden">
<a href="global.html#TaskOptions">TaskOptions</a>
</li>
<li class="hidden">
<a href="global.html#WrappedMethod">WrappedMethod</a>
</li>
</ul>
</div>
</nav>
<div id="resizer"></div>
<div class="main" id="main">
<section>
<article>
<pre class="prettyprint source linenums"><code>'use strict';
const fs = require('fs');
const path = require('path');
const os = require('os');
const EventEmitter = require('events');
const assert = require('assert');
const _ = require('lodash');
const semver = require('semver');
const readPkgUp = require('read-pkg-up');
const chalk = require('chalk');
const minimist = require('minimist');
const runAsync = require('run-async');
const createDebug = require('debug');
const memFsEditor = require('mem-fs-editor');
const packageJson = require('../package.json');
const Storage = require('./util/storage');
const promptSuggestion = require('./util/prompt-suggestion');
const EMPTY = '@@_YEOMAN_EMPTY_MARKER_@@';
const debug = createDebug('yeoman:generator');
const ENV_VER_WITH_VER_API = '2.9.0';
const mixins = [require('./actions/package-json')];
// eslint-disable-next-line unicorn/no-array-reduce
const Base = mixins.reduce((a, b) => b(a), EventEmitter);
// Ensure a prototype method is a candidate run by default
const methodIsValid = function (name) {
return name.charAt(0) !== '_' && name !== 'constructor';
};
/**
* Queue options.
* @typedef {Object} QueueOptions
* @property {string} [queueName] - Name of the queue.
* @property {boolean} [once] - Execute only once by namespace and taskName.
* @property {boolean} [run] - Run the queue if not running yet.
*/
/**
* Task options.
* @typedef {QueueOptions} TaskOptions
* @property {Function} [reject] - Reject callback.
*/
/**
* Priority object.
* @typedef {QueueOptions} Priority
* @property {string} priorityName - Name of the priority.
* @property {string} [before] - The queue which this priority should be added before.
*/
/**
* Complete Task object.
* @typedef {TaskOptions} Task
* @property {WrappedMethod} method - Function to be queued.
* @property {string} taskName - Name of the task.
*/
/**
* RunAsync creates a promise and executes wrappedMethod inside the promise.
* It replaces async property of the wrappedMethod's context with one RunAsync provides.
* async() simulates an async function by creating a callback.
*
* It supports promises/async and sync functions.
* - Promises/async: forward resolve/reject from the runAsync promise to the
* promise returned by the wrappedMethod.
* - Sync functions: resolves with the returned value.
* Can be a promise for chaining
* - Sync functions with callback (done = this.async()) calls:
* Reject with done(rejectValue) first argument
* Resolve with done(undefined, resolveValue) second argument
* - Callback must called when 'async()' was called inside a sync function.
* - Callback can be ignored when 'async()' was called inside a async function.
* @typedef {Function} WrappedMethod
*/
class Generator extends Base {
// If for some reason environment adds more queues, we should use or own for stability.
static get queues() {
return [
'initializing',
'prompting',
'configuring',
'default',
'writing',
'transform',
'conflicts',
'install',
'end'
];
}
/**
* @classdesc The `Generator` class provides the common API shared by all generators.
* It define options, arguments, file, prompt, log, API, etc.
*
* It mixes into its prototype all the methods found in the `actions/` mixins.
*
* Every generator should extend this base class.
*
* @constructor
* @augments actions/package-json
* @mixes actions/help
* @mixes actions/spawn-command
* @mixes actions/user
* @mixes actions/fs
* @mixes nodejs/EventEmitter
*
* @param {string[]} args - Provide arguments at initialization
* @param {Object} options - Provide options at initialization
* @param {Priority[]} [options.customPriorities] - Custom priorities
* @property {Object} env - the current Environment being run
* @property {String} resolved - the path to the current generator
* @property {String} description - Used in `--help` output
* @property {String} appname - The application name
* @property {Storage} config - `.yo-rc` config file manager
* @property {Object} fs - An instance of {@link https://github.com/SBoudrias/mem-fs-editor Mem-fs-editor}
* @property {Function} log - Output content through Interface Adapter
* @param {Object} features - Provide Generator features information
* @property {String} uniqueBy - The Generator instance unique identifier.
* The Environment will ignore duplicated identifiers.
* @property {String} unique - uniqueBy calculation method (undefined/argument/namespace)
* @property {boolean} tasksMatchingPriority - Only queue methods that matches a priority.
* @property {String} taskPrefix - Tasks methods starts with prefix. Allows api methods (non tasks) without prefix.
* @property {boolean|Function} customInstallTask - Provides a custom install task. Environment >= 3.2.0
* Environment built-in task will not be executed
* @property {boolean|Function} customCommitTask - Provides a custom commit task. Environment >= 3.2.0
* Environment built-in task will not be executed
*
* @example
* const Generator = require('yeoman-generator');
* module.exports = class extends Generator {
* writing() {
* this.fs.write(this.destinationPath('index.js'), 'const foo = 1;');
* }
* };
*/
constructor(args, options, features) {
super();
if (!Array.isArray(args)) {
features = options;
options = args;
args = [];
}
options = options || {};
this.options = options;
this._initOptions = _.clone(options);
this._args = args || [];
this._options = {};
this._arguments = [];
this._prompts = [];
this._composedWith = [];
this._namespace = this.options.namespace;
this._namespaceId = this.options.namespaceId;
this.yoGeneratorVersion = packageJson.version;
this.features = features || {unique: this.options.unique};
this.option('help', {
type: Boolean,
alias: 'h',
description: "Print the generator's options and usage"
});
this.option('skip-cache', {
type: Boolean,
description: 'Do not remember prompt answers',
default: false
});
this.option('skip-install', {
type: Boolean,
description: 'Do not automatically install dependencies',
default: false
});
this.option('force-install', {
type: Boolean,
description: 'Fail on install dependencies error',
default: false
});
this.option('ask-answered', {
type: Boolean,
description: 'Show prompts for already configured options',
default: false
});
this.env = this.options.env;
this.resolved = this.options.resolved || __filename;
this.description = this.description || '';
if (this.env) {
// Determine the app root
this.contextRoot = this.env.cwd;
this.destinationRoot(this.options.destinationRoot || this.env.cwd);
// Clear destionationRoot, _destinationRoot will take priority when composing, but not override passed options.
delete this.options.destinationRoot;
// Ensure source/destination path, can be configured from subclasses
this.sourceRoot(path.join(path.dirname(this.resolved), 'templates'));
this.fs = memFsEditor.create(this.env.sharedFs);
}
// Add convenience debug object
this._debug = createDebug(
this.options.namespace || 'yeoman:unknownnamespace'
);
// Expose utilities for dependency-less generators.
this._ = _;
if (this.options.help) {
return;
}
if (this.features.unique && !this.features.uniqueBy) {
const {namespace} = this.options;
let uniqueBy;
if (
this.features.unique === true ||
this.features.unique === 'namespace'
) {
uniqueBy = namespace;
} else if (
this.features.unique === 'argument' &&
this._args.length === 1
) {
const namespaceId = this.env
.requireNamespace(namespace)
.with({instanceId: this._args[0]});
uniqueBy = namespaceId.id;
} else {
throw new Error(
`Error generating a uniqueBy value. Uniqueness '${this.features.unique}' not supported by '${this.options.namespace}'`
);
}
this.features.uniqueBy = uniqueBy;
}
if (!this.env) {
throw new Error('This generator requires an environment.');
}
// Ensure the environment support features this yeoman-generator version require.
if (
!this.env ||
!this.env.adapter ||
!this.env.runLoop ||
!this.env.sharedFs ||
!this.env.fs
) {
throw new Error(
"Current environment doesn't provides some necessary feature this generator needs."
);
}
// Mirror the adapter log method on the generator.
//
// example:
// this.log('foo');
// this.log.error('bar');
this.log = this.env.adapter && this.env.adapter.log;
// Place holder for run-async callback.
this.async = () => () => {};
this.appname = this.determineAppname();
// Create config for the generator and instance
if (this._namespaceId && this._namespaceId.generator) {
this.generatorConfig = this.config.createStorage(
`:${this._namespaceId.generator}`
);
if (this._namespaceId.instanceId) {
this.instanceConfig = this.generatorConfig.createStorage(
`#${this._namespaceId.instanceId}`
);
}
}
this._globalConfig = this._getGlobalStorage();
// Queues map: generator's queue name => grouped-queue's queue name (custom name)
this._queues = {};
// Add original queues.
for (const queue of Generator.queues) {
this._queues[queue] = {priorityName: queue, queueName: queue};
}
// Add custom queues
if (Array.isArray(this.options.customPriorities)) {
this.registerPriorities(this.options.customPriorities);
}
this.compose = this.options.compose;
// Requires environment 3
if (!this.options.skipCheckEnv) {
this.checkEnvironmentVersion('3.0.0');
}
this.checkEnvironmentVersion('3.2.0', true);
}
/**
* Configure Generator behaviours.
*
* @param {Object} features
* @param {boolean|string} [features.unique] - Generates a uniqueBy id for the environment
* Accepts 'namespace' or 'true' for one instance by namespace
* Accepts 'argument' for one instance by namespace and 1 argument
*
*/
setFeatures(features) {
Object.assign(this.features, features);
}
/**
* Specifications for Environment features.
*
* @return {Object}
*/
getFeatures() {
return this.features;
}
/**
* Register priorities for this generator
*
* @param {Object[]} priorities - Priorities
* @param {String} priorities.priorityName - Priority name
* @param {String} [priorities.before] - The new priority will be queued before the `before` priority. Required for new priorities.
* @param {String} [priorities.queueName] - Name to be used at grouped-queue
* @param {boolean} [priorities.edit] - Edit a priority
* @param {boolean} [priorities.skip] - Queued manually only
* @param {Object[]|function} [priorities.args] - Arguments to pass to tasks
*/
registerPriorities(priorities) {
priorities = priorities.filter((priority) => {
if (priority.edit) {
const queue = this._queues[priority.priorityName];
if (!queue) {
throw new Error(
`Error editing priority ${priority.priorityName}, not found`
);
}
Object.assign(queue, {...priority, edit: undefined});
}
return !priority.edit;
});
const customPriorities = priorities.map((customPriority) => {
// Keep backward compatibility with name
const newPriority = {
priorityName: customPriority.name,
...customPriority
};
delete newPriority.name;
return newPriority;
});
// Sort customPriorities, a referenced custom queue must be added before the one that reference it.
customPriorities.sort((a, b) => {
if (a.priorityName === b.priorityName) {
throw new Error(`Duplicate custom queue ${a.name}`);
}
if (a.priorityName === b.before) {
return -1;
}
if (b.priorityName === a.before) {
return 1;
}
return 0;
});
// Add queue to runLoop
for (const customQueue of customPriorities) {
customQueue.queueName =
customQueue.queueName ||
`${this.options.namespace}#${customQueue.priorityName}`;
debug(`Registering custom queue ${customQueue.queueName}`);
this._queues[customQueue.priorityName] = customQueue;
if (this.env.runLoop.queueNames.includes(customQueue.queueName)) {
continue;
}
const beforeQueue = customQueue.before
? this._queues[customQueue.before].queueName
: undefined;
this.env.runLoop.addSubQueue(customQueue.queueName, beforeQueue);
}
}
checkEnvironmentVersion(packageDependency, version, warning = false) {
if (typeof version === 'boolean') {
warning = version;
version = undefined;
}
if (version === undefined) {
version = packageDependency;
packageDependency = 'yeoman-environment';
}
version = version || ENV_VER_WITH_VER_API;
const returnError = (currentVersion) => {
return new Error(
`This generator (${this.options.namespace}) requires ${packageDependency} at least ${version}, current version is ${currentVersion}, try reinstalling latest version of 'yo' or use '--ignore-version-check' option`
);
};
if (!this.env.getVersion) {
if (!this.options.ignoreVersionCheck && !warning) {
throw returnError(`less than ${ENV_VER_WITH_VER_API}`);
}
console.warn(
`It's not possible to check version with running Environment less than ${ENV_VER_WITH_VER_API}`
);
console.warn('Some features may be missing');
if (semver.lte(version, '2.8.1')) {
return undefined;
}
return false;
}
const runningVersion = this.env.getVersion(packageDependency);
if (runningVersion !== undefined && semver.lte(version, runningVersion)) {
return true;
}
if (this.options.ignoreVersionCheck || warning) {
console.warn(
`Current ${packageDependency} is not compatible with current generator, min required: ${version} current version: ${runningVersion}. Some features may be missing, try updating reinstalling 'yo'.`
);
return false;
}
throw returnError(runningVersion);
}
/**
* Convenience debug method
*
* @param {any} args parameters to be passed to debug
*/
debug(...args) {
this._debug(...args);
}
/**
* Register stored config prompts and optional option alternative.
*
* @param {Inquirer|Inquirer[]} questions - Inquirer question or questions.
* @param {Object|Boolean} [questions.exportOption] - Additional data to export this question as an option.
* @param {Storage|String} [question.storage=this.config] - Storage to store the answers.
*/
registerConfigPrompts(questions) {
questions = Array.isArray(questions) ? questions : [questions];
const getOptionTypeFromInquirerType = (type) => {
if (type === 'number') {
return Number;
}
if (type === 'confirm') {
return Boolean;
}
if (type === 'checkbox') {
return Array;
}
return String;
};
for (const q of questions) {
const question = {...q};
if (q.exportOption) {
const option =
typeof q.exportOption === 'boolean' ? {} : q.exportOption;
this.option({
name: q.name,
type: getOptionTypeFromInquirerType(q.type),
description: q.message,
...option,
storage: q.storage || this.config
});
}
this._prompts.push(question);
}
}
/**
* Prompt user to answer questions. The signature of this method is the same as {@link https://github.com/SBoudrias/Inquirer.js Inquirer.js}
*
* On top of the Inquirer.js API, you can provide a `{store: true}` property for
* every question descriptor. When set to true, Yeoman will store/fetch the
* user's answers as defaults.
*
* @param {object|object[]} questions Array of question descriptor objects. See {@link https://github.com/SBoudrias/Inquirer.js/blob/master/README.md Documentation}
* @param {Storage|String} [questions.storage] Storage object or name (generator property) to be used by the question to store/fetch the response.
* @param {Storage|String} [storage] Storage object or name (generator property) to be used by default to store/fetch responses.
* @return {Promise} prompt promise
*/
prompt(questions, storage) {
const checkInquirer = () => {
if (this.inquireSupportsPrefilled === undefined) {
this.checkEnvironmentVersion();
this.inquireSupportsPrefilled = this.checkEnvironmentVersion(
'inquirer',
'7.1.0'
);
}
};
if (storage !== undefined) {
checkInquirer();
}
const storageForQuestion = {};
const getAnswerFromStorage = (question) => {
let questionStorage = question.storage || storage;
questionStorage =
typeof questionStorage === 'string'
? this[questionStorage]
: questionStorage;
if (questionStorage) {
checkInquirer();
const {name} = question;
storageForQuestion[name] = questionStorage;
const value = questionStorage.getPath(name);
if (value !== undefined) {
question.default = (answers) => answers[name];
return [name, value];