Repository navigation
Expand file tree
/
Copy pathduo.js
More file actions
1155 lines (987 loc) · 25.1 KB
/
Copy pathduo.js
File metadata and controls
1155 lines (987 loc) · 25.1 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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Module dependencies.
*/
var Emitter = require('events').EventEmitter;
var clone = require('component-clone');
var stoj = require('duo-string-to-js');
var compat = require('duo-css-compat');
var parallel = require('co-parallel');
var Package = require('duo-package');
var debug = require('debug')('duo');
var filedeps = require('file-deps');
var Mapping = require('./mapping');
var exists = require('co-exists');
var extend = require('extend.js');
var fmt = require('util').format;
var parse = require('duo-parse');
var unyield = require('unyield');
var thunk = require('thunkify');
var Pack = require('duo-pack');
var main = require('duo-main');
var mkdir = require('mkdirp');
var Step = require('step.js');
var File = require('./file');
var fs = require('co-fs');
var co = require('co');
var cp = require('cp');
var path = require('path');
var basename = path.basename;
var relative = path.relative;
var extname = path.extname;
var dirname = path.dirname;
var resolve = path.resolve;
var join = path.join;
var slice = [].slice;
/**
* Thunks.
*/
mkdir = thunk(mkdir);
cp = thunk(cp);
/**
* Export `Duo`.
*/
module.exports = Duo;
/**
* Initialize a new `Duo` package with the path to the package's `root` folder.
*
* @param {String} root
* @return {Duo}
* @api public
*/
function Duo(root) {
if (!(this instanceof Duo)) return new Duo(root);
if (!root) throw error('duo requires a root directory');
Emitter.call(this);
this.root(root);
this.manifest('component.json');
this.installTo('components');
this.buildTo('build');
this.copy(false);
this.development(false);
this.concurrency(50);
this.cache(true);
this.assets = [];
this.mapping = {};
this.includes = {};
this.json = readJson(this.path('component.json'));
// plugins
this.plugins = new Step;
this.plugins.run = thunk(this.plugins.run);
}
/**
* Inherit from `Emitter`.
*/
Duo.prototype.__proto__ = Emitter.prototype;
/**
* Get or set the entry file for the package.
*
* You can either specify a single `path` to an entry file:
*
* duo.entry('index.js');
*
* Or you can pass a source string and a file type:
*
* duo.entry('var a = 0;', 'js');
*
* @param {String} path or source (optional)
* @param {String} type (optional)
* @return {Duo|File}
* @api public
*/
Duo.prototype.entry = function () {
switch (arguments.length) {
case 0:
return this._entry;
case 1:
this._entry = this.file({ path: arguments[0], entry: true });
return this;
case 2:
this._entry = this.file({ raw: arguments[0], type: arguments[1], entry: true });
return this;
}
};
/**
* Get or set the root `dir` for the package.
*
* @param {String} dir (optional)
* @return {Duo|String}
* @api public
*/
Duo.prototype.root = function (dir) {
if (!arguments.length) return this._root;
this._root = dir;
return this;
};
/**
* Get or set the `token` to authenticate to GitHub with.
*
* @param {String} token (optional)
* @return {Duo|String}
* @api public
*/
Duo.prototype.token = function (token) {
if (!arguments.length) return this._token;
this._token = token;
return this;
};
/**
* Get or set the manifest `name` to read settings from.
*
* @param {String} name
* @return {Duo|String}
* @api public
*/
Duo.prototype.manifest = function (name) {
if (!arguments.length) return this._manifest;
this._manifest = name;
return this;
};
/**
* Get or set a global variable `name` to expose the entry file by.
*
* @param {String} name (optional)
* @return {String|Duo}
* @api public
*/
Duo.prototype.global = function (name) {
if (!arguments.length) return this._global;
this._global = name;
return this;
};
/**
* Get or set standalone `name`, this will be used
* to create a standalone JS (umd).
*
* @param {String} name
* @return {String|Duo}
* @api public
*/
Duo.prototype.standalone = function(name){
if (!arguments.length) return this.umd;
this.umd = name;
return this;
};
/**
* Get or set whether to install development dependencies.
*
* @param {Boolean} value (optional)
* @return {Duo|Boolean}
* @api public
*/
Duo.prototype.development = function (value) {
if (!arguments.length) return this._development;
this._development = value;
return this;
};
/**
* Get or set whether to copy built files instead of symlinking them.
*
* @param {Boolean} value (optional)
* @return {Duo|Boolean}
* @api public
*/
Duo.prototype.copy = function (value) {
if (!arguments.length) return this._copy;
this._copy = value;
return this;
};
/**
* Get or set the download concurrency `value`.
*
* @param {Number} value (optional)
* @return {Duo|Number}
* @api public
*/
Duo.prototype.concurrency = function (value) {
if (!arguments.length) return this._concurrency;
this._concurrency = value;
return this;
};
/**
* Get or set whether the cache should be used
*
* @param {Boolean} value (optional)
* @return {Duo|Boolean}
* @api public
*/
Duo.prototype.cache = function (value) {
if (!arguments.length) return this._cache;
this._cache = value;
return this;
};
/**
* Get or set the `path` to install dependencies to.
*
* @param {String} path (optional)
* @return {Duo|String}
* @api public
*/
Duo.prototype.installTo = function (path) {
if (!arguments.length) return this._installTo;
this._installTo = path;
return this;
};
/**
* Get or set the `path` to build source to.
*
* @param {String} path (optional)
* @return {Duo|String}
* @api public
*/
Duo.prototype.buildTo = function (path) {
if (!arguments.length) return this._buildTo;
this._buildTo = path;
return this;
};
/**
* Use an async, sync or generator `plugin` function.
*
* @param {Function} plugin
* @return {Duo}
* @api public
*/
Duo.prototype.use = function (fn) {
if (~this.plugins.fns.indexOf(fn)) return this;
debug('using plugin: %s', fn.name || fn.toString());
this.plugins.use(fn);
return this;
};
/**
* Resolve `paths...` relative to the root directories.
*
* @param {String} paths...
* @return {String}
* @api public
*/
Duo.prototype.path = function () {
var strs = slice.call(arguments);
strs.unshift(this.root());
return path.join.apply(path, strs);
};
/**
* Resolve `paths...` relative to the build directory.
*
* @param {String} paths...
* @return {String}
* @api public
*/
Duo.prototype.buildPath = function () {
var strs = slice.call(arguments);
strs.unshift(this.buildTo());
return this.path.apply(this, strs);
};
/**
* Resolve `paths...` relative to the install directory.
*
* @param {String} paths...
* @return {String}
* @api public
*/
Duo.prototype.installPath = function () {
var strs = slice.call(arguments);
strs.unshift(this.installTo());
return this.path.apply(this, strs);
};
/**
* Include a runtime by `name` and `src`.
*
* @param {String} name
* @param {String} src
* @return {Duo}
* @api public
*/
Duo.prototype.include = function (name, src, type) {
this.includes[name] = {
id: name,
src: src,
entry: false,
include: true,
deps: includeDeps(src, type)
};
return this;
};
/**
* Check the includes hash for the given name
*
* @param {String} name
* @return {Boolean}
* @api public
*/
Duo.prototype.included = function (name) {
return name in this.includes;
};
/**
* Write the built source to a `path`, which defaults
* to the same name as the entry file's name.
*
* @param {String} path (optional)
* @param {Function} fn (optional)
* @return {Duo}
* @api public
*/
Duo.prototype.write = unyield(function *(path) {
var src = yield this.run();
var entry = this.entry();
var dir = this.buildPath();
var type = entry.type;
var rel = entry.id;
// resolve the path
path = resolve(dir, path || rel);
// change the extension if the type has changed
var base = basename(path, extname(path)) + '.' + type;
var dir = dirname(path);
// update the path
path = join(dir, base);
// write the file
yield mkdir(dir);
yield fs.writeFile(path, src);
return this;
});
/**
* Run duo on the entry file and return the built source.
*
* - Loads core duo plugins.
* - Map out the dependency grpah.
* - Add in the manually included files.
* - Write the assets to the `buildTo()` path.
* - Write out the map of the dependency graph to `components/duo.json`.
* - Build the source by passing the `mapping` into duo-pack.
*
* @param {Function} fn (optional)
* @return {String}
* @api public
*/
Duo.prototype.run = unyield(function *() {
if (!this.entry()) return '';
var path = this.installPath('duo.json');
var mapping = Mapping(path);
var global = this.global();
var file = this.entry();
var rel = file.id;
var opts = {};
// logging
this.emit('running', rel);
debug('running: %s', rel);
// idempotent across runs
this.assets = [];
// add core plugins
this
.use(compat())
.use(stoj());
// try to load duo.json mapping
if (this.cache()) this.mapping = yield mapping.read();
// ensure that the entry exists
if (!(yield file.exists())) {
throw error('cannot find entry: %s', rel);
}
// fetch the map of the dependency graph
var deps = yield this.dependencies(file);
// add "includes" to dependency mapping
deps = extend(deps, this.includes);
// add global
if (global) deps[rel].global = global;
// write out the asset files in parallel
yield this.parallel(this.assets);
// write out mapping
yield mkdir(this.installPath());
deps = yield mapping.update(deps);
// standalone?
if (this.standalone()) {
deps[rel].name = this.standalone();
opts.umd = true;
}
// pack the mapping
var pack = Pack(deps, opts);
if (this.development()) pack.development();
var src = pack.pack(rel);
// logging
this.emit('run', rel);
debug('ran: %s', rel);
return src;
});
/**
* Recursively transform, parse and fetch the dependencies of `file`.
* Returns a flat `map` of the full dependency graph.
*
* - `this.mapping` is a cache to only build files that have been changed.
* - `map` is a current snapshot of the dependency graph.
* - `map` gets built as we recurse.
*
* @param {File} file
* @param {Object} map
* @return {Object}
* @api private
*/
Duo.prototype.dependencies = function *(file, map) {
map = map || {};
var json = clone(this.mapping[file.id] || {});
var includes = this.includes;
var paths = [];
var gens = [];
// logging
debug('parsing: %s', file.id);
var isCached = file.mtime == json.mtime;
var isIncluded = json.include;
var isParsed = map[file.id];
// `file` already visited
if (isParsed) {
debug('%s, already parsed. ignoring', file.id);
return map;
}
// `file` included manually already
if (isIncluded) {
debug('%s, included manually. ignoring', file.id);
map[file.id] = json;
return map;
}
// check if the `file` has been modified
// if not, skip parsing and recurse its
// dependencies immediately.
if (this.cache() && isCached) {
debug('%s: has not been modified. skip parsing', file.id);
map[file.id] = json;
paths = values(json.deps);
gens = [];
// update the file with the cached
// dependency mapping
file.set(json);
// bundle if there is a non-supported file type
if (!supported(file.type)) this.assets.push(this.bundle(file.id));
// recurse the dependency's dependencies
var gens = this.recurse(paths, map);
yield this.parallel(gens);
return map;
}
// load the file, applying plugin transforms
yield file.load();
// not a supported file type, don't parse any farther
// but include in duo.json, for symlinking or copying
if (!supported(file.type)) {
this.assets.push(this.bundle(file.id));
delete file.attrs.src;
map[file.id] = file.json();
return map;
}
// parse content for dependencies
var deps = file.dependencies();
var depmap = {};
// logging
debug('%s deps: %j', file.id, deps);
// download and resolve the dependencies
for (var i = 0, dep; dep = deps[i++];) {
depmap[dep] = includes[dep] ? dep : this.dependency(dep, file, this.entry());
}
// resolve dependencies from entry files,
// and remove unresolved deps
depmap = compact(yield depmap);
paths = values(depmap);
// update the file with the resolved dependencies
file.set({ deps: depmap });
map[file.id] = file.json();
// recurse the dependency's dependencies
var gens = this.recurse(paths, map);
yield this.parallel(gens);
return map;
};
/**
* Create generators to recurse the next layer
* of dependencies in the dependency graph
*
* @param {Array} paths
* @param {Object} map
* @return {Array}
* @api private
*/
Duo.prototype.recurse = function (paths, map) {
var includes = this.includes;
var gens = [];
var path;
var file;
var root;
for (var i = 0, path; path = paths[i++];) {
if (includes[path]) continue;
path = this.path(path);
root = this.findRoot(path);
file = this.file({
root: root,
path: path
});
gens.push(this.dependencies(file, map));
}
return gens;
};
/**
* Resolve a single dependency's path. Supports local and remote dependencies.
* Fetches remote packages to components/ to resolve remote paths.
*
* Examples:
*
* 1. this.dependency('./one.js', file, entry)
* => lib/one/one.js
*
* 2. this.dependency('component/tip', file, entry)
* => components/component-tip@master/index.js
*
* 3. this.dependency('yields/skeleton', file, entry)
* => components/yields-skeleton@master/skeleton.css
*
* Where:
*
* `file` is the current file with the dependency `dep`.
* `entry` is the root file you passed into Duo.
*
* @param {String} dep
* @param {File} file
* @param {File} entry
* @return {String|Boolean}
* @api private
*/
Duo.prototype.dependency = function *(dep, file, entry) {
// ignore http dependencies
if (http(dep)) {
debug('%s: ignoring dependency "%s"', file.id, dep)
return false;
}
// `dep` is a local dependency
var local = yield this.resolve(dep, file, this.entry());
if (local) return relative(entry.root, local);
// `dep` is a remote dependency
var pkg = this.package(dep, file);
if (!pkg) {
debug('%s: cannot resolve "%s"', file.id, dep);
return false;
}
// install the dependency from github
// or another remote to 'components/'
yield pkg.fetch();
// resolve the remote dependency's path
var path = pkg.path(parse(dep).path || 'component.json');
var pkgfile = this.file({ root: pkg.path(), path: path });
var resolved = yield this.resolve(path, pkgfile, this.entry());
// logging
if (!resolved) debug('%s: cannot resolve "%s"', file.id, dep);
// return resolved or false
return resolved
? relative(entry.root, resolved)
: false;
};
/**
* Resolve the full local path of `dep`.
*
* TODO: clean & move this function into `duo-resolve`.
*
* Examples:
*
* 1. Local dependency:
*
* this.resolve('./one.js', file, entry)
* => lib/one/one.js
*
* 2. Remote dependency (after it's installed):
*
* this.resolve('$HOME/components/yields-skeleton@0.0.1/component.json', file, entry)
* => $HOME/components/yields-skeleton@0.0.1/skeleton.css
*
* Where:
*
* `file` is the current file with the dependency `dep`.
* `entry` is the root file you passed into Duo.
*
* @param {String} dep
* @param {String} root
* @return {String} path
* @api private
*/
Duo.prototype.resolve = function *(dep, file, entry) {
var isManifest = this.manifest() == basename(dep);
var ext = extension(dep) ? '' : '.' + entry.type;
var path = resolve(dirname(file.path), dep);
var isRelative = './' == dep.slice(0, 2);
var isParent = '..' == dep.slice(0, 2);
var isAbsolute = '/' == dep[0];
var ret;
if (isManifest) {
// `dep` is a component.json
var json = readJson(resolve(file.root, dep));
var entrypoint = main(json, entry.type) || 'index.' + entry.type;
ret = resolve(file.root, dirname(dep), entrypoint);
} else if (isAbsolute) {
// `dep` is an absolute path (relative to app or component root)
var relroot = this.findRoot(file.path);
ret = join(relroot, dep.replace(relroot, ''));
} else if (isParent) {
// `dep` is a parent path with ".."
ret = path;
} else if (isRelative) {
// `dep` is a relative path with "./"
ret = path;
} else if (yield isRelativeCSS(entry, path)) {
// Hack to support for CSS relative paths without "./"
// Example: body { background-image: url('image.jpg'); }
ret = path;
}
// Could not resolve
if (!ret) return;
// Strip any hashes, querystrings, etc from path.
// fonts/glyphicons-halflings-regular.eot?#iefix
ret = stripPath(ret);
// does one of these file paths exist?
//
// ./file.{{ext}}
// ./file/index.{{ext}}
return yield exists([
ret + ext,
join(ret, 'index' + ext)
]);
// check filesystem for relative asset
function *isRelativeCSS(entry, path) {
if ('css' != entry.type) return false;
path = stripPath(path);
if (!extension(path)) return false;
return yield exists(path);
}
};
/**
* Helper to create a file with `attrs`.
*
* @param {Object} attrs
* @return {File}
* @api private
*/
Duo.prototype.file = function (attrs) {
attrs.root = attrs.root || this.root();
return new File(attrs, this);
};
/**
* Helper to create a package from a `dep` with `file`.
*
* - Finds the necessary information to fetch `dep`
* - Adds in our github token
* - Sets up the events
*
* Examples:
*
* 1. "component/emitter": "0.1.0" in our manifest
*
* var pkg = this.package('emitter', file);
* pkg.slug(); => component/emitter@0.1.0
*
* 2. require('component/emitter@master:index.js')
*
* var pkg = this.package('component/emitter@master:index.js');
* pkg.slug(); => component/emitter@master
*
*
* @param {String} dep
* @param {File} file
* @return {Package|null}
* @api private
*/
Duo.prototype.package = function (dep, file) {
var manifest = readJson(join(file.root, this.manifest()));
var gh = this.findDependency(dep, manifest);
if (!gh) return null;
var token = this.token();
// initialize the package
var pkg = Package(gh.package, gh.ref);
pkg.directory(this.installPath());
if (token) pkg.token(token);
// forward package events to duo
pkg
.once('resolving', this.forward('resolving'))
.once('installing', this.forward('installing'))
.once('resolve', this.forward('resolve'))
.once('install', this.forward('install'));
return pkg;
};
/**
* Piece together the remote repository information from the dependency `dep`.
*
* - Consults the manifest for additional information, when needed.
* - Includes development dependencies in when `development()` is set
*
* Examples:
*
* 1. specific dependency. no `manifest`:
*
* this.findDependency('component/emitter', {})
* => {
* package: 'component/emitter',
* user: 'component',
* repo: 'emitter',
* ref: '*'
* }
*
* 2. vague dependency. "component/emitter": "0.1.0" is in the `manifest`:
*
* this.findDependency('emitter', manifest)
* => {
* package: 'component/emitter',
* user: 'component',
* repo: 'emitter',
* ref: '0.1.0'
* }
*
* 3. specific dependency. "component/emitter": "0.1.0" is in the `manifest`:
*
* this.findDependency('component/emitter@1.0.0', manifest)
* => {
* package: 'component/emitter',
* user: 'component',
* repo: 'emitter'
* ref: '0.1.0'
* }
*
* @param {String} dep
* @param {Object} manifest
* @return {Object}
* @api private
*/
Duo.prototype.findDependency = function(dep, manifest) {
var gh = parse(dep);
// We have all the information we need from `dep`.
// Set the `package` key and return.
if (gh.user && gh.repo && gh.ref) {
gh.package = gh.user + '/
AC91
' + gh.repo;
return gh;
}
var dev = this.development();
var deps = manifest.dependencies || {};
var rext = /([\.][a-z]+)?/;
// Include development dependencies if `development(true)` is set.
if (dev && manifest == this.json) {
deps = extend(deps, manifest.development || {});
}
// Build a regexp based on if we have the `user` key or not.
// If we have the `user` key, our search is more specific.
var re = gh.user
? new RegExp('^' + gh.user + '[\\/:]' + gh.repo + rext.source + '$', 'i')
: new RegExp('([\\/:])' + gh.repo + rext.source + '$', 'i');
// Not enough information yet.
// Search through the manifest for our dependency.
for (var dep in deps) {
if (re.test(dep) || dep.replace('/', '-') == gh.repo) {
gh.package = dep;
gh.ref = deps[dep];
return gh;
}
}
// We have the `user` and `repo` keys, but not the `ref`.
// Although vague, we have enough information to find our dependency.
// Set `ref` to `*` and add in our `package` key.
if (gh.user && gh.repo) {
gh.package = gh.user + '/' + gh.repo;
gh.ref = '*';
return gh;
}
// Not enough information to find the dependency.
return null;
};
/**
* Given a `path`, find the root.
* TODO: fix this for windows.
*
* - `root` is relative. It could be the project `root` or a component's `root`
*
* Examples:
*
* 1. Project root (app):
*
* this.findRoot('$HOME/app/lib/one/one.js')
* => $HOME/app
*
* 2. Component root
*
* this.findRoot('$HOME/app/components/component-reactive@1.2.0/lib/index.js')
* => $HOME/app/components/component-reactive@1.2.0
*
* @param {String} path
* @return {String} root
* @api private
*/
Duo.prototype.findRoot = function (path) {
var root = this.root();
while (path != '.' && path != root && !slug(path)) {
path = dirname(path);
}
if (path == '.') {
throw error('could not find root for %s', path);
}
return path;
function slug(path) {
return parse.slug.test(basename(path));
}
};
/**
* Copy over assets from a `path` relative to the assets path.
* This will symlink unless `duo.copy(true)` has been set.
*
* @param {String} path
* @return {Duo}
*/
Duo.prototype.bundle = function *(path) {
var log = this.copy() ? 'copying' : 'symlinking';
var action = this.copy() ? cp : fs.symlink;
var dest = this.buildPath(path);
var rm = fs.unlink;
// mkdir -p
yield mkdir(dirname(dest));
// fullpath
path = this.path(path);
// try removing any existing symlink or file first
try {
yield rm(dest);
} catch(e) {
if ('ENOENT' != e.code) throw e;
}
// symlink or copy the file
debug('%s: %s => %s', log, path, dest);
try {
yield action(path, dest);
} catch(e) {
if ('EEXIST' != e.code) throw e;
else debug('caught: %s already exists', dest);
}
return this;
};
/**
* Forward events with a `name`, adding the Duo instance as the final argument.
*
* @param {String} event
* @return {Function}
* @api private
*/
Duo.prototype.forward = function (name) {