forked from bobundov/bower-installer
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbower-installer.js
executable file
·165 lines (150 loc) · 4.78 KB
/
bower-installer.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
#!/usr/bin/env node
var _ = require('lodash'),
async = require('async'),
fileLib = require("node-fs"),
colors = require('colors'),
bower = require('bower'),
path = require('path'),
utils = require('./lib/utils'),
nopt = require('nopt'),
fs = require('fs'),
installer = require('./lib/installer');
var basePath = process.cwd(),
pathSep = '/',
knownOpts = {
'remove': Boolean,
'help': Boolean,
'keep': Boolean,
'silent': Boolean
},
shortHands = {
"r": ["--remove"],
"h": ["--help"],
"k": ["--keep"],
"s": ["--silent"]
},
cfg;
var options = nopt(knownOpts, shortHands, process.argv, 2);
if(options.help) {
console.log(("---------------------------------------------------------------------").blue);
console.log(("Bower Installer").green);
console.log(("---------------------------------------------------------------------").blue);
console.log("Tool for installing bower dependencies that won't include entire repos.");
console.log("Although Bower works great as a light-weight tool to quickly install ");
console.log("browser dependencies, it currently does not provide much functionality ");
console.log("for installing specific \"built\" components for the client.");
console.log(("---------------------------------------------------------------------").blue);
console.log(("Options:").green);
console.log("--remove [r] - Remove the bower_components directory after execution.")
console.log("--help [h] - Display this.");
console.log(("---------------------------------------------------------------------").blue);
return;
}
// Load configuration file
try {
cfg = require(path.join(basePath,'bower.json')).install;
} catch(e) {
cfg = require(path.join(basePath,'component.json')).install;
}
var paths;
var installPathFiles;
if (!options.silent) {
process.stdout.write('Setting up install paths...');
}
if(!cfg || !cfg.path) {
paths = "default";
installPathFiles = _.map(paths,basePath);
}else{
paths = _.isString(cfg.path) ? {all: cfg.path} : cfg.path;
installPathFiles = _.map( paths,
function(path) {
return (basePath + pathSep + path);
});
_.each(installPathFiles, function(file) {
if(!options.keep) {
utils.deleteFolderRecursive(file);
}
if(!fs.existsSync(file)) {
fileLib.mkdirSync(file, 0755, true);
}
});
}
if (!options.silent) {
process.stdout.write(("Finished\r\n").green);
process.stdout.write('Running bower install...');
}
bower.commands
.install()
.on('end', function (installed) {
if (!options.silent) {
process.stdout.write(("Finished\r\n").green);
}
bower.commands
.list({paths: true})
.on('end', function (data) {
if (!options.silent) {
console.log('Installing: ');
}
// The callback here will cascade downwards
// throughout asyncronous calls. This is necessary
// to determine when everything has finished.
async.each(_.map(data, function(dep, key) {
return {
dep: dep,
key: key
}
}), function(o, callback) {
var dep = o.dep,
key = o.key;
if(!cfg.ignore || (cfg.ignore && !_.contains(cfg.ignore, key)) ) {
if(_.isArray(dep)) {
async.each(dep, function(subDep, callback) {
installer.installDependency(subDep, key, cfg, paths, options.silent, callback);
}, callback);
} else {
installer.installDependency(dep, key, cfg, paths, options.silent, callback);
}
} else {
if (!options.silent) {
console.log(('\tIgnoring: ' + key).yellow);
}
}
}, function(err) {
if(err) {
if (!options.silent) {
console.error(('Error:').red, err);
}
} else {
if(options.remove) {
if (!options.silent) {
process.stdout.write('Removing bower_components dir...');
}
installer.removeComponentsDir(function(err) {
if (!options.silent) {
if(err) {
process.stdout.write(("Error").red, err);
} else {
process.stdout.write(("Finished\r\n").green);
}
}
})
} else {
if (!options.silent) {
console.log(('Success').green);
}
}
}
});
})
.on('error', function(error) {
if (!options.silent) {
console.error(error);
}
});
})
.on('error', function(error) {
if (!options.silent) {
process.stdout.write(("Error\r\n").red);
console.error(error);
}
});