-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathindex.js
More file actions
688 lines (627 loc) · 21.6 KB
/
Copy pathindex.js
File metadata and controls
688 lines (627 loc) · 21.6 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
// Copyright IBM Corp. and LoopBack contributors 2018,2020. All Rights Reserved.
// Node module: @loopback/cli
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
const BaseGenerator = require('../../lib/base-generator');
const {debug, debugJson, validateUrlOrFile, escapeComment} = require('./utils');
const {loadAndBuildSpec} = require('./spec-loader');
const utils = require('../../lib/utils');
const {parse} = require('url');
const path = require('path');
const semver = require('semver');
const slash = require('slash');
const {getControllerFileName, getServiceFileName} = require('./spec-helper');
const updateIndex = require('../../lib/update-index');
const MODEL = 'models';
const CONTROLLER = 'controllers';
const DATASOURCE = 'datasources';
const SERVICE = 'services';
const g = require('../../lib/globalize');
const json5 = require('json5');
const fs = require('fs');
const {Project, SyntaxKind} = require('ts-morph');
const isWindows = process.platform === 'win32';
module.exports = class OpenApiGenerator extends BaseGenerator {
// Note: arguments and options should be defined in the constructor.
constructor(args, opts) {
super(args, opts);
}
_setupGenerator() {
this.argument('url', {
description: g.f('URL or file path of the OpenAPI spec'),
required: false,
type: String,
});
this.option('url', {
description: g.f('URL or file path of the OpenAPI spec'),
required: false,
type: String,
});
this.option('validate', {
description: g.f('Validate the OpenAPI spec'),
required: false,
default: false,
type: Boolean,
});
this.option('server', {
description: g.f('Generate server-side controllers for the OpenAPI spec'),
required: false,
default: true,
type: Boolean,
});
this.option('client', {
description: g.f(
'Generate client-side service proxies for the OpenAPI spec',
),
required: false,
default: false,
type: Boolean,
});
this.option('datasource', {
type: String,
required: false,
description: g.f('A valid datasource name for the OpenAPI endpoint'),
});
this.option('baseModel', {
description: g.f('Base model class'),
required: false,
default: '',
type: String,
});
this.option('promote-anonymous-schemas', {
description: g.f('Promote anonymous schemas as models'),
required: false,
default: false,
type: Boolean,
});
this.option('outDir', {
description: g.f('Custom output directory for generated files.'),
required: false,
default: 'src',
type: String,
});
return super._setupGenerator();
}
setOptions() {
const result = super.setOptions();
if (this.options.config) {
const config =
typeof this.options.config === 'string'
? JSON.parse(this.options.config)
: this.options.config;
if (config.outDir) this.options.outDir = config.outDir;
if (config.url) this.options.url = config.url;
if (config.prefix) this.options.prefix = config.prefix;
if (config.client !== undefined) this.options.client = config.client;
if (config.server !== undefined) this.options.server = config.server;
}
return result;
}
checkLoopBackProject() {
return super.checkLoopBackProject();
}
async promptDataSourceName() {
if (this.shouldExit()) return false;
if (
this.options.baseModel &&
this.options.baseModel !== 'Model' &&
this.options.baseModel !== 'Entity'
) {
this.exit(
`Invalid baseModel: ${this.options.baseModel}. Valid values: Model, Entity.`,
);
return;
}
if (this.options.client !== true) return;
if (this.options.url) return;
debug('Prompting for a datasource');
// grab the datasource from the command line
const dsClass = this.options.datasource
? utils.toClassName(this.options.datasource) + 'DataSource'
: '';
debug(`command line datasource is ${dsClass}`);
const dataSourcesDir = this.destinationPath(
`${utils.sourceRootDir}/${utils.datasourcesDir}`,
);
const dataSourceClasses = await utils.getArtifactList(
dataSourcesDir,
'datasource',
true,
);
// List all data sources
const dataSourceList = dataSourceClasses.map(ds => ({
className: ds,
}));
debug('datasourceList from %s', dataSourcesDir, dataSourceList);
// Find openapi data sources
const openApiDataSources = dataSourceList.filter(ds => {
debug(`data source inspecting item: ${ds.className}`);
const dsObj = utils.getDataSourceConfig(dataSourcesDir, ds.className);
if (
dsObj.connector === 'openapi' ||
dsObj.connector === 'loopback-connector-openapi'
) {
ds.usePositionalParams = dsObj.positional;
ds.name = dsObj.name;
ds.className = utils.toClassName(dsObj.name) + 'DataSource';
ds.specPath = dsObj.spec;
return true;
}
});
debug('Data sources using openapi connector', openApiDataSources);
if (openApiDataSources.length === 0) {
return;
}
const matchedDs = openApiDataSources.find(ds => ds.className === dsClass);
if (matchedDs) {
this.dataSourceInfo = {
name: matchedDs.name,
className: matchedDs.className,
usePositionalParams: matchedDs.usePositionalParams,
specPath: matchedDs.specPath,
};
this.log(
g.f('Datasource %s - %s found for OpenAPI: %s'),
this.options.datasource,
this.dataSourceInfo.className,
this.dataSourceInfo.specPath,
);
return;
}
if (dsClass) return;
const answers = await this.prompt([
{
type: 'list',
name: 'dataSource',
message: g.f('Please select the datasource'),
choices: openApiDataSources.map(ds => ({
name: `${ds.name} - ${ds.className}`,
value: ds,
})),
default: `${openApiDataSources[0].name} - ${openApiDataSources[0].className}`,
validate: utils.validateClassName,
},
]);
debug('Datasource selected', answers);
if (answers && answers.dataSource) {
this.dataSourceInfo = {
name: answers.dataSource.name,
className: answers.dataSource.className,
usePositionalParams: answers.dataSource.usePositionalParams,
specPath: answers.dataSource.specPath,
};
this.log(
g.f('Datasource %s - %s selected: %s'),
this.dataSourceInfo.name,
this.dataSourceInfo.className,
this.dataSourceInfo.specPath,
);
}
}
async askForSpecUrlOrPath() {
if (this.shouldExit()) return;
if (this.dataSourceInfo && this.dataSourceInfo.specPath) {
this.url = this.dataSourceInfo.specPath;
return;
}
const prompts = [
{
name: 'url',
message: g.f('Enter the OpenAPI spec url or file path:'),
default: this.options.url,
validate: validateUrlOrFile,
when: this.options.url == null,
},
];
const answers = await this.prompt(prompts);
if (answers.url) {
this.url = answers.url.trim();
} else {
this.url = this.options.url;
}
}
async loadAndBuildApiSpec() {
if (this.shouldExit()) return;
try {
const result = await loadAndBuildSpec(this.url, {
log: this.log,
validate: this.options.validate,
promoteAnonymousSchemas: this.options['promote-anonymous-schemas'],
prefix:
this.options.outDir && this.options.outDir !== 'src'
? ''
: this.options.prefix,
previousPrefix: this.options.previousPrefix || '',
});
debugJson('OpenAPI spec', result.apiSpec);
Object.assign(this, result);
} catch (e) {
this.exit(e);
}
}
async selectControllers() {
if (this.shouldExit()) return;
this.controllerSpecs = this.controllerSpecs.map(c => {
if (this.options.prefix && c.tag && c.tag.includes(this.options.prefix)) {
const splited = c.tag.split(this.options.prefix);
c.tag = splited.join('');
}
return c;
});
const choices = this.controllerSpecs.map(c => {
const names = [];
if (this.options.server !== false) {
names.push(c.className);
}
if (this.options.client === true) {
names.push(c.serviceClassName);
}
const name = c.tag ? `[${c.tag}] ${names.join(' ')}` : names.join(' ');
return {
name,
value: c.className,
checked: true,
};
});
const prompts = [
{
name: 'controllerSelections',
message: g.f(
'Select controllers and/or service proxies to be generated:',
),
type: 'checkbox',
choices: choices,
default: choices.map(c => c.value),
// Require at least one item to be selected
// This prevents users from accidentally pressing ENTER instead of SPACE
// to select an item from the list
validate: result => !!result.length,
},
];
const selections = (await this.prompt(prompts)).controllerSelections;
this.selectedControllers = this.controllerSpecs.filter(c =>
selections.some(a => a === c.className),
);
this.selectedServices = this.selectedControllers;
this.selectedControllers.forEach(c => {
const originalClassName = c.className;
const originalServiceClassName = c.serviceClassName;
c.fileName = getControllerFileName(c.tag || originalClassName);
if (
this.options.prefix &&
(!this.options.outDir || this.options.outDir === 'src')
) {
c.fileName = this.options.prefix.toLowerCase() + '.' + c.fileName;
}
c.serviceFileName = getServiceFileName(c.tag || originalServiceClassName);
});
}
_generateControllers(withImplementation) {
const source = this.templatePath(
'src/controllers/controller-template.ts.ejs',
);
for (const c of this.selectedControllers) {
const controllerFile = c.fileName;
c.withImplementation = withImplementation;
if (debug.enabled) {
debug(`Artifact output filename set to: ${controllerFile}`);
}
const outDir = this.options.outDir || 'src';
const dest = this.destinationPath(
`${outDir}/controllers/${controllerFile}`,
);
if (debug.enabled) {
debug('Copying artifact to: %s', dest);
}
this.copyTemplatedFiles(source, dest, mixinEscapeComment(c));
}
}
async _generateDataSource() {
let specPath = this.url;
const parsed = parse(this.url);
if (
// Relative paths and UNIX paths don't have any protocol set
parsed.protocol == null ||
// Support absolute Windows paths, e.g. "C:\some\dir\api.yaml"
// When such path is parsed as a URL, we end up with the drive ("C:")
// recognized as the protocol.
(isWindows && parsed.protocol.match(/^[a-zA-Z]:$/))
) {
specPath = path.relative(this.destinationRoot(), this.url);
if (isWindows && !path.parse(specPath).root) {
// On Windows, convert the relative path to use Unix-style separator
// We need this behavior for our snapshot-based tests, but @bajtos
// thinks it is also nicer for users - at the end of the day,
// this is a spec URL and URLs always use forward-slash characters
specPath = slash(specPath);
}
}
this.dataSourceInfo.specPath = specPath;
const dsConfig = {
name: this.dataSourceInfo.name,
connector: 'openapi',
spec: this.dataSourceInfo.specPath,
validate: false,
positional:
this.dataSourceInfo.usePositionalParams !== false ? 'bodyLast' : false,
};
this.dataSourceInfo.dsConfigString = json5.stringify(dsConfig, null, 2);
const classTemplatePath = this.templatePath(
'src/datasources/datasource.ts.ejs',
);
const dataSourceFile = this.dataSourceInfo.outFile;
if (debug.enabled) {
debug(`Artifact output filename set to: ${dataSourceFile}`);
}
const outDir = this.options.outDir || 'src';
const dest = this.destinationPath(
`${outDir}/datasources/${dataSourceFile}`,
);
if (debug.enabled) {
debug('Copying artifact to: %s', dest);
}
const context = {...this.dataSourceInfo};
let dsClass = context.className;
dsClass = dsClass.endsWith('DataSource')
? dsClass.substring(0, dsClass.length - 'DataSource'.length)
: dsClass;
context.className = dsClass;
this.copyTemplatedFiles(classTemplatePath, dest, context);
this.dataSources = [{fileName: dataSourceFile}];
}
_generateServiceProxies() {
const source = this.templatePath(
'src/services/service-proxy-template.ts.ejs',
);
for (const c of this.selectedControllers) {
const file = c.serviceFileName;
if (debug.enabled) {
debug(`Artifact output filename set to: ${file}`);
}
const outDir = this.options.outDir || 'src';
const dest = this.destinationPath(`${outDir}/services/${file}`);
if (debug.enabled) {
debug('Copying artifact to: %s', dest);
}
const context = {
...mixinEscapeComment(c),
usePositionalParams: this.dataSourceInfo.usePositionalParams,
dataSourceName: this.dataSourceInfo.name,
dataSourceClassName: this.dataSourceInfo.className,
};
this.copyTemplatedFiles(source, dest, context);
}
}
_generateModels() {
const modelSource = this.templatePath('src/models/model-template.ts.ejs');
const typeSource = this.templatePath('src/models/type-template.ts.ejs');
for (const m of this.modelSpecs) {
if (!m.fileName) continue;
m.baseModel = this.options.baseModel;
const modelFile = m.fileName;
if (debug.enabled) {
debug(`Artifact output filename set to: ${modelFile}`);
}
const outDir = this.options.outDir || 'src';
const dest = this.destinationPath(`${outDir}/models/${modelFile}`);
if (debug.enabled) {
debug('Copying artifact to: %s', dest);
}
let source = m.kind === 'class' ? modelSource : typeSource;
if (
m.kind === 'class' &&
modelFile.includes('-with-relations.model.ts')
) {
const modelSourceWithExportsOnly = this.templatePath(
'src/models/model-template-with-re-exports-only.ts.ejs',
);
source = modelSourceWithExportsOnly;
if (this.options.prefix) m.prefix = this.options.prefix.toLowerCase();
const exportModelFileName = modelFile.split(
'-with-relations.model.ts',
)[0];
let modelName = exportModelFileName;
if (m.prefix) {
const stripped = exportModelFileName.split(m.prefix + '-')[1];
modelName = stripped ?? exportModelFileName;
}
m.exportModelName = utils.toClassName(modelName);
m.exportModelFileName = exportModelFileName + '.model';
if (m.prefix) m.prefix = utils.toClassName(m.prefix);
}
this.copyTemplatedFiles(source, dest, mixinEscapeComment(m));
}
}
// update index file for models and controllers
async _updateIndex(dir) {
const update = async files => {
const outDir = this.options.outDir || 'src';
const targetDir = this.destinationPath(`${outDir}/${dir}`);
for (const f of files) {
// Check all files being generated to ensure they succeeded
const status = this.conflicter.generationStatus[f];
if (status !== 'skip' && status !== 'identical') {
await updateIndex(targetDir, f, this.fs);
}
}
};
let files = undefined;
switch (dir) {
case MODEL:
files = this.modelSpecs.map(m => m.fileName);
break;
case CONTROLLER:
files = this.selectedControllers.map(m => m.fileName);
break;
case SERVICE:
files = this.selectedServices.map(m => m.serviceFileName);
break;
case DATASOURCE:
files = this.dataSources.map(m => m.fileName);
break;
}
if (files != null) {
await update(files);
}
}
async promptNewDataSourceName() {
if (this.shouldExit()) return false;
if (this.options.client !== true) return;
// skip if the data source already exists
if (this.dataSourceInfo && this.dataSourceInfo.name) return;
this.dataSourceInfo = {
name: this.options.datasource || 'openapi',
usePositionalParams: this.options.positional !== false,
};
debug('Prompting for artifact name');
const prompts = [
{
type: 'input',
name: 'dataSourceName',
// capitalization
message: g.f('DataSource name:'),
default: 'openapi',
validate: utils.validateClassName,
when: !this.options.datasource,
},
];
const answers = await this.prompt(prompts);
if (answers != null && answers.dataSourceName) {
this.dataSourceInfo.name = answers.dataSourceName;
}
// Setting up data for templates
this.dataSourceInfo.className =
utils.toClassName(this.dataSourceInfo.name) + 'DataSource';
this.dataSourceInfo.fileName = utils.toFileName(this.dataSourceInfo.name);
this.dataSourceInfo.outFile = `${this.dataSourceInfo.fileName}.datasource.ts`;
}
async scaffold() {
if (this.shouldExit()) return false;
this._generateModels();
await this._updateIndex(MODEL);
if (this.options.server !== false) {
this._generateControllers(this.options.client);
await this._updateIndex(CONTROLLER);
}
if (this.options.client === true) {
if (this.dataSourceInfo.outFile) {
await this._generateDataSource();
await this._updateIndex(DATASOURCE);
}
this._generateServiceProxies();
await this._updateIndex(SERVICE);
}
if (this.options.outDir && this.options.outDir !== 'src') {
await this._updateBootOptions();
}
}
install() {
if (this.shouldExit()) return false;
debug('install npm dependencies');
const pkgJson = this.packageJson || {};
const deps = pkgJson.get('dependencies') || {};
const pkgs = [];
const connectorVersionRange = deps['loopback-connector-openapi'];
if (!connectorVersionRange) {
// No dependency found for loopback-connector-openapi
pkgs.push('loopback-connector-openapi');
} else {
// `loopback-connector-openapi` exists - make sure its version range
// is >= 6.0.0
try {
const minVersion = semver.minVersion(connectorVersionRange);
if (semver.lt(minVersion, '6.0.0')) {
pkgs.push('loopback-connector-openapi@^6.0.0');
}
} catch (err) {
// The version can be a tarball
this.log(err);
}
}
if (pkgs.length === 0) return;
this.pkgManagerInstall(pkgs, {
npm: {
save: true,
},
});
}
async _updateBootOptions() {
const invokedFrom = this.destinationRoot();
const applicationPath = path.join(invokedFrom, 'src', 'application.ts');
if (!fs.existsSync(applicationPath)) return;
const relDir = (this.options.outDir || 'src').replace(/^src[\\\\/]/, '');
const artifactTypes = [
{
name: 'controllers',
dir: `${relDir}/controllers`,
extension: '.controller.js',
},
{
name: 'datasources',
dir: `${relDir}/datasources`,
extension: '.datasource.js',
},
{name: 'models', dir: `${relDir}/models`, extension: '.model.js'},
{name: 'services', dir: `${relDir}/services`, extension: '.service.js'},
];
const project = new Project({});
project.addSourceFilesAtPaths(`${invokedFrom}/src/**/*.ts`);
const applicationFile = project.getSourceFileOrThrow(applicationPath);
const constructor = applicationFile.getClasses()[0].getConstructors()[0];
const bootOptionsObject = constructor
.getDescendantsOfKind(SyntaxKind.BinaryExpression)
.find(expr => expr.getLeft().getText() === 'this.bootOptions')
?.getRight()
.asKindOrThrow(SyntaxKind.ObjectLiteralExpression);
this.log('bootOptions found: ' + !!bootOptionsObject);
if (bootOptionsObject) {
for (const artifact of artifactTypes) {
let artifactProperty = bootOptionsObject.getProperty(artifact.name);
if (!artifactProperty) {
bootOptionsObject.addPropertyAssignment({
name: artifact.name,
initializer: `{
dirs: ['${artifact.name}'],
extensions: ['${artifact.extension}'],
nested: true,
}`,
});
artifactProperty = bootOptionsObject.getProperty(artifact.name);
}
if (!artifactProperty) continue;
const artifactObject = artifactProperty.getInitializerIfKindOrThrow(
SyntaxKind.ObjectLiteralExpression,
);
let dirsProperty = artifactObject.getProperty('dirs');
if (!dirsProperty) {
artifactObject.addPropertyAssignment({
name: 'dirs',
initializer: `['${artifact.name}']`,
});
dirsProperty = artifactObject.getProperty('dirs');
}
if (!dirsProperty) continue;
const dirsArray = dirsProperty.getInitializerIfKindOrThrow(
SyntaxKind.ArrayLiteralExpression,
);
const exists = dirsArray.getElements().some(el => {
const literal = el.asKind(SyntaxKind.StringLiteral);
return literal?.getLiteralValue() === artifact.dir;
});
if (!exists) {
dirsArray.addElement(`'${artifact.dir}'`);
}
}
applicationFile.formatText();
applicationFile.saveSync();
}
}
async end() {
await super.end();
if (this.shouldExit()) return;
}
};
function mixinEscapeComment(context) {
return Object.assign(context, {escapeComment});
}