Base

new Base(args, options)

The Base 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.

Parameters:
Name Type Description
args String | Array
options Object
Properties:
Name Type Description
env Object

the current Environment being run

args Object

Provide arguments at initialization

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
var generator = require('yeoman-generator');
var MyGenerator = generator.Base.extend({
  writing: function() {
    this.fs.write('var foo = 1;', this.destinationPath('index.js'));
  }
});

Members

static extend

Extend this Class to create a new one inherithing this one. Also add a helper __super__ object pointing to the parent prototypes methods

Methods

_writeFiles(done)

Write memory fs file to disk and logging results

Parameters:
Name Type Description
done function

callback once files are written

argument(name, config)

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).

Options:

  • description Description for the argument
  • required Boolean whether it is required
  • optional Boolean whether it is optional
  • type String, Number, Array, or Object
  • default Default value for this argument
Parameters:
Name Type Description
name String
config Object

argumentsHelp() → {String}

Get help text for arguments

Returns:
String -

Text of options in formatted table

Mixes In:

bowerInstall(cmpntopt, optionsopt, cbopt, spawnOptionsopt)

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

cb function <optional>
spawnOptions Object <optional>

Options to pass child_process.spawn.

Mixes In:

composeWith(namespace, options, settingsopt) → {this}

Compose this generator with another one.

Parameters:
Name Type Attributes Description
namespace String

The generator namespace to compose with

options Object

The options passed to the Generator

settings Object <optional>

Settings hash on the composition relation

Properties
Name Type Attributes Default Description
local string <optional>

Path to a locally stored generator

link String <optional>
"weak"

If "strong", the composition will occured even when the composition is initialized by the end user

Returns:
this
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 });

desc(description)

Simple setter for custom description to append on help output.

Parameters:
Name Type Description
description String
Mixes In:

destinationPath(…path) → {String}

Join a path to the destination root.

Parameters:
Name Type Attributes Description
path String <repeatable>
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.write and this.copy)

Parameters:
Name Type Description
rootPath String

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()

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)

Mixes In:

help()

Tries to get the description from a USAGE file one folder above the source root otherwise uses a default description.

Mixes In:

installDependencies(optionsopt)

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>
Properties
Name Type Attributes Default Description
npm Boolean <optional>
true

whether to run npm install

bower Boolean <optional>
true

whether to run bower install

yarn Boolean <optional>
false

whether to run yarn install

skipMessage Boolean <optional>
false

whether to log the used commands

callback function <optional>

call once all commands have run

Mixes In:
Example
this.installDependencies({
  bower: true,
  npm: true,
  callback: function () {
    console.log('Everything is ready!');
  }
});

git.name()

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)

Mixes In:

npmInstall(pkgsopt, optionsopt, cbopt, spawnOptionsopt)

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

cb function <optional>
spawnOptions Object <optional>

Options to pass child_process.spawn.

Mixes In:

option(name, config)

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.

Options:

  • description Description for the option
  • type Either Boolean, String or Number
  • alias Option name alias (example -h and --help`)
  • default Default value
  • hide Boolean whether to hide from help
Parameters:
Name Type Description
name String
config Object

optionsHelp() → {String}

Get help text for options

Returns:
String -

Text of options in formatted table

Mixes In:

prompt(questions) → {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 {cache: true} property for every question descriptor. When set to true, Yeoman will store/fetch the user's answers as defaults.

Parameters:
Name Type Description
questions array

Array of question descriptor objects. See Documentation

Returns:
Promise

registerTransformStream(stream) → {this}

Add a transform stream to the commit stream.

Most usually, these transform stream will be Gulp plugins.

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

An array of Transform stream or a single one.

Returns:
this

rootGeneratorName() → {String}

Determine the root generator name (the one who's extending Base).

Returns:
String -

The name of the root generator

rootGeneratorVersion() → {String}

Determine the root generator version (the one who's extending Base).

Returns:
String -

The version of the root generator

run(cbopt)

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.

You can also supply the arguments for the method to be invoked. If none are provided, the same values used to initialize the invoker are used to initialize the invoked.

Parameters:
Name Type Attributes Description
cb function <optional>

runInstall(installer, pathsopt, optionsopt, cbopt, spawnOptionsopt)

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. (So don't combine the callback with this.async())

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

cb function <optional>
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, optopt)

Normalize a command across OS and spawn it (asynchronously).

Parameters:
Name Type Attributes Description
command String
args Array
opt object <optional>
Mixes In:

spawnCommandSync(command, args, optopt)

Normalize a command across OS and spawn it (synchronously).

Parameters:
Name Type Attributes Description
command String
args Array
opt object <optional>
Mixes In:

templatePath(…path) → {String}

Join a path to the source root.

Parameters:
Name Type Attributes Description
path String <repeatable>
Returns:
String -

joined path

usage()

Output usage information for this given generator, depending on its arguments or options.

Mixes In:

github.username()

Retrieves GitHub's username from the GitHub API.

Mixes In:

yarnInstall(pkgsopt, optionsopt, cbopt, spawnOptionsopt)

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

cb function <optional>
spawnOptions Object <optional>

Options to pass child_process.spawn.

Mixes In: