-
Notifications
You must be signed in to change notification settings - Fork 72
/
index.js
661 lines (609 loc) · 22.7 KB
/
index.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
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
"use strict";
const Generator = require("yeoman-generator");
const chalk = require("chalk");
const yosay = require("yosay");
const spawn = require("cross-spawn");
const { hasYarn } = require("yarn-or-npm");
const path = require("path");
const fs = require("fs");
const { rm } = require("fs").promises;
const glob = require("glob");
const { request } = require("@octokit/request");
const { Octokit } = require("@octokit/rest");
const { throttling } = require("@octokit/plugin-throttling");
const MyOctokit = Octokit.plugin(throttling);
const AdmZip = require("adm-zip");
// helper to retrieve config entries from npm
// --> npm config set easy-ui5_addGhOrg XYZ
const NPM_CONFIG_PREFIX = "easy-ui5_";
let npmConfig;
const getNPMConfig = (configName) => {
if (!npmConfig) {
npmConfig = require("libnpmconfig").read();
}
return npmConfig && npmConfig[`${NPM_CONFIG_PREFIX}${configName}`]
}
// the command line options of the generator
const generatorOptions = {
ghAuthToken: {
type: String,
description:
"GitHub authToken to optionally access private generator repositories",
npmConfig: true
},
ghOrg: {
type: String,
description: "GitHub organization to lookup for available generators",
default: "ui5-community",
hide: true // we don't want to recommend to use this option
},
subGeneratorPrefix: {
type: String,
description: "Prefix used for the lookup of the available generators",
default: "generator-ui5-",
hide: true // we don't want to recommend to use this option
},
addGhOrg: {
type: String,
description: `GitHub organization to lookup for additional available generators`,
hide: true, // we don't want to recommend to use this option
npmConfig: true
},
addSubGeneratorPrefix: {
type: String,
description: `Prefix used for the lookup of the additional available generators`,
default: "generator-",
hide: true, // we don't want to recommend to use this option
npmConfig: true
},
list: {
type: Boolean,
description: "List the available subcommands of the generator",
},
skipUpdate: {
type: Boolean,
description: "Skip the update of the plugin generator",
},
forceUpdate: {
type: Boolean,
description: "Force the update of the plugin generator",
},
offline: {
type: Boolean,
alias: "o",
description: "Running easy-ui5 in offline mode",
},
verbose: {
type: Boolean,
description: "Enable detailed logging",
},
plugins: {
type: Boolean,
alias: "p",
description: "Get detailed version information",
},
next: {
type: Boolean,
description: "Preview the next mode to consume templates from bestofui5.org",
},
};
const generatorArgs = {
generator: {
type: String,
required: false,
description: "Name of the generator to invoke (without the \"generator-ui5-\" prefix)",
},
subcommand: {
type: String,
required: false,
description: "Name of the subcommand to invoke (without the \"generator:\" prefix)",
},
};
module.exports = class extends Generator {
constructor(args, opts) {
super(args, opts, {
// disable the Yeoman 5 package-manager logic (auto install)!
customInstallTask: "disabled"
});
Object.keys(generatorArgs).forEach((argName) => {
// register the argument for being displayed in the help
this.argument(argName, generatorArgs[argName]);
});
Object.keys(generatorOptions).forEach((optionName) => {
const initialValue = this.options[optionName];
// register the option for being displayed in the help
this.option(optionName, generatorOptions[optionName]);
const defaultedValue = this.options[optionName];
if (generatorOptions[optionName].npmConfig) {
// if a value is set, use the set value (parameter has higher precedence than npm config)
// => this.option(...) applies the default value to this.options[...] used as last resort
this.options[optionName] = initialValue || getNPMConfig(optionName) || defaultedValue;
}
});
}
_showBusy(statusText) {
this._clearBusy();
const progressChars = ["\\", "|", "/", "-"];
let i = 0;
process.stdout.write(`\r${statusText} `);
this._busy = {
text: statusText,
timer: setInterval(() => {
process.stdout.write(`\r${statusText} ${progressChars[i++]}`);
i %= progressChars.length;
}, 250),
};
}
_clearBusy(newLine) {
if (this._busy) {
clearInterval(this._busy.timer);
process.stdout.write("\r".padEnd(this._busy.text.length + 3) + (newLine ? "\n" : ""));
delete this._busy;
}
}
async prompting() {
// Have Yeoman greet the user.
if (!this.options.embedded) {
this.log(yosay(`Welcome to the ${chalk.red("easy-ui5")} generator!`));
}
const home = path.join(__dirname, "..", "..");
// check the permissions to Easy UI5s plugin directory which must
// allow read/write to install additional plugin generators
let pluginsHome = path.join(home, "plugin-generators");
try {
fs.accessSync(pluginsHome, fs.constants.R_OK | fs.constants.W_OK);
} catch (e) {
pluginsHome = path.join(require("os").homedir(), ".npm", "_generator-easy-ui5", "plugin-generators");
if (this.options.verbose) {
console.error(`Plugin directory: ${chalk.green(pluginsHome)}`);
console.error(chalk.red(e.message));
}
fs.mkdirSync(pluginsHome, { recursive: true });
}
// log the plugins and configuration
if (this.options.plugins) {
const yeoman = require("yeoman-environment/package.json");
const components = {
"Node.js": process.version,
"yeoman-environment": yeoman.version,
"generator-easy-ui5": require(path.join(home, "package.json")).version,
"home": home,
"pluginsHome": pluginsHome,
};
Object.keys(components).forEach((component) => {
this.log(`${chalk.green(component)}: ${components[component]}`);
});
this.log(chalk.green("\nAvailable generators:"));
glob.sync(`${pluginsHome}/*/package.json`).forEach((plugin) => {
const name = plugin.match(/.*\/generator-(.+)\/package\.json/)[1];
const lib = require(plugin);
this.log(` - ${chalk.green(name)}: ${lib.version}`);
});
return;
}
// create the octokit client to retrieve the generators from GH org
// when not running in offline mode!
let octokit;
if (this.options.offline) {
this.log(
`Running in ${chalk.yellow(
"offline"
)} mode!`
);
} else {
octokit = new MyOctokit({
userAgent: `${this.rootGeneratorName()}:${this.rootGeneratorVersion()}`,
auth: this.options.ghAuthToken,
throttle: {
onRateLimit: (retryAfter, options) => {
this.log(
`${chalk.yellow("Hit the GitHub API limit!")} Request quota exhausted for this request.`
);
if (options.request.retryCount === 0) {
// only retries once
this.log(`Retrying after ${retryAfter} seconds. Alternatively, you can cancel this operation and supply an auth token with the \`--ghAuthToken\` option. For more details, run \`yo easy-ui5 --help\`. `);
return true;
}
},
onAbuseLimit: () => {
// does not retry, only logs a warning
this.log(
`${chalk.red("Hit the GitHub API limit again!")} Please supply an auth token with the \`--ghAuthToken\` option. For more details, run \`yo easy-ui5 --help\` `
);
},
}
});
}
// helper for filtering repos with corresponding subGenerator prefix
const filterReposWithSubGeneratorPrefix = (repos, subGeneratorPrefix) => {
if (!Array.isArray(repos)) {
return [];
}
return repos.filter((repo) =>
repo.name.startsWith(`${subGeneratorPrefix}`)
).map((repo) => {
return {
org: repo.owner?.login,
name: repo.name,
branch: repo.default_branch,
subGeneratorName: repo.name.substring(subGeneratorPrefix.length),
};
});
};
// helper to retrieve the available repositories for a GH org
const listGeneratorsForOrg = async (ghOrg, subGeneratorPrefix) => {
const response = await octokit.repos.listForOrg({
org: ghOrg,
});
return filterReposWithSubGeneratorPrefix(response?.data, subGeneratorPrefix);
};
// helper to retrieve the available repositories for a GH user
const listGeneratorsForUser = async (ghUser, subGeneratorPrefix) => {
const response = await octokit.repos.listForUser({
username: ghUser,
});
return filterReposWithSubGeneratorPrefix(response?.data, subGeneratorPrefix);
};
// determine the generator to be used
let generator;
// try to identify whether concrete generator is defined
if (!generator) {
// determine generator by ${owner}/${repo}(!${dir})? syntax, e.g.:
// > yo easy-ui5 SAP-samples/ui5-typescript-tutorial
// > yo easy-ui5 SAP-samples/ui5-typescript-tutorial#1.0
// > yo easy-ui5 SAP-samples/ui5-typescript-tutorial\!/generator
// > yo easy-ui5 SAP-samples/ui5-typescript-tutorial\!/generator#1.0
const reGenerator = /([^\/]+)\/([^\!\#]+)(?:\!([^\#]+))?(?:\#(.+))?/;
const matchGenerator = reGenerator.exec(this.options.generator);
if (matchGenerator) {
// derive and path the generator information from command line
const [owner, repo, dir = "/generator", branch] = matchGenerator.slice(1);
generator = {
org: owner,
name: repo,
branch,
dir,
pluginPath: `_/${owner}/${repo}`,
};
// log which generator is being used!
if (this.options.verbose) {
this.log(
`Using generator ${chalk.green(
`${owner}/${repo}!${dir}${branch ? "#" + branch : ""}`
)}`
);
}
}
}
// retrieve the available repositories (if no generator is specified specified directly)
let availGenerators;
if (!generator) {
if (this.options.offline) {
availGenerators = glob.sync(`${pluginsHome}/generator-*/package.json`).map((plugin) => {
const match = plugin.match(/.*\/(generator-(.+))\/package\.json/);
return {
org: "local",
name: match[1],
subGeneratorName: match[2].match(/(?:ui5-)?(.*)/)?.[1] || match[2],
local: true,
}
});
} else {
if (this.options.next) {
// check bestofui5.org for generators
try {
const response = await request({
method: "GET",
url: "https://raw.githubusercontent.com/ui5-community/bestofui5-data/live-data/data/data.json",
});
const data = JSON.parse(response.data);
availGenerators = data?.packages?.filter(entry => {
return entry.type === "generator";
}).map(entry => {
return {
org: entry.gitHubOwner,
name: entry.gitHubRepo,
subGeneratorName: entry.gitHubRepo.match(/(?:generator-(?:ui5-)?)(.*)/)?.[1] || entry.gitHubRepo,
};
});
} catch (e) {
console.error(`Failed to connect to bestofui5.org to retrieve all available generators! Run with --verbose for details!`);
if (this.options.verbose) {
console.error(e);
}
return;
}
} else {
// check the main GH org for generators
try {
availGenerators = await listGeneratorsForOrg(this.options.ghOrg, this.options.subGeneratorPrefix);
} catch (e) {
console.error(`Failed to connect to GitHub to retrieve all available generators for "${this.options.ghOrg}" organization! Run with --verbose for details!`);
if (this.options.verbose) {
console.error(e);
}
return;
}
// check the additional GH org for generators with a different prefix
try {
if (this.options.addGhOrg && this.options.addSubGeneratorPrefix) {
availGenerators = availGenerators.concat(await listGeneratorsForOrg(this.options.addGhOrg, this.options.addSubGeneratorPrefix));
}
} catch (e) {
if (this.options.verbose) {
this.log(`Failed to connect to GitHub retrieve additional generators for "${this.options.addGhOrg}" organization! Try to retrieve for user...`);
}
try {
availGenerators = availGenerators.concat(await listGeneratorsForUser(this.options.addGhOrg, this.options.addSubGeneratorPrefix));
} catch (e) {
console.error(`Failed to connect to GitHub to retrieve additional generators for organization or user "${this.options.addGhOrg}"! Run with --verbose for details!`);
if (this.options.verbose) {
console.error(e);
}
return;
}
}
}
}
}
// if no generator is provided and doesn't exist, ask for generator name
if (!generator) {
// check for provided generator being available on GH
generator = this.options.generator && availGenerators.find((repo) =>
repo.subGeneratorName === this.options.generator
);
// if no generator is provided and doesn't exist, ask for generator name
if (this.options.generator && !generator) {
this.log(
`The generator ${chalk.red(
this.options.generator
)} was not found. Please select an existing generator!`
);
}
// still not found, select a generator
if (!generator) {
const generatorIdx = (
await this.prompt([
{
type: "list",
name: "generator",
message: "Select your generator?",
choices: availGenerators.map((availGenerator, idx) => ({
name: `${availGenerator.subGeneratorName}${this.options.addGhOrg ? ` [${availGenerator.org}]` : ""}`,
value: idx,
})),
},
])
).generator;
generator = availGenerators[generatorIdx];
}
}
let generatorPath = path.join(pluginsHome, generator.pluginPath || generator.name);
if (!this.options.offline) {
// lookup the default path of the generator if not set
if (!generator.branch) {
try {
const repoInfo = await octokit.repos.get({
owner: generator.org,
repo: generator.name,
});
generator.branch = repoInfo.data.default_branch;
} catch (e) {
console.error(`Generator "${owner}/${repo}!${dir}${branch ? "#" + branch : ""}" not found! Run with --verbose for details!`);
if (this.options.verbose) {
console.error(e);
}
return;
}
}
// fetch the branch to retrieve the latest commit SHA
let commitSHA;
try {
// determine the commitSHA
const reqBranch = await octokit.repos.getBranch({
owner: generator.org,
repo: generator.name,
branch: generator.branch,
});
commitSHA = reqBranch.data.commit.sha;
} catch (e) {
console.error(chalk.red(`Failed to retrieve the branch "${generator.branch}" for repository "${generator.name}" for "${generator.org}" organization! Run with --verbose for details!`));
if (this.options.verbose) {
console.error(chalk.red(e.message));
}
return;
}
if (this.options.verbose) {
this.log(
`Using commit ${commitSHA} from @${generator.org}/${generator.name}#${generator.branch}...`
);
}
const shaMarker = path.join(generatorPath, `.${commitSHA}`);
if (fs.existsSync(generatorPath) && !this.options.skipUpdate) {
// check if the SHA marker exists to know whether the generator is up-to-date or not
if (this.options.forceUpdate || !fs.existsSync(shaMarker)) {
if (this.options.verbose) {
this.log(`Generator "${generator.name}" in "${generatorPath}" is outdated...`);
}
// remove if the SHA marker doesn't exist => outdated!
this._showBusy(` Removing old "${generator.name}" templates`);
await rm(generatorPath, { recursive: true });
}
}
// re-fetch the generator and extract into local plugin folder
if (!fs.existsSync(generatorPath)) {
if (this.options.verbose) {
this.log(`Extracting ZIP to "${generatorPath}"...`);
}
this._showBusy(` Downloading and extracting "${generator.name}" templates`);
const reqZIPArchive = await octokit.repos.downloadZipballArchive({
owner: generator.org,
repo: generator.name,
ref: commitSHA,
});
const buffer = Buffer.from(new Uint8Array(reqZIPArchive.data));
const zip = new AdmZip(buffer);
const zipEntries = zip.getEntries();
zipEntries.forEach((entry) => {
const match =
!entry.isDirectory && entry.entryName.match(/[^\/]+(\/.+)/);
let entryPath;
if (generator.dir && match && match[1].startsWith(generator.dir)) {
entryPath = path.dirname(match[1].substring(generator.dir.length));
} else if (!generator.dir && match) {
entryPath = path.dirname(match[1]);
}
if (entryPath) {
zip.extractEntryTo(
entry,
path.join(generatorPath, entryPath),
false,
true
);
}
});
fs.writeFileSync(shaMarker, commitSHA);
// run yarn/npm install
if (this.options.verbose) {
this.log("Installing the plugin dependencies...");
}
this._showBusy(` Preparing "${generator.name}"`);
await new Promise(function (resolve, reject) {
spawn((hasYarn() ? "yarn" : "npm"), ["install", "--no-progress", "--ignore-engines"], {
stdio: this.config.verbose ? "inherit" : "ignore",
cwd: generatorPath,
env: {
...process.env,
"NO_UPDATE_NOTIFIER": true
}
}).on("exit", function (code) {
resolve(code);
}).on("error", function (err) {
reject(err);
});
}.bind(this));
}
this._clearBusy(true);
}
// filter the local options and the help command
const opts = Object.keys(this._options).filter(
(optionName) =>
!(generatorOptions.hasOwnProperty(optionName) || optionName === "help")
);
// create the env for the plugin generator
const yeoman = require("yeoman-environment");
const env = yeoman.createEnv(this.args, opts);
// helper to derive the subcommand
function deriveSubcommand(namespace) {
const match = namespace.match(/[^:]+:(.+)/);
return match ? match[1] : namespace;
}
// filter the hidden subgenerators already
// -> subgenerators must be found in env as they are returned by lookup!
let subGenerators = env
.lookup({ localOnly: true, packagePaths: generatorPath })
.filter((sub) => {
const subGenerator = env.get(sub.namespace);
return !subGenerator.hidden;
});
// list the available subgenerators in the console (as help)
if (this.options.list) {
let maxLength = 0;
this.log(subGenerators
.map(sub => {
maxLength = Math.max(sub.namespace.length, maxLength);
return sub;
})
.reduce((output, sub) => {
const subGenerator = env.get(sub.namespace);
const displayName = subGenerator.displayName || "";
let line = ` ${deriveSubcommand(sub.namespace).padEnd(maxLength + 2)}`;
if (displayName) {
line += ` # ${subGenerator.displayName}`;
}
return `${output}\n${line}`;
}, `Subcommands (${subGenerators.length}):`));
return;
}
// if a subcommand is provided as argument, identify the matching subgenerator
// and remove the rest of the subgenerators from the list for later steps
if (this.options.subcommand) {
const selectedSubGenerator = subGenerators
.filter((sub) => {
// identify the subgenerator by subcommand
return new RegExp(`:${this.options.subcommand}$`).test(sub.namespace);
});
if (selectedSubGenerator.length == 1) {
subGenerators = selectedSubGenerator;
} else {
this.log(
`The generator ${chalk.red(
this.options.generator
)} has no subcommand ${chalk.red(
this.options.subcommand
)}. Please select an existing subcommand!`
);
}
}
// transform the list of the subgenerators and identify the
// default subgenerator for the default selection
let defaultSubGenerator;
let maxLength = 0;
subGenerators = subGenerators
.map(sub => {
const generator = env.get(sub.namespace);
let subcommand = deriveSubcommand(sub.namespace);
let displayName = generator.displayName || subcommand;
maxLength = Math.max(displayName.length, maxLength);
return {
subcommand,
displayName,
sub,
};
})
.map(({ subcommand, displayName, sub }) => {
const transformed = {
name: `${displayName.padEnd(maxLength + 2)} [${subcommand}]`,
value: sub.namespace,
};
if (/:app$/.test(sub.namespace)) {
defaultSubGenerator = transformed;
}
return transformed;
});
// at least 1 subgenerator must be present
if (subGenerators.length >= 1) {
// by default the 1st subgenerator is used
let subGenerator = subGenerators[0].value;
// if more than 1 subgenerator is present
// ask the developer to select one!
if (subGenerators.length > 1) {
subGenerator = (
await this.prompt([
{
type: "list",
name: "subGenerator",
message: "What do you want to do?",
default: defaultSubGenerator && defaultSubGenerator.value,
choices: subGenerators,
},
])
).subGenerator;
}
if (this.options.verbose) {
this.log(`Calling ${chalk.red(subGenerator)}...\n \\_ in: ${generatorPath}`);
}
// finally, run the subgenerator
env.run(subGenerator, {
verbose: this.options.verbose,
embedded: true,
});
} else {
this.log(
`The generator ${chalk.red(
this.options.generator
)} has no visible subgenerators!`
);
}
}
};