需求是想把项目中本地资源文件改成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);