├── .gitignore ├── LICENSE ├── README.md ├── bin └── autoinstall ├── index.js ├── package.json └── test ├── koa.js ├── relative.js └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store* 2 | *.log 3 | node_modules 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2014 Jonathan Ong me@jongleberry.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Autoinstall 3 | 4 | Automatically install dependencies in node.js. 5 | So you don't have to write them in your `package.json`, 6 | so you don't have to use [dependency-check](http://github.com/maxogden/dependency-check), 7 | or so do you don't have to do `npm install`. 8 | 9 | ## Development Status 10 | 11 | This is very hacky and uses node's undocumented internals. 12 | Thus, don't expect it to ever reach `1.0.0`. 13 | Also, this all should ideally be included with node 14 | once ES6 modules are supported. 15 | But who knows when that's going to happen. 16 | 17 | This uses [`child_process.execFileSync()`](http://nodejs.org/docs/v0.11.13/api/child_process.html#child_process_child_process_execfilesync_command_args_options), 18 | so is only supported in node `>= v0.11.13`ish. 19 | 20 | ## CLI Usage 21 | 22 | You can install `autoinstall` globally: 23 | 24 | ```bash 25 | npm i -g autoinstall 26 | ``` 27 | 28 | Then use `autoinstall(1)` like `node(1)`: 29 | 30 | ```bash 31 | autoinstall index.js 32 | ``` 33 | 34 | `autoinstall(1)` is also aliased as `a(1)`: 35 | 36 | ```bash 37 | a index.js 38 | ``` 39 | 40 | ## API Usage 41 | 42 | ```bash 43 | npm i autoinstall 44 | ``` 45 | 46 | Autoinstall packages node.js globally. 47 | Include it first before any other `require()` calls. 48 | 49 | ```js 50 | require('autoinstall') 51 | 52 | var express = require('express') 53 | ``` 54 | 55 | And you're all good! 56 | -------------------------------------------------------------------------------- /bin/autoinstall: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | 3 | var autoinstall = require.resolve('..') 4 | 5 | var args = process.argv.slice(2) 6 | 7 | var options = [] 8 | args = args.filter(function (arg) { 9 | if (arg[0] === '-') { 10 | options.push(arg) 11 | return false 12 | } 13 | return true 14 | }) 15 | 16 | if (args.length > 1) { 17 | throw new Error('more than 1 argument supplied') 18 | } 19 | 20 | var path = require('path') 21 | var file = path.resolve(args[0]) 22 | 23 | options.push('-e', 24 | "require('" + autoinstall + "');" + 25 | "require('" + file + "');" 26 | ) 27 | 28 | var spawn = require('child_process').spawn 29 | var proc = spawn('node', options, { 30 | stdio: [0, 1, 2] 31 | }) 32 | 33 | proc.on('exit', function (code) { 34 | process.exit(code) 35 | }) 36 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 2 | var Module = require('module') 3 | var debug = require('debug')('autoinstall') 4 | var execFile = require('child_process').execFileSync 5 | 6 | if (!execFile) { 7 | console.error() 8 | console.error('You can not use `autoinstall` with this version of node!') 9 | console.error('You must use node v0.11.13+, which has `child_process.execFileSync()`!') 10 | console.error() 11 | process.exit(1) 12 | } 13 | 14 | var resolve = Module._resolveFilename 15 | Module._resolveFilename = function (path) { 16 | try { 17 | return resolve.apply(Module, arguments) 18 | } catch (err) { 19 | if (err.code !== 'MODULE_NOT_FOUND') throw err 20 | } 21 | 22 | var name = path.split('/')[0] 23 | debug('downloading package: %s', name) 24 | execFile('npm', ['install', name]) 25 | 26 | return resolve.apply(Module, arguments) 27 | } 28 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "autoinstall", 3 | "description": "automatically install dependencies via `require()`", 4 | "version": "0.3.1", 5 | "author": { 6 | "name": "Jonathan Ong", 7 | "email": "me@jongleberry.com", 8 | "url": "http://jongleberry.com", 9 | "twitter": "https://twitter.com/jongleberry" 10 | }, 11 | "license": "MIT", 12 | "repository": "normalize/autoinstall", 13 | "dependencies": { 14 | "debug": "2.6.8" 15 | }, 16 | "scripts": { 17 | "test": "DEBUG=autoinstall node test/test.js" 18 | }, 19 | "engines": { 20 | "node": ">= 0.11.13" 21 | }, 22 | "bin": { 23 | "autoinstall": "bin/autoinstall", 24 | "a": "bin/autoinstall" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /test/koa.js: -------------------------------------------------------------------------------- 1 | 2 | require('koa') 3 | console.log('koa passed!') 4 | -------------------------------------------------------------------------------- /test/relative.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/normalize/autoinstall/8d477dca4fddb9bbdfffe70a6665f544abc742a3/test/relative.js -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | 2 | var execFile = require('child_process').execFileSync 3 | var assert = require('assert') 4 | 5 | // delete all the unnecessary dependencies 6 | execFile('npm', ['prune']) 7 | 8 | // install the hook 9 | require('..') 10 | 11 | // test relative paths 12 | assert(require('./relative')) 13 | 14 | // require a bunch of shit 15 | assert(require('debug')) 16 | assert(require('mocha')) 17 | assert(require('should')) 18 | assert('function', typeof require('type-is')) 19 | 20 | console.log('tests pass!!!') 21 | --------------------------------------------------------------------------------