Composability is a way to combine smaller parts to make one large thing. Sort of like Voltron®
-
A generator can decide to compose itself with another generator (e.g., generator-backbone uses generator-mocha ). -
An end user may also initiate the composition (e.g., Simon wants to generate a Backbone project with SASS and Rails). Note: end user initiated composition is a planned feature and currently not available.
this.composeWith()
API
generatorPath - A full path pointing to the generator you want to compose with (usually using require.resolve() ). options - An Object containing options to pass to the composed generator once it runs.
this . composeWith ( require . resolve ( 'generator-bootstrap/generators/app' ), { preprocessor : 'sass' });
this . composeWith ( 'backbone:route' , { rjs : true });
composing with a Generator class
Generator - The generator class to compose with path - The path to the generator files
// Import generator-node's main generator
const NodeGenerator = require ( 'generator-node/generators/app/index.js' );
// Compose with it
this . composeWith ({
Generator : NodeGenerator ,
path : require . resolve ( 'generator-node/generators/app' )
});
execution example
// In my-generator/generators/turbo/index.js
module . exports = class extends Generator {
prompting () {
this . log ( 'prompting - turbo' );
}
writing () {
this . log ( 'writing - turbo' );
}
};
// In my-generator/generators/electric/index.js
module . exports = class extends Generator {
prompting () {
this . log ( 'prompting - zap' );
}
writing () {
this . log ( 'writing - zap' );
}
};
// In my-generator/generators/app/index.js
module . exports = class extends Generator {
initializing () {
this . composeWith ( require . resolve ( '../ turbo' ));
this . composeWith ( require . resolve ( '../ electric' ));
}
};
prompting - turbo prompting - zap writing - turbo writing - zap
dependencies or peerDependencies
dependencies get installed local to the generator. It is the best option to control the version of the dependency used. This is the preferred option. peerDependencies get installed alongside the generator, as a sibling. For example, if generator-backbone declared generator-gruntfile as a peer dependency, the folder tree would look this way: ├───generator-backbone/ └───generator-gruntfile/ devDependencies for testing and development utility. This is not needed here.
{
"peerDependencies" : {
"generator-gruntfile" : "*" ,
"generator-bootstrap" : ">=1.0.0"
}
}