-
Notifications
You must be signed in to change notification settings - Fork 11
/
install.js
386 lines (312 loc) · 12.2 KB
/
install.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
* fis
* http://fis.baidu.com/
*/
'use strict';
exports.name = 'install';
exports.usage = '[options] <components...>';
exports.desc = 'install components';
var logger = require('./lib/logger');
var factory = require('./lib/remotes/factory.js');
var _ = require('./lib/util.js');
var fs = require('fs');
var path = require('path');
var exists = fs.existsSync;
var write = fs.writeFileSync;
var Promise = require('bluebird');
var semver = require('semver');
exports.register = function(commander) {
commander
.option('--save', 'save component(s) dependencies into `component.json` file.')
.option('-r, --root <path>', 'set project root')
.option('--verbose', 'enable verbose mode')
.option('--download-gitlab-from-svn', 'you don\'t need this.')
.action(function() {
var args = [].slice.call(arguments);
var options = args.pop();
var settings = {
save: !!options.save,
root: options.root || '',
downloadGitlabFromSvn: options.downloadGitlabFromSvn,
components: args.concat()
};
Promise
// resolve project root.
// 根据 fis-conf.js 所在目录来决定 root 是哪个目录。
.try(function() {
if (!settings.root) {
var findup = require('findup');
return new Promise(function(resolve, reject) {
var fup = findup(process.cwd(), 'fis-conf.js');
var dir = null;
fup.on('found', function(found) {
dir = found;
fup.stop();
});
fup.on('error', reject);
fup.on('end', function() {
resolve(dir);
});
})
.then(function(dir) {
if (dir && dir !== process.cwd()) {
fis.log.notice('Detect `fis-conf.js` is under `' + dir.green + '`. \nComponents will be installed under the folder too.');
}
settings.root = dir || process.cwd();
});
}
})
// load fis-conf.js if exists.
// 读取用户配置信息。
.then(function() {
var filepath = path.resolve(settings.root, 'fis-conf.js');
if (exists(filepath)) {
try {
require(filepath);
} catch (e) {
// fis.log.warning('Load fis-conf.js failure with message ' + e.message);
}
}
// 应用 fis-conf.js
settings.componentsDir = path.join(settings.root,
fis.config.get('component.dir') || '/components');
settings.protocol = fis.config.get('component.protocol', 'github');
settings.github = fis.config.get('component.github', {
author: "fis-components"
});
settings.gitlab = fis.config.get('component.gitlab', {});
settings.lights = fis.config.get('component.lights', {});
})
.then(function() {
var roadmap = fis.config.get('roadmap.path', []);
var good = false;
var folder = path.basename(fis.config.get('component.dir') || '/components');
if (fis.config.get('component.skipRoadmapCheck', false)) {
return;
}
roadmap.every(function(item) {
var reg = item.reg.toString();
if (~reg.indexOf(folder) && item.isMod) {
good = true;
return false;
}
return true;
});
good || logger.warn('Please copy the following rule to you `roadmap.path`\n settings in `fis-conf.js`:\n' +
'{\n' +
' reg: /^\\/' + folder + '\\/.*\\.js$/i,\n' +
' isMod: true,\n' +
' jswrapper: {\n' +
' type: \'amd\'\n' +
' }\n' +
'}\n'
);
})
// 读取 components.json 如果存在
.then(function() {
var components = settings.components;
var path = require('path');
var exists = require('fs').existsSync;
var componentJson = path.join(settings.root, 'component.json');
if (!components.length && !exists(componentJson)) {
throw new Error('missing `component.json`');
}
if (exists(componentJson)) {
var config = Promise.promisify(require('./lib/config'));
return config(componentJson)
.then(function(ret) {
settings.config = ret;
[]
.push
.apply(settings.components, ret.dependencies || []);
[]
.push
.apply(settings.components, ret.devDependencies || []);
ret.protocol && (settings.protocol = ret.protocol);
ret.github && _.mixin(settings.github, ret.github);
ret.gitlab && _.mixin(settings.gitlab, ret.gitlab);
ret.lights && _.mixin(settings.lights, ret.lights);
});
}
})
.then(function() {
var components = settings.components || [];
factory.setSettings(settings);
// validate and filter invalid dependencies.
// 过滤掉不能识别的依赖。
return strToRemote(components, false, settings);
})
// finally get components list.
.then(function(components) {
if (!components.length) {
logger.warn('Installed nothing!');
return;
}
var collector = require('./lib/collector.js');
var SimpleTick = require('./lib/tick.js');
var bar = new SimpleTick('analyzing dependencies ', {
keepAlive: true
});
var specified = {};
components.forEach(function(item) {
specified[item.name] = item._version;
});
return collector(components)
.then(function(components) {
bar.clear();
bar = null;
return components;
})
.then(function(components) {
// console.log(components);
var finalList = [];
var posMap = {};
var alerted = {};
var notified = {};
components.forEach(function(item) {
if (typeof posMap[item.name] !== 'undefined') {
var target = finalList[posMap[item.name]];
var finalItem = target;
// 版本一致直接跳过。
if (target.version === item.version) {
return;
}
// 原有版本不符合制定版本要求,而先版本符合,那就直接替换吧。
if (specified[item.name] && !satisfies(target.version, specified[item.name]) && satisfies(item.version, specified[item.name])) {
finalItem = item;
finalList.splice(posMap[item.name], 1, item);
return;
}
// 现有版本不符合制定要求,那还是不要换了。
if (specified[item.name] && !satisfies(item.version, specified[item.name])) {
!notified[item.name + '@' + item.version] && fis.log.warning(item.name + '@' + item.version + ' don\'t satisfy the version ' + item.name + '@' + specified[item.name] + ' you specified. The version ' + finalItem.name + '@' + finalItem.version + ' will be keeped!');
notified[item.name + '@' + item.version] = true;
return;
}
// 用户没有指定要什么版本,那就用最新的版本
// 不满足需求,那就在入口指定版本吧。
if (semver.valid(item.version) && semver.validRange(target.version) && semver.gt(item.version, target.version)) {
finalItem = item;
finalList.splice(posMap[item.name], 1, item);
}
if (!alerted[item.name + '@' + item.version]) {
fis.log.warning(item.name + '@' + item.version + ' conflict againest with ' + target.name + '@' + target.version + ', version ' + finalItem.name + '@' + finalItem.version + ' will be used!');
alerted[item.name + '@' + item.version] = true;
}
} else {
posMap[item.name] = finalList.length;
finalList.push(item);
}
});
// console.log(finalList.map(function(item) {
// return item.name + '@' + item.version
// }));
// 过滤掉本地的。
components = finalList.filter(function(item) {
return item.location !== 'local';
});
if (!components.length) {
console.log('Already installed');
return;
}
return Promise
.reduce(components, function(collection, component) {
var SimpleTick = require('./lib/tick.js');
var ProgressBar = require('progress');
var bar;
var progress = function(percent, loaded, total) {
if (total && process.stderr.isTTY) {
bar = bar || new ProgressBar('downloading `' + component.address + '` [:bar] :percent :etas', {
complete: '=',
incomplete: ' ',
width: 20,
total: total,
clear: true
});
bar.update(percent);
} else {
bar = bar || new SimpleTick('downloading `' + component.address + '` ');
bar.tick();
}
};
return component
.install(progress)
.then(function(component) {
if (bar instanceof SimpleTick) {
bar.clear();
}
collection.push(component);
return collection;
})
// .catch(function(e) {
// logger.error('\x1b[31m%s\x1b[0m', e.message);
// fis.log.debug(e.stack);
// });
}, [])
.then(function(components) {
var last = components.length - 1;
var arrs = components.map(function(item, index) {
return (index === last ? '└── ' : '├── ') + item.type + ':' + item.address + '@' + item.version;
});
console.log('\nInstalled\n%s', arrs.join('\n'));
return components;
});
})
.catch(function(e) {
bar && bar.clear;
console.log('\x1b[31m%s\x1b[0m', e.message);
logger.debug(e.stack);
process.exit(1);
});
})
// 保存 components.json
.then(function(installed) {
// 如果指定了 --save, 则需要把数据写入到 components.josn 文件里面。
if (settings.save && args.length && installed && installed.length) {
var config = settings.config || {};
var specified = strToRemote(args.concat(), false, settings);
config.dependencies = config.dependencies || [];
var oldList = strToRemote(config.dependencies, false, settings);
specified.forEach(function(item) {
var idx;
if (!~_.indexOfArray('name', item.name, oldList) && ~(idx = _.indexOfArray('name', item.name, installed))) {
var found = installed[idx];
config.dependencies.push(found.type + ':' + found.address + '@' + found.version);
}
});
var componentJson = path.join(settings.root, 'component.json');
write(componentJson, JSON.stringify(config, null, 2));
}
})
// error handle
.catch(function(e) {
if (/Not\s+Found/i.test(e.message)) {
logger.warn('`fis install` now is for installing commponents, you may use `\x1b[31mlights install\x1b[0m` instead.');
}
console.log('\x1b[31m%s\x1b[0m', e.message);
logger.debug(e.stack);
});
});
};
function strToRemote(components, ignoreInvalid, settings) {
return components
.map(function(component) {
var type = factory.detect(component);
if (!type) {
ignoreInvalid || logger.warn('`%s` is not a valid repository.', component);
return null;
}
return factory(component, null, {
downloadGitlabFromSvn: settings.downloadGitlabFromSvn
});
})
.filter(function(item) {
return item != null;
});
}
function satisfies(version, range) {
if (range === '*') {
return true;
}
return semver.valid(version) && semver.validRange(range) && semver.satisfies(version, range);
}