Skip to content

Commit

Permalink
Merge 10b728e into 1cd1031
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcoScabbiolo committed Dec 22, 2018
2 parents 1cd1031 + 10b728e commit ef83dab
Show file tree
Hide file tree
Showing 7 changed files with 321 additions and 343 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
node_modules
coverage
.nyc_output
yarn.lock
23 changes: 23 additions & 0 deletions .vscode/launch.json
@@ -0,0 +1,23 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Mocha All",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": ["--timeout", "999999", "--colors", "${workspaceFolder}/test"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Mocha Current File",
"program": "${workspaceFolder}/node_modules/mocha/bin/_mocha",
"args": ["--timeout", "999999", "--colors", "${file}"],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen"
}
]
}
88 changes: 61 additions & 27 deletions lib/index.js
Expand Up @@ -496,23 +496,32 @@ class Generator extends EventEmitter {

/**
* Compose this generator with another one.
* @param {String} namespace The generator namespace to compose with
* @param {String|Object} generator The path to the generator module or an object (see examples)
* @param {Object} options The options passed to the Generator
* @param {Object} [settings] Settings hash on the composition relation
* @param {string} [settings.local] Path to a locally stored generator
* @param {String} [settings.link="weak"] If "strong", the composition will occured
* even when the composition is initialized by
* the end user
* @return {this}
* @return {this} This generator
*
* @example <caption>Using a peerDependency generator</caption>
* this.composeWith('bootstrap', { sass: true });
*
* @example <caption>Using a direct dependency generator</caption>
* this.composeWith(require.resolve('generator-bootstrap/app/main.js'), { sass: true });
*
* @example <caption>Passing a Generator class</caption>
* this.composeWith({ Generator: MyGenerator, path: '../generator-bootstrap/app/main.js' }, { sass: true });
*/
composeWith(modulePath, options) {
let generator;
composeWith(generator, options) {
let instantiatedGenerator;

const instantiate = (Generator, path) => {
Generator.resolved = require.resolve(path);
Generator.namespace = this.env.namespace(path);

return this.env.instantiate(Generator, {
options,
arguments: options.arguments
});
};

options = options || {};

// Pass down the default options so they're correctly mirrored down the chain.
Expand All @@ -528,29 +537,54 @@ class Generator extends EventEmitter {
options
);

try {
const Generator = require(modulePath); // eslint-disable-line import/no-dynamic-require
Generator.resolved = require.resolve(modulePath);
Generator.namespace = this.env.namespace(modulePath);
generator = this.env.instantiate(Generator, {
options,
arguments: options.arguments
});
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
generator = this.env.create(modulePath, {
options,
arguments: options.arguments
});
} else {
throw err;
if (typeof generator === 'string') {
try {
const Generator = require(generator); // eslint-disable-line import/no-dynamic-require
instantiatedGenerator = instantiate(Generator, generator);
} catch (err) {
if (err.code === 'MODULE_NOT_FOUND') {
instantiatedGenerator = this.env.create(generator, {
options,
arguments: options.arguments
});
} else {
throw err;
}
}
} else {
assert(
generator.Generator,
`${chalk.red('Missing Generator property')}\n` +
`When passing an object to Generator${chalk.cyan(
'#composeWith'
)} include the generator class to run in the ${chalk.cyan(
'Generator'
)} property\n\n` +
`this.composeWith({\n` +
` ${chalk.yellow('Generator')}: MyGenerator,\n` +
` ...\n` +
`});`
);
assert(
typeof generator.path === 'string',
`${chalk.red('path property is not a string')}\n` +
`When passing an object to Generator${chalk.cyan(
'#composeWith'
)} include the path to the generators files in the ${chalk.cyan(
'path'
)} property\n\n` +
`this.composeWith({\n` +
` ${chalk.yellow('path')}: '../my-generator',\n` +
` ...\n` +
`});`
);
instantiatedGenerator = instantiate(generator.Generator, generator.path);
}

if (this._running) {
generator.run();
instantiatedGenerator.run();
} else {
this._composedWith.push(generator);
this._composedWith.push(instantiatedGenerator);
}

return this;
Expand Down
4 changes: 2 additions & 2 deletions lib/util/prompt-suggestion.js
Expand Up @@ -36,8 +36,8 @@ const getCheckboxDefault = (question, defaultValue) => {
* @private
*/
const getListDefault = (question, defaultValue) => {
const choiceValues = question.choices.map(
choice => (typeof choice === 'object' ? choice.value : choice)
const choiceValues = question.choices.map(choice =>
typeof choice === 'object' ? choice.value : choice
);
return choiceValues.indexOf(defaultValue);
};
Expand Down

0 comments on commit ef83dab

Please sign in to comment.