From 9d5333157569d2d4c4be33d3c64cb32de1bcd7a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=20M=C3=A9ndez?= Date: Tue, 31 Mar 2020 14:34:38 +0200 Subject: [PATCH] feat: Move template files to their own directory (#275) * feat: Move template files to their own directory * Allow node_modules to be copied * Move filters and hooks to the root directory of the template package * Change Nunjucks default directory, otherwise partials would not work --- lib/generator.js | 42 +++++++++++++----------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/lib/generator.js b/lib/generator.js index ee5a1cf3d..7e56cdaaa 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -38,37 +38,20 @@ parser.registerSchemaParser([ 'application/raml+yaml;version=1.0', ], ramlDtParser); -const FILTERS_DIRNAME = '.filters'; -const PARTIALS_DIRNAME = '.partials'; -const HOOKS_DIRNAME = '.hooks'; -const NODE_MODULES_DIRNAME = 'node_modules'; +const FILTERS_DIRNAME = 'filters'; +const HOOKS_DIRNAME = 'hooks'; const CONFIG_FILENAME = '.tp-config.json'; const PACKAGE_JSON_FILENAME = 'package.json'; -const PACKAGE_LOCK_FILENAME = 'package-lock.json'; const ROOT_DIR = path.resolve(__dirname, '..'); const DEFAULT_TEMPLATES_DIR = path.resolve(ROOT_DIR, 'node_modules'); +const TEMPLATE_CONTENT_DIRNAME = 'template'; const shouldIgnoreFile = filePath => - filePath.startsWith(`.git${path.sep}`) - || filePath.startsWith(`${PARTIALS_DIRNAME}${path.sep}`) - || filePath.startsWith(`${FILTERS_DIRNAME}${path.sep}`) - || filePath.startsWith(`${HOOKS_DIRNAME}${path.sep}`) - || path.basename(filePath) === CONFIG_FILENAME - || path.basename(filePath) === PACKAGE_JSON_FILENAME - || path.basename(filePath) === PACKAGE_LOCK_FILENAME - || filePath.startsWith(`${NODE_MODULES_DIRNAME}${path.sep}`); + filePath.startsWith(`.git${path.sep}`); const shouldIgnoreDir = dirPath => dirPath === '.git' - || dirPath.startsWith(`.git${path.sep}`) - || dirPath === PARTIALS_DIRNAME - || dirPath.startsWith(`${PARTIALS_DIRNAME}${path.sep}`) - || dirPath === FILTERS_DIRNAME - || dirPath.startsWith(`${FILTERS_DIRNAME}${path.sep}`) - || dirPath === HOOKS_DIRNAME - || dirPath.startsWith(`${HOOKS_DIRNAME}${path.sep}`) - || dirPath === NODE_MODULES_DIRNAME - || dirPath.startsWith(`${NODE_MODULES_DIRNAME}${path.sep}`); + || dirPath.startsWith(`.git${path.sep}`); class Generator { /** @@ -156,13 +139,14 @@ class Generator { 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); this.configNunjucks(); await this.loadTemplateConfig(); this.registerHooks(); await this.registerFilters(); if (this.entrypoint) { - const entrypointPath = path.resolve(this.templateDir, 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)); @@ -272,7 +256,7 @@ class Generator { * * @example * const Generator = require('asyncapi-generator'); - * const content = await Generator.getTemplateFile('html', '.partials/content.html'); + * const content = await Generator.getTemplateFile('html', 'partials/content.html'); * * @static * @param {String} templateName Name of the template to generate. @@ -419,7 +403,7 @@ class Generator { return new Promise((resolve, reject) => { xfs.mkdirpSync(this.targetDir); - const walker = xfs.walk(this.templateDir, { + const walker = xfs.walk(this.templateContentDir, { followLinks: false }); @@ -431,7 +415,7 @@ class Generator { if (Object.prototype.hasOwnProperty.call(fileNamesForSeparation, prop)) { if (stats.name.includes(`$$${prop}$$`)) { await this.generateSeparateFiles(asyncapiDocument, fileNamesForSeparation[prop], prop, stats.name, root); - const templateFilePath = path.relative(this.templateDir, path.resolve(root, stats.name)); + 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) @@ -451,7 +435,7 @@ class Generator { walker.on('directory', async (root, stats, next) => { try { - const relativeDir = path.relative(this.templateDir, path.resolve(root, stats.name)); + const relativeDir = path.relative(this.templateContentDir, path.resolve(root, stats.name)); const dirPath = path.resolve(this.targetDir, relativeDir); if (!shouldIgnoreDir(relativeDir)) { xfs.mkdirpSync(dirPath); @@ -508,7 +492,7 @@ class Generator { */ async generateSeparateFile(asyncapiDocument, name, component, template, fileName, baseDir) { try { - const relativeBaseDir = path.relative(this.templateDir, baseDir); + const relativeBaseDir = path.relative(this.templateContentDir, baseDir); const newFileName = fileName.replace(`\$\$${template}\$\$`, filenamify(name, { replacement: '-', maxLength: 255 })); const targetFile = path.resolve(this.targetDir, relativeBaseDir, newFileName); const relativeTargetFile = path.relative(this.targetDir, targetFile); @@ -540,7 +524,7 @@ class Generator { async generateFile(asyncapiDocument, fileName, baseDir) { try { const sourceFile = path.resolve(baseDir, fileName); - const relativeSourceFile = path.relative(this.templateDir, sourceFile); + const relativeSourceFile = path.relative(this.templateContentDir, sourceFile); const targetFile = path.resolve(this.targetDir, relativeSourceFile); const relativeTargetFile = path.relative(this.targetDir, targetFile);