├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── example.js ├── fixtures ├── package.json └── tmpl.json ├── index.js ├── package.json └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | end_of_line = lf 7 | charset = utf-8 8 | indent_size = 2 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | insert_final_newline = false 15 | 16 | [**/{actual,fixtures,expected}/**] 17 | trim_trailing_whitespace = false 18 | insert_final_newline = false 19 | 20 | [**/templates/**] 21 | trim_trailing_whitespace = false 22 | insert_final_newline = false 23 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "ecmaFeatures": { 3 | "modules": true, 4 | "experimentalObjectRestSpread": true 5 | }, 6 | 7 | "env": { 8 | "browser": false, 9 | "es6": true, 10 | "node": true, 11 | "mocha": true 12 | }, 13 | 14 | "globals": { 15 | "document": false, 16 | "navigator": false, 17 | "window": false 18 | }, 19 | 20 | "rules": { 21 | "accessor-pairs": 2, 22 | "arrow-spacing": [2, { "before": true, "after": true }], 23 | "block-spacing": [2, "always"], 24 | "brace-style": [2, "1tbs", { "allowSingleLine": true }], 25 | "comma-dangle": [2, "never"], 26 | "comma-spacing": [2, { "before": false, "after": true }], 27 | "comma-style": [2, "last"], 28 | "constructor-super": 2, 29 | "curly": [2, "multi-line"], 30 | "dot-location": [2, "property"], 31 | "eol-last": 2, 32 | "eqeqeq": [2, "allow-null"], 33 | "generator-star-spacing": [2, { "before": true, "after": true }], 34 | "handle-callback-err": [2, "^(err|error)$" ], 35 | "indent": [2, 2, { "SwitchCase": 1 }], 36 | "key-spacing": [2, { "beforeColon": false, "afterColon": true }], 37 | "keyword-spacing": [2, { "before": true, "after": true }], 38 | "new-cap": [2, { "newIsCap": true, "capIsNew": false }], 39 | "new-parens": 2, 40 | "no-array-constructor": 2, 41 | "no-caller": 2, 42 | "no-class-assign": 2, 43 | "no-cond-assign": 2, 44 | "no-const-assign": 2, 45 | "no-control-regex": 2, 46 | "no-debugger": 2, 47 | "no-delete-var": 2, 48 | "no-dupe-args": 2, 49 | "no-dupe-class-members": 2, 50 | "no-dupe-keys": 2, 51 | "no-duplicate-case": 2, 52 | "no-empty-character-class": 2, 53 | "no-eval": 2, 54 | "no-ex-assign": 2, 55 | "no-extend-native": 2, 56 | "no-extra-bind": 2, 57 | "no-extra-boolean-cast": 2, 58 | "no-extra-parens": [2, "functions"], 59 | "no-fallthrough": 2, 60 | "no-floating-decimal": 2, 61 | "no-func-assign": 2, 62 | "no-implied-eval": 2, 63 | "no-inner-declarations": [2, "functions"], 64 | "no-invalid-regexp": 2, 65 | "no-irregular-whitespace": 2, 66 | "no-iterator": 2, 67 | "no-label-var": 2, 68 | "no-labels": 2, 69 | "no-lone-blocks": 2, 70 | "no-mixed-spaces-and-tabs": 2, 71 | "no-multi-spaces": 2, 72 | "no-multi-str": 2, 73 | "no-multiple-empty-lines": [2, { "max": 1 }], 74 | "no-native-reassign": 0, 75 | "no-negated-in-lhs": 2, 76 | "no-new": 2, 77 | "no-new-func": 2, 78 | "no-new-object": 2, 79 | "no-new-require": 2, 80 | "no-new-wrappers": 2, 81 | "no-obj-calls": 2, 82 | "no-octal": 2, 83 | "no-octal-escape": 2, 84 | "no-proto": 0, 85 | "no-redeclare": 2, 86 | "no-regex-spaces": 2, 87 | "no-return-assign": 2, 88 | "no-self-compare": 2, 89 | "no-sequences": 2, 90 | "no-shadow-restricted-names": 2, 91 | "no-spaced-func": 2, 92 | "no-sparse-arrays": 2, 93 | "no-this-before-super": 2, 94 | "no-throw-literal": 2, 95 | "no-trailing-spaces": 0, 96 | "no-undef": 2, 97 | "no-undef-init": 2, 98 | "no-unexpected-multiline": 2, 99 | "no-unneeded-ternary": [2, { "defaultAssignment": false }], 100 | "no-unreachable": 2, 101 | "no-unused-vars": [2, { "vars": "all", "args": "none" }], 102 | "no-useless-call": 0, 103 | "no-with": 2, 104 | "one-var": [0, { "initialized": "never" }], 105 | "operator-linebreak": [0, "after", { "overrides": { "?": "before", ":": "before" } }], 106 | "padded-blocks": [0, "never"], 107 | "quotes": [2, "single", "avoid-escape"], 108 | "radix": 2, 109 | "semi": [2, "always"], 110 | "semi-spacing": [2, { "before": false, "after": true }], 111 | "space-before-blocks": [2, "always"], 112 | "space-before-function-paren": [2, "never"], 113 | "space-in-parens": [2, "never"], 114 | "space-infix-ops": 2, 115 | "space-unary-ops": [2, { "words": true, "nonwords": false }], 116 | "spaced-comment": [0, "always", { "markers": ["global", "globals", "eslint", "eslint-disable", "*package", "!", ","] }], 117 | "use-isnan": 2, 118 | "valid-typeof": 2, 119 | "wrap-iife": [2, "any"], 120 | "yoda": [2, "never"] 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # always ignore files 2 | *.DS_Store 3 | *.sublime-* 4 | 5 | # test related, or directories generated by tests 6 | test/actual 7 | actual 8 | coverage 9 | 10 | # npm 11 | node_modules 12 | npm-debug.log 13 | 14 | # misc 15 | _gh_pages 16 | benchmark 17 | bower_components 18 | vendor 19 | temp 20 | tmp 21 | TODO.md 22 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '5' 5 | - '4' 6 | - '0.12' 7 | - '0.10' 8 | matrix: 9 | fast_finish: true 10 | allow_failures: 11 | - node_js: '4' 12 | - node_js: '0.10' 13 | - node_js: '0.12' 14 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | ## Usage 2 | 3 | Note that if you use [base][] directly you will also need to let the plugin know that it is being registered on a Base "application" (since `Base` can be used to create anything, like `views`, `collections` etc.). 4 | 5 | ```js 6 | var npm = require('{%= name %}'); 7 | var Base = require('base'); 8 | var app = new Base({isApp: true}); // <= 9 | app.use(npm()); 10 | 11 | // install npm packages `micromatch` and `is-absolute` to devDependencies 12 | app.npm.devDependencies(['micromatch', 'is-absolute'], function(err) { 13 | if (err) throw err; 14 | }); 15 | ``` 16 | 17 | ## API 18 | {%= apidocs("index.js") %} 19 | 20 | ## History 21 | 22 | **v0.4.1** 23 | - fixes [issue #2](https://github.com/node-base/base-npm/issues/2) to use the `app.cwd` when available to ensure npm modules are installed to the correct folder 24 | 25 | **v0.4.0** 26 | - adds `global` method for installing with the `--global` flag 27 | - adds `exists` method for checking if a package exists on `npm` 28 | - removes [base-questions][] 29 | - removes `askInstall` method (moved to [base-npm-prompt][]) 30 | 31 | **v0.3.0** 32 | 33 | - improved instance checks 34 | - adds [base-questions][] 35 | - adds `dependencies` method 36 | - adds `devDependencies` method 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016, Jon Schlinkert. 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 | # base-npm [![NPM version](https://img.shields.io/npm/v/base-npm.svg?style=flat)](https://www.npmjs.com/package/base-npm) [![NPM downloads](https://img.shields.io/npm/dm/base-npm.svg?style=flat)](https://npmjs.org/package/base-npm) [![Build Status](https://img.shields.io/travis/node-base/base-npm.svg?style=flat)](https://travis-ci.org/node-base/base-npm) 2 | 3 | Base plugin that adds methods for programmatically running npm commands. 4 | 5 | You might also be interested in [base-bower](https://github.com/jonschlinkert/base-bower). 6 | 7 | ## Install 8 | 9 | Install with [npm](https://www.npmjs.com/): 10 | 11 | ```sh 12 | $ npm install --save base-npm 13 | ``` 14 | 15 | ## Usage 16 | 17 | Note that if you use [base](https://github.com/node-base/base) directly you will also need to let the plugin know that it is being registered on a Base "application" (since `Base` can be used to create anything, like `views`, `collections` etc.). 18 | 19 | ```js 20 | var npm = require('base-npm'); 21 | var Base = require('base'); 22 | var app = new Base({isApp: true}); // <= 23 | app.use(npm()); 24 | 25 | // install npm packages `micromatch` and `is-absolute` to devDependencies 26 | app.npm.devDependencies(['micromatch', 'is-absolute'], function(err) { 27 | if (err) throw err; 28 | }); 29 | ``` 30 | 31 | ## API 32 | 33 | ### [.npm](index.js#L37) 34 | 35 | Execute `npm install` with the given `args`, package `names` and callback. 36 | 37 | **Params** 38 | 39 | * `args` **{String|Array}** 40 | * `names` **{String|Array}** 41 | * `cb` **{Function}**: Callback 42 | 43 | **Example** 44 | 45 | ```js 46 | app.npm('--save', ['isobject'], function(err) { 47 | if (err) throw err; 48 | }); 49 | ``` 50 | 51 | ### [.npm.install](index.js#L63) 52 | 53 | Install one or more packages. Does not save anything to package.json. Equivalent of `npm install foo`. 54 | 55 | **Params** 56 | 57 | * `names` **{String|Array}**: package names 58 | * `cb` **{Function}**: Callback 59 | 60 | **Example** 61 | 62 | ```js 63 | app.npm.install('isobject', function(err) { 64 | if (err) throw err; 65 | }); 66 | ``` 67 | 68 | ### [.npm.latest](index.js#L81) 69 | 70 | (Re-)install and save the latest version of all `dependencies` and `devDependencies` currently listed in package.json. 71 | 72 | **Params** 73 | 74 | * `cb` **{Function}**: Callback 75 | 76 | **Example** 77 | 78 | ```js 79 | app.npm.latest(function(err) { 80 | if (err) throw err; 81 | }); 82 | ``` 83 | 84 | ### [.npm.dependencies](index.js#L115) 85 | 86 | Execute `npm install --save` with one or more package `names`. Updates `dependencies` in package.json. 87 | 88 | **Params** 89 | 90 | * `names` **{String|Array}** 91 | * `cb` **{Function}**: Callback 92 | 93 | **Example** 94 | 95 | ```js 96 | app.npm.dependencies('micromatch', function(err) { 97 | if (err) throw err; 98 | }); 99 | ``` 100 | 101 | ### [.npm.devDependencies](index.js#L145) 102 | 103 | Execute `npm install --save-dev` with one or more package `names`. Updates `devDependencies` in package.json. 104 | 105 | **Params** 106 | 107 | * `names` **{String|Array}** 108 | * `cb` **{Function}**: Callback 109 | 110 | **Example** 111 | 112 | ```js 113 | app.npm.devDependencies('isobject', function(err) { 114 | if (err) throw err; 115 | }); 116 | ``` 117 | 118 | ### [.npm.global](index.js#L174) 119 | 120 | Execute `npm install --global` with one or more package `names`. 121 | 122 | **Params** 123 | 124 | * `names` **{String|Array}** 125 | * `cb` **{Function}**: Callback 126 | 127 | **Example** 128 | 129 | ```js 130 | app.npm.global('mocha', function(err) { 131 | if (err) throw err; 132 | }); 133 | ``` 134 | 135 | ### [.npm.exists](index.js#L201) 136 | 137 | Check if one or more names exist on npm. 138 | 139 | **Params** 140 | 141 | * `names` **{String|Array}** 142 | * `cb` **{Function}**: Callback 143 | * `returns` **{Object}**: Object of results where the `key` is the name and the value is `true` or `false`. 144 | 145 | **Example** 146 | 147 | ```js 148 | app.npm.exists('isobject', function(err, results) { 149 | if (err) throw err; 150 | console.log(results.isobject); 151 | }); 152 | //=> true 153 | ``` 154 | 155 | ## History 156 | 157 | **v0.4.1** 158 | 159 | * fixes [issue #2](https://github.com/node-base/base-npm/issues/2) to use the `app.cwd` when available to ensure npm modules are installed to the correct folder 160 | 161 | **v0.4.0** 162 | 163 | * adds `global` method for installing with the `--global` flag 164 | * adds `exists` method for checking if a package exists on `npm` 165 | * removes [base-questions](https://github.com/node-base/base-questions) 166 | * removes `askInstall` method (moved to [base-npm-prompt](https://github.com/node-base/base-npm-prompt)) 167 | 168 | **v0.3.0** 169 | 170 | * improved instance checks 171 | * adds [base-questions](https://github.com/node-base/base-questions) 172 | * adds `dependencies` method 173 | * adds `devDependencies` method 174 | 175 | ## About 176 | 177 | ### Related projects 178 | 179 | * [base-questions](https://www.npmjs.com/package/base-questions): Plugin for base-methods that adds methods for prompting the user and storing the answers on… [more](https://github.com/node-base/base-questions) | [homepage](https://github.com/node-base/base-questions "Plugin for base-methods that adds methods for prompting the user and storing the answers on a project-by-project basis.") 180 | * [base-task](https://www.npmjs.com/package/base-task): base plugin that provides a very thin wrapper around [https://github.com/doowb/composer](https://github.com/doowb/composer) for adding task methods to… [more](https://github.com/node-base/base-task) | [homepage](https://github.com/node-base/base-task "base plugin that provides a very thin wrapper around for adding task methods to your application.") 181 | * [base](https://www.npmjs.com/package/base): base is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting… [more](https://github.com/node-base/base) | [homepage](https://github.com/node-base/base "base is the foundation for creating modular, unit testable and highly pluggable node.js applications, starting with a handful of common methods, like `set`, `get`, `del` and `use`.") 182 | 183 | ### Contributing 184 | 185 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). 186 | 187 | ### Building docs 188 | 189 | _(This document was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme) (a [verb](https://github.com/verbose/verb) generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_ 190 | 191 | To generate the readme and API documentation with [verb](https://github.com/verbose/verb): 192 | 193 | ```sh 194 | $ npm install -g verb verb-generate-readme && verb 195 | ``` 196 | 197 | ### Running tests 198 | 199 | Install dev dependencies: 200 | 201 | ```sh 202 | $ npm install -d && npm test 203 | ``` 204 | 205 | ### Author 206 | 207 | **Jon Schlinkert** 208 | 209 | * [github/jonschlinkert](https://github.com/jonschlinkert) 210 | * [twitter/jonschlinkert](http://twitter.com/jonschlinkert) 211 | 212 | ### License 213 | 214 | Copyright © 2016, [Jon Schlinkert](https://github.com/jonschlinkert). 215 | Released under the [MIT license](https://github.com/node-base/base-npm/blob/master/LICENSE). 216 | 217 | *** 218 | 219 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.1.30, on September 11, 2016._ -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var npm = require('./'); 4 | var Base = require('base'); 5 | var app = new Base(); 6 | app.use(npm()); 7 | 8 | // app.npm.dependencies(function(err) { 9 | // if (err) throw err; 10 | // }); 11 | 12 | // app.npm.devDependencies('isobject', function(err) { 13 | // if (err) throw err; 14 | // }); 15 | 16 | app.npm.latest(function(err) { 17 | if (err) throw err; 18 | }); 19 | -------------------------------------------------------------------------------- /fixtures/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fixtures", 3 | "private": true, 4 | "version": "0.0.0", 5 | "description": "", 6 | "main": "index.js", 7 | "license": "MIT" 8 | } -------------------------------------------------------------------------------- /fixtures/tmpl.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fixtures", 3 | "private": true, 4 | "version": "0.0.0", 5 | "description": "", 6 | "main": "index.js", 7 | "license": "MIT" 8 | } 9 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * base-npm (https://github.com/jonschlinkert/base-npm) 3 | * 4 | * Copyright (c) 2016, Jon Schlinkert. 5 | * Licensed under the MIT License. 6 | */ 7 | 8 | 'use strict'; 9 | 10 | var path = require('path'); 11 | var isValid = require('is-valid-app'); 12 | var extend = require('extend-shallow'); 13 | var spawn = require('cross-spawn'); 14 | var request = require('min-request'); 15 | var reduce = require('async-array-reduce'); 16 | 17 | module.exports = function(options) { 18 | return function(app) { 19 | if (!isValid(app, 'base-npm')) return; 20 | 21 | /** 22 | * Execute `npm install` with the given `args`, package `names` 23 | * and callback. 24 | * 25 | * ```js 26 | * app.npm('--save', ['isobject'], function(err) { 27 | * if (err) throw err; 28 | * }); 29 | * ``` 30 | * @name .npm 31 | * @param {String|Array} `args` 32 | * @param {String|Array} `names` 33 | * @param {Function} `cb` Callback 34 | * @api public 35 | */ 36 | 37 | this.define('npm', npm); 38 | function npm(cmds, args, cb) { 39 | var cwd = app.cwd || process.cwd(); 40 | args = arrayify(cmds).concat(arrayify(args)); 41 | spawn('npm', args, {cwd: cwd, stdio: 'inherit'}) 42 | .on('error', cb) 43 | .on('close', function(code, err) { 44 | cb(err, code); 45 | }); 46 | }; 47 | 48 | /** 49 | * Install one or more packages. Does not save anything to 50 | * package.json. Equivalent of `npm install foo`. 51 | * 52 | * ```js 53 | * app.npm.install('isobject', function(err) { 54 | * if (err) throw err; 55 | * }); 56 | * ``` 57 | * @name .npm.install 58 | * @param {String|Array} `names` package names 59 | * @param {Function} `cb` Callback 60 | * @api public 61 | */ 62 | 63 | npm.install = function(args, cb) { 64 | npm('install', args, cb); 65 | }; 66 | 67 | /** 68 | * (Re-)install and save the latest version of all `dependencies` 69 | * and `devDependencies` currently listed in package.json. 70 | * 71 | * ```js 72 | * app.npm.latest(function(err) { 73 | * if (err) throw err; 74 | * }); 75 | * ``` 76 | * @name .npm.latest 77 | * @param {Function} `cb` Callback 78 | * @api public 79 | */ 80 | 81 | npm.latest = function(names, cb) { 82 | if (typeof names !== 'function') { 83 | npm.install(latest(names), cb); 84 | return; 85 | } 86 | 87 | if (typeof names === 'function') { 88 | cb = names; 89 | } 90 | 91 | var devDeps = latest(pkg(app, 'devDependencies')); 92 | var deps = latest(pkg(app, 'dependencies')); 93 | 94 | npm.dependencies(deps, function(err) { 95 | if (err) return cb(err); 96 | npm.devDependencies(devDeps, cb); 97 | }); 98 | }; 99 | 100 | /** 101 | * Execute `npm install --save` with one or more package `names`. 102 | * Updates `dependencies` in package.json. 103 | * 104 | * ```js 105 | * app.npm.dependencies('micromatch', function(err) { 106 | * if (err) throw err; 107 | * }); 108 | * ``` 109 | * @name .npm.dependencies 110 | * @param {String|Array} `names` 111 | * @param {Function} `cb` Callback 112 | * @api public 113 | */ 114 | 115 | npm.dependencies = function(names, cb) { 116 | var args = [].concat.apply([], [].slice.call(arguments)); 117 | cb = args.pop(); 118 | 119 | if (args.length === 0) { 120 | args = pkg(app, 'dependencies'); 121 | } 122 | 123 | if (!args.length) { 124 | cb(); 125 | return; 126 | } 127 | npm.install(['--save'].concat(args), cb); 128 | }; 129 | 130 | /** 131 | * Execute `npm install --save-dev` with one or more package `names`. 132 | * Updates `devDependencies` in package.json. 133 | * 134 | * ```js 135 | * app.npm.devDependencies('isobject', function(err) { 136 | * if (err) throw err; 137 | * }); 138 | * ``` 139 | * @name .npm.devDependencies 140 | * @param {String|Array} `names` 141 | * @param {Function} `cb` Callback 142 | * @api public 143 | */ 144 | 145 | npm.devDependencies = function(names, cb) { 146 | var args = [].concat.apply([], [].slice.call(arguments)); 147 | cb = args.pop(); 148 | 149 | if (args.length === 0) { 150 | args = pkg(app, 'devDependencies'); 151 | } 152 | 153 | if (!args.length) { 154 | cb(); 155 | return; 156 | } 157 | npm.install(['--save-dev'].concat(args), cb); 158 | }; 159 | 160 | /** 161 | * Execute `npm install --global` with one or more package `names`. 162 | * 163 | * ```js 164 | * app.npm.global('mocha', function(err) { 165 | * if (err) throw err; 166 | * }); 167 | * ``` 168 | * @name .npm.global 169 | * @param {String|Array} `names` 170 | * @param {Function} `cb` Callback 171 | * @api public 172 | */ 173 | npm.global = function(names, cb) { 174 | var args = [].concat.apply([], [].slice.call(arguments)); 175 | cb = args.pop(); 176 | 177 | if (!args.length) { 178 | cb(); 179 | return; 180 | } 181 | npm.install(['--global'].concat(args), cb); 182 | }; 183 | 184 | /** 185 | * Check if one or more names exist on npm. 186 | * 187 | * ```js 188 | * app.npm.exists('isobject', function(err, results) { 189 | * if (err) throw err; 190 | * console.log(results.isobject); 191 | * }); 192 | * //=> true 193 | * ``` 194 | * @name .npm.exists 195 | * @param {String|Array} `names` 196 | * @param {Function} `cb` Callback 197 | * @returns {Object} Object of results where the `key` is the name and the value is `true` or `false`. 198 | * @api public 199 | */ 200 | 201 | npm.exists = function(names, cb) { 202 | var names = [].concat.apply([], [].slice.call(arguments)); 203 | cb = names.pop(); 204 | 205 | reduce(names, {}, function(acc, name, next) { 206 | checkName(name, function(err, exists) { 207 | if (err) return next(err); 208 | acc[name] = exists; 209 | next(null, acc); 210 | }); 211 | }, cb); 212 | }; 213 | 214 | /** 215 | * Aliases 216 | */ 217 | 218 | npm.saveDev = npm.devDependencies; 219 | npm.save = npm.dependencies; 220 | }; 221 | }; 222 | 223 | /** 224 | * Get the package.json for the current project 225 | */ 226 | 227 | function pkg(app, prop) { 228 | return Object.keys(pkgData(app)[prop] || {}); 229 | } 230 | 231 | function pkgPath(app) { 232 | return path.resolve(app.cwd || process.cwd(), 'package.json'); 233 | } 234 | 235 | function pkgData(app) { 236 | return app.pkg ? app.pkg.data : require(pkgPath(app)); 237 | } 238 | 239 | /** 240 | * Prefix package names with `@latest` 241 | */ 242 | 243 | function latest(keys) { 244 | if (typeof keys === 'string') { 245 | keys = [keys]; 246 | } 247 | if (!Array.isArray(keys)) { 248 | return []; 249 | } 250 | return keys.map(function(key) { 251 | return key.charAt(0) !== '-' ? (key + '@latest') : key; 252 | }); 253 | } 254 | 255 | /** 256 | * Cast `val` to an array 257 | */ 258 | 259 | function arrayify(val) { 260 | return val ? (Array.isArray(val) ? val : [val]) : []; 261 | } 262 | 263 | /** 264 | * Check if a name exists on npm. 265 | * 266 | * ```js 267 | * checkName('foo', function(err, exists) { 268 | * if (err) throw err; 269 | * console.log(exists); 270 | * }); 271 | * //=> true 272 | * ``` 273 | * @param {String} `name` Name of module to check. 274 | * @param {Function} `cb` Callback function 275 | */ 276 | 277 | function checkName(name, cb) { 278 | request(`https://registry.npmjs.org/${name}/latest`, {}, function(err, res, msg) { 279 | if (err) return cb(err); 280 | 281 | if (typeof msg === 'string' && msg === 'Package not found') { 282 | cb(null, false); 283 | return; 284 | } 285 | 286 | if (res && res.statusCode === 404) { 287 | return cb(null, false); 288 | } 289 | 290 | cb(null, true); 291 | }); 292 | } 293 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "base-npm", 3 | "description": "Base plugin that adds methods for programmatically running npm commands.", 4 | "version": "0.4.1", 5 | "homepage": "https://github.com/node-base/base-npm", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "repository": "node-base/base-npm", 8 | "bugs": { 9 | "url": "https://github.com/node-base/base-npm/issues" 10 | }, 11 | "license": "MIT", 12 | "files": [ 13 | "index.js" 14 | ], 15 | "main": "index.js", 16 | "engines": { 17 | "node": ">=0.10.0" 18 | }, 19 | "scripts": { 20 | "test": "mocha" 21 | }, 22 | "dependencies": { 23 | "async-array-reduce": "^0.1.0", 24 | "cross-spawn": "^4.0.0", 25 | "extend-shallow": "^2.0.1", 26 | "is-valid-app": "^0.1.1", 27 | "min-request": "^1.4.1" 28 | }, 29 | "devDependencies": { 30 | "base": "^0.11.0", 31 | "delete": "^0.3.2", 32 | "gulp-format-md": "^0.1.9", 33 | "mocha": "^2.5.3", 34 | "pkg-store": "^0.2.2" 35 | }, 36 | "keywords": [ 37 | "api", 38 | "base", 39 | "baseplugin", 40 | "cli", 41 | "commands", 42 | "dev", 43 | "flags", 44 | "install", 45 | "npm", 46 | "plugin", 47 | "save", 48 | "savedev", 49 | "spawn" 50 | ], 51 | "verb": { 52 | "run": true, 53 | "toc": false, 54 | "layout": "default", 55 | "tasks": [ 56 | "readme" 57 | ], 58 | "plugins": [ 59 | "gulp-format-md" 60 | ], 61 | "related": { 62 | "highlight": "base-bower", 63 | "list": [ 64 | "base", 65 | "base-questions", 66 | "base-task" 67 | ] 68 | }, 69 | "reflinks": [ 70 | "base-npm-prompt", 71 | "base-questions", 72 | "verb", 73 | "base" 74 | ], 75 | "lint": { 76 | "reflinks": true 77 | } 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | require('mocha'); 4 | var assert = require('assert'); 5 | var path = require('path'); 6 | var Base = require('base'); 7 | var Pkg = require('pkg-store'); 8 | var del = require('delete'); 9 | var npm = require('./'); 10 | var app, pkg; 11 | 12 | var fixtures = path.resolve.bind(path, __dirname, 'fixtures'); 13 | var cwd = process.cwd(); 14 | 15 | describe('base-npm', function() { 16 | this.timeout(20000); 17 | 18 | beforeEach(function() { 19 | process.chdir(fixtures()); 20 | pkg = new Pkg(process.cwd()); 21 | app = new Base(); 22 | app.isApp = true; 23 | app.use(npm()); 24 | }); 25 | 26 | afterEach(function(cb) { 27 | del(fixtures('package.json'), function(err) { 28 | if (err) return cb(err); 29 | pkg.data = require(fixtures('tmpl.json')); 30 | pkg.save(); 31 | cb(); 32 | }); 33 | }); 34 | 35 | after(function() { 36 | process.chdir(cwd); 37 | }); 38 | 39 | it('should export a function', function() { 40 | assert.equal(typeof npm, 'function'); 41 | }); 42 | 43 | it('should install globally and not save to package.json', function(cb) { 44 | app.npm.global('node-foo', function(err) { 45 | if (err) return cb(err); 46 | assert(!pkg.has('dependencies.node-foo')); 47 | assert(!pkg.has('devDependencies.node-foo')); 48 | cb(); 49 | }); 50 | }); 51 | 52 | it('should install and not save to package.json', function(cb) { 53 | app.npm.install('isobject', function(err) { 54 | if (err) return cb(err); 55 | assert(!pkg.has('dependencies.isobject')); 56 | assert(!pkg.has('devDependencies.isobject')); 57 | cb(); 58 | }); 59 | }); 60 | 61 | it('should install to dependencies in package.json', function(cb) { 62 | app.npm.save('isobject', function(err) { 63 | if (err) return cb(err); 64 | assert(pkg.has('dependencies.isobject')); 65 | cb(); 66 | }); 67 | }); 68 | 69 | it('should install to devDependencies in package.json', function(cb) { 70 | app.npm.saveDev('isobject', function(err) { 71 | if (err) return cb(err); 72 | assert(pkg.has('devDependencies.isobject')); 73 | cb(); 74 | }); 75 | }); 76 | 77 | it('should install the latest version of a package', function(cb) { 78 | var obj = new Pkg(process.cwd()); 79 | obj.set('devDependencies', {isobject: '*'}); 80 | obj.save(); 81 | 82 | app.npm.latest(['isobject', '--save-dev'], function(err) { 83 | if (err) return cb(err); 84 | var pkg2Path = fixtures('package.json'); 85 | var pkg2 = require(pkg2Path); 86 | assert(pkg2.devDependencies.hasOwnProperty('isobject')); 87 | assert.notEqual(pkg2.devDependencies.isobject, '*'); 88 | 89 | del(pkg2, cb); 90 | }); 91 | }); 92 | 93 | it('should return `true` when module exists on npm', function(cb) { 94 | var name = 'base-npm'; 95 | app.npm.exists(name, function(err, exists) { 96 | if (err) return cb(err); 97 | assert.equal(typeof exists, 'object'); 98 | assert.equal(exists[name], true); 99 | cb(); 100 | }); 101 | }); 102 | 103 | it('should return `false` when module does not exist on npm', function(cb) { 104 | var name = 'ljalskdjflkadsflkjalksdjlkasdflkjsdfljlklk'; 105 | app.npm.exists(name, function(err, exists) { 106 | if (err) return cb(err); 107 | assert.equal(typeof exists, 'object'); 108 | assert.equal(exists[name], false); 109 | cb(); 110 | }); 111 | }); 112 | 113 | it('should return mixed results when some modules exist and some do not', function(cb) { 114 | var names = ['base-npm', 'ljalskdjflkadsflkjalksdjlkasdflkjsdfljlklk']; 115 | app.npm.exists(names, function(err, exists) { 116 | if (err) return cb(err); 117 | assert.equal(typeof exists, 'object'); 118 | assert.equal(exists[names[0]], true); 119 | assert.equal(exists[names[1]], false); 120 | cb(); 121 | }); 122 | }); 123 | 124 | describe('cwd', function() { 125 | var nestedPkg; 126 | beforeEach(function() { 127 | nestedPkg = new Pkg(fixtures('nested')); 128 | nestedPkg.data = require(fixtures('tmpl.json')); 129 | nestedPkg.save(); 130 | }); 131 | 132 | afterEach(function(cb) { 133 | del(fixtures('nested'), cb); 134 | }); 135 | 136 | it('should install to the specified cwd', function(cb) { 137 | app.cwd = fixtures('nested'); 138 | app.npm.saveDev('isobject', function(err) { 139 | if (err) return cb(err); 140 | nestedPkg = new Pkg(fixtures('nested')); 141 | assert(nestedPkg.has('devDependencies.isobject')); 142 | cb(); 143 | }); 144 | }); 145 | }); 146 | }); 147 | --------------------------------------------------------------------------------