generator-util
Advanced tools
Comparing version 0.1.1 to 0.2.0
43
index.js
@@ -16,8 +16,7 @@ /*! | ||
var debug = require('debug')('generators:util'); | ||
var debug = require('debug')('base:generators:util'); | ||
var utils = require('lazy-cache')(require); | ||
/* eslint-disable no-native-reassign */ | ||
// eslint-disable no-native-reassign | ||
var fn = require; | ||
/* eslint-disable no-undef */ | ||
// eslint-disable no-undef | ||
require = utils; | ||
@@ -179,2 +178,21 @@ | ||
/** | ||
* Return the filepath for `configfile` or undefined | ||
* if the file does not exist. | ||
* | ||
* @param {String} `configfile` | ||
* @param {Object} `options` | ||
* @return {String} | ||
*/ | ||
utils.configfile = function(filepath, options) { | ||
var opts = utils.extend({cwd: process.cwd()}, options); | ||
var configpath = path.resolve(opts.cwd, filepath); | ||
if (!utils.exists(configpath)) { | ||
throw new Error('file "' + configpath + '" does not exist'); | ||
} | ||
return utils.tryRequire(configpath); | ||
}; | ||
/** | ||
* Try to `require.resolve` module `name`, first locally | ||
@@ -236,4 +254,5 @@ * then in the globaly npm directory. Fails silently | ||
/** | ||
* Try to require the given module, failing silently if | ||
* it doesn't exist. | ||
* 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. | ||
* | ||
@@ -244,11 +263,13 @@ * ```js | ||
* @param {String} `name` The module name or file path | ||
* @param {Object} `options` | ||
* @return {any|Null} Returns the value of requiring the specified module, or `null` | ||
* @return {any|undefined} Returns the value of requiring the specified module, or `undefined` if unsuccessful. | ||
* @api public | ||
*/ | ||
utils.tryRequire = function(fp, options) { | ||
utils.tryRequire = function(name) { | ||
try { | ||
return require(utils.tryResolve(fp, options)); | ||
} catch (err) {} | ||
return require(name); | ||
} catch (err) {}; | ||
try { | ||
return require(path.resolve(name)); | ||
} catch (err) {}; | ||
}; | ||
@@ -255,0 +276,0 @@ |
{ | ||
"name": "generator-util", | ||
"description": "Utils for `generate` generators.", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"homepage": "https://github.com/jonschlinkert/generator-util", | ||
@@ -6,0 +6,0 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", |
@@ -32,3 +32,3 @@ # generator-util [](https://www.npmjs.com/package/generator-util) [](https://travis-ci.org/jonschlinkert/generator-util) | ||
### [.isObject](index.js#L52) | ||
### [.isObject](index.js#L51) | ||
@@ -52,3 +52,3 @@ Returns true if a the given `value` is an object. | ||
### [.arrayify](index.js#L71) | ||
### [.arrayify](index.js#L70) | ||
@@ -72,3 +72,3 @@ Cast the given `value` to an array. | ||
### [.exists](index.js#L90) | ||
### [.exists](index.js#L89) | ||
@@ -92,3 +92,3 @@ Return true if a filepath exists on the file system. | ||
### [.toAlias](index.js#L114) | ||
### [.toAlias](index.js#L113) | ||
@@ -113,3 +113,3 @@ 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. | ||
### [.toFullname](index.js#L144) | ||
### [.toFullname](index.js#L143) | ||
@@ -134,3 +134,3 @@ Opposite of `.toAlias`, creates a generator name from the given `alias` and `namespace`. | ||
### [.toGeneratorPath](index.js#L164) | ||
### [.toGeneratorPath](index.js#L163) | ||
@@ -151,3 +151,3 @@ Create an object-path for looking up a generator. | ||
### [.tryResolve](index.js#L197) | ||
### [.tryResolve](index.js#L215) | ||
@@ -174,5 +174,5 @@ Try to `require.resolve` module `name`, first locally then in the globaly npm directory. Fails silently if not found. | ||
### [.tryRequire](index.js#L246) | ||
### [.tryRequire](index.js#L264) | ||
Try to require the given module, failing silently if it doesn't exist. | ||
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. | ||
@@ -182,4 +182,3 @@ **Params** | ||
* `name` **{String}**: The module name or file path | ||
* `options` **{Object}** | ||
* `returns` **{any|Null}**: Returns the value of requiring the specified module, or `null` | ||
* `returns` **{any|undefined}**: Returns the value of requiring the specified module, or `undefined` if unsuccessful. | ||
@@ -230,2 +229,2 @@ **Example** | ||
_This file was generated by [verb](https://github.com/verbose/verb) on January 27, 2016._ | ||
_This file was generated by [verb](https://github.com/verbose/verb) on January 29, 2016._ |
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
14870
244
220
4