| 680 | } |
| 681 | |
| 682 | function runWatchify(config, handlers) { |
| 683 | var bOpts = config.bOpts || {}; |
| 684 | bOpts.debug = true; |
| 685 | |
| 686 | var tsifyOpts = config.tsifyOpts || {}; |
| 687 | var beforeBundle = config.beforeBundle || function() {}; |
| 688 | |
| 689 | var watcher = new EventEmitter(); |
| 690 | watcher.close = function () {}; |
| 691 | |
| 692 | var b = watchify(browserify(watchify.args)); |
| 693 | b._watcher = function () { return watcher; }; |
| 694 | b.plugin(tsify, tsifyOpts); |
| 695 | beforeBundle(b); |
| 696 | |
| 697 | b.on('update', rebundle); |
| 698 | rebundle(); |
| 699 | |
| 700 | function rebundle() { |
| 701 | var calledBack = false; |
| 702 | var errors = []; |
| 703 | |
| 704 | b.bundle() |
| 705 | .on('error', function (error) { |
| 706 | errors.push(error); |
| 707 | }) |
| 708 | .pipe(es.wait(function (err, actual) { |
| 709 | if (calledBack) |
| 710 | throw new Error('Bundling completed after build timeout expired'); |
| 711 | calledBack = true; |
| 712 | callNextCallback(errors, actual.toString()); |
| 713 | })); |
| 714 | |
| 715 | setTimeout(function () { |
| 716 | if (calledBack) |
| 717 | return; |
| 718 | calledBack = true; |
| 719 | callNextCallback(errors, null); |
| 720 | }, buildTimeout); |
| 721 | } |
| 722 | |
| 723 | function callNextCallback(errors, actual) { |
| 724 | // hack to wait for Watchify to finish adding any outstanding watchers |
| 725 | setTimeout(function () { |
| 726 | var cb = handlers.shift(); |
| 727 | cb(errors, actual, triggerChange, b); |
| 728 | }, 100); |
| 729 | } |
| 730 | |
| 731 | function triggerChange() { |
| 732 | watcher.emit('change'); |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | function expectNoErrors(t, errors) { |
| 737 | t.deepEqual(errors, [], 'Should have no compilation errors'); |