Vue工程打包后重新修改BaseUrl(Revise BaseUrl after Vue project packaging)

在实际工程中遇到一种情况,需要在打包后修改app的名称即BaseUrl。所以写了一个脚本进行实现。

本来打包时设置的BaseUrl=apps001,访问时的路径为http://ip:port/apps001
现在需要把路由地址修改为apps002,也就是在访问的时候访问http://ip:port/apps002。为实现这个目标特此编写该脚本。

The original ‘BaseUrl=apps001’ was set during packaging, and the path for accessing it ishttp://ip:port/apps001.
Now we need to modify the routing address to ‘apps002’, which means accessing it during accesshttp://ip:port/apps002. To achieve this goal, this script is hereby written.

具体代码如下:

const fs = require("fs");
const path = require("path");
const folderName = path.resolve("dist");
const ignoreFlag = "// -- RENAME IGNORE FLAGS --";
let oldAppName;
let newAppName;
function distForderExists(path) {
  try {
    if (!fs.existsSync(path)) {
      return false;
    } else {
      console.log(`  ···${folderName} folder exists`);
      return true;
    }
  } catch (err) {
    console.error(err);
    return false;
  }
}
function replaceInFile(filePath) {
  let data = fs.readFileSync(filePath, { encoding: "utf8" });
  if (data.indexOf(ignoreFlag) > -1) {
    // console.log(`  ··· File ${filePath} was ignored ···`);
    process.stdout.write(`  ··· File ${filePath} was ignored ··· \r`);
    return;
  }
  var result = data.replace(oldAppName, newAppName);

  fs.writeFileSync(filePath, result);
  // console.log(`  ··· File ${filePath} replace end ···`);
  process.stdout.write(`  ··· File ${filePath} replace end ·· \r`);
}
/**
 * Find all files inside a dir, recursively.
 * @function getAllFiles
 * @param  {string} dir Dir path string.
 * @return {string[]} Array with all file names that are inside the directory.
 */
const getAllFiles = (dir) =>
  fs.readdirSync(dir).reduce((files, file) => {
    const name = path.join(dir, file);
    const isDirectory = fs.statSync(name).isDirectory();
    return isDirectory ? [...files, ...getAllFiles(name)] : [...files, name];
  }, []);
function replaceInDirectory(dirPath) {
  const extNames = [".js", ".html", ".css"];
  console.log(`  ···Replacing dir: ${dirPath}`);
  const files = getAllFiles(dirPath);
  for (let index = 0; index < files.length; index++) {
    const file = files[index];
    if (extNames.indexOf(path.extname(file)) === -1) continue;
    process.stdout.write(`  ···Replacing ${file} \r`);
    replaceInFile(file);
  }
}
// //
console.log("··· Rename start ···");
console.log(process.argv);
if (!distForderExists(folderName)) return false;
if (process.argv.length !== 4) {
  console.warn("!!!■ 参数必须指定原App名称和新App名称。");
  console.warn("!!!■ 例如: npm run renameApp apps-old apps-new");
  return false;
}
oldAppName = String(process.argv.slice(2, 3));
newAppName = String(process.argv.slice(3, 4));
console.log(`  ···Old AppName: '${oldAppName}'`);
console.log(`  ···New AppName: '${newAppName}'`);
const newAppPathAbs = path.resolve(newAppName);
console.log(`  ···New AppPath: '${newAppPathAbs}'`);
console.log(`  ···Remove folder: '${newAppPathAbs}'`);
fs.existsSync(newAppPathAbs) && fs.rmSync(newAppPathAbs, { recursive: true, force: true });
console.log(`  ···Copy ${folderName} to folder: '${newAppPathAbs}'`);
fs.cpSync(folderName, newAppPathAbs, { recursive: true });
console.log(`  ···Replace all appNames: '${newAppPathAbs}'`);
replaceInDirectory(newAppPathAbs);
console.log("··· Rename end ···");

调用:

node renameApp oldAppName newAppName
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

丷丩

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

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

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

打赏作者

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

抵扣说明:

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

余额充值