-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstall.js
83 lines (76 loc) · 3.05 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
var fs = require('fs'),
path = require('path'),
existsSync = fs.existsSync || path.existsSync,
async = require('async'),
colors = require('colors'),
Columnizer = require('columnizer'),
table = new Columnizer;
pkg = JSON.parse(fs.readFileSync('package.json')),
clientModules = pkg.clientmodules,
copied = [],
directory = pkg.clientmodulesDir || 'clientmodules';
function pad(string, target) {
console.log('target - string.length', target - string.length);
return string + (new Array((target - string.length) < 0 ? 0 : target - string.length).join(' '));
}
if (clientModules && clientModules.forEach) {
try {
fs.mkdirSync(directory);
} catch (e) {}
async.forEach(clientModules, function (item, loopCb) {
fs.readFile('node_modules/' + item + '/package.json', function (err, text) {
if (err) return loopCb(err);
var parsed = JSON.parse(text),
fileName = item + '.js',
path = 'node_modules/' + item + '/',
mainFile = function () {
var res;
if (existsSync(path + fileName)) {
return path + fileName;
} else if (existsSync(path + 'lib/' + fileName)) {
return path + 'lib/' + fileName;
} else if (existsSync(path + 'build/' + fileName)) {
return path + 'build/' + fileName;
} else {
return false;
}
}(),
dependencies = parsed.dependencies;
// simple dependencies check
if (dependencies && Object.keys(dependencies).length) {
// loop through dependencies to see if we have them
// in clientmodules (a simple version-free check is intentional)
for (var dep in dependencies) {
if (clientModules.indexOf(dep) === -1) {
console.log(('warning: ' + item + ' is depends on ' + dep + ' which isn\'t listed in clientmodules').yellow);
}
}
}
if (!mainFile) {
loopCb();
return;
}
writeStream = fs.createWriteStream(directory + '/' + item + '.js'),
readStream = fs.createReadStream(mainFile);
writeStream.once('close', function () {
table.row(' ', (item + '.js').green, mainFile);
loopCb()
});
readStream.pipe(writeStream);
});
}, function (err) {
var sep = '\n ';
if (!err) {
console.log('clientmodules copied the following files into the ' + directory.green + ' directory:');
table.print();
process.exit(0);
} else {
console.error(err);
console.log('Oops... something didn\'t work');
process.exit(1);
}
});
} else {
console.log("Add a 'clientmodules' array to package.json");
process.exit(1);
}