node 替换文本内容和移动文件夹

需求是想把项目中本地资源文件改成oss资源;然后把图片移动到指定文件夹,上传到oss

const path = require("path");
// const fs = require("fs");
const fs = require("fs-extra");
const inputPath = "../newFarm2/newFarmUni3/"; //项目打包地址
const imgUrl = "https://yjy.yiyiny.com/static/images/20221122/";

const srcFolderPath =
 inputPath + "dist/dev/mp-weixin/static/images/";  //原项目地址文件
const destFolderPath = "./uploadImages/static/images";//文件移动地址


//执行文本替换
modifyFile();

function modifyFile() {
  //配置要独立app文件地址
  let url = inputPath + "dist/dev/mp-weixin/common/assets.js";

  //读取要修改的项目文件
  let old1 = fs.readFileSync(url, "utf8");

  old1 = old1.replace(/\="\/static\//gi, '="' + imgUrl + "static/");
  old1 = old1.replace(
    /"\/pagesComm\/static\//gi,
    '"' + imgUrl + "pagesComm/static/"
  );
  old1 = old1.replace(
    /"\/pagesShop\/static\//gi,
    '"' + imgUrl + "pagesShop/static/"
  );
  old1 = old1.replace(
    /"\/pagesFarm\/static\//gi,
    '"' + imgUrl + "pagesFarm/static/"
  );
  old1 = old1.replace(
    /"\/pagesFarmMarket\/static\//gi,
    '"' + imgUrl + "pagesFarmMarket/static/"
  );
  old1 = old1.replace(
    /"\/pagesExtra\/static\//gi,
    '"' + imgUrl + "pagesExtra/static/"
  );

  //写入文件
  fs.writeFileSync(url, old1, "utf8");

  console.log("文件替换完成");
}
// 文件夹移动
// windows下权限不足不能使用移动,需要先copy再删除
function moveFiles(srcFolderPath, destFolderPath) {
  // fs.moveSync(srcFolderPath, destFolderPath, { overwrite: true });
  // console.log(name + "Folder moved successfully!");
  fs.copySync(srcFolderPath, destFolderPath);
  fs.removeSync(srcFolderPath);
  console.log( "Folder moved successfully!");
}
}

moveFiles(srcFolderPath, destFolderPath);

你可能感兴趣的:(node.js,js,前端)