Repository navigation
Expand file tree
/
Copy path_duo
More file actions
executable file
·476 lines (389 loc) · 9.07 KB
/
Copy path_duo
File metadata and controls
executable file
·476 lines (389 loc) · 9.07 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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!/usr/bin/env node
/**
* Module dependencies.
*/
var detect = require('language-classifier');
var Command = require('commander').Command;
var exists = require('fs').existsSync;
var resolve = require('path').resolve;
var dirname = require('path').dirname;
var mkdirp = require('mkdirp').sync;
var Logger = require('stream-log');
var stat = require('fs').statSync;
var stdin = require('get-stdin');
var Watch = require('duo-watch');
var join = require('path').join;
var spawn = require('win-fork');
var pkg = require('../package');
var Batch = require('batch');
var stdout = process.stdout;
var cwd = process.cwd();
/**
* Logger.
*/
var logger = new Logger(process.stderr)
.type('building', '36m')
.type('built', '36m')
.type('installing', '36m')
.type('installed', '36m')
.type('finding', '36m')
.type('found', '36m')
.type('using', '36m');
/**
* Error.
*/
logger.type('error', '31m', function () {
logger.end();
process.exit(1);
});
/**
* Program.
*/
var program = new Command('duo')
.version(pkg.version)
.usage('[options] command|[file, ...] [out]')
.option('-c, --copy', 'opt to copy files instead of symlink', false)
.option('-C, --no-cac
8000
he', 'enable or disable the cache during build', true)
.option('-d, --development', 'include development dependencies.', false)
.option('-g, --global <name>', 'expose entry as a global <name>', null)
.option('-o, --output <dir>', 'set the output directory, defaulted to build/', null)
.option('-q, --quiet', 'only print to stderr when there is an error', false)
.option('-r, --root <dir>', 'root directory to build from.', null)
.option('-t, --type <type>', 'set the entry type', null)
.option('-u, --use <plugin>', 'use transform plugin', null)
.option('-v, --verbose', 'show as much logs as possible', false)
.option('-w, --watch', 'watch for changes and rebuild', false)
.option('-s, --standalone <standalone>', 'outputs standalone javascript umd <standalone>', '')
.parse(process.argv);
/**
* Help.
*/
program.on('--help', function () {
console.log(' Usage:');
console.log();
console.log(' # build in.js to out.js');
console.log(' $ duo in.js > out.js');
console.log();
console.log(' # build in.css to out.css');
console.log(' $ duo in.css > out.css');
console.log();
console.log(' # build all files to duo.assets() (default: build/)');
console.log(' $ duo *.{js,css}');
console.log();
console.log(' # build all files to the out/ folder');
console.log(' $ duo *.{js,css} out');
console.log();
console.log(' # build from stdin and output out.css');
console.log(' $ duo < in.css > out.css');
console.log();
console.log(' # build using a plugin');
console.log(' $ npm install duo-whitespace');
console.log(' $ duo --use duo-whitespace in.styl > out.css');
console.log();
console.log(' Commands:');
console.log();
console.log(' ls list all dependencies.');
console.log(' duplicates show all duplicates.');
console.log();
process.exit(0);
});
/**
* Language mapping.
*/
var langmap = {
javascript: 'js',
css: 'css'
};
/**
* Command.
*/
var command = program.args[0];
/**
* $PATH.
*/
var paths = [__dirname].concat(process.env.PATH.split(':'));
/**
* Bin.
*/
var bin = ['duo', command].join('-');
/**
* Quiet flag.
*/
var quiet = program.quiet;
/**
* Root.
*/
var root = findroot(program.root);
/**
* Asset path.
*/
var outdir = program.output || assets(program.args);
/**
* Watching.
*/
var watching = false;
/**
* Plugins.
*/
var plugins = usePlugins(program.use);
/**
* Custom executable.
*/
if (command && !isFile(command)) {
var args = process.argv.slice(3);
// find executable
var exec = paths.reduce(function (binary, path) {
path = resolve(path, bin);
return exists(path) && stat(path)
? path
: binary;
}, bin);
// does not exist
if (!exists(exec)) {
logger.error(bin + '(1) does not exist');
return;
}
// spawn
args.unshift(exec);
// proxy subcommands through gnode(1)
var gnode = require.resolve('gnode');
var bin = join(dirname(gnode), 'bin', 'gnode');
args.unshift(bin);
var proc = spawn('node', args, { stdio: 'inherit', customFds: [0, 1, 2] });
proc.on('close', process.exit.bind(process));
return;
}
/**
* Actions.
*/
if (command) build(program.args.filter(globs));
else if (!process.stdin.isTTY) input();
else program.help();
/**
* Accept standard input.
*/
function input() {
stdin(function (src) {
var type = program.type || langmap[detect(src)];
if (!type) logger.error('could not detect the file type');
var duo = create(root).entry(src, type);
duo.run(function (err, str) {
if (err) throw error(err);
logger.end();
stdout.write(str);
process.exit(0);
});
});
}
/**
* Build the entries.
*
* @param {Array} entries
*/
function build(entries) {
outdir && !program.output && entries.pop();
var len = entries.length;
return !len
? program.help()
: 1 == len && !outdir
? print(entries[0])
: write(entries);
}
/**
* Print to stdout.
*
* @param {String} entry
*/
function print(entry) {
create(entry).run(function (err, src) {
if (err) return error(err);
logger.end();
if (src) stdout.write(src);
// watch or exit
program.watch
? watch(print)
: process.exit(0);
});
}
/**
* Write the entries.
*
* @param {Array} entries
*/
function write(entries) {
entries = 'string' == typeof entries ? [entries] : entries;
var batch = new Batch;
var push = batch.push.bind(batch);
var duos = entries
.map(multiple)
.map(push);
batch.end(function (err) {
if (err) return error(err);
logger.end();
// watch or exit
program.watch
? watch(write)
: process.exit(0);
});
// write multiple entries to
// the directory `out`
function multiple(entry) {
return function (done) {
create(entry).write(done);
}
}
}
/**
* Create a duo instance.
*
* @param {entry} entry
* @return {Duo}
*/
function create(entry) {
var duo = Duo(root)
.entry(resolve(program.root || cwd, entry))
.development(!! program.development)
.copy(program.copy)
.cache(program.cache);
// standalone
var name = program.standalone;
name && duo.standalone(name);
// global
program.global && duo.global(program.global);
// verbose
if (program.verbose) {
duo.on('resolving', log('finding'));
duo.on('resolve', log('found'));
duo.on('installing', log('installing'));
}
// output dir
outdir && duo.buildTo(outdir);
program.out && duo.buildTo(program.out);
// use plugins
plugins.forEach(function (plugin) {
duo.use(plugin);
});
// events
if (!quiet) {
duo.on('install', log('installed'));
duo.on('running', log('building'));
duo.on('run', log('built'));
}
return duo;
}
/**
* Watch.
*
* @param {Function} action
*/
function watch(action) {
if (watching) return;
watching = true;
Watch(root).watch(action);
}
/**
* Log an event.
*
* @param {String} event
* @return {Function}
*/
function log (event) {
return function (pkg) {
pkg = pkg.slug ? pkg.slug() : pkg;
pkg = 'source.' + (program.type || 'js') == pkg ? 'from stdin' : pkg;
logger[event](pkg);
}
}
/**
* Error.
*/
function error(err) {
err = 'string' == typeof err ? new Error(err) : err;
logger.error(err.stack);
logger.end();
}
/**
* Find the root.
*
* @param {String} root
* @param {String}
*/
function findroot(root) {
if (root) return resolve(cwd, root);
var sep = require('path').sep;
var parts = cwd.split(sep);
var path = cwd;
while (!exists(join(path, 'component.json')) && parts.length > 1) {
parts.pop();
path = parts.join(sep);
}
return parts.length <= 1
? cwd
: path;
}
/**
* Check if `entries` contains an asset path.
*
* @param {Array} entries
* @return {String|Boolean}
*/
function assets(entries) {
if (!entries.length) return false;
var len = entries.length;
var last = entries[len - 1];
return !isFile(last) && last;
}
/**
* Filter out unexpanded globs.
*
* @param {String} entry
* @return {Boolean}
*/
function globs(path) {
return !/\*/.test(path);
}
/**
* Simple hueristic to check if `path` is a file.
*
* @param {String} path
* @return {Boolean}
*/
function isFile(path) {
if (/^[^\s]+\.\w*$/g.test(path)) return true;
try {
return stat(path).isFile();
} catch (e) {
return false;
}
}
/**
* Retrieve an array of plugins from `--use`.
*
* @param {String} plugins
* @return {Array}
*/
function usePlugins(use) {
if (!use) return [];
return use.split(",").map(function (plugin) {
try {
var local = resolve(root, plugin);
var npm = resolve(root, "node_modules", plugin);
var cwd = resolve(process.cwd(), "node_modules", plugin);
var mod;
if (exists(local)) mod = require(local);
else if (exists(npm)) mod = require(npm);
else mod = require(cwd);
!quiet && logger.using(plugin);
return mod();
} catch (e) {
error(e);
}
});
}
/**
* Lazy-load Duo
*/
function Duo() {
var duo = require('..');
return duo.apply(duo, arguments);
}