forked from bobundov/bower-installer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbower-installer.js
executable file
·190 lines (169 loc) · 5.65 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
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
#!/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,
'remove_install_path': Boolean,
'silent': Boolean
},
shortHands = {
"r": ["--remove"],
"h": ["--help"],
"p": ["--remove-install-path"],
"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("--remove-install-path [p] - Remove the install destination paths before installing dependencies.")
console.log("--help [h] - Display this.");
console.log(("---------------------------------------------------------------------").blue);
return;
}
// Load configuration file
try {
cfg = require(path.join(basePath,'bower.json')).install;
} catch(e) {
try {
cfg = require(path.join(basePath,'component.json')).install;
} catch(e) {
throw new Error('Neither bower.json nor component.json present');
}
}
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;
if (cfg.base !== undefined) {
_.each(paths, function(path, key) { paths[key] = cfg.base + pathSep + path; });
} else {
installPathFiles = _.map( paths,
function(path) {
return (basePath + pathSep + path);
});
_.each(installPathFiles, function(file) {
// if file path has variables like {name} do not create/delete folder
// the folder will be created later in the installer
if (!utils.hasVariableInPath(file)) {
if(options['remove-install-path']) {
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;
var keypath = basePath + pathSep + bower.config.directory + pathSep + key;
var pakcfg = utils.getJSON(keypath, pathSep);
if (!pakcfg) {
pakcfg = {name: key};
}
pakcfg.key = key;
if(!cfg.ignore || (cfg.ignore && !_.contains(cfg.ignore, key)) ) {
if(_.isArray(dep)) {
async.each(dep, function(subDep, callback) {
installer.installDependency(subDep, pakcfg, cfg, paths, options.silent, callback);
}, callback);
} else {
installer.installDependency(dep, pakcfg, 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);
}
});