我刚刚编写了一个小脚本来将.txt文件中的所有变量替换为JS文件中的值
示例:
Txt文件示例(值):
Hi = "HELLO WORLD",
Hey = /someregex/g,
Hh = 'haha';
脚本示例:
window[Hi] = true;
"someregex hi".replace(Hey, "")
window[Hh] = 1;
这是我的剧本:
var fs = require("fs")
var script = fs.readFileSync("./script.js", "utf8");
var vars = fs.readFileSync("./vars.txt", "utf8");
var replace = {}
var spl = vars.replace(/\r\n/g, "").replace(/ /g, "").split(",");
console.log("caching variables")
for(var dt of spl) {
var splt = dt.split(" = ");
var name = splt[0];
var val = splt[1];
if(!name || !val) {
continue;
}
if(val.endsWith(";")) {
val = val.slice(0, -1);
}
replace[name] = val;
}
console.log("Variables are in cache!")
console.log("Replacing variables in script")
var i = 1;
var t = Object.keys(replace).length;
for(var var_name in replace) {
var var_val = replace[var_name];
var regex = new RegExp(var_name, "g");
console.log(i, "/", t, "Replacing", var_name, "with", var_val, "regex", regex)
script = script.replace(regex, var_val);
i++;
}
console.log("DONE!")
fs.writeFileSync("./dec.js", script, "utf8")
然而,当i ~= 100
时,我有这个错误:
RangeError: Invalid string length
at RegExp.[Symbol.replace] (native)
at String.replace (native)
编辑:另外,我可以看到node.js进程正在使用~400MB的RAM,当它达到900MB时我有错误
怎么了?