最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Node.js實(shí)現(xiàn)批量替換文件內(nèi)容示例

 更新時(shí)間:2023年08月08日 14:10:13   作者:郝同學(xué)1208  
這篇文章主要為大家介紹了Node.js實(shí)現(xiàn)批量替換文件內(nèi)容示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

文章序

將國(guó)際化文件中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實(shí)現(xiàn)批量替換文件內(nèi)容示例的詳細(xì)內(nèi)容,更多關(guān)于Node.js批量替換文件內(nèi)容的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 關(guān)于express與koa的使用對(duì)比詳解

    關(guān)于express與koa的使用對(duì)比詳解

    很多人都在問(wèn)到底該用Koa還是express,所以下面這篇文章就來(lái)給大家再次的對(duì)比了關(guān)于express與koa的相關(guān)資料,通過(guò)對(duì)比大家可以更好的進(jìn)行選擇,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • Node中fs文件系統(tǒng)模塊的使用方法詳解

    Node中fs文件系統(tǒng)模塊的使用方法詳解

    fs?模塊是?Node.js?官方提供的、用來(lái)操作文件的模塊(內(nèi)置api),它提供了一系列的方法和屬性,用來(lái)滿足用戶對(duì)文件的操作需求,本文給大家介紹了Node中fs文件系統(tǒng)模塊的使用方法,需要的朋友可以參考下
    2024-03-03
  • Node爬取大批量文件的方法示例

    Node爬取大批量文件的方法示例

    這篇文章主要介紹了Node爬取大批量文件的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • 管理nodejs版本工具nvm詳解

    管理nodejs版本工具nvm詳解

    文章介紹了nvm(Node Version Manager)的安裝與使用,包括基礎(chǔ)命令如安裝、切換Node版本、設(shè)置鏡像源等,以及可能遇到的問(wèn)題解決方法
    2026-04-04
  • 學(xué)習(xí)使用ExpressJS 4.0中的新Router的用法

    學(xué)習(xí)使用ExpressJS 4.0中的新Router的用法

    ExpressJS 4.0中提出了新的路由Router,提供了路由應(yīng)有的API,本文詳細(xì)的介紹了ExpressJS 4.0中的新Router的用法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2018-11-11
  • better?sqlite3安裝node?gyp原生模塊編譯prebuild-install

    better?sqlite3安裝node?gyp原生模塊編譯prebuild-install

    這篇文章主要為大家介紹了Nodejs關(guān)于原生模塊編譯node-gyp?+?prebuild-install?(以安裝?better-sqlite3為例)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • Node.js與PHP、Python的字符處理性能對(duì)比

    Node.js與PHP、Python的字符處理性能對(duì)比

    因?yàn)楹罄m(xù)考慮實(shí)現(xiàn) Fl 引擎的Node.js版本,所以對(duì)比了下Node.js和PHP的字符處理性能。發(fā)現(xiàn)Node.js真是甩了PHP幾條街啊,再測(cè)試了下Python,比PHP還慢。
    2014-07-07
  • Node.js中DNS模塊學(xué)習(xí)總結(jié)

    Node.js中DNS模塊學(xué)習(xí)總結(jié)

    本篇文章給大家詳細(xì)介紹了Node.js中DNS模塊的相關(guān)知識(shí)點(diǎn),以及相關(guān)的實(shí)例代碼做了分享,有興趣的朋友參考下。
    2018-02-02
  • PHPStorm中如何對(duì)nodejs項(xiàng)目進(jìn)行單元測(cè)試詳解

    PHPStorm中如何對(duì)nodejs項(xiàng)目進(jìn)行單元測(cè)試詳解

    這篇文章主要給大家介紹了關(guān)于PHPStorm中如何對(duì)nodejs項(xiàng)目進(jìn)行單元測(cè)試的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • node.js中對(duì)Event Loop事件循環(huán)的理解與應(yīng)用實(shí)例分析

    node.js中對(duì)Event Loop事件循環(huán)的理解與應(yīng)用實(shí)例分析

    這篇文章主要介紹了node.js中對(duì)Event Loop事件循環(huán)的理解與應(yīng)用,結(jié)合實(shí)例形式分析了node.js中Event Loop事件循環(huán)相關(guān)原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下
    2020-02-02

最新評(píng)論

宕昌县| 张家口市| 三门峡市| 朝阳区| 榆社县| 青海省| 昌吉市| 乌兰县| 蓝田县| 敦煌市| 定西市| 鲜城| 太康县| 类乌齐县| 大庆市| 普兰店市| 池州市| 南华县| 泗阳县| 濉溪县| 汨罗市| 兰州市| 阿坝| 汪清县| 通州市| 友谊县| 镇康县| 绥德县| 墨玉县| 蕉岭县| 贵州省| 延寿县| 大新县| 安阳县| 交城县| 湖口县| 长治县| 中牟县| 花莲市| 宜城市| 临高县|