forked from jonschlinkert/npm-install-global
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (128 loc) · 3.12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*!
* npm-install-global <https://github.com/jonschlinkert/npm-install-global>
*
* Copyright (c) 2016-2017, Jon Schlinkert.
* Released under the MIT License.
*/
'use strict';
var fs = require('fs');
var path = require('path');
var gm = require('global-modules');
var spawn = require('cross-spawn');
/**
* Execute `npm --global` with the given command and one or more package names.
* This is the base for [install]() and [uninstall]().
*
* ```js
* npm('install', 'verb', function(err) {
* if (err) throw err;
* });
* ```
* @param {String|Array} `names` One or more package names.
* @param {Function} `cb` Callback
* @api public
*/
function npm(cmd, names, cb) {
if (typeof cb !== 'function') {
throw new TypeError('expected callback to be a function');
}
if (typeof names === 'string') {
names = [names];
}
if (!Array.isArray(names)) {
cb(new TypeError('expected names to be a string or array'));
return;
}
var args = arrayify(cmd).concat(names);
spawn('npm', args, {stdio: 'inherit'})
.on('error', cb)
.on('close', function(code, err) {
cb(err, code);
});
}
/**
* Execute `npm [cmd] --global` with one or more package `names`.
*
* ```js
* npm.global('install', 'generate', function(err) {
* if (err) throw err;
* });
* ```
* @param {String} `cmd` The command to run
* @param {String|Array} `names` One or more package names.
* @param {Function} `cb` Callback
* @api public
*/
npm.global = function(cmd, names, cb) {
npm([cmd, '--global'], names, cb);
};
/**
* Execute `npm install --global` with one or more package `names`.
*
* ```js
* npm.install('generate', function(err) {
* if (err) throw err;
* });
* ```
* @param {String|Array} `names` One or more package names.
* @param {Function} `cb` Callback
* @api public
*/
npm.install = function(names, cb) {
npm.global('install', names, cb);
};
/**
* Install the given packages if they are not already installed.
*
* ```js
* npm.maybeInstall(['foo', 'bar', 'baz'], function(err) {
* if (err) throw err;
* });
* ```
* @param {String|Array} `names` One or more package names.
* @param {Function} `cb` Callback
* @api public
*/
npm.maybeInstall = function(names, cb) {
if (typeof names === 'string') {
names = [names];
}
names = names.filter(function(name) {
return !isInstalled(name);
});
if (names.length === 0) {
return cb(null, []);
}
npm.global('install', names, function(err) {
if (err) return cb(err);
cb(null, names);
});
};
/**
* Execute `npm uninstall --global` with one or more package `names`.
*
* ```js
* npm.uninstall('yeoman', function(err) {
* if (err) throw err;
* });
* ```
* @param {String|Array} `names` One or more package names.
* @param {Function} `cb` Callback
* @api public
*/
npm.uninstall = function(names, cb) {
npm.global('uninstall', names, cb);
};
/**
* Returns true if package `name` is already installed globally
*/
function isInstalled(name) {
return fs.existsSync(path.resolve(gm, name));
}
function arrayify(val) {
return val ? (Array.isArray(val) ? val : [val]) : [];
}
/**
* Expose `npm`
*/
module.exports = npm;