new Generator (args, options, features)
Parameters:
| Name | Type | Description | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
args |
Array.<string> |
Provide arguments at initialization |
||||||||
options |
Object |
Provide options at initialization Properties
|
||||||||
features |
Object |
Provide Generator features information |
Properties:
| Name | Type | Description |
|---|---|---|
env |
Object |
the current Environment being run |
resolved |
String |
the path to the current generator |
description |
String |
Used in
|
appname |
String |
The application name |
config |
Storage |
|
fs |
Object |
An instance of Mem-fs-editor |
log |
function |
Output content through Interface Adapter |
uniqueBy |
String |
The Generator instance unique identifier. The Environment will ignore duplicated identifiers. |
unique |
String |
uniqueBy calculation method (undefined/argument/namespace) |
tasksMatchingPriority |
boolean |
Only queue methods that matches a priority. |
taskPrefix |
String |
Tasks methods starts with prefix. Allows api methods (non tasks) without prefix. |
customInstallTask |
boolean | function |
Provides a custom install task. Environment >= 3.2.0 Environment built-in task will not be executed |
customCommitTask |
boolean | function |
Provides a custom commit task. Environment >= 3.2.0 Environment built-in task will not be executed |
- Mixes In:
-
- actions/help
- actions/spawn-command
- actions/user
- actions/fs
- nodejs/EventEmitter
Example
const Generator = require('yeoman-generator');
module.exports = class extends Generator {
writing() {
this.fs.write(this.destinationPath('index.js'), 'const foo = 1;');
}
};
Extends
Members
-
config
-
Generator config Storage.
-
packageJson
-
Package.json Storage resolved to
this.destinationPath('package.json').Environment watches for package.json changes at
this.env.cwd, and triggers an package manager install if it has been committed to disk. If package.json is at a different folder, like a changed generator root, propagate it to the Environment likethis.env.cwd = this.destinationPath().Example
this.packageJson.merge({ scripts: { start: 'webpack --serve', }, dependencies: { ... }, peerDependencies: { ... }, });
Methods
-
_templateData (path) → {Object}
-
Utility method to get a formatted data for templates.
Parameters:
Name Type Description pathString path to the storage key.
Returns:
Object -data to be passed to the templates.
- Mixes In:
-
async addDependencies (dependencies) → {Promise}
-
Add dependencies to the destination the package.json.
Environment watches for package.json changes at
this.env.cwd, and triggers an package manager install if it has been committed to disk. If package.json is at a different folder, like a changed generator root, propagate it to the Environment likethis.env.cwd = this.destinationPath().Parameters:
Name Type Description dependenciesObject | string | Array.<string> Returns:
Promise -a 'packageName: packageVersion' object
- Inherited From:
-
async addDevDependencies (dependencies) → {Promise}
-
Add dependencies to the destination the package.json.
Environment watches for package.json changes at
this.env.cwd, and triggers an package manager install if it has been committed to disk. If package.json is at a different folder, like a changed generator root, propagate it to the Environment likethis.env.cwd = this.destinationPath().Parameters:
Name Type Description dependenciesObject | string | Array.<string> Returns:
Promise -a 'packageName: packageVersion' object
- Inherited From:
-
argument (name, config) → {this}
-
Adds an argument to the class and creates an attribute getter for it.
Arguments are different from options in several aspects. The first one is how they are parsed from the command line, arguments are retrieved based on their position.
Besides, arguments are used inside your code as a property (
this.argument), while options are all kept in a hash (this.options).Parameters:
Name Type Description nameString Argument name
configObject Argument options
Properties
Name Type Attributes Description typeany String, Number, Array, or Object
descriptionstring <optional>
Description for the argument
requiredboolean <optional>
required` Boolean whether it is required
optionalboolean <optional>
Boolean whether it is optional
defaultany <optional>
Default value for this argument
Returns:
this -This generator
-
argumentsHelp () → {String}
-
Get help text for arguments
Returns:
String -Text of options in formatted table
- Mixes In:
-
cancelCancellableTasks ()
-
Ignore cancellable tasks.
-
composeWith (generator, args opt , options opt , immediately opt ) → { Generator }
-
Compose this generator with another one.
Parameters:
Name Type Attributes Default Description generatorString | Object | Array The path to the generator module or an object (see examples)
argsArray <optional>
Arguments passed to the Generator
optionsObject <optional>
The options passed to the Generator
immediatelyboolean <optional>
false Boolean whether to queue the Generator immediately
Examples
Using a peerDependency generator
this.composeWith('bootstrap', { sass: true });Using a direct dependency generator
this.composeWith(require.resolve('generator-bootstrap/app/main.js'), { sass: true });Passing a Generator class
this.composeWith({ Generator: MyGenerator, path: '../generator-bootstrap/app/main.js' }, { sass: true }); -
copyDestination (from, to, …args) → {*}
-
Copy file from destination folder to another destination folder. mem-fs-editor method's shortcut, for more information see mem-fs-editor . Shortcut for this.fs.copy(this.destinationPath(from), this.destinationPath(to)).
Parameters:
Name Type Attributes Description fromString absolute file path or relative to destination folder.
toString absolute file path or relative to destination folder.
args* <repeatable>
for more information see mem-fs-editor
- Mixes In:
-
copyTemplate (from, to, …args) → {*}
-
Copy file from templates folder to destination folder. mem-fs-editor method's shortcut, for more information see mem-fs-editor . Shortcut for this.fs.copy(this.templatePath(from), this.destinationPath(to))
Parameters:
Name Type Attributes Description fromString absolute file path or relative to templates folder.
toString absolute file path or relative to destination folder.
args* <repeatable>
for more information see mem-fs-editor
- Mixes In:
-
copyTemplateAsync (from, to, …args) → {*}
-
Copy file from templates folder to destination folder. mem-fs-editor method's shortcut, for more information see mem-fs-editor . Shortcut for this.fs.copy(this.templatePath(from), this.destinationPath(to))
Parameters:
Name Type Attributes Description fromString absolute file path or relative to templates folder.
toString absolute file path or relative to destination folder.
args* <repeatable>
for more information see mem-fs-editor
- Mixes In:
-
createStorage (storePath, path opt , options opt ) → { Storage }
-
Return a storage instance.
Parameters:
Name Type Attributes Description storePathString The path of the json file
pathString <optional>
The name in which is stored inside the json
optionsboolean | Object <optional>
Treat path as an lodash path
-
debug (…args)
-
Convenience debug method
Parameters:
Name Type Attributes Description argsany <repeatable>
parameters to be passed to debug
-
deleteDestination (filepath, …args) → {*}
-
Delete file from destination folder mem-fs-editor method's shortcut, for more information see mem-fs-editor . Shortcut for this.fs.delete(this.destinationPath(filepath)).
Parameters:
Name Type Attributes Description filepathString absolute file path or relative to destination folder.
args* <repeatable>
for more information see mem-fs-editor
- Mixes In:
-
desc (description)
-
Simple setter for custom
descriptionto append on help output.Parameters:
Name Type Description descriptionString - Mixes In:
-
destinationPath (…dest) → {String}
-
Join a path to the destination root.
Parameters:
Name Type Attributes Description destString <repeatable>
path parts
Returns:
String -joined path
-
destinationRoot (rootPath) → {String}
-
Change the generator destination root directory. This path is used to find storage, when using a file system helper method (like
this.writeandthis.copy)Parameters:
Name Type Description rootPathString new destination root path
Returns:
String -destination root path
-
determineAppname () → {String}
-
Determines the name of the application.
First checks for name in bower.json. Then checks for name in package.json. Finally defaults to the name of the current directory.
Returns:
String -The name of the application
-
git.email () → {String}
-
Retrieves user's email from Git in the global scope or the project scope (it'll take what Git will use in the current context)
Returns:
String -configured git email or undefined
- Mixes In:
-
existsDestination (filepath, …args) → {*}
-
Exists file on destination folder. mem-fs-editor method's shortcut, for more information see mem-fs-editor . Shortcut for this.fs.exists(this.destinationPath(filepath)).
Parameters:
Name Type Attributes Description filepathString absolute file path or relative to destination folder.
args* <repeatable>
for more information see mem-fs-editor
- Mixes In:
-
getFeatures () → {Object}
-
Specifications for Environment features.
Returns:
Object -
help () → {String}
-
Tries to get the description from a USAGE file one folder above the source root otherwise uses a default description
Returns:
String -Help message of the generator
- Mixes In:
-
moveDestination (from, to) → {*}
-
Move file from destination folder to another destination folder. mem-fs-editor method's shortcut, for more information see mem-fs-editor . Shortcut for this.fs.move(this.destinationPath(from), this.destinationPath(to)).
Parameters:
Name Type Description fromString absolute file path or relative to destination folder.
toString absolute file path or relative to destination folder.
- Mixes In:
-
git.name () → {String}
-
Retrieves user's name from Git in the global scope or the project scope (it'll take what Git will use in the current context)
Returns:
String -configured git name or undefined
- Mixes In:
-
option (name opt , config) → {this}
-
Adds an option to the set of generator expected options, only used to generate generator usage. By default, generators get all the cli options parsed by nopt as a
this.optionshash object.Parameters:
Name Type Attributes Description nameString <optional>
Option name
configObject Option options
Properties
Name Type Attributes Description typeany Either Boolean, String or Number
descriptionstring <optional>
Description for the option
defaultany <optional>
Default value
aliasany <optional>
Option name alias (example
-hand --help`)hideany <optional>
Boolean whether to hide from help
storageStorage <optional>
Storage to persist the option
Returns:
this -This generator
-
optionsHelp () → {String}
-
Get help text for options
Returns:
String -Text of options in formatted table
- Mixes In:
-
prompt (questions, storage opt ) → {Promise}
-
Prompt user to answer questions. The signature of this method is the same as Inquirer.js
On top of the Inquirer.js API, you can provide a
{store: true}property for every question descriptor. When set to true, Yeoman will store/fetch the user's answers as defaults.Parameters:
Name Type Attributes Description questionsobject | Array.<object> Array of question descriptor objects. See Documentation
Properties
Name Type Attributes Description storageStorage | String <optional>
Storage object or name (generator property) to be used by the question to store/fetch the response.
storageStorage | String <optional>
Storage object or name (generator property) to be used by default to store/fetch responses.
Returns:
Promise -prompt promise
-
queueMethod (method:, methodName opt , queueName opt , reject opt )
-
Schedule methods on a run queue.
Parameters:
Name Type Attributes Description method:function | Object Method to be scheduled or object with function properties.
methodNameString <optional>
Name of the method (task) to be scheduled.
queueNameString <optional>
Name of the queue to be scheduled on.
rejectfunction <optional>
Reject callback.
-
queueTask (task:)
-
Schedule tasks on a run queue.
Parameters:
Name Type Description task:Task Task to be queued.
-
queueTaskGroup (taskGroup:, taskOptions opt )
-
Schedule tasks from a group on a run queue.
Parameters:
Name Type Attributes Description taskGroup:Object Object containing tasks.
taskOptionsTaskOptions <optional>
options.
-
queueTasks () → {Promise}
-
Queue generator tasks.
Returns:
Promise -
queueTransformStream (streams, options) → {this}
-
Add a transform stream to the commit stream.
Most usually, these transform stream will be Gulp plugins.
Parameters:
Name Type Description streamsstream.Transform | Array.<stream.Transform> An array of Transform stream or a single one.
optionsany Environment.applyTransforms options parameter
Returns:
this -This generator
-
readDestination (filepath, …args) → {*}
-
Read file from destination folder mem-fs-editor method's shortcut, for more information see mem-fs-editor . Shortcut for this.fs.read(this.destinationPath(filepath)).
Parameters:
Name Type Attributes Description filepathString absolute file path or relative to destination folder.
args* <repeatable>
for more information see mem-fs-editor
- Mixes In:
-
readDestinationJSON (filepath, …args) → {*}
-
Read JSON file from destination folder mem-fs-editor method's shortcut, for more information see mem-fs-editor . Shortcut for this.fs.readJSON(this.destinationPath(filepath)).
Parameters:
Name Type Attributes Description filepathString absolute file path or relative to destination folder.
args* <repeatable>
for more information see mem-fs-editor
- Mixes In:
-
readTemplate (filepath, …args) → {*}
-
Read file from templates folder. mem-fs-editor method's shortcut, for more information see mem-fs-editor . Shortcut for this.fs.read(this.templatePath(filepath))
Parameters:
Name Type Attributes Description filepathString absolute file path or relative to templates folder.
args* <repeatable>
for more information see mem-fs-editor
- Mixes In:
-
registerConfigPrompts (questions)
-
Register stored config prompts and optional option alternative.
Parameters:
Name Type Attributes Default Description questionsInquirer | Array.<Inquirer> Inquirer question or questions.
Properties
Name Type Attributes Description exportOptionObject | Boolean <optional>
Additional data to export this question as an option.
question.storageStorage | String <optional>
this.config Storage to store the answers.
-
registerPriorities (priorities)
-
Register priorities for this generator
Parameters:
Name Type Description prioritiesArray.<Object> Priorities
Properties
Name Type Attributes Description priorityNameString Priority name
beforeString <optional>
The new priority will be queued before the
beforepriority. Required for new priorities.queueNameString <optional>
Name to be used at grouped-queue
editboolean <optional>
Edit a priority
skipboolean <optional>
Queued manually only
argsArray.<Object> | function <optional>
Arguments to pass to tasks
-
renderTemplate (source, destination opt , templateData opt , templateOptions opt , copyOptions opt )
-
Copy a template from templates folder to the destination.
Parameters:
Name Type Attributes Description sourceString | Array template file, absolute or relative to templatePath().
destinationString | Array <optional>
destination, absolute or relative to destinationPath().
templateDataObject <optional>
ejs data
templateOptionsObject <optional>
ejs options
copyOptionsObject <optional>
mem-fs-editor copy options
- Mixes In:
-
renderTemplateAsync (source, destination opt , templateData opt , templateOptions opt , copyOptions opt )
-
Copy a template from templates folder to the destination.
Parameters:
Name Type Attributes Description sourceString | Array template file, absolute or relative to templatePath().
destinationString | Array <optional>
destination, absolute or relative to destinationPath().
templateDataObject <optional>
ejs data
templateOptionsObject <optional>
ejs options
copyOptionsObject <optional>
mem-fs-editor copy options
- Mixes In:
-
renderTemplates (templates, templateData opt )
-
Copy templates from templates folder to the destination.
Parameters:
Name Type Attributes Description templatesArray template file, absolute or relative to templatePath().
Properties
Name Type Attributes Description whenfunction <optional>
conditional if the template should be written. First argument is the templateData, second is the generator.
sourceString | Array template file, absolute or relative to templatePath().
destinationString | Array <optional>
destination, absolute or relative to destinationPath().
templateOptionsObject <optional>
ejs options
copyOptionsObject <optional>
mem-fs-editor copy options
templateDataObject <optional>
ejs data
- Mixes In:
-
renderTemplatesAsync (templates, templateData opt )
-
Copy templates from templates folder to the destination.
Parameters:
Name Type Attributes Description templatesArray template file, absolute or relative to templatePath().
Properties
Name Type Attributes Description whenfunction <optional>
conditional if the template should be written. First argument is the templateData, second is the generator.
sourceString | Array template file, absolute or relative to templatePath().
destinationString | Array <optional>
destination, absolute or relative to destinationPath().
templateOptionsObject <optional>
ejs options
copyOptionsObject <optional>
mem-fs-editor copy options
templateDataObject <optional>
ejs data
- Mixes In:
-
rootGeneratorName () → {String}
-
Determine the root generator name (the one who's extending Generator).
Returns:
String -The name of the root generator
-
rootGeneratorVersion () → {String}
-
Determine the root generator version (the one who's extending Generator).
Returns:
String -The version of the root generator
-
run () → {Promise}
-
Runs the generator, scheduling prototype methods on a run queue. Method names will determine the order each method is run. Methods without special names will run in the default queue.
Any method named
constructorand any methods prefixed by a_won't be scheduled.Returns:
Promise -Resolved once the process finish
-
setFeatures (features)
-
Configure Generator behaviours.
Parameters:
Name Type Description featuresObject Properties
Name Type Attributes Description uniqueboolean | string <optional>
Generates a uniqueBy id for the environment Accepts 'namespace' or 'true' for one instance by namespace Accepts 'argument' for one instance by namespace and 1 argument
-
sourceRoot (rootPath) → {String}
-
Change the generator source root directory. This path is used by multiples file system methods like (
this.readandthis.copy)Parameters:
Name Type Description rootPathString new source root path
Returns:
String -source root path
-
spawnCommand (command, args, opt opt ) → {String}
-
Normalize a command across OS and spawn it (asynchronously).
Parameters:
Name Type Attributes Description commandString program to execute
argsArray list of arguments to pass to the program
optobject <optional>
any cross-spawn options
Returns:
String -spawned process reference
- Mixes In:
-
spawnCommandSync (command, args, opt opt ) → {String}
-
Normalize a command across OS and spawn it (synchronously).
Parameters:
Name Type Attributes Description commandString program to execute
argsArray list of arguments to pass to the program
optobject <optional>
any cross-spawn options
Returns:
String -spawn.sync result
- Mixes In:
-
startOver (options opt )
-
Start the generator again.
Parameters:
Name Type Attributes Description optionsObject <optional>
options.
-
templatePath (…dest) → {String}
-
Join a path to the source root.
Parameters:
Name Type Attributes Description destString <repeatable>
path parts
Returns:
String -joined path
-
usage () → {String}
-
Output usage information for this given generator, depending on its arguments or options
Returns:
String -Usage information of the generator
- Mixes In:
-
github.username () → {Promise}
-
Retrieves GitHub's username from the GitHub API
Returns:
Promise -Resolved with the GitHub username or rejected if unable to get the information
- Mixes In:
-
writeDestination (filepath, …args) → {*}
-
Write file to destination folder mem-fs-editor method's shortcut, for more information see mem-fs-editor . Shortcut for this.fs.write(this.destinationPath(filepath)).
Parameters:
Name Type Attributes Description filepathString absolute file path or relative to destination folder.
args* <repeatable>
for more information see mem-fs-editor
- Mixes In:
-
writeDestinationJSON (filepath, …args) → {*}
-
Write json file to destination folder mem-fs-editor method's shortcut, for more information see mem-fs-editor . Shortcut for this.fs.writeJSON(this.destinationPath(filepath)).
Parameters:
Name Type Attributes Description filepathString absolute file path or relative to destination folder.
args* <repeatable>
for more information see mem-fs-editor
- Mixes In: