let env = require('./env.js')

const {PLATFORM} = env

const fs = require("fs");
//此处如果是用HBuilderX创建的项目manifest.json文件在项目跟目录，如果是 cli 创建的则在 src 下，这里要注意
//process.env.UNI_INPUT_DIR为项目所在的绝对路径，经测试，相对路径会找不到文件
const revert = process.argv[process.argv.length - 1];
// 根据还原参数区分使用原路径(最后执行命令的时候已经没有UNI全局变量了)
const root = revert == "revert" ? "src" : process.env.UNI_INPUT_DIR;

const manifestPath = root + "/manifest.json";
let Manifest = fs.readFileSync(manifestPath, {
  encoding: "utf-8",
});

// console.log(Manifest,'ManifestManifest')

function replaceManifest(path, value) {
  const arr = path.split(".");
  const len = arr.length;
  const lastItem = arr[len - 1];

  let i = 0;
  let ManifestArr = Manifest.split(/\n/);

  for (let index = 0; index < ManifestArr.length; index++) {
    const item = ManifestArr[index];
    if (new RegExp(`"${arr[i]}"`).test(item)) ++i;
    if (i === len) {
      const hasComma = /,/.test(item);
      ManifestArr[index] = item.replace(
        new RegExp(`"${lastItem}"[\\s\\S]*:[\\s\\S]*`),
        `"${lastItem}" : ${value}${hasComma ? "," : ""}`
      );
      break;
    }
  }

  Manifest = ManifestArr.join("\n");
}

// 动态配置appid
if (PLATFORM === "enterprises") {
  // 企事业
  let title = '"御河调度"'
  replaceManifest("name", title);
  replaceManifest("h5.router.base", '"/mobile/"');
  replaceManifest("h5.title", title);
  
} else if (PLATFORM === "sanitation") {
  // 环卫
   let title = '"智慧环卫一体化管理平台"'
  replaceManifest("name", title);
  replaceManifest("h5.router.base", '"/mobile/"');
  replaceManifest("h5.title", title);
  
} else if (PLATFORM === "mine") {
	  // 矿山
	let title = '"矿山车辆管理平台"'
	replaceManifest("name", '"矿山车辆管理平台"');
	replaceManifest("h5.router.base", '"/mobileMine/"');
	replaceManifest("h5.title", title);
}

// console.log(Manifest,'Manifest')

fs.writeFileSync(manifestPath, Manifest, {
  flag: "w",
});




