Node.js實現(xiàn)批量替換文件內(nèi)容示例
更新時間:2023年08月08日 14:10:13 作者:郝同學(xué)1208
這篇文章主要為大家介紹了Node.js實現(xiàn)批量替換文件內(nèi)容示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
文章序
將國際化文件中key: value形式改為value: value形式
代碼
main.js
const fs = require('fs');
// const args = process.argv;
// console.log(args);
let i18nKeyMap = {};
// const dictnoryMap = new Map([
// ["components", true],
// ["hooks", true],
// ["pages", true],
// ["store", true],
// ["utils", true]
// ])
try {
let fileStr = fs.readFileSync('./source/basic/locale/lang/en.js', 'utf-8');
// let fileStr = fs.readFileSync('./source/track/locale/lang/en.js', 'utf-8');
// let fileStr = fs.readFileSync('./source/emap/locale/lang/en.js', 'utf-8');
fileStr = fileStr.replaceAll(/[\n\r]/g, "").replaceAll(/`/g, "\"").replaceAll(/\/\/[^"'{]*/g, "");
const leftIndex = fileStr.indexOf('{');
const rightIndex = fileStr.lastIndexOf('}');
let innerStr = fileStr.substring(leftIndex + 1, rightIndex).replaceAll(/{/g, "[").replaceAll(/}/g, "]");
const objStr = "{" + (innerStr[innerStr.length - 1] === "," ?
innerStr.substring(0, innerStr.length - 1) :
innerStr) + "}";
i18nKeyMap = JSON.parse(objStr)
} catch (err) {
console.log("handle i18n error, the path is: src/locale/lang/en.js");
console.log("handle i18n error, the reason is: " + err + "\n");
}
handleWorkReplace('./source/basic');
handleDictReplace('./source/basic/locale/lang')
// handleWorkReplace('./source/track');
// handleDictReplace('./source/track/locale/lang')
// handleWorkReplace('./source/emap');
// handleDictReplace('./source/emap/locale/lang')
console.log("執(zhí)行完畢!")
function handleDictReplace(path) {
try {
let fileList = fs.readdirSync(path);
for (let i = 0; i < fileList.length; i++) {
replaceDictFile(path + '/' + fileList[i]);
}
} catch (err) {
console.log("handle i18n error, the path is: " + path);
console.log("handle i18n error, the reason is: " + err + "\n");
}
}
function handleWorkReplace(path) {
try {
let fileList = fs.readdirSync(path);
let item = null;
for (let i = 0; i < fileList.length; i++) {
item = fileList[i];
const nameList = item.split(".");
// if (item.indexOf(".") > -1) {
if (nameList[nameList.length - 1] === "vue" || nameList[nameList.length - 1] === "js") {
replaceWorkFile(path + '/' + item);
} else if(item.indexOf(".") === -1) {
recursionFun(path + "/" + item);
}
// } else if (dictnoryMap.has(item)) {
// recursionFun(path + "/" + item);
// }
}
} catch (err) {
console.log("handle i18n error, the path is: " + path);
console.log("handle i18n error, the reason is: " + err + "\n");
}
}
function recursionFun(path) {
try {
let item = null;
let fileList = fs.readdirSync(path);
for (let i = 0; i < fileList.length; i++) {
item = fileList[i];
const nameList = item.split(".");
// if (item.indexOf(".") === -1) {
// recursionFun(path + "/" + item);
// } else {
// replaceWorkFile(path + '/' + item);
// }
if (nameList[nameList.length - 1] === "vue" || nameList[nameList.length - 1] === "js") {
replaceWorkFile(path + '/' + item);
} else if(item.indexOf(".") === -1) {
recursionFun(path + "/" + item);
}
}
} catch (err) {
console.log("handle i18n error, the path is: " + path);
console.log("handle i18n error, the reason is: " + err + "\n");
}
}
function replaceDictFile(path) {
try {
let fileStr = fs.readFileSync(path, 'utf-8');
for (let key in i18nKeyMap) {
//language不做處理
if (key === "language") continue;
const leftIndex = fileStr.indexOf(key);
fileStr = fileStr.substring(0, leftIndex) + i18nKeyMap[key] + fileStr.substring(leftIndex + key.length);
}
fileStr = fileStr.replaceAll(/\[/g, "{").replaceAll(/\]/g, "}");
fs.writeFileSync(path, fileStr);
} catch (err) {
console.log("handle i18n error, the path is: " + path);
console.log("handle i18n error, the reason is: " + err + "\n");
}
}
function replaceWorkFile(path) {
try {
let fileStr = fs.readFileSync(path, 'utf-8');
let writeStream = "";
let leftIndex = 0;
let rightIndex = 0;
for (let i = 0; i < fileStr.length; i++) {
let preChar = fileStr[i - 1];
let curChar = fileStr[i];
let nextChar = fileStr[i + 1];
let furtherChar = fileStr[i + 2];
if (curChar === "t" && nextChar === "(" && !/[a-zA-Z0-9]/.test(preChar) && /["'`]/.test(furtherChar)) {
leftIndex = i + 3;
for (let j = i + 2; j < fileStr.length; j++) {
if (/[[${(]/.test(fileStr[j])) {
console.log("handle i18n error, the path is: " + path);
console.log("handle i18n error, A part of the line is: " + fileStr.substr(j - 10, 30) + "\n" + "\n");
for (let k = j + 1; k < fileStr.length; k++) {
if (fileStr[k + 1] === ")" && /["'`]/.test(fileStr[k])) {
writeStream += fileStr.substring(i, k + 2);
i = k + 1;
break;
}
}
break;
}
if (fileStr[j + 1] === ")" && /["'`]/.test(fileStr[j])) {
rightIndex = j;
const replaceStr = i18nKeyMap[fileStr.substring(leftIndex, rightIndex)];
if (!replaceStr) {
console.log("handle i18n error, the path is: " + path);
console.log("handle i18n error, the reason is: " + fileStr.substring(leftIndex, rightIndex) + " has no key in en.js!" + "\n");
writeStream += fileStr.substring(i, j + 2);
i = j + 1;
break;
}
writeStream += "t(`" + replaceStr + "`)"
i = j + 1;
break;
}
}
} else {
writeStream += fileStr[i];
}
}
fs.writeFileSync(path, writeStream);
} catch (err) {
console.log("handle i18n error, the path is: " + path);
console.log("handle i18n error, the reason is: " + err + "\n");
}
}以上就是Node.js實現(xiàn)批量替換文件內(nèi)容示例的詳細內(nèi)容,更多關(guān)于Node.js批量替換文件內(nèi)容的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
從零開始學(xué)習(xí)Node.js系列教程三:圖片上傳和顯示方法示例
這篇文章主要介紹了Node.js圖片上傳和顯示方法,結(jié)合實例形式分析了nodejs基于http傳輸圖片文件及顯示圖片的相關(guān)實現(xiàn)步驟與操作技巧,需要的朋友可以參考下2017-04-04
Electron調(diào)用外接攝像頭并拍照上傳實現(xiàn)詳解
這篇文章主要為大家介紹了Electron調(diào)用外接攝像頭并拍照上傳實例實現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-02-02
Linux環(huán)境部署node服務(wù)并啟動詳細步驟
最近用node.js開發(fā)了一個web項目,下面這篇文章主要給大家介紹了關(guān)于Linux環(huán)境部署node服務(wù)并啟動的詳細步驟,文中通過圖文以及示例代碼介紹的非常詳細,需要的朋友可以參考下2023-05-05
Node.JS段點續(xù)傳:Nginx配置文件分段下載功能的實現(xiàn)方法
在Node.JS中可以配置這個標(biāo)簽來實現(xiàn)文件的分段下載。這篇文章給大家介紹了Node.JS段點續(xù)傳:Nginx配置文件分段下載功能的實現(xiàn)方法,需要的朋友參考下吧2018-03-03
基于Koa(nodejs框架)對json文件進行增刪改查的示例代碼
這篇文章主要介紹了基于Koa(nodejs框架)對json文件進行增刪改查的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02

