Skip to content

Commit

Permalink
chore: rename {.gitignore} and {.npmignore} (#380)
Browse files Browse the repository at this point in the history
* Rename {.gitignore} and {.npmignore}

* Replace resolve with join because resolve gives us absolute paths

* Improve js docs and remove console.log
  • Loading branch information
fmvilas authored Jun 10, 2020
1 parent f2941c8 commit 8b877f6
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ 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 TEMPLATE_CONTENT_DIRNAME = 'template';
Expand Down Expand Up @@ -164,7 +166,7 @@ class Generator {
}

if (this.debug) log.setLevel('debug');

const { name: templatePkgName, path: templatePkgPath } = await this.installTemplate(this.install);
this.templateDir = templatePkgPath;
this.templateName = templatePkgName;
Expand Down Expand Up @@ -548,7 +550,7 @@ class Generator {
async generateFile(asyncapiDocument, fileName, baseDir) {
const sourceFile = path.resolve(baseDir, fileName);
const relativeSourceFile = path.relative(this.templateContentDir, sourceFile);
const targetFile = path.resolve(this.targetDir, relativeSourceFile);
const targetFile = path.resolve(this.targetDir, this.maybeRenameSourceFile(relativeSourceFile));
const relativeTargetFile = path.relative(this.targetDir, targetFile);

if (shouldIgnoreFile(relativeSourceFile)) return;
Expand All @@ -564,7 +566,7 @@ class Generator {
server: server ? server.json() : undefined,
},
}, this.templateConfig.conditionalFiles[relativeSourceFile].subject);

if (!source) return log.debug(`${relativeSourceFile} was not generated because ${this.templateConfig.conditionalFiles[relativeSourceFile].subject} specified in template configuration in conditionalFiles was not found in provided AsyncAPI specification file`);

if (source) {
Expand All @@ -580,6 +582,25 @@ class Generator {
await writeFile(targetFile, parsedContent, { encoding: 'utf8', mode: stats.mode });
}

/**
* 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 a template string and outputs it.
*
Expand Down

0 comments on commit 8b877f6

Please sign in to comment.