Socket
Socket
Sign inDemoInstall

lazy-require

Package Overview
Dependencies
Maintainers
1
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lazy-require - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

HISTORY.md

18

LICENSE.md

@@ -1,5 +0,15 @@

(The MIT License)
Copyright © 2013+ [Bevry Pty Ltd](http://bevry.me) <us@bevry.me>
<!-- LICENSEFILE/ -->
# License
Unless stated otherwise all works are:
- Copyright &copy; 2013+ Bevry Pty Ltd <us@bevry.me> (http://bevry.me)
and licensed under:
- The incredibly [permissive](http://en.wikipedia.org/wiki/Permissive_free_software_licence) [MIT License](http://opensource.org/licenses/mit-license.php)
## MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

@@ -10,1 +20,5 @@

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
<!-- /LICENSEFILE -->

@@ -1,37 +0,178 @@

// Generated by CoffeeScript 1.6.3
var extractOpts, lazyRequire, safeps;
// Generated by CoffeeScript 1.9.1
(function() {
var complete, extractOptsAndCallback, fsUtil, lazyRequire, pathUtil, safeps;
safeps = require('safeps');
pathUtil = require('path');
extractOpts = require('extract-opts').extractOpts;
fsUtil = require('fs');
lazyRequire = {
lazyRequire: function(name, opts, next) {
var err, m, _ref;
_ref = extractOpts(opts, next), opts = _ref[0], next = _ref[1];
safeps = require('safeps');
extractOptsAndCallback = require('extract-opts');
complete = function(error, result, next) {
if (next) {
next(error, result);
return null;
} else {
return error || result;
}
};
lazyRequire = module.exports = function(name, opts, next) {
var ref;
ref = extractOptsAndCallback(opts, next), opts = ref[0], next = ref[1];
if (next) {
return lazyRequire.async(name, opts, next);
} else {
return lazyRequire.sync(name, opts, next);
}
};
lazyRequire.auto = function(name, opts, next) {
var ref;
ref = extractOptsAndCallback(opts, next), opts = ref[0], next = ref[1];
if (!next || lazyRequire.canSyncInstall()) {
return lazyRequire.requireOrInstallSync(name, opts, next);
} else {
return lazyRequire.requireOrInstallAsync(name, opts, next);
}
};
lazyRequire.sync = function(name, opts, next) {
var error, ref, result;
ref = extractOptsAndCallback(opts, next), opts = ref[0], next = ref[1];
result = lazyRequire.require(name, opts);
if (result instanceof Error) {
result = lazyRequire.installSync(name, opts);
if (result instanceof Error) {
error = result;
result = null;
}
}
return complete(error, result, next);
};
lazyRequire.async = function(name, opts, next) {
var ref;
ref = extractOptsAndCallback(opts, next), opts = ref[0], next = ref[1];
lazyRequire.require(name, opts, function(err, result) {
if (result) {
return next(err, result);
}
return lazyRequire.installAsync(name, opts, next);
});
return null;
};
lazyRequire.require = function(name, opts, next) {
var e1, e2, error, path, ref, result;
ref = extractOptsAndCallback(opts, next), opts = ref[0], next = ref[1];
try {
m = require(name);
result = require(name);
} catch (_error) {
e1 = _error;
error = e1;
if (opts.cwd) {
path = pathUtil.join(opts.cwd, 'node_modules', name);
try {
result = require(path);
error = null;
} catch (_error) {
e2 = _error;
error = e2;
}
}
}
return complete(error, result, next);
};
lazyRequire.canSave = function(name, opts, next) {
var err, error, ref, result;
ref = extractOptsAndCallback(opts, next), opts = ref[0], next = ref[1];
if (opts.cwd == null) {
opts.cwd = process.cwd();
}
opts.packagePath = pathUtil.join(opts.cwd, 'package.json');
try {
result = fsUtil.existsSync(packagePath) === true;
} catch (_error) {
err = _error;
m = null;
error = err;
}
if (m) {
return next(null, m);
return complete(error, result, next);
};
lazyRequire.canSyncInstall = function(opts, next) {
var error, ref, result;
ref = extractOptsAndCallback(opts, next), opts = ref[0], next = ref[1];
result = safeps.hasSpawnSync();
if (result instanceof Error) {
error = result;
result = null;
}
return safeps.spawn(['npm', 'install', name], opts, function(err) {
var _err;
if (err) {
return next(err, m);
return complete(error, result, next);
};
lazyRequire.installAsync = function(name, opts, next) {
var args, error, ref;
ref = extractOptsAndCallback(opts, next), opts = ref[0], next = ref[1];
if (opts.save == null) {
opts.save = false;
}
if (opts.cwd == null) {
opts.cwd = process.cwd();
}
if (process.browser === true) {
error = new Error("lazy-require: installing in the browser is not possible");
} else if (opts.save === true && lazyRequire.canSave() === false) {
error = new Error("lazy-require: cannot save the module as `opts.cwd` did not contain a `package.json` file");
} else {
args = ['npm', 'install', name];
if (opts.save === true) {
args.push('--save');
delete opts.save;
}
try {
m = require(name);
} catch (_error) {
_err = _error;
return next(err, m);
safeps.spawn(args, opts, function(err) {
if (err) {
return next(err);
}
return lazyRequire.require(name, opts, next);
});
return null;
}
return complete(error, null, next);
};
lazyRequire.installSync = function(name, opts, next) {
var args, error, ref, spawnResult;
ref = extractOptsAndCallback(opts, next), opts = ref[0], next = ref[1];
if (opts.save == null) {
opts.save = false;
}
if (opts.cwd == null) {
opts.cwd = process.cwd();
}
if (process.browser === true) {
error = new Error("lazy-require: installing in the browser is not possible");
} else if (lazyRequire.canSyncInstall() === false) {
error = new Error("lazy-require: installing synchronously is not possible");
} else if (opts.save === true && lazyRequire.canSave() === false) {
error = new Error("lazy-require: cannot save the module as `opts.cwd` did not contain a `package.json` file");
} else {
args = ['npm', 'install', name];
if (opts.save === true) {
args.push('--save');
delete opts.save;
}
return next(err, m);
});
}
};
spawnResult = safeps.spawnSync(args, opts);
if (spawnResult instanceof Error) {
error = spawnResult;
} else {
return lazyRequire.require(name, opts, next);
}
}
return complete(error, null, next);
};
module.exports = lazyRequire;
}).call(this);

111

package.json
{
"name": "lazy-require",
"version": "1.0.0",
"description": "Lazy require allows you to require modules lazily, meaning that when you lazy require a missing module, it is automatically installed. If the installation or require fails, the error is returned to the lazy require callback.",
"homepage": "https://github.com/bevry/lazy-require",
"keywords": [
"require", "lazy", "optionalDependencies", "optDependencies", "dependencies", "deps"
],
"author": "Bevry Pty Ltd <us@bevry.me> (http://bevry.me)",
"maintainers": [
"Benjamin Lupton <b@lupton.cc> (https://github.com/balupton)"
],
"contributors": [
"Benjamin Lupton <b@lupton.cc> (https://github.com/balupton)"
],
"bugs": {
"url": "https://github.com/bevry/lazy-require/issues"
},
"repository" : {
"type": "git",
"url": "http://github.com/bevry/lazy-require.git"
},
"engines" : {
"node": ">=0.4"
},
"dependencies": {
"extract-opts": "~2.2.0",
"safeps": "~2.2.3"
},
"devDependencies": {
"rimraf": "~2.2.0",
"coffee-script": "~1.6.2",
"joe": "~1.2.0",
"joe-reporter-console": "~1.2.1",
"chai": "~1.7.0"
},
"directories": {
"lib": "./out/lib"
},
"scripts": {
"test": "node ./out/test/lazy-require-test.js"
},
"main": "./out/lib/lazy-require.js"
}
"title": "Lazy Require",
"name": "lazy-require",
"version": "2.0.0",
"description": "Lazy require allows you to require modules lazily, meaning that when you lazy require a module that is missing, lazy require will automatically install it for you.",
"homepage": "https://github.com/bevry/lazy-require",
"license": {
"type": "MIT"
},
"keywords": [
"require",
"lazy",
"optionalDependencies",
"optDependencies",
"dependencies",
"deps"
],
"badges": {
"travis": true,
"npm": true,
"npmdownloads": true,
"david": true,
"daviddev": true,
"gratipay": "bevry",
"flattr": "344188/balupton-on-Flattr",
"paypal": "QB8GQPZAH84N6",
"bitcoin": "https://coinbase.com/checkouts/9ef59f5479eec1d97d63382c9ebcb93a",
"wishlist": "http://amzn.com/w/2F8TXKSNAFG4V"
},
"author": "2013+ Bevry Pty Ltd <us@bevry.me> (http://bevry.me)",
"maintainers": [
"Benjamin Lupton <b@lupton.cc> (https://github.com/balupton)"
],
"contributors": [
"Benjamin Lupton <b@lupton.cc> (https://github.com/balupton)"
],
"bugs": {
"url": "https://github.com/bevry/lazy-require/issues"
},
"repository": {
"type": "git",
"url": "http://github.com/bevry/lazy-require.git"
},
"engines": {
"node": ">=0.4"
},
"dependencies": {
"extract-opts": "^3.0.1",
"safeps": "^3.0.1"
},
"devDependencies": {
"assert-helpers": "^1.0.2",
"coffee-script": "^1.9.1",
"joe": "^1.6.0",
"joe-reporter-console": "^1.2.1",
"projectz": "^0.5.0",
"rimraf": "^2.3.2"
},
"directories": {
"lib": "./out/lib"
},
"scripts": {
"test": "node ./out/test/lazy-require-test.js"
},
"main": "./out/lib/lazy-require.js",
"cakeConfiguration": {
"COFFEE_SRC_PATH": "src"
}
}

@@ -0,15 +1,41 @@

<!-- TITLE/ -->
# Lazy Require
[![Build Status](https://secure.travis-ci.org/bevry/lazy-require.png?branch=master)](http://travis-ci.org/bevry/lazy-require)
[![NPM version](https://badge.fury.io/js/lazy-require.png)](https://npmjs.org/package/lazy-require)
[![Flattr this project](https://raw.github.com/balupton/flattr-buttons/master/badge-89x18.gif)](http://flattr.com/thing/344188/balupton-on-Flattr)
<!-- /TITLE -->
Lazy require allows you to require modules lazily, meaning that when you lazy require a missing module, it is automatically installed. If the installation or require fails, the error is returned to the lazy require callback.
<!-- BADGES/ -->
[![Build Status](https://img.shields.io/travis/bevry/lazy-require/master.svg)](http://travis-ci.org/bevry/lazy-require "Check this project's build status on TravisCI")
[![NPM version](https://img.shields.io/npm/v/lazy-require.svg)](https://npmjs.org/package/lazy-require "View this project on NPM")
[![NPM downloads](https://img.shields.io/npm/dm/lazy-require.svg)](https://npmjs.org/package/lazy-require "View this project on NPM")
[![Dependency Status](https://img.shields.io/david/bevry/lazy-require.svg)](https://david-dm.org/bevry/lazy-require)
[![Dev Dependency Status](https://img.shields.io/david/dev/bevry/lazy-require.svg)](https://david-dm.org/bevry/lazy-require#info=devDependencies)<br/>
[![Gratipay donate button](https://img.shields.io/gratipay/bevry.svg)](https://www.gratipay.com/bevry/ "Donate weekly to this project using Gratipay")
[![Flattr donate button](https://img.shields.io/badge/flattr-donate-yellow.svg)](http://flattr.com/thing/344188/balupton-on-Flattr "Donate monthly to this project using Flattr")
[![PayPayl donate button](https://img.shields.io/badge/paypal-donate-yellow.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=QB8GQPZAH84N6 "Donate once-off to this project using Paypal")
[![BitCoin donate button](https://img.shields.io/badge/bitcoin-donate-yellow.svg)](https://coinbase.com/checkouts/9ef59f5479eec1d97d63382c9ebcb93a "Donate once-off to this project using BitCoin")
[![Wishlist browse button](https://img.shields.io/badge/wishlist-donate-yellow.svg)](http://amzn.com/w/2F8TXKSNAFG4V "Buy an item on our wishlist for us")
<!-- /BADGES -->
<!-- DESCRIPTION/ -->
Lazy require allows you to require modules lazily, meaning that when you lazy require a module that is missing, lazy require will automatically install it for you.
<!-- /DESCRIPTION -->
<!-- INSTALL/ -->
## Install
1. [Install Node.js](http://bevry.me/node/install)
2. `npm install --save lazy-require`
### [NPM](http://npmjs.org/)
- Use: `require('lazy-require')`
- Install: `npm install --save lazy-require`
<!-- /INSTALL -->

@@ -21,23 +47,112 @@

// Import
var lazyRequire = require('lazy-require').lazyRequire;
var lazyRequire = require('lazy-require')
// Load a module lazily
lazyRequire('iconv', function(err,iconv){
// Check
if (err) return console.log('iconv failed to load because of:', err.stack);
// Success
// iconv....
});
// Attempt to load the module `ambi`, if it doesn't exist, then try to install it and load it again
// Do this synchronously, only available in Node 0.12 and above
var ambi = lazyRequire('ambi', {/* options */})
if ( ambi instanceof Error ) {
// Error ....
console.log('ambi failed to load because of:', ambi.stack)
} else {
// Success ...
}
// Attempt to load the module `ambi`, if it doesn't exist, then try to install it and load it again
// Do this synchronously but with a callback, only available in Node 0.12 and above
lazyRequire.sync('ambi', {/* options */}, function(err, ambi){
// Error ...
if (err) return console.log('ambi failed to load because of:', err.stack)
// Success ...
})
// Attempt to load the module `ambi`, if it doesn't exist, then try to install it and load it again
// Do this asynchronously, available in all node versions
lazyRequire('ambi', {/* options */}, function(err, ambi){
// Error ...
if (err) return console.log('ambi failed to load because of:', err.stack)
// Success ...
})
// Attempt to load the module `ambi`, if it doesn't exist, then try to install it and load it again
// Do this synchronously if supported (Node 0.12 and above), otherwise do it asynchronously
lazyRequire.auto('ambi', {/* options */}, function(err, ambi){
// Error ...
if (err) return console.log('ambi failed to load because of:', err.stack)
// Success ...
})
```
The following options are supported:
- `cwd` the module path that you would like the requested package to be installed to if it doesn't exist, always recommended
- `save` if the requested package doesn't exist, would you like to do a `npm --save` for it to add it to your `cwd`'s `package.json` dependencies?
There's some other methods also available to you, for now, read the source to discover them.
<!-- HISTORY/ -->
## History
[You can discover the history inside the `History.md` file](https://github.com/bevry/lazy-require/blob/master/History.md#files)
[Discover the change history by heading on over to the `HISTORY.md` file.](https://github.com/bevry/lazy-require/blob/master/HISTORY.md#files)
<!-- /HISTORY -->
<!-- CONTRIBUTE/ -->
## Contribute
[Discover how you can contribute by heading on over to the `CONTRIBUTING.md` file.](https://github.com/bevry/lazy-require/blob/master/CONTRIBUTING.md#files)
<!-- /CONTRIBUTE -->
<!-- BACKERS/ -->
## Backers
### Maintainers
These amazing people are maintaining this project:
- Benjamin Lupton <b@lupton.cc> (https://github.com/balupton)
### Sponsors
No sponsors yet! Will you be the first?
[![Gratipay donate button](https://img.shields.io/gratipay/bevry.svg)](https://www.gratipay.com/bevry/ "Donate weekly to this project using Gratipay")
[![Flattr donate button](https://img.shields.io/badge/flattr-donate-yellow.svg)](http://flattr.com/thing/344188/balupton-on-Flattr "Donate monthly to this project using Flattr")
[![PayPayl donate button](https://img.shields.io/badge/paypal-donate-yellow.svg)](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=QB8GQPZAH84N6 "Donate once-off to this project using Paypal")
[![BitCoin donate button](https://img.shields.io/badge/bitcoin-donate-yellow.svg)](https://coinbase.com/checkouts/9ef59f5479eec1d97d63382c9ebcb93a "Donate once-off to this project using BitCoin")
[![Wishlist browse button](https://img.shields.io/badge/wishlist-donate-yellow.svg)](http://amzn.com/w/2F8TXKSNAFG4V "Buy an item on our wishlist for us")
### Contributors
These amazing people have contributed code to this project:
- [Benjamin Lupton](https://github.com/balupton) <b@lupton.cc> — [view contributions](https://github.com/bevry/lazy-require/commits?author=balupton)
[Become a contributor!](https://github.com/bevry/lazy-require/blob/master/CONTRIBUTING.md#files)
<!-- /BACKERS -->
<!-- LICENSE/ -->
## License
Licensed under the incredibly [permissive](http://en.wikipedia.org/wiki/Permissive_free_software_licence) [MIT License](http://creativecommons.org/licenses/MIT/)
<br/>Copyright © 2013+ [Bevry Pty Ltd](http://bevry.me)
Unless stated otherwise all works are:
- Copyright &copy; 2013+ Bevry Pty Ltd <us@bevry.me> (http://bevry.me)
and licensed under:
- The incredibly [permissive](http://en.wikipedia.org/wiki/Permissive_free_software_licence) [MIT License](http://opensource.org/licenses/mit-license.php)
<!-- /LICENSE -->

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc