Skip to content

Commit

Permalink
fix: Update Generator CLI forceInstall parameter to install (#359)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastian-palma committed Jun 5, 2020
1 parent 406c069 commit b1b7696
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function generate(targetDir) {
noOverwriteGlobs,
disabledHooks,
forceWrite: program.forceWrite,
forceInstall: program.install,
install: program.install,
debug: program.debug
});

Expand Down
3 changes: 3 additions & 0 deletions lib/__mocks__/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const utils = jest.genMockFromModule('../utils');
const { getInvalidOptions } = require.requireActual('../utils');

utils.getInvalidOptions = getInvalidOptions;

utils.__files = {};
utils.readFile = jest.fn(async (filePath) => {
Expand Down
8 changes: 6 additions & 2 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const {
writeFile,
copyFile,
exists,
fetchSpec
fetchSpec,
getInvalidOptions
} = require('./utils');
const { registerFilters } = require('./filtersRegistry');
const { registerHooks } = require('./hooksRegistry');
Expand All @@ -46,6 +47,7 @@ const PACKAGE_JSON_FILENAME = 'package.json';
const ROOT_DIR = path.resolve(__dirname, '..');
const DEFAULT_TEMPLATES_DIR = path.resolve(ROOT_DIR, 'node_modules');
const TEMPLATE_CONTENT_DIRNAME = 'template';
const GENERATOR_OPTIONS = ['debug', 'disabledHooks', 'entrypoint', 'forceWrite', 'install', 'noOverwriteGlobs', 'output', 'templateParams'];

const shouldIgnoreFile = filePath =>
filePath.startsWith(`.git${path.sep}`);
Expand Down Expand Up @@ -83,10 +85,12 @@ class Generator {
* @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.
*/
constructor(templateName, targetDir, { templateParams = {}, entrypoint, noOverwriteGlobs, disabledHooks, output = 'fs', forceWrite = false, install = false, debug = false } = {}) {
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. */
Expand Down
10 changes: 10 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,13 @@ utils.getGeneratorVersion = () => {
return packageJson.version;
};

/**
* Filters out the Generator invalid options given
*
* @param {Array}
* @returns {Array}
*/
utils.getInvalidOptions = (generatorOptions, options) => {
if (typeof options !== 'object') return [];
return Object.keys(options).filter(param => !generatorOptions.includes(param));
};
30 changes: 30 additions & 0 deletions test/generator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,36 @@ describe('Generator', () => {
expect(gen.templateParams.test).toStrictEqual(true);
});

it('throws an error indicating an unexpected param was given', () => {
const t = () => new Generator('testTemplate', __dirname, {
entrypoint: 'test-entrypoint',
noOverwriteGlobs: ['test-globs'],
disabledHooks: ['test-hooks'],
output: 'string',
forceWrite: true,
forceInstall: true,
templateParams: {
test: true,
}
});
expect(t).toThrow('These options are not supported by the generator: forceInstall');
});

it('throws an error indicating multiple unexpected params were given', () => {
const t = () => new Generator('testTemplate', __dirname, {
entrypoint: 'test-entrypoint',
noOverwriteGlobs: ['test-globs'],
disabledHooks: ['test-hooks'],
output: 'string',
write: true,
forceInstall: true,
templateParams: {
test: true,
}
});
expect(t).toThrow('These options are not supported by the generator: write, forceInstall');
});

it('fails if no templateName is given', () => {
const t = () => new Generator();
expect(t).toThrow('No template name has been specified.');
Expand Down

0 comments on commit b1b7696

Please sign in to comment.