forked from Pictela/gulp-token-replace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
99 lines (90 loc) · 3.05 KB
/
index.js
File metadata and controls
99 lines (90 loc) · 3.05 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
'use strict';
var concat = require('concat-stream');
var through = require('through2');
var PluginError = require('plugin-error');
module.exports = function(options) {
options = injectDefaultOptions(options);
return through.obj(function(file, encoding, callback) {
try {
if (file.isNull()) {
return callback(null, file);
} else if (file.isStream()) {
file.contents.pipe(concat(function(data) {
file.contents = Buffer.from(replace(String(data), options));
return callback(null, file);
}));
} else if (file.isBuffer()) {
file.contents = Buffer.from(replace(String(file.contents), options));
return callback(null, file);
} else {
return callback(new PluginError('gulp-token-replace', new Error('unknown type of file')));
}
} catch (e) {
return callback(new PluginError('gulp-token-replace', e));
}
});
};
function injectDefaultOptions(options) {
options = options || {};
options.prefix = options.prefix || '{{';
options.suffix = options.suffix || '}}';
options.tokens = options.tokens || options.global || {};
options.preserveUnknownTokens = options.preserveUnknownTokens || false;
options.delimiter = options.delimiter || '.';
return options;
}
function replace(text, options) {
options = injectDefaultOptions(options);
var includeRegExp = new RegExp(escapeRegExp(options.prefix) + "(.+?)" + escapeRegExp(options.suffix), "g");
var retVal = text;
var regExpResult;
while (regExpResult = includeRegExp.exec(text)) {
var arrayDetected = false;
var arrayItemId = -1;
var fullMatch = regExpResult[0];
var tokenName = regExpResult[1];
if (tokenName.indexOf('[') > 0) {
arrayItemId = tokenName.toString().split('[')[1].split(']')[0];
tokenName = tokenName.toString().split('[')[0];
}
var tokenValue = getTokenValue(options.tokens, tokenName, options.delimiter);
if (tokenValue === null) {
if (options.preserveUnknownTokens === "error") {
throw new Error(`Undefined token: ${options.prefix}${tokenName}${options.suffix}`);
}
if (!options.preserveUnknownTokens) {
tokenValue = '';
}
}
if (tokenValue !== null) {
if (typeof tokenValue == 'object') {
if (Array.isArray(tokenValue)) {
if (arrayItemId > -1) {
tokenValue = tokenValue[arrayItemId];
} else {
tokenValue = JSON.stringify(tokenValue).split(',');
}
} else {
tokenValue = JSON.stringify(tokenValue);
}
}
retVal = retVal.replace(fullMatch, tokenValue);
}
}
return retVal;
}
function escapeRegExp(text) {
return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
function getTokenValue(tokens, tokenName, delimiter) {
var tmpTokens = tokens;
var tokenNameParts = tokenName.split(delimiter);
for (var i = 0; i < tokenNameParts.length; i++) {
if (tmpTokens.hasOwnProperty(tokenNameParts[i])) {
tmpTokens = tmpTokens[tokenNameParts[i]];
} else {
return null;
}
}
return tmpTokens;
}