Generator (args, options)

The Generator class provides the common API shared by all generators. It define options, arguments, file, prompt, log, API, etc.

It mixes into its prototype all the methods found in the actions/ mixins.

Every generator should extend this base class.

new Generator (args, options)

Parameters:
Name Type Description
args Array.<string>

Provide arguments at initialization

options Object

Provide options at initialization

Properties
Name Type Attributes Description
customPriorities Array.< Priority > <optional>

Custom priorities

Properties:
Name Type Description
env Object

the current Environment being run

resolved String

the path to the current generator

description String

Used in --help output

appname String

The application name

config Storage

.yo-rc config file manager

fs Object

An instance of Mem-fs-editor

log function

Output content through Interface Adapter

Mixes In:
Example
const Generator = require('yeoman-generator');
module.exports = class extends Generator {
  writing() {
    this.fs.write(this.destinationPath('index.js'), 'const foo = 1;');
  }
};

Methods

_templateData (path) → {Object}

Utility method to get a formatted data for templates.

Parameters:
Name Type Description
path String

path to the storage key.

Returns:
Object -

data to be passed to the templates.

Mixes In:

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
name String

Argument name

config Object

Argument options

Properties
Name Type Attributes Description
type any

String, Number, Array, or Object

description string <optional>

Description for the argument

required boolean <optional>

required` Boolean whether it is required

optional boolean <optional>

Boolean whether it is optional

default any <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:

bowerInstall (cmpnt opt , options opt , spawnOptions opt )

Receives a list of components and an options object to install through bower.

The installation will automatically run during the run loop install phase.

Parameters:
Name Type Attributes Description
cmpnt String | Array <optional>

Components to install

options Object <optional>

Options to pass to dargs as arguments

spawnOptions Object <optional>

Options to pass child_process.spawn .

Mixes In:

cancelCancellableTasks ()

Ignore cancellable tasks.

composeWith (generator, options opt , returnNewGenerator opt ) → {this|Object}

Compose this generator with another one.

Parameters:
Name Type Attributes Default Description
generator String | Object | Array

The path to the generator module or an object (see examples)

options Object <optional>

The options passed to the Generator

returnNewGenerator boolean <optional>
false

Returns the created generator instead of returning this.

Returns:
this | Object -

This generator or the composed generator when returnNewGenerator=true

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
from String

absolute file path or relative to destination folder.

to String

absolute file path or relative to destination folder.

args * <repeatable>

for more information see mem-fs-editor

Returns:
* -

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
from String

absolute file path or relative to templates folder.

to String

absolute file path or relative to destination folder.

args * <repeatable>

for more information see mem-fs-editor

Returns:
* -

for more information see mem-fs-editor

Mixes In:

createStorage (storePath, path opt , lodashPath opt ) → { Storage }

Return a storage instance.

Parameters:
Name Type Attributes Default Description
storePath String

The path of the json file

path String <optional>

The name in which is stored inside the json

lodashPath String <optional>
false

Treat path as an lodash path

Returns:
Storage -

json storage

debug (…args)

Convenience debug method

Parameters:
Name Type Attributes Description
args any <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
filepath String

absolute file path or relative to destination folder.

args * <repeatable>

for more information see mem-fs-editor

Returns:
* -

for more information see mem-fs-editor

Mixes In:

desc (description)

Simple setter for custom description to append on help output.

Parameters:
Name Type Description
description String
Mixes In:

destinationPath (…dest) → {String}

Join a path to the destination root.

Parameters:
Name Type Attributes Description
dest String <repeatable>

path parts

Returns:
String -

joined path

destinationRoot (rootPath, skipEnvironment) → {String}

Change the generator destination root directory. This path is used to find storage, when using a file system helper method (like this.write and this.copy )

Parameters:
Name Type Default Description
rootPath String

new destination root path

skipEnvironment Boolean false

don't update the environment cwd/chdir.

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
filepath String

absolute file path or relative to destination folder.

args * <repeatable>

for more information see mem-fs-editor

Returns:
* -

for more information see mem-fs-editor

Mixes In:

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:

installDependencies (options opt )

Runs npm and bower , in sequence, in the generated directory and prints a message to let the user know.

Parameters:
Name Type Attributes Description
options Object <optional>

options

Properties
Name Type Attributes Default Description
npm Boolean | Object <optional>
true

whether to run npm install or can be options to pass to dargs as arguments

bower Boolean | Object <optional>
true

whether to run bower install or can be options to pass to dargs as arguments

yarn Boolean | Object <optional>
false

whether to run yarn install or can be options to pass to dargs as arguments

skipMessage Boolean <optional>
false

whether to log the used commands

Mixes In:
Examples
this.installDependencies({
  bower: true,
  npm: true
});
this.installDependencies({
  yarn: {force: true},
  npm: false
});

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
from String

absolute file path or relative to destination folder.

to String

absolute file path or relative to destination folder.

Returns:
* -

for more information see mem-fs-editor

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:

npmInstall (pkgs opt , options opt , spawnOptions opt )

Receives a list of packages and an options object to install through npm.

The installation will automatically run during the run loop install phase.

Parameters:
Name Type Attributes Description
pkgs String | Array <optional>

Packages to install

options Object <optional>

Options to pass to dargs as arguments

spawnOptions Object <optional>

Options to pass child_process.spawn .

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.options hash object.

Parameters:
Name Type Attributes Description
name String <optional>

Option name

config Object

Option options

Properties
Name Type Attributes Description
type any

Either Boolean, String or Number

description string <optional>

Description for the option

default any <optional>

Default value

alias any <optional>

Option name alias (example -h and --help`)

hide any <optional>

Boolean whether to hide from help

storage Storage <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
questions object | Array.<object>

Array of question descriptor objects. See Documentation

Properties
Name Type Attributes Description
storage Storage | String <optional>

Storage object or name (generator property) to be used by the question to store/fetch the response.

storage Storage | String <optional>

Storage object or name (generator property) to be used by default to store/fetch responses.

Returns:
Promise -

prompt promise

queueBasicTasks ()

Queue generator's basic tasks, only once execution is required for each environment.

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.

methodName String <optional>

Name of the method (task) to be scheduled.

queueName String <optional>

Name of the queue to be scheduled on.

reject function <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 methods on a run queue.

Parameters:
Name Type Attributes Description
taskGroup: Object

Object containing tasks.

taskOptions TaskOptions <optional>

options.

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
filepath String

absolute file path or relative to destination folder.

args * <repeatable>

for more information see mem-fs-editor

Returns:
* -

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
filepath String

absolute file path or relative to destination folder.

args * <repeatable>

for more information see mem-fs-editor

Returns:
* -

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
filepath String

absolute file path or relative to templates folder.

args * <repeatable>

for more information see mem-fs-editor

Returns:
* -

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
questions Inquirer | Array.<Inquirer>

Inquirer question or questions.

Properties
Name Type Attributes Description
exportOption Object | Boolean <optional>

Additional data to export this question as an option.

question.storage Storage | String <optional>
this.config

Storage to store the answers.

registerPriorities (priorities)

Register priorities for this generator

Parameters:
Name Type Description
priorities Array.<Object>

Priorities

Properties
Name Type Attributes Description
priorityName String

Priority name

before String

The new priority will be queued before the before priority.

queueName String <optional>

Name to be used at grouped-queue

registerTransformStream (streams) → {this}

Add a transform stream to the commit stream.

Most usually, these transform stream will be Gulp plugins.

Parameters:
Name Type Description
streams stream.Transform | Array.<stream.Transform>

An array of Transform stream or a single one.

Returns:
this -

This generator

renderTemplate (source, destination opt , templateData opt , templateOptions opt , copyOptions opt )

Copy a template from templates folder to the destination.

Parameters:
Name Type Attributes Description
source String | Array

template file, absolute or relative to templatePath().

destination String | Array <optional>

destination, absolute or relative to destinationPath().

templateData Object <optional>

ejs data

templateOptions Object <optional>

ejs options

copyOptions Object <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
templates Array

template file, absolute or relative to templatePath().

Properties
Name Type Attributes Description
when function <optional>

conditional if the template should be written. First argument is the templateData, second is the generator.

source String | Array

template file, absolute or relative to templatePath().

destination String | Array <optional>

destination, absolute or relative to destinationPath().

templateOptions Object <optional>

ejs options

copyOptions Object <optional>

mem-fs-editor copy options

templateData Object <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 (cb opt ) → {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 constructor and any methods prefixed by a _ won't be scheduled.

Parameters:
Name Type Attributes Description
cb function <optional>

Deprecated: prefer to use the promise interface

Returns:
Promise -

Resolved once the process finish

scheduleInstallTask (installer, paths opt , options opt , spawnOptions opt )

Combine package manager cmd line arguments and run the install command.

During the install step, every command will be scheduled to run once, on the run loop.

Parameters:
Name Type Attributes Description
installer String

Which package manager to use

paths String | Array <optional>

Packages to install. Use an empty string for npm install

options Object <optional>

Options to pass to dargs as arguments

spawnOptions Object <optional>

Options to pass child_process.spawn . ref https://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options

Mixes In:

sourceRoot (rootPath) → {String}

Change the generator source root directory. This path is used by multiples file system methods like ( this.read and this.copy )

Parameters:
Name Type Description
rootPath String

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
command String

program to execute

args Array

list of arguments to pass to the program

opt object <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
command String

program to execute

args Array

list of arguments to pass to the program

opt object <optional>

any cross-spawn options

Returns:
String -

spawn.sync result

Mixes In:

startOver (options opt )

Start the generator again.

Parameters:
Name Type Attributes Description
options Object <optional>

options.

templatePath (…dest) → {String}

Join a path to the source root.

Parameters:
Name Type Attributes Description
dest String <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
filepath String

absolute file path or relative to destination folder.

args * <repeatable>

for more information see mem-fs-editor

Returns:
* -

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
filepath String

absolute file path or relative to destination folder.

args * <repeatable>

for more information see mem-fs-editor

Returns:
* -

for more information see mem-fs-editor

Mixes In:

yarnInstall (pkgs opt , options opt , spawnOptions opt )

Receives a list of packages and an options object to install through yarn.

The installation will automatically run during the run loop install phase.

Parameters:
Name Type Attributes Description
pkgs String | Array <optional>

Packages to install

options Object <optional>

Options to pass to dargs as arguments

spawnOptions Object <optional>

Options to pass child_process.spawn .

Mixes In: