Skip to content
Mickael Daniel edited this page Jan 30, 2013 · 3 revisions

Environment#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.

Returns the environment instance.

Environment#engine

Configures the engine to use for this environment.

  • engine - String matching an available engine, or a Function taking src and data as argument and returning the rendered template.

Returns the environment.

Environment#appendPath

Appends a filepath to the list of loadpaths.

  • filepath - Absolute or relative (to current working dir) path to add.

Returns the environment.

Environment#appendLookup

Appends a new filepath to the list of lookups path. This should be a relative filepath, like support/scaffold. Environments are created with lib/generators as a lookup path by default.

  • filepath - Relative path to append to the list of lookup paths.

Returns the environment.

Environment#help

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

  • name - String name of the binary name to output (defaults: init)

Returns the help output.

Environment#register

Registers a specific generator to this environment. A generator can be a simple function or an object extending from Generators.Base. The later method is favored as it allows you to specify options / arguments for self-documented generator with USAGE: and so on.

In case of a simple function, the generator does show up in the --help output, but without any kind of arguments / options. You must document them manually with your USAGE file.

In any case, the API available in generators is the same. Raw functions are executed within the context of a new Generators.Base.

register() can also take Strings, in which case it is considered a filepath to require()

  • name - A string or Function. If it's a string, then it is considered to be a package to require. If it's a function, then the generator is registered directly.

  • namespace - An optional String namespace to register this generator with

Returns the environment.

Environment#namespaces

Returns the list of registered namespace.

Environment#get

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)

  • namespace - A String matching the namespace of the generator.

Returns the generator (constructor or raw function) or undefined.

Environment#create

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.

  • namespace - A string matching the namespace to lookup.
  • options - An optional hash object with:
    • arguments - The list of arguments (usually CLI args)
    • options - Hash of options (usually CLI options)

An error is raised on invalid namespace.

Returns the generator instance.

Environment#run

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

  • arguments - A space-separated String or Array of arguments.
  • options - (optional) An Hash object of options.
  • done - (optional) A Function to call on completion, eg. when the generator and all its hooks are done.

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

Returns the resolved generator.

Environment#lookup

Receives namespaces in an array and tries to find matching generators in the load paths.

We lookup namespaces in several places, namely this.lookups list of relatives directory path. A generator- prefix is added if a namespace wasn't require()-able directly, matching generator-* kind of pattern in npm installed package.

You can also lookup using glob-like star pattern, eg. angular:* gets expanded to angular/*/index.js.

The default alias generator-$1 lookup is added automatically.

  • namespaces - A single namespace or an Array of namespaces to look for.
  • basedir - The base directory to work from (defaults: cwd)

Examples

// search for all angular generators in the load path
env.lookup('angular:*');

// register any valid set of generator in the load paths
env.lookup('*:*');

Returns the environment.

Environment#alias

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

  • match - A String or RegExp for pattern match
  • value - The String replacement value

Examples

env.alias(/^([a-zA-Z0-9:\*]+)$/, 'generator-$1');
env.alias(/^([^:]+)$/, '$1:app');
env.alias(/^([^:]+)$/, '$1:all');
env.alias('foo');
// => generator-foo:all

Returns the environment when acing as a setter, or the alias result for getter.

Environment#namespace

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

Returns the resolved namespace

Environment#prefix

Adds the namespace prefix to this environment, such as generator-*, used when resolving namespace, replacing the leading * in the namespace by the configured prefix(es).

Examples

this.prefix('generator-')

Returns the environment

Environment#suffix

Get or set the namespace suffix to this environment, such as */index.js, used when resolving namespace, replacing the last * in the namespace by the configured suffix.

Examples

this.suffix('*/index.js')
this.suffix();
// => '*/index.js'

Returns the environment

Environment#plugins

Walk up the filesystem looking for a node_modules folder, and add it if found to the load path.

  • filename - The base filename to look for when walking up the file system. Defaults to node_modules.
  • basedir - The base directory to look for when walking up the file system. Defaults to process.cwd()

Environment#remote

Install an npm package locally, expanding github like user/repo pattern to the remote tarball for master.

It is taking care of potential remote packages (or local on the current file system) by delegating the groundwork of getting the package to npm.

  • name - The remote name to fetch from, that is simply passed to npm install

Returns the environment.