-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
239 lines (203 loc) · 6.72 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
/*!
* base-npm-prompt (https://github.com/node-base/base-npm-prompt)
*
* Copyright (c) 2016, Jon Schlinkert.
* Licensed under the MIT License.
*/
'use strict';
var utils = require('./utils');
module.exports = function(config) {
return function(app) {
if (!utils.isValid(app, 'base-npm-prompt')) {
return;
}
if (typeof app.npm === 'undefined') {
throw new Error('expected the base-npm plugin to be registered');
}
var npm = app.npm;
/**
* Prompts the user to ask if they want to install the packages listed on
* `app.cache.install.dependencies` or `app.cache.install.devDependencies` based on
* the given `type`.
*
* ```js
* app.npm.prompt('dependencies', function(err) {
* if (err) return console.error(err):
* });
* ```
* @name .npm.prompt
* @param {String} `type` dependency type to install (dependencies or devDependencies)
* @param {Object} `options`
* @param {Function} `cb` Callback
* @api public
*/
utils.define(npm, 'prompt', function(type, options, cb) {
if (typeof options === 'function') {
cb = options;
options = {};
}
var opts = utils.extend({type: type}, options);
install(opts, cb);
});
/**
* Prompts the user to ask if they want to install the given package(s).
* Requires the [base-questions][] plugin to be registered first.
*
* ```js
* app.npm.askInstall('isobject', function(err) {
* if (err) throw err;
* });
* ```
* @name .npm.askInstall
* @param {String|Array} `names` One or more package names.
* @param {Object} `options`
* @param {Function} `cb` Callback
* @api public
*/
utils.define(npm, 'askInstall', function(names, options, cb) {
install(names, options, cb);
});
/**
* Prompts the user to ask if they want to check if the given package(s)
* exist on npm, then install the ones that do exist.
* Requires the [base-questions][] plugin to be registered first.
*
* ```js
* app.npm.checkInstall('isobject', function(err) {
* if (err) throw err;
* });
* ```
* @name .npm.checkInstall
* @param {String|Array} `names` One or more package names.
* @param {Object} `options`
* @param {Function} `cb` Callback
* @api public
*/
utils.define(npm, 'checkInstall', function(names, options, cb) {
if (typeof names !== 'string' && !Array.isArray(names)) {
npm.checkInstall([], names, options);
return;
}
if (typeof options === 'function') {
cb = options;
options = {};
}
// setup options specific for `checkInstall` to
// show a different message and check the modules exist before installing.
var opts = utils.extend({
namespace: 'base-npm-check-install-',
message: function(names, type) {
var target = names.length > 1 ? type : `"${names[0]}" to "${type}"`;
return `Would you like to check for and install ${target}?`;
},
handleAnswer: function(type, answer, cb) {
npm.exists(answer, function(err, exists) {
if (err) return cb(err);
utils.each(Object.keys(exists), function(key, next) {
if (exists[key]) {
console.log(utils.log.green(utils.log.success), key);
} else {
console.log(utils.log.red(utils.log.error), key);
}
if (exists[key] === false) return next();
npm[type](key, next);
}, cb);
});
}
}, options);
// run the actual ask and install method
install(names, opts, cb);
});
/**
* Does the actual ask and installing based on the options passed in.
* Default logic is the original `askInstall` logic.
*
* @param {String|Array} `names` One or more package names.
* @param {Object} `options`
* @param {Function} `cb` Callback
*/
function install(names, options, cb) {
if (typeof names !== 'string' && !Array.isArray(names)) {
install([], names, options);
return;
}
if (typeof options === 'function') {
cb = options;
options = {};
}
// register the base questions plugin
if (typeof app.ask !== 'function') {
app.use(utils.questions());
}
// extend `data` onto options, which is used
// by question-store to pre-populate answers
options = utils.extend({}, options, options.data);
var type = options.type || 'devDependencies';
var key = (options.namespace || 'base-npm-install-') + type; // namespace to validate conflicts
names = arrayify(names);
names = filterKeys(app, type, names);
if (names.length === 0) return cb();
// allow a custom message string or function.
var msg = options.message || function(names, type) {
var target = names.length > 1 ? type : `"${names[0]}" to "${type}"`;
return `Would you like to install ${target}?`;
};
// allow a custom answer handler
var handleAnswer = options.handleAnswer || function(type, answer, cb) {
npm[type](answer, cb);
};
app.on('ask', function(answer, key, question, answers) {
if (typeof answer !== 'undefined' && question.name === key) {
question.options.skip = true;
answers[key] = answer;
}
});
// allow prompt to be suppressed for unit tests
if (options.noprompt === true) {
options[key] = true;
}
if (typeof msg === 'function') {
msg = msg(names, type);
}
if (names.length === 1) {
app.confirm(key, {message: msg, save: false});
} else {
app.choices(key, {message: msg, save: false}, names);
}
// ask the actual question
app.ask(key, options, function(err, answers) {
if (err) return cb(err);
var answer = answers[key];
if (answer === true) {
answer = names;
}
answer = arrayify(answer);
if (answer.length) {
handleAnswer(type, answer, cb);
} else {
cb();
}
});
}
};
};
/**
* Filter out any package names that have already been installed.
* Also include any that are stored on `app.cache.install[prop]`.
*/
function filterKeys(app, prop, names) {
var all = names.concat(app.get(['cache.install', prop]) || []);
if (!app.pkg || !app.pkg.data) {
return all;
}
var depKeys = Object.keys(app.pkg.get(prop) || {});
return all.filter(function(name) {
return depKeys.indexOf(name) === -1;
});
}
/**
* Cast `val` to an array
*/
function arrayify(val) {
return val ? (Array.isArray(val) ? val : [val]) : [];
}