-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathstub-generator.js
81 lines (63 loc) · 2.28 KB
/
stub-generator.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
var Plugin = require('broccoli-plugin');
var FSTree = require('fs-tree-diff');
var walkSync = require('walk-sync');
var Stubs = require('./stubs');
var fs = require('fs');
var md5Hex = require('md5-hex');
var debug = require('debug')('ember-browserify:stub-generator:');
module.exports = StubGenerator;
function StubGenerator(inputTree, options) {
if (!(this instanceof StubGenerator)) {
return new StubGenerator([inputTree], options);
}
if (Array.isArray(inputTree) || !inputTree) {
throw new Error('Expects one inputTree');
}
Plugin.call(this, [inputTree], options);
this._persistentOutput = true;
// setup persistent state
this._previousTree = new FSTree();
this.stubs = new Stubs();
this._fileToChecksumMap = {};
}
StubGenerator.prototype = Object.create(Plugin.prototype);
StubGenerator.prototype.constructor = StubGenerator;
StubGenerator.prototype.build = function() {
var start = Date.now();
var inputPath = this.inputPaths[0];
var previous = this._previousTree;
// get patchset
var input = walkSync.entries(inputPath, [ '**/*.js' ]);
debug('input: %d', input.length);
var next = this._previousTree = FSTree.fromEntries(input);
var patchset = previous.calculatePatch(next);
debug('patchset: %d', patchset.length);
var applyPatch = Date.now();
// process patchset
patchset.forEach(function(patch) {
var operation = patch[0];
var path = patch[1];
var fullPath = inputPath + '/' + path;
switch (operation) {
case 'unlink': this.stubs.delete(fullPath); break;
case 'create':
case 'change': this.stubs.set(fullPath, fs.readFileSync(fullPath)); break;
}
}, this);
debug('patched applied in: %dms', Date.now() - applyPatch);
// apply output
this.writeFileIfContentChanged(this.outputPath + '/browserify_stubs.js', this.stubs.toAMD());
debug('build in %dms', Date.now() - start);
};
StubGenerator.prototype.writeFileIfContentChanged = function(fullPath, content) {
var previous = this._fileToChecksumMap[fullPath];
var next = md5Hex(content);
if (previous === next) {
debug('cache hit, no change to: %s', fullPath);
// hit
} else {
debug('cache miss, write to: %s', fullPath);
fs.writeFileSync(fullPath, content);
this._fileToChecksumMap[fullPath] = next; // update map
}
};