zjffun blog

JS 批量简体转繁体

更新于 写于 前端JavaScript

安装依赖

bash
npm i simplebig

Node.js 代码

js
const fs = require("fs");
const path = require("path");
const S = require("simplebig");

// 要转换的文件夹
const dirPath = "./test";
// 要转换的文件的后缀
const extWhiteList = [".js", ".json", ".wxml", ".wxss"];

traverseDir(dirPath);

function traverseDir(dirPath) {
  fs.readdirSync(dirPath).forEach(function(file) {
    let filepath = path.join(dirPath, file);
    let stat = fs.statSync(filepath);
    if (stat.isDirectory()) {
      traverseDir(filepath);
    } else {
      if (~extWhiteList.indexOf(path.extname(file))) {
        let content = fs.readFileSync(filepath);
        fs.writeFileSync(filepath, S.s2t(content.toString()));
        console.log(filepath);
      }
    }
  });
}