Skip to content

Commit

Permalink
feat: Move template files to their own directory (#275)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
fmvilas authored Mar 31, 2020
1 parent 248e4f6 commit 9d53331
Showing 1 changed file with 13 additions and 29 deletions.
42 changes: 13 additions & 29 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
});

Expand All @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 9d53331

Please sign in to comment.