generator-util
Version:
Utils for `generate` generators.
329 lines (211 loc) • 8 kB
Markdown
# generator-util [](https://www.npmjs.com/package/generator-util) [](https://travis-ci.org/jonschlinkert/generator-util)
> Utils for `generate` generators.
## TOC
- [Install](#install)
- [Usage](#usage)
- [API](#api)
- [Related projects](#related-projects)
- [Contributing](#contributing)
- [Building docs](#building-docs)
- [Running tests](#running-tests)
- [Author](#author)
- [License](#license)
_(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_
## Install
Install with [npm](https://www.npmjs.com/):
```sh
$ npm install generator-util --save
```
## Usage
```js
var util = require('generator-util');
```
## API
### [.homeRelative](index.js#L99)
Return a home-relative filepath
**Params**
* `filepath` **{String}**
* `returns` **{String}**
**Example**
```js
utils.homeRelative('foo');
//=> 'dev/foo'
```
### [.isDirectory](index.js#L120)
Return true if a filepath exists and is a directory.
**Params**
* `filepath` **{String}**
* `returns` **{Boolean}**
### [.renameKey](index.js#L133)
Rename the `key` used for storing views/templates
**Params**
* `key` **{String}**
* `view` **{Object}**: the `renameKey` method is used by [templates](https://github.com/jonschlinkert/templates) for both setting and getting templates. When setting, `view` is exposed as the second parameter.
* `returns` **{String}**
### [.toAlias](index.js#L157)
Opposite of `.toFullname`, creates an "alias" from the given `name` by either stripping `options.prefix` from the name, or just removing everything up to the first dash. If `options.alias` is a function, it will be used instead.
**Params**
* `name` **{String}**
* `options` **{Object}**
* `returns` **{String}**
**Example**
```js
utils.toAlias('generate-foo');
//=> 'foo';
utils.toAlias('a-b-c', {prefix: 'a-b'});
//=> 'c';
```
### [.toFullname](index.js#L187)
Opposite of `.toAlias`, creates a generator name from the given `alias` and `namespace`.
**Params**
* `alias` **{String}**
* `namespace` **{String}**
* `returns` **{String}**
**Example**
```js
utils.toFullname('foo', 'generate');
//=> 'generate-foo';
utils.toFullname('generate-bar', 'generate');
//=> 'generate-bar'
```
### [.toGeneratorPath](index.js#L234)
Create an object-path for looking up a generator.
**Params**
* `name` **{String}**
* `returns` **{String}**
**Example**
```js
utils.toGeneratorPath('a.b.c');
//=> 'generators.a.generators.b.generators.c'
```
### [.getGenerator](index.js#L256)
Get a generator from `app`.
**Params**
* `app` **{Object}**
* `name` **{String}**: Generator name
* `returns` **{Object}**: Returns the generator instance.
### [.tryResolve](index.js#L299)
Try to `require.resolve` module `name`, first locally then in the globaly npm directory. Fails silently if not found.
**Params**
* `name` **{String}**: The name or filepath of the module to resolve
* `options` **{Object}**: Pass `options.cwd` and/or `options.configfile` (filename) to modify the path used by `resolve`.
* `returns` **{String|undefined}**
**Example**
```js
utils.tryResolve('foo');
// or
utils.tryResolve('generate-foo');
// or
utils.tryResolve('generate-foo', {cwd: require('global-modules')});
// or
utils.tryResolve('./foo/bar/baz.js');
```
### [.tryRequire](index.js#L341)
Try to require the given module, failing silently if it doesn't exist. The function first calls `require` on the given `name`, then tries `require(path.resolve(name))` before giving up.
**Params**
* `name` **{String}**: The module name or file path
* `returns` **{any|undefined}**: Returns the value of requiring the specified module, or `undefined` if unsuccessful.
**Example**
```js
utils.tryRequire('foo');
```
### [.tableize](index.js#L376)
Modified from the `tableize` lib, which replaces
dashes with underscores, and we don't want that behavior.
Tableize `obj` by flattening and normalizing the keys.
**Params**
* **{Object}**: obj
* `returns` **{Object}**
### [.isFunction](index.js#L431)
Returns true if the given `value` is a function.
**Params**
* `value` **{any}**
* `returns` **{Boolean}**
**Example**
```js
utils.isFunction('foo');
//=> false
utils.isFunction(function() {});
//=> true
```
### [.isBoolean](index.js#L451)
Returns true if the given `value` is a boolean.
**Params**
* `value` **{any}**
* `returns` **{Boolean}**
**Example**
```js
utils.isBoolean('foo');
//=> false
utils.isBoolean(false);
//=> true
```
### [.isString](index.js#L471)
Returns true if a the given `value` is a string.
**Params**
* `value` **{any}**
* `returns` **{Boolean}**
**Example**
```js
utils.isString('foo');
//=> false
utils.isString({});
//=> true
```
### [.isObject](index.js#L491)
Returns true if a the given `value` is an object.
**Params**
* `value` **{any}**
* `returns` **{Boolean}**
**Example**
```js
utils.isObject('foo');
//=> false
utils.isObject({});
//=> true
```
### [.arrayify](index.js#L510)
Cast the given `value` to an array.
**Params**
* `value` **{String|Array}**
* `returns` **{Array}**
**Example**
```js
utils.arrayify('foo');
//=> ['foo']
utils.arrayify(['foo']);
//=> ['foo']
```
## Related projects
* [base](https://www.npmjs.com/package/base): base is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting… [more](https://www.npmjs.com/package/base) | [homepage](https://github.com/node-base/base)
* [base-fs](https://www.npmjs.com/package/base-fs): base-methods plugin that adds vinyl-fs methods to your 'base' application for working with the file… [more](https://www.npmjs.com/package/base-fs) | [homepage](https://github.com/jonschlinkert/base-fs)
* [base-pipeline](https://www.npmjs.com/package/base-pipeline): base-methods plugin that adds pipeline and plugin methods for dynamically composing streaming plugin pipelines. | [homepage](https://github.com/jonschlinkert/base-pipeline)
* [base-plugins](https://www.npmjs.com/package/base-plugins): Upgrade's plugin support in base applications to allow plugins to be called any time after… [more](https://www.npmjs.com/package/base-plugins) | [homepage](https://github.com/jonschlinkert/base-plugins)
* [base-runner](https://www.npmjs.com/package/base-runner): Orchestrate multiple instances of base-methods at once. | [homepage](https://github.com/jonschlinkert/base-runner)
* [base-tasks](https://www.npmjs.com/package/base-tasks): base-methods plugin that provides a very thin wrapper around [https://github.com/jonschlinkert/composer](https://github.com/jonschlinkert/composer) for adding task methods to… [more](https://www.npmjs.com/package/base-tasks) | [homepage](https://github.com/jonschlinkert/base-tasks)
* [generate](https://www.npmjs.com/package/generate): Fast, composable, highly extendable project generator with a user-friendly and expressive API. | [homepage](https://github.com/generate/generate)
## Contributing
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/generator-util/issues/new).
## Building docs
Generate readme and API documentation with [verb](https://github.com/verbose/verb):
```sh
$ npm install verb && npm run docs
```
Or, if [verb](https://github.com/verbose/verb) is installed globally:
```sh
$ verb
```
## Running tests
Install dev dependencies:
```sh
$ npm install -d && npm test
```
## Author
**Jon Schlinkert**
* [github/jonschlinkert](https://github.com/jonschlinkert)
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert)
## License
Copyright © 2016 [Jon Schlinkert](https://github.com/jonschlinkert)
Released under the [MIT license](https://github.com/jonschlinkert/generator-util/blob/master/LICENSE).
***
_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on March 04, 2016._