forked from jspm/jspm-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jspm.js
executable file
·289 lines (251 loc) · 8.77 KB
/
jspm.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
#!/usr/bin/env node
/*
* Copyright 2014 Guy Bedford (http://guybedford.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var ui = require('./lib/ui');
var config = require('./lib/config');
var pkg = require('./lib/package');
var core = require('./lib/core');
var bundle = require('./lib/bundle');
var semver = require('./lib/semver');
var build = require('./lib/build');
process.on('uncaughtException', function(err) {
ui.log('err', err.stack || err);
});
if (require.main !== module)
return;
(function() {
function showInstructions() {
ui.log('\n'
+ ' \033[47m\033[1m \033[0m\n'
+ ' \033[47m\033[93m\033[1m jspm \033[0m\033[90m '
+ 'Browser Package Management'
+ ' \033[0m\n'
+ ' \033[47m\033[1m \033[0m\n'
+ '\n'
+ 'jspm install <name[=version]> [-o {package override}] [-f --force] \n'
+ ' install Install / update from package.json\n'
+ ' install jquery Install a package from the registry\n'
+ ' install npm:underscore Install latest version from NPM\n'
+ ' install [email protected] Install latest minor version\n'
+ ' install [email protected] Install an exact version\n'
+ ' install jquery npm:underscore Install multiple packages\n'
+ ' install jquery=1.1.1 Install a package to a specific version\n'
+ ' install [email protected]=1.2.3 Install a version range to a specific version\n'
+ '\n'
+ 'jspm inject <name[=version]> [-o {package override}] [-f --force] \n'
+ ' inject jquery Identical to install, but injects config\n'
+ ' only instead of downloading the package\n'
+ '\n'
+ 'jspm uninstall name Uninstall a package and any orphaned deps\n'
+ '\n'
+ 'jspm update [-f -force] Check and update existing modules\n'
+ '\n'
+ 'jspm init Create / recreate the configuration file\n'
+ '\n'
+ 'jspm dl-loader Download the jspm browser loader\n'
+ '\n'
+ 'jspm setmode <mode>\n'
+ ' setmode local Switch to locally downloaded libraries\n'
+ ' setmode remote Switch to CDN external package sources\n'
+ ' setmode dev Switch to the development baseURL\n'
+ ' setmode production Switch to the production baseURL\n'
+ '\n'
+ ''
+ 'jspm depcache [moduleName] Stores dep cache in config for flat pipelining\n'
+ 'jspm bundle [moduleName] [file] Creates a single-file bundle for a module\n'
+ '\n'
+ 'jspm config <property> <value> Set global configuration\n'
+ ' config github.username githubusername \n'
+ ' config github.password githubpassword \n'
);
}
var args = process.argv.splice(2);
switch(args[0]) {
case 'inject':
var inject = true;
case 'install':
var options = readOptions(args, ['--force', '--https', '--override']);
options.inject = inject;
var args = options.args;
var depMap;
for (var i = 1; i < (options.override || args.length); i++) {
depMap = depMap || {};
var name, target;
var arg = args[i];
if (arg.indexOf('=') == -1) {
// install [email protected] -> install jquery=^1.2.3
name = arg.split('@')[0];
target = arg.split('@')[1] || '';
// valid semver -> make semver compatibility default
if (target && target.match(semver.semverRegEx))
target = '^' + target;
}
else {
name = arg.split('=')[0];
target = arg.split('=')[1];
}
depMap[name] = target;
}
if (options.override)
options.override = eval('(' + args.splice(options.override).join(' ') + ')');
if (options.https)
pkg.https = true;
// no install package -> install from package.json dependencies
(depMap ? core.install(depMap, options) : core.install(true, options))
.then(function() {
return core.checkDlLoader()
})
.then(function() {
return core.setMode(inject ? 'remote' : 'local')
})
.then(function() {
ui.log('');
ui.log('ok', 'Install complete');
process.exit();
}, function(err) {
// something happened (cancel / err)
ui.log('err', err.stack || err);
ui.log('warn', 'Installation changes not saved');
process.exit(1);
});
break;
case 'update':
var options = readOptions(args, ['--force', '--https']);
core.install(true, options)
.then(function() {
ui.log('');
ui.log('ok', 'Update complete');
}, function(err) {
ui.log('err', err.stack || err);
ui.log('warn', 'Update changes not saved');
process.exit(1);
});
break;
case 'uninstall':
core.uninstall(args.splice(1))
.then(function(removed) {
if (removed) {
ui.log('');
ui.log('ok', 'Uninstall complete');
}
else
ui.log('info', 'Nothing to remove');
}, function(err) {
ui.log('err', err.stack || err);
ui.log('warn', 'Uninstall changes not saved');
process.exit(1);
});
break;
case 'clean':
core.clean();
break;
case 'init':
core.init();
break;
case 'prune':
core.prune();
break;
case 'dl-loader':
core.dlLoader();
break;
case 'setmode':
core.setMode(args[1]);
break;
case 'depcache':
bundle.depCache(args[1]);
break;
case 'bundle':
var options = readOptions(args, ['--inject']);
var inject = !!options.inject;
var bArgs = options.args;
if (bArgs.length < 2) {
ui.log('warn', 'No main entry point is provided, please specify the module to build');
}
else {
var secondLastArg = bArgs[bArgs.length - 2].trim();
var signChar = secondLastArg.substr(secondLastArg.length - 1, 1);
var expression = "";
var fileName = undefined;
// we can write: jspm bundle app + other
if (["+", "-"].indexOf(signChar) != -1) {
expression = bArgs.splice(1, bArgs.length - 1).join(' ');
}
// or we can write: jspm bundle app + other out.js
else {
expression = bArgs.splice(1, bArgs.length - 2).join(' ');
fileName = bArgs[bArgs.length - 1];
}
bundle.bundle(expression, fileName, inject);
}
break;
case 'build':
core.build()
break;
case 'compile':
var options = readOptions(args, ['--transpile', '--minify', '--removeJSExtensions'], ['--map', '--format']);
if (options.map) {
var mapParts = options.map.split('=');
options.map = {};
options.map[mapParts[0]] = mapParts[1];
}
build.compileDir(args[1], options)
.then(function() {
ui.log('ok', 'Compilation complete');
}, function(e) {
ui.log('err', e.stack || e);
});
break;
case 'config':
var property = args[1];
var value = args.splice(2).join(' ');
config.set(property, value);
break;
case '--help':
case '-h':
showInstructions();
break;
default:
if (args[0])
ui.log('Invalid argument ' + args[0]);
showInstructions();
}
})();
function readOptions(args, flags, settings) {
settings = settings || [];
var argOptions = { args: [] };
for (var i = 0; i < args.length; i++) {
if (args[i].substr(0, 2) == '--') {
for (var j = 0; j < flags.length; j++)
if (flags[j] == args[i])
argOptions[flags[j].substr(2)] = i;
for (var j = 0; j < settings.length; j++)
if (settings[j] == args[i])
argOptions[settings[j].substr(2)] = args[++i];
}
else if (args[i].substr(0, 1) == '-' && args[i].length > 1) {
var opts = args[i].substr(1);
for (var j = 0; j < opts.length; j++) {
for (var k = 0; k < flags.length; k++) {
if (flags[k].substr(2, 1) == opts[j])
argOptions[flags[k].substr(2)] = argOptions.args.length;
}
}
}
else
argOptions.args.push(args[i]);
}
return argOptions;
}