-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
292 lines (252 loc) · 6.65 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*!
* base-npm (https://github.com/jonschlinkert/base-npm)
*
* Copyright (c) 2016, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
var path = require('path');
var isValid = require('is-valid-app');
var extend = require('extend-shallow');
var spawn = require('cross-spawn');
var request = require('min-request');
var reduce = require('async-array-reduce');
module.exports = function(options) {
return function(app) {
if (!isValid(app, 'base-npm')) return;
/**
* Execute `npm install` with the given `args`, package `names`
* and callback.
*
* ```js
* app.npm('--save', ['isobject'], function(err) {
* if (err) throw err;
* });
* ```
* @name .npm
* @param {String|Array} `args`
* @param {String|Array} `names`
* @param {Function} `cb` Callback
* @api public
*/
this.define('npm', npm);
function npm(cmds, args, cb) {
var cwd = app.cwd || process.cwd();
args = arrayify(cmds).concat(arrayify(args));
spawn('npm', args, {cwd: cwd, stdio: 'inherit'})
.on('error', cb)
.on('close', function(code, err) {
cb(err, code);
});
};
/**
* Install one or more packages. Does not save anything to
* package.json. Equivalent of `npm install foo`.
*
* ```js
* app.npm.install('isobject', function(err) {
* if (err) throw err;
* });
* ```
* @name .npm.install
* @param {String|Array} `names` package names
* @param {Function} `cb` Callback
* @api public
*/
npm.install = function(args, cb) {
npm('install', args, cb);
};
/**
* (Re-)install and save the latest version of all `dependencies`
* and `devDependencies` currently listed in package.json.
*
* ```js
* app.npm.latest(function(err) {
* if (err) throw err;
* });
* ```
* @name .npm.latest
* @param {Function} `cb` Callback
* @api public
*/
npm.latest = function(names, cb) {
if (typeof names !== 'function') {
npm.install(latest(names), cb);
return;
}
if (typeof names === 'function') {
cb = names;
}
var devDeps = latest(pkg(app, 'devDependencies'));
var deps = latest(pkg(app, 'dependencies'));
npm.dependencies(deps, function(err) {
if (err) return cb(err);
npm.devDependencies(devDeps, cb);
});
};
/**
* Execute `npm install --save` with one or more package `names`.
* Updates `dependencies` in package.json.
*
* ```js
* app.npm.dependencies('micromatch', function(err) {
* if (err) throw err;
* });
* ```
* @name .npm.dependencies
* @param {String|Array} `names`
* @param {Function} `cb` Callback
* @api public
*/
npm.dependencies = function(names, cb) {
var args = [].concat.apply([], [].slice.call(arguments));
cb = args.pop();
if (args.length === 0) {
args = pkg(app, 'dependencies');
}
if (!args.length) {
cb();
return;
}
npm.install(['--save'].concat(args), cb);
};
/**
* Execute `npm install --save-dev` with one or more package `names`.
* Updates `devDependencies` in package.json.
*
* ```js
* app.npm.devDependencies('isobject', function(err) {
* if (err) throw err;
* });
* ```
* @name .npm.devDependencies
* @param {String|Array} `names`
* @param {Function} `cb` Callback
* @api public
*/
npm.devDependencies = function(names, cb) {
var args = [].concat.apply([], [].slice.call(arguments));
cb = args.pop();
if (args.length === 0) {
args = pkg(app, 'devDependencies');
}
if (!args.length) {
cb();
return;
}
npm.install(['--save-dev'].concat(args), cb);
};
/**
* Execute `npm install --global` with one or more package `names`.
*
* ```js
* app.npm.global('mocha', function(err) {
* if (err) throw err;
* });
* ```
* @name .npm.global
* @param {String|Array} `names`
* @param {Function} `cb` Callback
* @api public
*/
npm.global = function(names, cb) {
var args = [].concat.apply([], [].slice.call(arguments));
cb = args.pop();
if (!args.length) {
cb();
return;
}
npm.install(['--global'].concat(args), cb);
};
/**
* Check if one or more names exist on npm.
*
* ```js
* app.npm.exists('isobject', function(err, results) {
* if (err) throw err;
* console.log(results.isobject);
* });
* //=> true
* ```
* @name .npm.exists
* @param {String|Array} `names`
* @param {Function} `cb` Callback
* @returns {Object} Object of results where the `key` is the name and the value is `true` or `false`.
* @api public
*/
npm.exists = function(names, cb) {
var names = [].concat.apply([], [].slice.call(arguments));
cb = names.pop();
reduce(names, {}, function(acc, name, next) {
checkName(name, function(err, exists) {
if (err) return next(err);
acc[name] = exists;
next(null, acc);
});
}, cb);
};
/**
* Aliases
*/
npm.saveDev = npm.devDependencies;
npm.save = npm.dependencies;
};
};
/**
* Get the package.json for the current project
*/
function pkg(app, prop) {
return Object.keys(pkgData(app)[prop] || {});
}
function pkgPath(app) {
return path.resolve(app.cwd || process.cwd(), 'package.json');
}
function pkgData(app) {
return app.pkg ? app.pkg.data : require(pkgPath(app));
}
/**
* Prefix package names with `@latest`
*/
function latest(keys) {
if (typeof keys === 'string') {
keys = [keys];
}
if (!Array.isArray(keys)) {
return [];
}
return keys.map(function(key) {
return key.charAt(0) !== '-' ? (key + '@latest') : key;
});
}
/**
* Cast `val` to an array
*/
function arrayify(val) {
return val ? (Array.isArray(val) ? val : [val]) : [];
}
/**
* Check if a name exists on npm.
*
* ```js
* checkName('foo', function(err, exists) {
* if (err) throw err;
* console.log(exists);
* });
* //=> true
* ```
* @param {String} `name` Name of module to check.
* @param {Function} `cb` Callback function
*/
function checkName(name, cb) {
request(`https://registry.npmjs.org/${name}/latest`, {}, function(err, res, msg) {
if (err) return cb(err);
if (typeof msg === 'string' && msg === 'Package not found') {
cb(null, false);
return;
}
if (res && res.statusCode === 404) {
return cb(null, false);
}
cb(null, true);
});
}