-
Notifications
You must be signed in to change notification settings - Fork 2
/
tasks.js
374 lines (302 loc) · 9.6 KB
/
tasks.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
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
/**
* @module tasks
*
* Contains tasks that are shared between multiple sub generators.
*/
'use strict';
const fs = require('fs');
const Immutable = require('immutable');
const constants = require('./constants');
const path = require('path');
const utils = require('./utils');
/**
* Sets default properties relevant for multiple subgens on the generator object.
* @param generator instance of yeoman base generator
*/
function injectDefaultConstructor(generator) {
/**
* Map of availableExtgens generators.
* @type {Array}
*/
generator.availableExtgens = [];
/**
* Free to use message object on the generator.
* Generators may use this pojo to store arbitrary data on the generator (e.g. to pass data to another priority).
* @type {{}}
*/
generator.genData = {};
/**
* Basename of the host generator.
* @type String
*/
generator.hostBaseName = null;
/**
* Fully qualified package name of the host generator.
* @type {?String}
*/
generator.hostFullName = null;
/**
* Package entry from npm list as Json.
* @type {?Json}
*/
generator.hostPkg = null;
/**
* Json representation of all installed packages in depth=0
* We cache this value in a variable since invoking the shell command is expensive and slow.
* @type {?Json}
*/
generator.pkgList = null;
/**
* Map with configuration data for subgenext (loaded from subgenext.json if exists)
* @type {Immutable.Map}
*/
generator.subgenConfig = Immutable.Map();
generator.option('host', {
desc: 'Name of the host generator',
type: 'String'
});
generator.option('yes', {
desc: 'Confirm all yesnos with yes',
type: 'Boolean'
});
}
/**
* Injects property and configuration for positional `subgen` argument.
* @param generator
*/
function makeSubgenAware(generator) {
/**
* Name of the subgen to apply operation on.
* @type {?String}
*/
generator.subgenName = null;
/**
* Package entry from npm list as Json.
* @type {?Json}
*/
generator.subgenPkg = null;
/**
* Source directory of installed subgen.
* @type {?string}
*/
generator.subgenSrc = null;
/**
* Destination directory of subgen in host generator.
* @type {null}
*/
generator.subgenDest = null;
generator.argument('subgen', {
type: String,
required: true
});
}
/**
* Activates an external subgen by linking inside the hostgen.
* @param generator
*/
function activateSubgen(generator) {
if (!fs.existsSync(generator.subgenDest)) {
var done = generator.async();
const run = (shouldProceed) => {
if (!shouldProceed) {
generator.env.error('Operation cancelled by user.');
done();
}
fs.symlinkSync(generator.subgenSrc, generator.subgenDest);
generator.genData.activation = utils.buildSuccess({});
done();
};
if (utils.shouldAutoConfirmYesnos(generator.options.yes)) {
run(true);
} else {
generator.prompt({
type: 'confirm',
name: 'proceed',
message: `I will create a symlink\nfrom =>\t${generator.subgenSrc}\nin =>\t${generator.subgenDest}\nConfirm to proceed:`
})
.then(
answers => run(answers.proceed)
);
}
} else {
const err = 'Subgen with name ' + generator.subgenName + ' is already activated. If you want to update it, make ' +
'sure to deactivate it first. This is a NOOP.';
generator.genData.activation = utils.buildError(err);
}
}
/**
* Activates an external subgen by unlinking it from the hostgen.
* @param generator
*/
function deactivateSubgen(generator) {
if (fs.existsSync(generator.subgenDest)) {
var done = generator.async();
const run = (shouldProceed) => {
if (!shouldProceed) {
generator.env.error('Operation cancelled by user.');
done();
}
fs.unlinkSync(generator.subgenDest);
generator.genData.deactivation = utils.buildSuccess({});
done();
};
if (utils.shouldAutoConfirmYesnos(generator.options.yes)) {
run(true);
} else {
generator.prompt({
type: 'confirm',
name: 'proceed',
message: `I will remove this symlink\n=> ${generator.subgenDest}\nConfirm to proceed:`
})
.then(
answers => run(answers.proceed)
);
}
} else {
const err = `Subgen with name "${generator.subgenName}" doesn't seem to be activated. This is a NOOP.`;
generator.genData.deactivation = utils.buildError(err);
}
}
function loadSubgenConfig(generator) {
const cfgPath = path.join(generator.env.cwd, 'subgenext.json');
if (generator.fs.exists(cfgPath)) {
generator.subgenConfig = generator.subgenConfig.mergeDeep(Immutable.fromJS(require(cfgPath)));
}
}
/**
* Validates input for --host option.
*/
function validateHostName(generator) {
const defaultHost = generator.subgenConfig.get('defaultHost', false);
if (typeof generator.options.host === 'undefined' && !defaultHost) {
generator.env.error('Please provide the name of the host generator by appending --host=<generator-name>');
}
generator.hostBaseName = generator.options.host || defaultHost;
generator.hostFullName = 'generator-' + generator.hostBaseName;
}
/**
* Validates input for the positional `subgen` argument
*/
function validateSubgenName(generator) {
generator.subgenName = generator.subgen;
}
/**
* Caches root paths to installed npm packages into class member.
*/
function cacheInstalledPackages(generator) {
generator.pkgList = utils.populatePkgStoreFromPaths(
utils.getInstalledPkgPaths(generator.env.getNpmPaths())
);
}
/**
* Validates that Yeoman can resolve the host generator.
*/
function validateHostgenExists(generator) {
if (!generator.env.getGeneratorNames().some(name => name === generator.hostBaseName)) {
generator.env.error(`Couldn't verify that host generator ${generator.hostFullName} is installed.`);
}
}
/**
* Populates `hostPkg` class member with package information from the host generator.
* @param generator
*/
function populateHostgenPkg(generator) {
validateHostgenExists(generator);
const pkgQ = utils.getPkgInfo(generator.hostFullName, generator.pkgList);
generator.hostPkg = pkgQ.data.get('pkg');
}
/**
* Validates that the required sub generator exists in the installed packages.
*
* @depends
*/
function validateSubgenExists(generator) {
// TODO Fix ambiguity problems with subgen validation check.
// Currently all we do is matching `subgenName` as a substring, which has two problems:
// 1. ambiguous when multiple subgens share a token, e.g. subgen-helloworld, subgen-helloworld-evening
// 2. ambiguous on vendor and contrib subgens with same name (e.g. subgen-controller and contrib-subgen-controller)
if (!utils.checkPkgExists(generator.subgenName, generator.availableExtgens, false)) {
generator.env.error(`Couldn't verify that subgen ${generator.subgenName} is installed.`);
}
}
/**
* Scans package for installed subgens.
*/
function scanForInstalledSubgens(generator) {
const prefix = constants.SUBGEN_PREFIX_PATTERNS;
const hostName = generator.hostBaseName;
const pkgList = generator.pkgList;
const extgens = utils.findExternalSubgens(prefix, hostName, pkgList);
generator.availableExtgens = extgens.data.get('results');
}
/**
* Adds activation state information to the list of availableExtgens external subgens.
*/
function checkActivationState(generator) {
const hostPkg = generator.hostPkg
generator.availableExtgens = generator.availableExtgens.map((subgen) => {
const subgenBaseName = subgen.get('basename');
const activationState = utils.checkActivationState(hostPkg, subgenBaseName).data.get('result');
return subgen.set('isActivated', activationState);
});
}
/**
* Populates class member with subgen package information.
* @param generator
*/
function setSubgenPkg(generator) {
const subgen = utils.getPkgInfo(generator.subgenName, generator.availableExtgens, false);
generator.subgenPkg = subgen.data.get('pkg');
}
/**
* Populates class member with subgen source path.
*/
function setSubgenSrcPath(generator) {
const pkg = generator.subgenPkg;
generator.subgenSrc = path.join(pkg.get('path'), 'generators', generator.subgenName);
}
/**
* Validates the existence of a subgen and populates it.
*/
function populateSubgenPkg(generator) {
validateSubgenExists(generator);
setSubgenPkg(generator);
}
/**
* Populates class member with subgen destination path.
*/
function setSubgenDestPath(generator) {
const pkg = generator.hostPkg;
generator.subgenDest = path.join(pkg.get('path'), 'generators', generator.subgenName);
}
/**
* Adds information whether found subgens are declared to be compatible with the hostgen version.
*/
function validateCompatibility(generator) {
const hostPkg = generator.hostPkg;
const subgenPkg = generator.subgenPkg;
const dependency = utils.checkHostgenDependency(hostPkg, subgenPkg);
if(dependency.hasError) {
const available = dependency.data.get('err').get('available');
const required = dependency.data.get('err').get('required');
generator.env.error(`Couldn't verify that host generator ${generator.hostFullName} satisfies the sub generators dependency.\nFound ${
available}, required ${required}`);
}
}
module.exports = {
activateSubgen,
cacheInstalledPackages,
checkActivationState,
deactivateSubgen,
setSubgenDestPath,
setSubgenSrcPath,
injectDefaultConstructor,
loadSubgenConfig,
makeSubgenAware,
populateHostgenPkg,
populateSubgenPkg,
scanForInstalledSubgens,
validateCompatibility,
validateHostName,
validateSubgenName
};