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("文件替换完成");
}
// 文件夹移动
function moveFiles(srcFolderPath, destFolderPath, name) {
  // fs.moveSync(srcFolderPath, destFolderPath, { overwrite: true });
  try {
    //检查某个目录是否存在
    fs.exists(srcFolderPath, (exists) => {
      console.log(exists ? "Found " : "Not Found!");
      if (exists) {
        // windows下权限不去不能使用移动,需要先copy再删除
        fs.copySync(srcFolderPath, destFolderPath);
        fs.removeSync(srcFolderPath);
        console.log(name + " Folder moved successfully!");
      }
    });
  } catch (e) {
    //TODO handle the exception
    console.log(e);
  }
}

moveFiles(srcFolderPath, destFolderPath);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

给钱,谢谢!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值