Skip to content

Commit

Permalink
fix: make conditionalFiles work if given object is not there
Browse files Browse the repository at this point in the history
  • Loading branch information
derberg committed May 28, 2020
1 parent 3fb2fb7 commit 736bfa0
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Usage: cli [options] <asyncapi> <template>
Options:
-V, --version output the version number
-d, --disable-hook <hookType> disable a specific hook type
--debug 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
--debug enable more specific errors in the console.
-i, --install installs the template and its dependencies (defaults to false)
-n, --no-overwrite <glob> glob or path of the file(s) to skip when regenerating
-o, --output <outputDir> directory where to put the generated files (defaults to current directory)
Expand Down
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ program
template = tmpl;
})
.option('-d, --disable-hook <hookType>', 'disable a specific hook type', disableHooksParser)
.option('--debug', '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')
.option('--debug', 'enable more specific errors in the console')
.option('-i, --install', 'installs the template and its dependencies (defaults to false)')
.option('-n, --no-overwrite <glob>', 'glob or path of the file(s) to skip when regenerating', noOverwriteParser)
.option('-o, --output <outputDir>', 'directory where to put the generated files (defaults to current directory)', parseOutput, process.cwd())
Expand Down
10 changes: 7 additions & 3 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const jmespath = require('jmespath');
const filenamify = require('filenamify');
const git = require('simple-git/promise');
const npmi = require('npmi');
const log = require('loglevel');
const { validateTemplateConfig } = require('./templateConfigValidator');
const {
convertMapToObject,
Expand Down Expand Up @@ -158,6 +159,8 @@ class Generator {
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;
Expand Down Expand Up @@ -332,7 +335,7 @@ class Generator {
// The template name doesn't look like a file system path but we find
// that the package is already installed and it's a symbolic link.
const { resolvedLink } = await getLocalTemplateDetails(templatePath);
console.info(`This template has already been installed and it's pointing to your filesystem at ${resolvedLink}.`);
log.debug(`This template has already been installed and it's pointing to your filesystem at ${resolvedLink}.`);
}
installedPkg = require(path.resolve(templatePath, PACKAGE_JSON_FILENAME));
}
Expand Down Expand Up @@ -557,14 +560,15 @@ 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) {
const validate = this.templateConfig.conditionalFiles[relativeSourceFile].validate;
const valid = validate(source);
if (!valid) return;
if (!valid) return log.debug(`${relativeSourceFile} was not generated because condition specified for this file in template configuration in conditionalFiles matched`);
}
}

if (this.isNonRenderableFile(relativeSourceFile)) return await copyFile(sourceFile, targetFile);

const parsedContent = await this.renderFile(asyncapiDocument, sourceFile);
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@
"fs.extra": "^1.3.2",
"jmespath": "^0.15.0",
"js-yaml": "^3.13.1",
"levenshtein-edit-distance": "^2.0.5",
"loglevel": "^1.6.8",
"markdown-it": "^8.4.1",
"minimatch": "^3.0.4",
"node-fetch": "^2.6.0",
"npmi": "^4.0.0",
"nunjucks": "^3.2.0",
"semver": "^7.3.2",
"simple-git": "^1.131.0",
"levenshtein-edit-distance": "^2.0.5"
"simple-git": "^1.131.0"
},
"devDependencies": {
"@semantic-release/commit-analyzer": "^8.0.1",
Expand Down
3 changes: 3 additions & 0 deletions test/__mocks__/loglevel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const log = jest.genMockFromModule('loglevel');

module.exports = log;
7 changes: 4 additions & 3 deletions test/generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const fs = require('fs');
const path = require('path');
const Generator = require('../lib/generator');
const log = require('loglevel');

const streetlightYAML = fs.readFileSync(path.resolve(__dirname, './docs/streetlights.yml'), 'utf8');

Expand Down Expand Up @@ -343,13 +344,13 @@ describe('Generator', () => {
});

it('works with an npm package that has already been installed as a local template', async () => {
console.info = jest.fn();
log.debug = jest.fn();
utils.__isFileSystemPathValue = false;
utils.__isLocalTemplateValue = true;
utils.__getLocalTemplateDetailsResolvedLinkValue = '/path/to/template/nameOfTestTemplate';
const gen = new Generator('nameOfTestTemplate', __dirname);
const gen = new Generator('nameOfTestTemplate', __dirname, {debug: true});
await gen.installTemplate();
expect(console.info).toHaveBeenCalledWith('This template has already been installed and it\'s pointing to your filesystem at /path/to/template/nameOfTestTemplate.');
expect(log.debug).toHaveBeenCalledWith('This template has already been installed and it\'s pointing to your filesystem at /path/to/template/nameOfTestTemplate.');
expect(npmiMock).toHaveBeenCalledTimes(0);
});

Expand Down

0 comments on commit 736bfa0

Please sign in to comment.