Class: Environment

Environment

new Environment(args, opts, adaperopt)

Environment object is responsible of handling the lifecyle and bootstrap of generators in a specific environment (your app).

It provides a high-level API to create and run generators, as well as further tuning where and how a generator is resolved.

An environment is created using a list of arguments and a Hash of options. Usually, this is the list of arguments you get back from your CLI options parser.

An optional adapter can be passed to provide interaction in non-CLI environment (e.g. IDE plugins), otherwise a TerminalAdapter is instantiated by default

Parameters:
Name Type Attributes Description
args String | Array
opts Object
adaper TerminalAdapter <optional>

A TerminalAdapter instance or another object implementing this adapter interface. This is how you'd interface Yeoman with a GUI or an editor.

Mixes In:
Source:

Members

(static) util

Expose the utilities on the module

Source:
See:
  • env/util

Methods

(static) createEnv() → {Environment}

Factory method to create an environment instance. Take same parameters as the Environment constructor.

Source:
See:
  • This method take the same arguments as Environment constructor
Returns:

a new Environment instance

Type
Environment

(static) enforceUpdate(env) → {Environment}

Make sure the Environment present expected methods if an old version is passed to a Generator.

Parameters:
Name Type Description
env Environment
Source:
Returns:

The updated env

Type
Environment

(static) namespaceToName(namespace) → {String}

Convert a generators namespace to its name

Parameters:
Name Type Description
namespace String
Source:
Returns:
Type
String

alias(match, value)

Get or create an alias.

Alias allows the get() and lookup() methods to search in alternate filepath for a given namespaces. It's used for example to map generator-* npm package to their namespace equivalent (without the generator- prefix), or to default a single namespace like angular to angular:app or angular:all.

Given a single argument, this method acts as a getter. When both name and value are provided, acts as a setter and registers that new alias.

If multiple alias are defined, then the replacement is recursive, replacing each alias in reverse order.

An alias can be a single String or a Regular Expression. The finding is done based on .match().

Parameters:
Name Type Description
match String | RegExp
value String
Mixes In:
Source:
Example
env.alias(/^([a-zA-Z0-9:\*]+)$/, 'generator-$1');
    env.alias(/^([^:]+)$/, '$1:app');
    env.alias(/^([^:]+)$/, '$1:all');
    env.alias('foo');
    // => generator-foo:all

create(namespace, options)

Create is the Generator factory. It takes a namespace to lookup and optional hash of options, that lets you define arguments and options to instantiate the generator with.

An error is raised on invalid namespace.

Parameters:
Name Type Description
namespace String
options Object
Source:

error(err) → {Error}

Error handler taking err instance of Error.

The error event is emitted with the error object, if no error listener is registered, then we throw the error.

Parameters:
Name Type Description
err Object
Source:
Returns:

err

Type
Error

findGeneratorsIn(List) → {Array}

Search npm for every available generators. Generators are npm packages who's name start with generator- and who're placed in the top level node_module path. They can be installed globally or locally.

Parameters:
Name Type Description
List Array

of search paths

Mixes In:
Source:
Returns:

List of the generator modules path

Type
Array

get(namespace) → {Generator|null}

Get a single generator from the registered list of generators. The lookup is based on generator's namespace, "walking up" the namespaces until a matching is found. Eg. if an angular:common namespace is registered, and we try to get angular:common:all then we get angular:common as a fallback (unless an angular:common:all generator is registered).

Parameters:
Name Type Description
namespace String
Source:
Returns:
  • the generator registered under the namespace
Type
Generator | null

getByPath(path) → {Generator|null}

Get a generator by path instead of namespace.

Parameters:
Name Type Description
path String
Source:
Returns:
  • the generator found at the location
Type
Generator | null

getGeneratorNames() → {Array}

Get registered generators names

Source:
Returns:
Type
Array

getGeneratorsMeta() → {Object}

Returns stored generators meta

Source:
Returns:
Type
Object

getNpmPaths() → {Array}

Get the npm lookup directories (node_modules/)

Mixes In:
Source:
Returns:

lookup paths

Type
Array

help(name)

Outputs the general help and usage. Optionally, if generators have been registered, the list of available generators is also displayed.

Parameters:
Name Type Description
name String
Source:

instantiate(namespace, options)

Instantiate a Generator with metadatas

Parameters:
Name Type Description
namespace String
options Object
Properties
Name Type Description
arguments Array | String

Arguments to pass the instance

options Object

Options to pass the instance

Source:

lookup(cb)

Search for generators and their sub generators.

A generator is a :lookup/:name/index.js file placed inside an npm package.

Defaults lookups are:

  • ./
  • generators/
  • lib/generators/

So this index file node_modules/generator-dummy/lib/generators/yo/index.js would be registered as dummy:yo generator.

Parameters:
Name Type Description
cb function

Callback called once the lookup is done. Take err as first parameter.

Mixes In:
Source:

namespace(filepath)

Given a String filepath, tries to figure out the relative namespace.

Examples:

this.namespace('backbone/all/index.js');
// => backbone:all

this.namespace('generator-backbone/model');
// => backbone:model

this.namespace('backbone.js');
// => backbone

this.namespace('generator-mocha/backbone/model/index.js');
// => mocha:backbone:model
Parameters:
Name Type Description
filepath String
Source:

namespaces() → {Array}

Returns the list of registered namespace.

Source:
Returns:
Type
Array

register(name, namespace) → {String}

Registers a specific generator to this environment. This generator is stored under provided namespace, or a default namespace format if none if available.

Parameters:
Name Type Description
name String

Filepath to the a generator or a npm package name

namespace String

Namespace under which register the generator (optional)

Source:
Returns:

namespace - Namespace assigned to the registered generator

Type
String

registerStub(Generator, namespace) → {this}

Register a stubbed generator to this environment. This method allow to register raw functions under the provided namespace. registerStub will enforce the function passed to extend the Base generator automatically.

Parameters:
Name Type Description
Generator function

A Generator constructor or a simple function

namespace String

Namespace under which register the generator

Source:
Returns:
Type
this

resolveModulePath(moduleId) → {String}

Resolve a module path

Parameters:
Name Type Description
moduleId String

Filepath or module name

Source:
Returns:
  • The resolved path leading to the module
Type
String

run(args, options, done)

Tries to locate and run a specific generator. The lookup is done depending on the provided arguments, options and the list of registered generators.

When the environment was unable to resolve a generator, an error is raised.

Parameters:
Name Type Description
args String | Array
options Object
done function
Source: