Skip to content

Commit

Permalink
Switch composeWith to use environment.
Browse files Browse the repository at this point in the history
  • Loading branch information
mshima committed Feb 20, 2021
1 parent e9d0a15 commit 8f4afe9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 79 deletions.
137 changes: 60 additions & 77 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,6 @@ const methodIsValid = function (name) {
return !['_', '#'].includes(name.charAt(0)) && name !== 'constructor';
};

// New runWithOptions should take precedence if exists.
const runGenerator = (generator) => {
if (generator.queueTasks) {
generator.queueTasks();
} else if (generator.runWithOptions) {
generator.runWithOptions();
} else {
generator.run();
}
};

/**
* Queue options.
* @typedef {Object} QueueOptions
Expand Down Expand Up @@ -941,7 +930,7 @@ class Generator extends Base {
const eventName = `done$${
namespace || 'unknownnamespace'
}#${methodName}`;
debug(`Emiting event ${eventName}`);
debug(`Done event ${eventName}`);
self.env.emit(eventName, {
namespace,
generator: self,
Expand Down Expand Up @@ -1030,11 +1019,6 @@ class Generator extends Base {
* @return {Promise} Resolved once the process finish
*/
run() {
this.emit('run');
this.env.runLoop.once('end', () => {
this.debug('Generator has ended');
this.emit('end');
});
return this.env.runGenerator(this);
}

Expand All @@ -1043,7 +1027,9 @@ class Generator extends Base {
`Queueing generator ${this.options.namespace} with generator version ${this.yoGeneratorVersion}`
);
this.queueOwnTasks();
this._composedWith.forEach(runGenerator);
this._composedWith.forEach((generator) =>
this.env.queueGenerator(generator, false)
);
this._composedWith = [];
}

Expand All @@ -1062,17 +1048,40 @@ class Generator extends Base {
* @example <caption>Passing a Generator class</caption>
* this.composeWith({ Generator: MyGenerator, path: '../generator-bootstrap/app/main.js' }, { sass: true });
*/
composeWith(generator, options) {
composeWith(generator, args, options) {
if (typeof args === 'boolean') {
args = [];
} else if (!Array.isArray(args) && typeof args === 'object') {
options = args;
args = options.arguments || options.args || [];
}

if (typeof options === 'boolean') {
options = {};
} else {
options = options || {};
}

let instantiatedGenerator;

if (Array.isArray(generator)) {
return generator.map((gen) => this.composeWith(gen, options));
return generator.map((gen) => this.composeWith(gen, args, options));
}

// Pass down the default options so they're correctly mirrored down the chain.
options = {
...options,
skipInstall: this.options.skipInstall || this.options['skip-install'],
'skip-install': this.options.skipInstall || this.options['skip-install'],
skipCache: this.options.skipCache || this.options['skip-cache'],
'skip-cache': this.options.skipCache || this.options['skip-cache'],
forceInstall: this.options.forceInstall || this.options['force-install'],
'force-install':
this.options.forceInstall || this.options['force-install'],
skipLocalCache: this.options.skipLocalCache,
destinationRoot: this._destinationRoot
};

const instantiate = (Generator, path) => {
if (path === 'unknown') {
Generator.resolved = path;
Expand All @@ -1082,87 +1091,61 @@ class Generator extends Base {

Generator.namespace = this.env.namespace(path);

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

options = options || {};

// Pass down the default options so they're correctly mirrored down the chain.
options = _.extend(
{
skipInstall: this.options.skipInstall || this.options['skip-install'],
'skip-install':
this.options.skipInstall || this.options['skip-install'],
skipCache: this.options.skipCache || this.options['skip-cache'],
'skip-cache': this.options.skipCache || this.options['skip-cache'],
forceInstall:
this.options.forceInstall || this.options['force-install'],
'force-install':
this.options.forceInstall || this.options['force-install'],
skipLocalCache: this.options.skipLocalCache,
destinationRoot: this._destinationRoot
},
options
);

if (typeof generator === 'string') {
try {
// Allows to run a local generator without namespace.
const GeneratorImport = require(generator);
const Generator =
typeof GeneratorImport.default === 'function'
? GeneratorImport.default
: GeneratorImport;

instantiatedGenerator = instantiate(Generator, generator);
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
instantiatedGenerator = this.env.create(generator, {
options,
arguments: options.arguments
});
} else {
throw error;
}
} catch {
instantiatedGenerator = this.env.create(generator, args, options);
}
} else {
const {Generator, path} = generator;
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` +
`});`
Generator,
`${chalk.red('Missing Generator property')}
When passing an object to Generator${chalk.cyan(
'#composeWith'
)} include the generator class to run in the ${chalk.cyan(
'Generator'
)} property
this.composeWith({
${chalk.yellow('Generator')}: MyGenerator,
...\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` +
`});`
typeof path === 'string',
`${chalk.red('path property is not a string')}
When passing an object to Generator${chalk.cyan(
'#composeWith'
)} include the path to the generators files in the ${chalk.cyan(
'path'
)} property
this.composeWith({
${chalk.yellow('path')}: '../my-generator',
...
});`
);
instantiatedGenerator = instantiate(generator.Generator, generator.path);
instantiatedGenerator = instantiate(Generator, path);
}

if (!instantiatedGenerator) {
return instantiatedGenerator;
}

if (this._running) {
runGenerator(instantiatedGenerator);
this.env.queueGenerator(instantiatedGenerator);
} else {
this._composedWith.push(instantiatedGenerator);
}
Expand Down
5 changes: 3 additions & 2 deletions test/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe('Base', () => {
return helpers
.create(
path.join(__dirname, './fixtures/options-generator'),
{},
{namespace: 'options-generator'},
{createEnv: yeoman.createEnv}
)
.withOptions({testOption: false})
Expand Down Expand Up @@ -211,7 +211,7 @@ describe('Base', () => {
});

it('turn on _running flag', function () {
this.testGen.run();
this.testGen.queueTasks();
assert.ok(this.testGen._running);
});

Expand Down Expand Up @@ -1553,6 +1553,7 @@ describe('Base', () => {
afterEnd: {priorityName: 'afterEnd', queueName: 'dummy#afterEnd'}
});
assert.deepStrictEqual(this.env.runLoop.queueNames, [
'environment:run',
'initializing',
'prompting',
'common#preConfiguring1',
Expand Down

0 comments on commit 8f4afe9

Please sign in to comment.