generator-util
Advanced tools
Comparing version 0.2.0 to 0.2.1
227
index.js
@@ -11,2 +11,4 @@ /*! | ||
var path = require('path'); | ||
var resolveCache = {}; | ||
var requireCache = {}; | ||
@@ -17,2 +19,3 @@ /** | ||
var find = require('./lib/find'); | ||
var debug = require('debug')('base:generators:util'); | ||
@@ -38,2 +41,62 @@ var utils = require('lazy-cache')(require); | ||
/** | ||
* Returns true if the given `value` is a function. | ||
* | ||
* ```js | ||
* utils.isFunction('foo'); | ||
* //=> false | ||
* | ||
* utils.isFunction(function() {}); | ||
* //=> true | ||
* ``` | ||
* | ||
* @param {any} `value` | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
utils.isFunction = function(value) { | ||
return utils.typeOf(value) === 'function'; | ||
}; | ||
/** | ||
* Returns true if the given `value` is a boolean. | ||
* | ||
* ```js | ||
* utils.isBoolean('foo'); | ||
* //=> false | ||
* | ||
* utils.isBoolean(false); | ||
* //=> true | ||
* ``` | ||
* | ||
* @param {any} `value` | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
utils.isBoolean = function(value) { | ||
return utils.typeOf(value) === 'boolean'; | ||
}; | ||
/** | ||
* Returns true if a the given `value` is a string. | ||
* | ||
* ```js | ||
* utils.isString('foo'); | ||
* //=> false | ||
* | ||
* utils.isString({}); | ||
* //=> true | ||
* ``` | ||
* | ||
* @param {any} `value` | ||
* @return {Boolean} | ||
* @api public | ||
*/ | ||
utils.isString = function(value) { | ||
return utils.typeOf(value) === 'string'; | ||
}; | ||
/** | ||
* Returns true if a the given `value` is an object. | ||
@@ -97,2 +160,15 @@ * | ||
/** | ||
* Rename the `key` used for storing views/templates | ||
* | ||
* @param {String} `key` | ||
* @param {Object} `view` the `renameKey` method is used by [templates][] for both setting and getting templates. When setting, `view` is exposed as the second parameter. | ||
* @return {String} | ||
* @api public | ||
*/ | ||
utils.renameKey = function(key, view) { | ||
return view ? view.filename : path.basename(key, path.extname(key)); | ||
}; | ||
/** | ||
* Opposite of `.toFullname`, creates an "alias" from the given | ||
@@ -126,4 +202,4 @@ * `name` by either stripping `options.prefix` from the name, or | ||
} | ||
var basename = path.basename(name, path.extname(name)); | ||
return basename.slice(basename.indexOf('-') + 1); | ||
name = path.basename(name, path.extname(name)); | ||
return name.slice(name.indexOf('-') + 1); | ||
}; | ||
@@ -150,5 +226,13 @@ | ||
var opts = utils.extend({}, options); | ||
if (alias.indexOf(opts.prefix) === -1) { | ||
return opts.prefix + '-' + alias; | ||
var prefix = opts.prefix || opts.modulename; | ||
if (typeof prefix === 'undefined') { | ||
throw new Error('expected prefix to be a string'); | ||
} | ||
// if it's a filepath, just return it | ||
if (utils.isAbsolute(alias)) { | ||
return alias; | ||
} | ||
if (alias.indexOf(prefix) === -1) { | ||
return prefix + '-' + alias; | ||
} | ||
return alias; | ||
@@ -169,3 +253,3 @@ }; | ||
utils.toGeneratorPath = function(name) { | ||
utils.toGeneratorPath = function(name, prefix) { | ||
if (/[\\\/]/.test(name)) { | ||
@@ -180,6 +264,19 @@ return null; | ||
} | ||
return 'generators.' + name; | ||
return prefix === false ? name : ('generators.' + name); | ||
}; | ||
/** | ||
* Get a generator from `app`. | ||
* | ||
* @param {Object} `app` | ||
* @param {String} `name` Generator name | ||
* @return {Object} Returns the generator instance. | ||
* @api public | ||
*/ | ||
utils.getGenerator = function(app, name) { | ||
return app.get(utils.toGeneratorPath(name)); | ||
}; | ||
/** | ||
* Return the filepath for `configfile` or undefined | ||
@@ -193,5 +290,5 @@ * if the file does not exist. | ||
utils.configfile = function(filepath, options) { | ||
utils.configfile = function(configfile, options) { | ||
var opts = utils.extend({cwd: process.cwd()}, options); | ||
var configpath = path.resolve(opts.cwd, filepath); | ||
var configpath = path.resolve(opts.cwd, configfile); | ||
@@ -227,33 +324,21 @@ if (!utils.exists(configpath)) { | ||
debug('tryResolve: "%s"', name); | ||
var key = name + '::' + opts.configfile; | ||
if (utils.isAbsolute(name) && utils.exists(name)) { | ||
return name; | ||
var filepath = find.resolveModule(name, opts); | ||
if (!utils.exists(filepath)) return; | ||
if (resolveCache[key]) { | ||
return resolveCache[key]; | ||
} | ||
var filepath = path.resolve(name); | ||
if (utils.exists(filepath)) { | ||
return filepath; | ||
} | ||
filepath = opts.cwd ? path.resolve(opts.cwd, name) : name; | ||
if (filepath.indexOf(opts.configfile) === -1) { | ||
filepath = path.join(filepath, opts.configfile); | ||
} | ||
// try to resolve `name` from working directory | ||
try { | ||
debug('resolving: "%s", from cwd: "%s"', filepath, opts.cwd); | ||
return utils.resolve.sync(filepath); | ||
var modulepath = utils.resolve.sync(filepath); | ||
if (modulepath) { | ||
return (resolveCache[key] = modulepath); | ||
} | ||
} catch (err) {} | ||
// if a cwd was defined, go directly to jail, don't pass go. | ||
if (typeof opts.cwd === 'string' && opts.cwd !== utils.gm) { | ||
return; | ||
filepath = path.join(filepath, opts.configfile); | ||
if (utils.exists(filepath)) { | ||
return (resolveCache[key] = filepath); | ||
} | ||
// try resolve `name` in global npm modules | ||
try { | ||
debug('resolving from global modules: "%s"', name); | ||
return utils.resolve.sync(name, {basedir: utils.gm}); | ||
} catch (err) {} | ||
}; | ||
@@ -274,12 +359,80 @@ | ||
utils.tryRequire = function(name) { | ||
utils.tryRequire = function(name, options) { | ||
var fn; | ||
if (requireCache[name]) { | ||
return requireCache[name]; | ||
} | ||
try { | ||
return require(name); | ||
} catch (err) {}; | ||
fn = require(name); | ||
if (fn) { | ||
return (requireCache[name] = fn); | ||
} | ||
} catch (err) { | ||
handleError(err); | ||
} | ||
var filepath = utils.tryResolve(name, options); | ||
if (!filepath) return; | ||
try { | ||
return require(path.resolve(name)); | ||
} catch (err) {}; | ||
fn = require(filepath); | ||
if (fn) { | ||
return (requireCache[name] = fn); | ||
} | ||
} catch (err) { | ||
handleError(err); | ||
} | ||
}; | ||
/** | ||
* 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. | ||
* | ||
* @param {Object} obj | ||
* @return {Object} | ||
* @api public | ||
*/ | ||
utils.tableize = function(obj) { | ||
var table = {}; | ||
flatten(table, obj, ''); | ||
return table; | ||
}; | ||
/** | ||
* Recursively flatten object keys to use dot-notation. | ||
* | ||
* @param {Object} `table` | ||
* @param {Object} `obj` | ||
* @param {String} `parent` | ||
*/ | ||
function flatten(table, obj, parent) { | ||
for (var key in obj) { | ||
if (obj.hasOwnProperty(key)) { | ||
var val = obj[key]; | ||
key = parent + key; | ||
if (utils.isObject(val)) { | ||
flatten(table, val, key + '.'); | ||
} else { | ||
table[key] = val; | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* Placeholder | ||
*/ | ||
function handleError(err) { | ||
if (err.code !== 'MODULE_NOT_FOUND') { | ||
throw err; | ||
} | ||
} | ||
/** | ||
* Expose `utils` | ||
@@ -286,0 +439,0 @@ */ |
{ | ||
"name": "generator-util", | ||
"description": "Utils for `generate` generators.", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"homepage": "https://github.com/jonschlinkert/generator-util", | ||
@@ -6,0 +6,0 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", |
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
17647
380