forked from jamesshore/lets_code_javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs.js
More file actions
126 lines (112 loc) · 4.06 KB
/
args.js
File metadata and controls
126 lines (112 loc) · 4.06 KB
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
var browserify = require('../');
var path = require('path');
var spawn = require('child_process').spawn;
var parseShell = require('shell-quote').parse;
var duplexer = require('duplexer');
module.exports = function (args) {
var argv = require('optimist')(args)
.boolean(['deps','pack','ig','dg', 'im', 'd','list'])
.string(['s'])
.alias('insert-globals', 'ig')
.alias('detect-globals', 'dg')
.alias('ignore-missing', 'im')
.alias('debug', 'd')
.alias('standalone', 's')
.alias('ig', 'fast')
.alias('noparse', 'noParse')
.default('ig', false)
.default('im', false)
.default('dg', true)
.default('d', false)
.argv
;
var entries = argv._.concat(argv.e).concat(argv.entry)
.filter(Boolean).map(function(entry) {
return path.resolve(process.cwd(), entry);
});
if (argv.s && entries.length === 0
&& [].concat(argv.r, argv.require).filter(Boolean).length === 1) {
entries.push([].concat(argv.r, argv.require).filter(Boolean)[0]);
argv.r = argv.require = [];
}
var b = browserify({
noParse: [].concat(argv.noparse).filter(Boolean),
extensions: [].concat(argv.extension).filter(Boolean),
entries: entries
});
b.argv = argv;
[].concat(argv.i).concat(argv.ignore).filter(Boolean)
.forEach(function (i) { b.ignore(i) })
;
[].concat(argv.r).concat(argv.require).filter(Boolean)
.forEach(function (r) {
var xs = r.split(':');
b.require(xs[0], { expose: xs.length === 1 ? xs[0] : xs[1] })
})
;
// resolve any external files and add them to the bundle as externals
[].concat(argv.x).concat(argv.external).filter(Boolean)
.forEach(function (x) {
if (/^[\/.]/.test(x)) b.external(path.resolve(process.cwd(), x))
else b.external(x)
})
;
[].concat(argv.t).concat(argv.transform).filter(Boolean)
.forEach(function (t) { b.transform(t) })
;
[].concat(argv.c).concat(argv.command).filter(Boolean)
.forEach(function (c) {
var cmd = parseShell(c);
b.transform(function (file) {
var env = Object.keys(process.env).reduce(function (acc, key) {
acc[key] = process.env[key];
return acc;
}, {});
env.FILENAME = file;
var ps = spawn(cmd[0], cmd.slice(1), { env: env });
var error = '';
ps.stderr.on('data', function (buf) { error += buf });
ps.on('exit', function (code) {
if (code === 0) return;
console.error([
'error running source transform command: ' + c,
error.split('\n').join('\n '),
''
].join('\n'));
process.exit(1);
});
return duplexer(ps.stdin, ps.stdout);
});
})
;
if (argv.standalone === true) {
process.nextTick(function () {
b.emit('error', '--standalone requires an export name argument');
});
return b;
}
var bundleOpts = {
detectGlobals: argv['detect-globals'] !== false && argv.dg !== false,
insertGlobals: argv['insert-globals'] || argv.ig,
ignoreMissing: argv['ignore-missing'] || argv.im,
debug: argv['debug'] || argv.d,
standalone: argv['standalone'] || argv.s
};
var bundle = b.bundle;
b.bundle = function (opts, cb) {
if (!opts) opts = {};
if (typeof opts === 'function') { cb = opts; opts = {} };
var bopts = copy(bundleOpts);
Object.keys(opts).forEach(function (key) {
bopts[key] = opts[key];
});
return bundle.call(b, bopts, cb);
};
return b;
};
function copy (obj) {
return Object.keys(obj).reduce(function (acc, key) {
acc[key] = obj[key];
return acc;
}, {});
}