├── .gitignore ├── LICENSE ├── README.md ├── index.js ├── package-lock.json └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | sandbox.js 3 | sandbox/* 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2020 Mathias Buus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # npm-auto 2 | 3 | Auto installs npm dependencies from the script you want to run and runs the script 4 | 5 | ``` 6 | npm install npm-auto 7 | ``` 8 | 9 | ## Usage 10 | 11 | First install npm-auto 12 | 13 | ``` sh 14 | npm install -g npm-auto 15 | ``` 16 | 17 | Then make a script that uses some dependencies 18 | 19 | ``` js 20 | const hypercore = require('hypercore') 21 | console.log('hypercore is', hypercore) 22 | ``` 23 | 24 | Then simply run the script with `npm-auto`. 25 | It will `npm install` any deps used by the script if they are not resolvable already. 26 | 27 | ``` sh 28 | npm-auto my-script.js 29 | ``` 30 | 31 | No more manual npm installs for gists! 32 | 33 | ## License 34 | 35 | MIT 36 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env node 2 | const detective = require('detective') 3 | const fs = require('fs') 4 | const path = require('path') 5 | const m = require('module') 6 | const { spawnSync } = require('child_process') 7 | 8 | const filename = process.argv[2] 9 | const { resolve } = m.createRequire(path.resolve(filename)) 10 | 11 | if (!filename) { 12 | console.error('Usage: npm-auto ...args') 13 | process.exit(1) 14 | } 15 | 16 | const deps = detective(fs.readFileSync(filename)) 17 | .filter(d => m.builtinModules.indexOf(d) === -1) 18 | .filter(unresolved) 19 | .map(onlyModule) 20 | .filter(dups) 21 | 22 | if (deps.length) { 23 | fs.mkdirSync('node_modules', { recursive: true }) 24 | spawnSync('npm', ['install', '--no-save'].concat(deps), { stdio: 'inherit' }) 25 | } 26 | spawnSync(process.execPath, process.argv.slice(2), { stdio: 'inherit' }) 27 | 28 | function unresolved (d) { 29 | try { 30 | resolve(d) 31 | return false 32 | } catch (_) { 33 | return true 34 | } 35 | } 36 | 37 | function onlyModule (n) { 38 | if (n[0] === '@') return n.split('/').slice(0, 2).join('/') 39 | return n.split('/')[0] 40 | } 41 | 42 | function dups (n, i, all) { 43 | return all.indexOf(n) === i 44 | } 45 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "npm-auto", 3 | "version": "1.0.2", 4 | "lockfileVersion": 3, 5 | "requires": true, 6 | "packages": { 7 | "": { 8 | "name": "npm-auto", 9 | "version": "1.0.2", 10 | "license": "MIT", 11 | "dependencies": { 12 | "detective": "^5.2.0" 13 | }, 14 | "bin": { 15 | "npm-auto": "index.js" 16 | }, 17 | "devDependencies": {} 18 | }, 19 | "node_modules/acorn": { 20 | "version": "7.4.1", 21 | "resolved": "https://registry.npmjs.org/acorn/-/acorn-7.4.1.tgz", 22 | "integrity": "sha512-nQyp0o1/mNdbTO1PO6kHkwSrmgZ0MT/jCCpNiwbUjGoRN4dlBhqJtoQuCnEOKzgTVwg0ZWiCoQy6SxMebQVh8A==", 23 | "bin": { 24 | "acorn": "bin/acorn" 25 | }, 26 | "engines": { 27 | "node": ">=0.4.0" 28 | } 29 | }, 30 | "node_modules/acorn-node": { 31 | "version": "1.8.2", 32 | "resolved": "https://registry.npmjs.org/acorn-node/-/acorn-node-1.8.2.tgz", 33 | "integrity": "sha512-8mt+fslDufLYntIoPAaIMUe/lrbrehIiwmR3t2k9LljIzoigEPF27eLk2hy8zSGzmR/ogr7zbRKINMo1u0yh5A==", 34 | "dependencies": { 35 | "acorn": "^7.0.0", 36 | "acorn-walk": "^7.0.0", 37 | "xtend": "^4.0.2" 38 | } 39 | }, 40 | "node_modules/acorn-walk": { 41 | "version": "7.2.0", 42 | "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-7.2.0.tgz", 43 | "integrity": "sha512-OPdCF6GsMIP+Az+aWfAAOEt2/+iVDKE7oy6lJ098aoe59oAmK76qV6Gw60SbZ8jHuG2wH058GF4pLFbYamYrVA==", 44 | "engines": { 45 | "node": ">=0.4.0" 46 | } 47 | }, 48 | "node_modules/defined": { 49 | "version": "1.0.1", 50 | "resolved": "https://registry.npmjs.org/defined/-/defined-1.0.1.tgz", 51 | "integrity": "sha512-hsBd2qSVCRE+5PmNdHt1uzyrFu5d3RwmFDKzyNZMFq/EwDNJF7Ee5+D5oEKF0hU6LhtoUF1macFvOe4AskQC1Q==", 52 | "funding": { 53 | "url": "https://github.com/sponsors/ljharb" 54 | } 55 | }, 56 | "node_modules/detective": { 57 | "version": "5.2.1", 58 | "resolved": "https://registry.npmjs.org/detective/-/detective-5.2.1.tgz", 59 | "integrity": "sha512-v9XE1zRnz1wRtgurGu0Bs8uHKFSTdteYZNbIPFVhUZ39L/S79ppMpdmVOZAnoz1jfEFodc48n6MX483Xo3t1yw==", 60 | "dependencies": { 61 | "acorn-node": "^1.8.2", 62 | "defined": "^1.0.0", 63 | "minimist": "^1.2.6" 64 | }, 65 | "bin": { 66 | "detective": "bin/detective.js" 67 | }, 68 | "engines": { 69 | "node": ">=0.8.0" 70 | } 71 | }, 72 | "node_modules/minimist": { 73 | "version": "1.2.8", 74 | "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", 75 | "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", 76 | "funding": { 77 | "url": "https://github.com/sponsors/ljharb" 78 | } 79 | }, 80 | "node_modules/xtend": { 81 | "version": "4.0.2", 82 | "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", 83 | "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==", 84 | "engines": { 85 | "node": ">=0.4" 86 | } 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "npm-auto", 3 | "version": "1.0.2", 4 | "description": "Auto installs npm dependencies from the script you want to run and runs the script", 5 | "main": "index.js", 6 | "bin": { 7 | "npm-auto": "./index.js" 8 | }, 9 | "dependencies": { 10 | "detective": "^5.2.0" 11 | }, 12 | "devDependencies": {}, 13 | "repository": { 14 | "type": "git", 15 | "url": "https://github.com/mafintosh/npm-auto.git" 16 | }, 17 | "author": "Mathias Buus (@mafintosh)", 18 | "license": "MIT", 19 | "bugs": { 20 | "url": "https://github.com/mafintosh/npm-auto/issues" 21 | }, 22 | "homepage": "https://github.com/mafintosh/npm-auto" 23 | } 24 | --------------------------------------------------------------------------------