-
-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathgenerator.js
858 lines (768 loc) · 34.1 KB
/
generator.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
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
const path = require('path');
const fs = require('fs');
const xfs = require('fs.extra');
const minimatch = require('minimatch');
const parser = require('@asyncapi/parser');
const { configureReact, renderReact, saveRenderedReactContent } = require('./renderer/react');
const { configureNunjucks, renderNunjucks } = require('./renderer/nunjucks');
const { parse, AsyncAPIDocument } = parser;
const ramlDtParser = require('@asyncapi/raml-dt-schema-parser');
const openapiSchemaParser = require('@asyncapi/openapi-schema-parser');
const avroSchemaParser = require('@asyncapi/avro-schema-parser');
const jmespath = require('jmespath');
const filenamify = require('filenamify');
const git = require('simple-git');
const log = require('loglevel');
const Arborist = require('@npmcli/arborist');
const { validateTemplateConfig } = require('./templateConfigValidator');
const {
convertMapToObject,
isFileSystemPath,
readFile,
readDir,
writeFile,
copyFile,
exists,
fetchSpec,
getInvalidOptions,
isReactTemplate,
isJsFile,
registerSourceMap,
registerTypeScript,
getTemplateDetails,
getMapBaseUrlToFolderResolver
} = require('./utils');
const { registerFilters } = require('./filtersRegistry');
const { registerHooks } = require('./hooksRegistry');
const FILTERS_DIRNAME = 'filters';
const HOOKS_DIRNAME = 'hooks';
const CONFIG_FILENAME = 'package.json';
const PACKAGE_JSON_FILENAME = 'package.json';
const GIT_IGNORE_FILENAME = '{.gitignore}';
const NPM_IGNORE_FILENAME = '{.npmignore}';
const ROOT_DIR = path.resolve(__dirname, '..');
const DEFAULT_TEMPLATES_DIR = path.resolve(ROOT_DIR, 'node_modules');
const TRANSPILED_TEMPLATE_LOCATION = '__transpiled';
const TEMPLATE_CONTENT_DIRNAME = 'template';
const GENERATOR_OPTIONS = ['debug', 'disabledHooks', 'entrypoint', 'forceWrite', 'install', 'noOverwriteGlobs', 'output', 'templateParams', 'mapBaseUrlToFolder'];
const logMessage = require('./logMessages');
const shouldIgnoreFile = filePath =>
filePath.startsWith(`.git${path.sep}`);
const shouldIgnoreDir = dirPath =>
dirPath === '.git'
|| dirPath.startsWith(`.git${path.sep}`);
parser.registerSchemaParser(openapiSchemaParser);
parser.registerSchemaParser(ramlDtParser);
parser.registerSchemaParser(avroSchemaParser);
registerSourceMap();
registerTypeScript();
class Generator {
/**
* Instantiates a new Generator object.
*
* @example
* const path = require('path');
* const generator = new Generator('@asyncapi/html-template', path.resolve(__dirname, 'example'));
*
* @example <caption>Passing custom params to the template</caption>
* const path = require('path');
* const generator = new Generator('@asyncapi/html-template', path.resolve(__dirname, 'example'), {
* templateParams: {
* sidebarOrganization: 'byTags'
* }
* });
*
* @param {String} templateName Name of the template to generate.
* @param {String} targetDir Path to the directory where the files will be generated.
* @param {Object} options
* @param {String} [options.templateParams] Optional parameters to pass to the template. Each template define their own params.
* @param {String} [options.entrypoint] Name of the file to use as the entry point for the rendering process. Use in case you want to use only a specific template file. Note: this potentially avoids rendering every file in the template.
* @param {String[]} [options.noOverwriteGlobs] List of globs to skip when regenerating the template.
* @param {Object<String, Boolean | String | String[]>} [options.disabledHooks] Object with hooks to disable. The key is a hook type. If key has "true" value, then the generator skips all hooks from the given type. If the value associated with a key is a string with the name of a single hook, then the generator skips only this single hook name. If the value associated with a key is an array of strings, then the generator skips only hooks from the array.
* @param {String} [options.output='fs'] Type of output. Can be either 'fs' (default) or 'string'. Only available when entrypoint is set.
* @param {Boolean} [options.forceWrite=false] Force writing of the generated files to given directory even if it is a git repo with unstaged files or not empty dir. Default is set to false.
* @param {Boolean} [options.install=false] Install the template and its dependencies, even when the template has already been installed.
* @param {Boolean} [options.debug=false] Enable more specific errors in the console. At the moment it only shows specific errors about filters. Keep in mind that as a result errors about template are less descriptive.
* @param {Object<String, String>} [options.mapBaseUrlToFolder] Optional parameter to map schema references from a base url to a local base folder e.g. url=https://schema.example.com/crm/ folder=./test/docs/ .
*/
constructor(templateName, targetDir, { templateParams = {}, entrypoint, noOverwriteGlobs, disabledHooks, output = 'fs', forceWrite = false, install = false, debug = false, mapBaseUrlToFolder = {} } = {}) {
const invalidOptions = getInvalidOptions(GENERATOR_OPTIONS, arguments[arguments.length - 1] || []);
if (invalidOptions.length) throw new Error(`These options are not supported by the generator: ${invalidOptions.join(', ')}`);
if (!templateName) throw new Error('No template name has been specified.');
if (!entrypoint && !targetDir) throw new Error('No target directory has been specified.');
if (!['fs', 'string'].includes(output)) throw new Error(`Invalid output type ${output}. Valid values are 'fs' and 'string'.`);
/** @type {String} Name of the template to generate. */
this.templateName = templateName;
/** @type {String} Path to the directory where the files will be generated. */
this.targetDir = targetDir;
/** @type {String} Name of the file to use as the entry point for the rendering process. Use in case you want to use only a specific template file. Note: this potentially avoids rendering every file in the template. */
this.entrypoint = entrypoint;
/** @type {String[]} List of globs to skip when regenerating the template. */
this.noOverwriteGlobs = noOverwriteGlobs || [];
/** @type {Object<String, Boolean | String | String[]>} Object with hooks to disable. The key is a hook type. If key has "true" value, then the generator skips all hooks from the given type. If the value associated with a key is a string with the name of a single hook, then the generator skips only this single hook name. If the value associated with a key is an array of strings, then the generator skips only hooks from the array. */
this.disabledHooks = disabledHooks || {};
/** @type {String} Type of output. Can be either 'fs' (default) or 'string'. Only available when entrypoint is set. */
this.output = output;
/** @type {Boolean} Force writing of the generated files to given directory even if it is a git repo with unstaged files or not empty dir. Default is set to false. */
this.forceWrite = forceWrite;
/** @type {Boolean} Enable more specific errors in the console. At the moment it only shows specific errors about filters. Keep in mind that as a result errors about template are less descriptive. */
this.debug = debug;
/** @type {Boolean} Install the template and its dependencies, even when the template has already been installed. */
this.install = install;
/** @type {Object} The template configuration. */
this.templateConfig = {};
/** @type {Object} Hooks object with hooks functions grouped by the hook type. */
this.hooks = {};
/** @type {Object} Maps schema URL to folder. */
this.mapBaseUrlToFolder = mapBaseUrlToFolder;
// Load template configuration
/** @type {Object} The template parameters. The structure for this object is based on each individual template. */
this.templateParams = {};
Object.keys(templateParams).forEach(key => {
const self = this;
Object.defineProperty(this.templateParams, key, {
enumerable: true,
get() {
if (!self.templateConfig.parameters || !self.templateConfig.parameters[key]) {
throw new Error(`Template parameter "${key}" has not been defined in the package.json file under generator property. Please make sure it's listed there before you use it in your template.`);
}
return templateParams[key];
}
});
});
}
/**
* Generates files from a given template and an AsyncAPIDocument object.
*
* @example
* generator
* .generate(myAsyncAPIdocument)
* .then(() => {
* console.log('Done!');
* })
* .catch(console.error);
*
* @example <caption>Using async/await</caption>
* try {
* await generator.generate(myAsyncAPIdocument);
* console.log('Done!');
* } catch (e) {
* console.error(e);
* }
*
* @param {AsyncAPIDocument} asyncapiDocument AsyncAPIDocument object to use as source.
* @return {Promise}
*/
async generate(asyncapiDocument) {
if (!(asyncapiDocument instanceof AsyncAPIDocument)) throw new Error('Parameter "asyncapiDocument" must be an AsyncAPIDocument object.');
this.asyncapi = asyncapiDocument;
if (this.output === 'fs') {
xfs.mkdirpSync(this.targetDir);
if (!this.forceWrite) await this.verifyTargetDir(this.targetDir);
} else if (this.output === 'string' && this.entrypoint === undefined) {
throw new Error('Parameter entrypoint is required when using output = "string"');
}
if (this.debug) log.setLevel('debug');
const { name: templatePkgName, path: templatePkgPath } = await this.installTemplate(this.install);
this.templateDir = templatePkgPath;
this.templateName = templatePkgName;
this.templateContentDir = path.resolve(this.templateDir, TEMPLATE_CONTENT_DIRNAME);
await this.loadTemplateConfig();
validateTemplateConfig(this.templateConfig, this.templateParams, asyncapiDocument);
await this.configureTemplate();
if (!isReactTemplate(this.templateConfig)) {
await registerFilters(this.nunjucks, this.templateConfig, this.templateDir, FILTERS_DIRNAME);
}
await registerHooks(this.hooks, this.templateConfig, this.templateDir, HOOKS_DIRNAME);
await this.launchHook('generate:before');
if (this.entrypoint) {
const entrypointPath = path.resolve(this.templateContentDir, this.entrypoint);
if (!(await exists(entrypointPath))) throw new Error(`Template entrypoint "${entrypointPath}" couldn't be found.`);
if (this.output === 'fs') {
await this.generateFile(asyncapiDocument, path.basename(entrypointPath), path.dirname(entrypointPath));
await this.launchHook('generate:after');
} else if (this.output === 'string') {
return this.renderFile(asyncapiDocument, entrypointPath);
}
} else {
await this.generateDirectoryStructure(asyncapiDocument);
await this.launchHook('generate:after');
}
}
/**
* Configure the templates based the desired renderer.
*/
async configureTemplate() {
if (isReactTemplate(this.templateConfig)) {
await configureReact(this.templateDir, this.templateContentDir, TRANSPILED_TEMPLATE_LOCATION);
} else {
this.nunjucks = configureNunjucks(this.debug, this.templateDir);
}
}
/**
* Generates files from a given template and AsyncAPI string.
*
* @example
* const asyncapiString = `
* asyncapi: '2.0.0'
* info:
* title: Example
* version: 1.0.0
* ...
* `;
* generator
* .generateFromString(asyncapiString)
* .then(() => {
* console.log('Done!');
* })
* .catch(console.error);
*
* @example <caption>Using async/await</caption>
* const asyncapiString = `
* asyncapi: '2.0.0'
* info:
* title: Example
* version: 1.0.0
* ...
* `;
*
* try {
* await generator.generateFromString(asyncapiString);
* console.log('Done!');
* } catch (e) {
* console.error(e);
* }
*
* @param {String} asyncapiString AsyncAPI string to use as source.
* @param {Object} [parserOptions={}] AsyncAPI parser options. Check out {@link https://www.github.com/asyncapi/parser-js|@asyncapi/parser} for more information.
* @return {Promise}
*/
async generateFromString(asyncapiString, parserOptions = {}) {
if (!asyncapiString || typeof asyncapiString !== 'string') throw new Error('Parameter "asyncapiString" must be a non-empty string.');
/** @type {String} AsyncAPI string to use as a source. */
this.originalAsyncAPI = asyncapiString;
/** @type {AsyncAPIDocument} Parsed AsyncAPI schema. See {@link https://github.com/asyncapi/parser-js/blob/master/API.md#module_@asyncapi/parser+AsyncAPIDocument|AsyncAPIDocument} for details on object structure. */
const asyncapi = await parse(asyncapiString, parserOptions);
return this.generate(asyncapi);
}
/**
* Generates files from a given template and AsyncAPI file stored on external server.
*
* @example
* generator
* .generateFromURL('https://example.com/asyncapi.yaml')
* .then(() => {
* console.log('Done!');
* })
* .catch(console.error);
*
* @example <caption>Using async/await</caption>
* try {
* await generator.generateFromURL('https://example.com/asyncapi.yaml');
* console.log('Done!');
* } catch (e) {
* console.error(e);
* }
*
* @param {String} asyncapiURL Link to AsyncAPI file
* @return {Promise}
*/
async generateFromURL(asyncapiURL) {
const doc = await fetchSpec(asyncapiURL);
const parserOptions = {};
if (this.mapBaseUrlToFolder.url) {
parserOptions.resolve = {resolver: getMapBaseUrlToFolderResolver(this.mapBaseUrlToFolder)};
}
return this.generateFromString(doc, parserOptions);
}
/**
* Generates files from a given template and AsyncAPI file.
*
* @example
* generator
* .generateFromFile('asyncapi.yaml')
* .then(() => {
* console.log('Done!');
* })
* .catch(console.error);
*
* @example <caption>Using async/await</caption>
* try {
* await generator.generateFromFile('asyncapi.yaml');
* console.log('Done!');
* } catch (e) {
* console.error(e);
* }
*
* @param {String} asyncapiFile AsyncAPI file to use as source.
* @return {Promise}
*/
async generateFromFile(asyncapiFile) {
const doc = await readFile(asyncapiFile, { encoding: 'utf8' });
const parserOptions = { path: asyncapiFile };
if (this.mapBaseUrlToFolder.url) {
parserOptions.resolve = {resolver: getMapBaseUrlToFolderResolver(this.mapBaseUrlToFolder)};
}
return this.generateFromString(doc, parserOptions);
}
/**
* Returns the content of a given template file.
*
* @example
* const Generator = require('asyncapi-generator');
* const content = await Generator.getTemplateFile('@asyncapi/html-template', 'partials/content.html');
*
* @example <caption>Using a custom `templatesDir`</caption>
* const Generator = require('asyncapi-generator');
* const content = await Generator.getTemplateFile('@asyncapi/html-template', 'partials/content.html', '~/my-templates');
*
* @static
* @param {String} templateName Name of the template to generate.
* @param {String} filePath Path to the file to render. Relative to the template directory.
* @param {String} [templatesDir=DEFAULT_TEMPLATES_DIR] Path to the directory where the templates are installed.
* @return {Promise}
*/
static async getTemplateFile(templateName, filePath, templatesDir = DEFAULT_TEMPLATES_DIR) {
return await readFile(path.resolve(templatesDir, templateName, filePath), 'utf8');
}
/**
* Downloads and installs a template and its dependencies
*
* @param {Boolean} [force=false] Whether to force installation (and skip cache) or not.
*/
installTemplate(force = false) {
return new Promise(async (resolve, reject) => {
if (!force) {
let pkgPath;
let installedPkg;
let packageVersion;
try {
installedPkg = getTemplateDetails(this.templateName, PACKAGE_JSON_FILENAME);
pkgPath = installedPkg && installedPkg.pkgPath;
packageVersion = installedPkg && installedPkg.version;
log.debug(logMessage.templateSource(pkgPath));
if (packageVersion) log.debug(logMessage.templateVersion(packageVersion));
return resolve({
name: installedPkg.name,
path: pkgPath
});
} catch (e) {
log.debug(logMessage.packageNotAvailable(pkgPath), e);
// We did our best. Proceed with installation...
}
}
const debugMessage = force ? logMessage.TEMPLATE_INSTALL_FLAG_MSG : logMessage.TEMPLATE_INSTALL_DISK_MSG;
log.debug(logMessage.installationDebugMessage(debugMessage));
if (isFileSystemPath(this.templateName)) log.debug(logMessage.NPM_INSTALL_TRIGGER);
const arb = new Arborist({
path: ROOT_DIR
});
try {
const installResult = await arb.reify({
add: [this.templateName],
saveType: 'prod',
save: false
});
const addResult = arb[Symbol.for('resolvedAdd')];
if (!addResult) return reject('Unable to resolve the name of the added package. It was most probably not added to node_modules successfully');
const packageName = addResult[0].name;
const packageVersion = installResult.children.get(packageName).version;
const packagePath = installResult.children.get(packageName).path;
if (!isFileSystemPath(this.templateName)) log.debug(logMessage.templateSuccessfullyInstalled(packageName, packagePath));
if (packageVersion) log.debug(logMessage.templateVersion(packageVersion));
return resolve({
name: packageName,
path: packagePath,
});
} catch (err) {
reject(err);
}
});
}
/**
* Returns all the parameters on the AsyncAPI document.
*
* @private
* @param {AsyncAPIDocument} asyncapiDocument AsyncAPI document to use as the source.
*/
getAllParameters(asyncapiDocument) {
const parameters = new Map();
if (asyncapiDocument.hasChannels()) {
asyncapiDocument.channelNames().forEach(channelName => {
const channel = asyncapiDocument.channel(channelName);
for (const [key, value] of Object.entries(channel.parameters())) {
parameters.set(key, value);
}
});
}
if (asyncapiDocument.hasComponents()) {
for (const [key, value] of Object.entries(asyncapiDocument.components().parameters())) {
parameters.set(key, value);
}
}
return parameters;
}
/**
* Generates the directory structure.
*
* @private
* @param {AsyncAPIDocument} asyncapiDocument AsyncAPI document to use as the source.
* @return {Promise}
*/
generateDirectoryStructure(asyncapiDocument) {
const objectMap = {};
asyncapiDocument.allSchemas().forEach((schema, schemaId) => { if (schema.type() === 'object') objectMap[schemaId] = schema; });
return new Promise((resolve, reject) => {
xfs.mkdirpSync(this.targetDir);
const walker = xfs.walk(this.templateContentDir, {
followLinks: false
});
walker.on('file', async (root, stats, next) => {
try {
await this.filesGenerationHandler(asyncapiDocument, objectMap, root, stats, next);
} catch (e) {
reject(e);
}
});
walker.on('directory', async (root, stats, next) => {
try {
this.ignoredDirHandler(root, stats, next);
} catch (e) {
reject(e);
}
});
walker.on('errors', (root, nodeStatsArray) => {
reject(nodeStatsArray);
});
walker.on('end', async () => {
resolve();
});
});
}
/**
* Makes sure that during directory structure generation ignored dirs are not modified
* @private
*
* @param {String} root Dir name.
* @param {String} stats Information about the file.
* @param {Function} next Callback function
*/
ignoredDirHandler(root, stats, next) {
const relativeDir = path.relative(this.templateContentDir, path.resolve(root, stats.name));
const dirPath = path.resolve(this.targetDir, relativeDir);
if (!shouldIgnoreDir(relativeDir)) {
xfs.mkdirpSync(dirPath);
}
next();
}
/**
* Makes sure that during directory structure generation ignored dirs are not modified
* @private
*
* @param {AsyncAPIDocument} asyncapiDocument AsyncAPI document to use as the source.
* @param {String} objectMap Map of schemas of type object
* @param {String} root Dir name.
* @param {String} stats Information about the file.
* @param {Function} next Callback function
*/
async filesGenerationHandler(asyncapiDocument, objectMap, root, stats, next) {
const fileNamesForSeparation = {
channel: asyncapiDocument.channels(),
message: convertMapToObject(asyncapiDocument.allMessages()),
securityScheme: asyncapiDocument.components() ? asyncapiDocument.components().securitySchemes() : {},
schema: asyncapiDocument.components() ? asyncapiDocument.components().schemas() : {},
objectSchema: objectMap,
parameter: convertMapToObject(this.getAllParameters(asyncapiDocument)),
everySchema: convertMapToObject(asyncapiDocument.allSchemas()),
};
// Check if the filename dictates it should be separated
let wasSeparated = false;
for (const prop in fileNamesForSeparation) {
if (Object.prototype.hasOwnProperty.call(fileNamesForSeparation, prop) && stats.name.includes(`$$${prop}$$`)) {
await this.generateSeparateFiles(asyncapiDocument, fileNamesForSeparation[prop], prop, stats.name, root);
const templateFilePath = path.relative(this.templateContentDir, path.resolve(root, stats.name));
fs.unlink(path.resolve(this.targetDir, templateFilePath), next);
wasSeparated = true;
//The filename can only contain 1 specifier (message, scheme etc)
break;
}
}
// If it was not separated process normally
if (!wasSeparated) {
await this.generateFile(asyncapiDocument, stats.name, root);
next();
}
}
/**
* Generates all the files for each in array
*
* @private
* @param {AsyncAPIDocument} asyncapiDocument AsyncAPI document to use as the source.
* @param {Array} array The components/channels to generate the separeted files for.
* @param {String} template The template filename to replace.
* @param {String} fileName Name of the file to generate for each security schema.
* @param {String} baseDir Base directory of the given file name.
* @returns {Promise}
*/
generateSeparateFiles(asyncapiDocument, array, template, fileName, baseDir) {
const promises = [];
Object.keys(array).forEach((name) => {
const component = array[name];
promises.push(this.generateSeparateFile(asyncapiDocument, name, component, template, fileName, baseDir));
});
return Promise.all(promises);
}
/**
* Generates a file for a component/channel
*
* @private
* @param {AsyncAPIDocument} asyncapiDocument AsyncAPI document to use as the source.
* @param {String} name The name of the component (filename to use)
* @param {Object} component The component/channel object used to generate the file.
* @param {String} template The template filename to replace.
* @param {String} fileName Name of the file to generate for each security schema.
* @param {String} baseDir Base directory of the given file name.
* @returns {Promise}
*/
async generateSeparateFile(asyncapiDocument, name, component, template, fileName, baseDir) {
const relativeBaseDir = path.relative(this.templateContentDir, baseDir);
const setFileTemplateNameHookName = 'setFileTemplateName';
let filename = name;
if (this.isHookAvailable(setFileTemplateNameHookName)) {
const filenamesFromHooks = await this.launchHook(setFileTemplateNameHookName, { originalFilename: filename });
//Use the result of the first hook
filename = filenamesFromHooks[0];
} else {
filename = filenamify(filename, { replacement: '-', maxLength: 255 });
}
const newFileName = fileName.replace(`\$\$${template}\$\$`, filename);
const targetFile = path.resolve(this.targetDir, relativeBaseDir, newFileName);
const relativeTargetFile = path.relative(this.targetDir, targetFile);
const shouldOverwriteFile = this.shouldOverwriteFile(relativeTargetFile);
if (!shouldOverwriteFile) return;
//Ensure the same object are parsed to the renderFile method as before.
const temp = {};
const key = template === 'everySchema' || template === 'objectSchema' ? 'schema' : template;
temp[`${key}Name`] = name;
temp[key] = component;
await this.renderAndWriteToFile(asyncapiDocument, path.resolve(baseDir, fileName), targetFile, temp);
}
/**
* Renders a template and writes the result into a file.
*
* @private
* @param {AsyncAPIDocument} asyncapiDocument AsyncAPI document to pass to the template.
* @param {String} templateFilePath Path to the input file being rendered.
* @param {String} outputPath Path to the resulting rendered file.
* @param {Object} [extraTemplateData] Extra data to pass to the template.
*/
async renderAndWriteToFile(asyncapiDocument, templateFilePath, outputpath, extraTemplateData) {
const renderContent = await this.renderFile(asyncapiDocument, templateFilePath, extraTemplateData);
if (renderContent === undefined) {
return;
} else if (isReactTemplate(this.templateConfig)) {
await saveRenderedReactContent(renderContent, outputpath);
} else {
await writeFile(outputpath, renderContent);
}
}
/**
* Generates a file.
*
* @private
* @param {AsyncAPIDocument} asyncapiDocument AsyncAPI document to use as the source.
* @param {String} fileName Name of the file to generate for each channel.
* @param {String} baseDir Base directory of the given file name.
* @return {Promise}
*/
async generateFile(asyncapiDocument, fileName, baseDir) {
const sourceFile = path.resolve(baseDir, fileName);
const relativeSourceFile = path.relative(this.templateContentDir, sourceFile);
const targetFile = path.resolve(this.targetDir, this.maybeRenameSourceFile(relativeSourceFile));
const relativeTargetFile = path.relative(this.targetDir, targetFile);
if (shouldIgnoreFile(relativeSourceFile)) return;
const shouldOverwriteFile = this.shouldOverwriteFile(relativeTargetFile);
if (!shouldOverwriteFile) return;
if (this.templateConfig.conditionalFiles && this.templateConfig.conditionalFiles[relativeSourceFile]) {
const server = this.templateParams.server && asyncapiDocument.server(this.templateParams.server);
const source = jmespath.search({
...asyncapiDocument.json(),
...{
server: server ? server.json() : undefined,
},
}, this.templateConfig.conditionalFiles[relativeSourceFile].subject);
if (!source) return log.debug(logMessage.relativeSourceFileNotGenerated(relativeSourceFile, this.templateConfig.conditionalFiles[relativeSourceFile].subject));
if (source) {
const validate = this.templateConfig.conditionalFiles[relativeSourceFile].validate;
const valid = validate(source);
if (!valid) return log.debug(logMessage.conditionalFilesMatched(relativeSourceFile));
}
}
if (this.isNonRenderableFile(relativeSourceFile)) return await copyFile(sourceFile, targetFile);
await this.renderAndWriteToFile(asyncapiDocument, sourceFile, targetFile);
}
/**
* It may rename the source file name in cases where special names are not supported, like .gitignore or .npmignore.
*
* Since we're using npm to install templates, these files are never downloaded (that's npm behavior we can't change).
* @private
* @param {String} sourceFile Path to the source file
* @returns {String} New path name
*/
maybeRenameSourceFile(sourceFile) {
switch (path.basename(sourceFile)) {
case GIT_IGNORE_FILENAME:
return path.join(path.dirname(sourceFile), '.gitignore');
case NPM_IGNORE_FILENAME:
return path.join(path.dirname(sourceFile), '.npmignore');
default:
return sourceFile;
}
}
/**
* Renders the content of a file and outputs it.
*
* @private
* @param {AsyncAPIDocument} asyncapiDocument AsyncAPI document to pass to the template.
* @param {String} filePath Path to the file you want to render.
* @param {Object} [extraTemplateData] Extra data to pass to the template.
* @return {Promise<string|TemplateRenderResult|Array<TemplateRenderResult>|undefined>}
*/
async renderFile(asyncapiDocument, filePath, extraTemplateData = {}) {
if (isReactTemplate(this.templateConfig)) {
return await renderReact(asyncapiDocument, filePath, extraTemplateData, this.templateDir, this.templateContentDir, TRANSPILED_TEMPLATE_LOCATION, this.templateParams, this.debug, this.originalAsyncAPI);
}
const templateString = await readFile(filePath, 'utf8');
return renderNunjucks(asyncapiDocument, templateString, filePath, extraTemplateData, this.templateParams, this.originalAsyncAPI, this.nunjucks);
}
/**
* Checks if a given file name matches the list of non-renderable files.
*
* @private
* @param {string} fileName Name of the file to check against a list of glob patterns.
* @return {boolean}
*/
isNonRenderableFile(fileName) {
const nonRenderableFiles = this.templateConfig.nonRenderableFiles || [];
if (!Array.isArray(nonRenderableFiles)) return false;
if (nonRenderableFiles.some(globExp => minimatch(fileName, globExp))) return true;
if (isReactTemplate(this.templateConfig) && !isJsFile(fileName)) return true;
return false;
}
/**
* Checks if a given file should be overwritten.
*
* @private
* @param {string} filePath Path to the file to check against a list of glob patterns.
* @return {boolean}
*/
async shouldOverwriteFile(filePath) {
if (!Array.isArray(this.noOverwriteGlobs)) return true;
const fileExists = await exists(path.resolve(this.targetDir, filePath));
if (!fileExists) return true;
return !this.noOverwriteGlobs.some(globExp => minimatch(filePath, globExp));
}
/**
* Loads the template configuration.
* @private
*/
async loadTemplateConfig() {
try {
const configPath = path.resolve(this.templateDir, CONFIG_FILENAME);
if (!fs.existsSync(configPath)) {
this.templateConfig = {};
return;
}
const json = await readFile(configPath, { encoding: 'utf8' });
const generatorProp = JSON.parse(json).generator;
this.templateConfig = generatorProp || {};
} catch (e) {
this.templateConfig = {};
}
await this.loadDefaultValues();
}
/**
* Loads default values of parameters from template config. If value was already set as parameter it will not be
* overriden.
* @private
*/
async loadDefaultValues() {
const parameters = this.templateConfig.parameters;
const defaultValues = Object.keys(parameters || {}).filter(key => parameters[key].default);
defaultValues.filter(dv => this.templateParams[dv] === undefined).forEach(dv =>
Object.defineProperty(this.templateParams, dv, {
enumerable: true,
get() {
return parameters[dv].default;
}
})
);
}
/**
* Launches all the hooks registered at a given hook point/name.
*
* @param {string} hookName
* @param {*} hookArguments
* @private
*/
async launchHook(hookName, hookArguments) {
let disabledHooks = this.disabledHooks[hookName] || [];
if (disabledHooks === true) return;
if (typeof disabledHooks === 'string') disabledHooks = [disabledHooks];
const hooks = this.hooks[hookName];
if (!Array.isArray(hooks)) return;
const promises = hooks.map(async (hook) => {
if (typeof hook !== 'function') return;
if (disabledHooks.includes(hook.name)) return;
return await hook(this, hookArguments);
}).filter(Boolean);
return Promise.all(promises);
}
/**
* Check if any hooks are available
*
* @param {string} hookName
* @private
*/
isHookAvailable(hookName) {
const hooks = this.hooks[hookName];
if (this.disabledHooks[hookName] === true
|| !Array.isArray(hooks)
|| hooks.length === 0) return false;
let disabledHooks = this.disabledHooks[hookName] || [];
if (typeof disabledHooks === 'string') disabledHooks = [disabledHooks];
return !!hooks.filter(h => !disabledHooks.includes(h.name)).length;
}
/**
* Check if given directory is a git repo with unstaged changes and is not in .gitignore or is not empty
* @private
* @param {String} dir Directory that needs to be tested for a given condition.
*/
async verifyTargetDir(dir) {
const isGitRepo = await git(dir).checkIsRepo();
if (isGitRepo) {
//Need to figure out root of the repository to properly verify .gitignore
const root = await git(dir).revparse(['--show-toplevel']);
const gitInfo = git(root);
//Skipping verification if workDir inside repo is declared in .gitignore
const workDir = path.relative(root, dir);
if (workDir) {
const checkGitIgnore = await gitInfo.checkIgnore(workDir);
if (checkGitIgnore.length !== 0) return;
}
const gitStatus = await gitInfo.status();
//New files are not tracked and not visible as modified
const hasUntrackedUnstagedFiles = gitStatus.not_added.length !== 0;
const stagedFiles = gitStatus.staged;
const modifiedFiles = gitStatus.modified;
const hasModifiedUstagedFiles = (modifiedFiles.filter(e => stagedFiles.indexOf(e) === -1)).length !== 0;
if (hasModifiedUstagedFiles || hasUntrackedUnstagedFiles) throw new Error(`"${this.targetDir}" is in a git repository with unstaged changes. Please commit your changes before proceeding or add proper directory to .gitignore file. You can also use the --force-write flag to skip this rule (not recommended).`);
} else {
const isDirEmpty = (await readDir(dir)).length === 0;
if (!isDirEmpty) throw new Error(`"${this.targetDir}" is not an empty directory. You might override your work. To skip this rule, please make your code a git repository or use the --force-write flag (not recommended).`);
}
}
}
Generator.DEFAULT_TEMPLATES_DIR = DEFAULT_TEMPLATES_DIR;
Generator.TRANSPILED_TEMPLATE_LOCATION = TRANSPILED_TEMPLATE_LOCATION;
module.exports = Generator;