-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
132 lines (102 loc) · 3.37 KB
/
index.js
File metadata and controls
132 lines (102 loc) · 3.37 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
'use strict';
var fs = require('fs-extra');
var path = require('path');
var Plugin = require('broccoli-plugin');
var crypto = require('crypto')
var debug = require('debug')('broccoli-config-replace');
// inlined from broccoli-kitchen-sink-helpers
function hashStrings (strings) {
var joinedStrings = strings.join('\x00')
return crypto.createHash('md5').update(new Buffer(joinedStrings, 'utf8')).digest('hex')
}
function ConfigReplace(inputNode, configNode, options) {
options = options || {};
// this._super();
Plugin.call(this, [inputNode, configNode], {
annotation: options.annotation
});
this._persistentOutput = true;
this.options = options;
this._cache = {};
}
ConfigReplace.prototype = Object.create(Plugin.prototype);
ConfigReplace.prototype.constructor = ConfigReplace;
ConfigReplace.prototype.build = function () {
var config = this.getConfig();
this.options.files.forEach(function(file) {
var key = this.deriveCacheKey(file);
var entry = this._cache[key.hash];
var contents, filePath;
debug('cache hit: %o, for: %s', !!entry, file);
if (entry) { return; }
filePath = file;
contents = this.processFile(config, filePath);
this.updateCache(key, contents);
filePath = path.join(this.outputPath, filePath);
this.writeFile(filePath, contents);
}, this);
};
ConfigReplace.prototype.deriveCacheKey = function(file) {
var configStat = fs.statSync(this.getConfigPath());
var fileStat = fs.statSync(this.inputPaths[0] + '/' + file);
if (configStat.isDirectory()) {
throw new Error('Must provide a path for the config file, you supplied a directory');
}
var patterns = this.options.patterns.reduce(function(a, b) {
return a + b.match;
}, '');
return {
file: file,
hash: hashStrings([
file,
this.configPath,
patterns,
configStat.size,
configStat.mode,
configStat.mtime.getTime(),
fileStat.size,
fileStat.mode,
fileStat.mtime.getTime(),
])
};
};
ConfigReplace.prototype.processFile = function(config, filePath) {
filePath = path.join(this.inputPaths[0], filePath);
var contents = fs.readFileSync(filePath, { encoding: 'utf8' });
this.options.patterns.forEach(function(pattern) {
var replacement = pattern.replacement;
if (typeof replacement === 'function') {
replacement = function() {
var args = Array.prototype.slice.call(arguments);
return pattern.replacement.apply(null, [config].concat(args));
}
}
contents = contents.replace(pattern.match, replacement);
});
return contents;
};
ConfigReplace.prototype.writeFile = function(destPath, contents) {
if (!fs.existsSync(path.dirname(destPath))) {
fs.mkdirpSync(path.dirname(destPath));
}
fs.writeFileSync(destPath, contents, { encoding: 'utf8' });
};
ConfigReplace.prototype.updateCache = function(key, contents) {
Object.keys(this._cache).forEach(function(hash) {
if (this._cache[hash].file === key.file) {
delete this._cache[hash];
}
}, this);
this._cache[key.hash] = {
file: key.file,
contents: contents
};
};
ConfigReplace.prototype.getConfigPath = function() {
return path.join(this.inputPaths[1], this.options.configPath);
};
ConfigReplace.prototype.getConfig = function() {
var contents = fs.readFileSync(this.getConfigPath(), { encoding: 'utf8' });
return JSON.parse(contents);
};
module.exports = ConfigReplace;