-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (56 loc) · 1.95 KB
/
index.js
File metadata and controls
60 lines (56 loc) · 1.95 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
'use strict';
var through = require('through2');
var fs = require('fs');
var rs = require('replacestream');
var _p = require('path');
function normalizeKey(json) {
var o = {};
for(var key in json) o[_p.normalize(key)] = json[key];
json = null;
return o;
}
function jReplacer(options) {
var json;
if (typeof options.src == 'string') {
json = JSON.parse(fs.readFileSync(options.src, 'utf8'));
} else if (typeof options.src == 'object') {
json = options.src;
}
json = normalizeKey(json);
var isLoose = options.mode === 'loose', matcher;
// identify is a prefix string
var identify = options.identify || '%%';
var stream = through.obj(function(file, enc, cb) {
// get the file path
var path = file.path, search, replacement;
if(file.isNull()) {
return cb(null, file);
}
for(var key in json) {
matcher = key;
if (isLoose) matcher = matcher.substr(0, matcher.lastIndexOf('.'));
if(path.indexOf(matcher) != -1) {
for(var i in json[key]) {
search = identify + i;
replacement = json[key][i];
// handle stream
if(file.isStream()) {
file.contents = file.contents.pipe(rs(search, replacement));
}
// handle buffer
if(file.isBuffer()) {
if(search instanceof RegExp) {
file.contents = new Buffer(String(file.contents).replace(search, replacement));
} else {
var chunk = String(file.contents).split(search);
file.contents = new Buffer(chunk.join(replacement));
}
}
}
}
}
return cb(null, file);
});
return stream;
}
module.exports = jReplacer;