index.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env node
  2. const fs = require("fs").promises;
  3. const path = require("path");
  4. const program = require("commander");
  5. const configFilePath = path.join(__dirname, "..", "project.config.json");
  6. const devFilePath = path.join(__dirname, "..", "config", "dev.js");
  7. const prodFilePath = path.join(__dirname, "..", "config", "prod.js");
  8. /**
  9. * 项目配置对象
  10. * 因现在惠融好借和惠融钱包的图片资源都是一样的,所以LOGO和HOME_LOGO暂时这部分代码暂时注释掉
  11. */
  12. const projectConfigs = {
  13. hrqb: {
  14. appId: "458",
  15. miniAppId: "wxc7ed88182aa77a68",
  16. LOGO: "https://comon-image.oss-cn-hangzhou.aliyuncs.com/miniPrograme/logo.png",
  17. HOME_LOGO:
  18. "https://comon-image.oss-cn-hangzhou.aliyuncs.com/miniPrograme/home_logo_hrqb.png",
  19. },
  20. hrhaojie: {
  21. appId: "472",
  22. miniAppId: "wxaca93174bcaa6453",
  23. LOGO: "https://comon-image.oss-cn-hangzhou.aliyuncs.com/miniPrograme/logo.png",
  24. HOME_LOGO:
  25. "https://comon-image.oss-cn-hangzhou.aliyuncs.com/miniPrograme/home_logo_hrqb.png",
  26. },
  27. };
  28. // 读取并解析 JSON 文件
  29. async function readJsonFile(filePath) {
  30. try {
  31. const data = await fs.readFile(filePath, "utf8");
  32. return JSON.parse(data);
  33. } catch (error) {
  34. throw new Error(`读取文件失败: ${filePath}, 错误: ${error.message}`);
  35. }
  36. }
  37. // 写入 JSON 文件
  38. async function writeJsonFile(filePath, data) {
  39. const jsonData = JSON.stringify(data, null, 2);
  40. try {
  41. await fs.writeFile(filePath, jsonData, "utf8");
  42. } catch (error) {
  43. throw new Error(`写入文件失败: ${filePath}, 错误: ${error.message}`);
  44. }
  45. }
  46. // 更新配置项在指定文件中
  47. async function updateConfigInFile(filePath, newAPPID, newLOGO, newHOME_LOGO) {
  48. try {
  49. let data = await fs.readFile(filePath, "utf8");
  50. const updates = [
  51. { key: "APPID: ", value: `'"${newAPPID}"',` },
  52. // 因现在惠融好借和惠融钱包的图片资源都是一样的,所以LOGO和HOME_LOGO暂时这部分代码暂时注释掉
  53. // { key: "LOGO: ", value: `'"${newLOGO}"',` },
  54. // { key: "HOME_LOGO: ", value: `'"${newHOME_LOGO}"',` },
  55. ];
  56. let updatedData = data;
  57. for (const { key, value } of updates) {
  58. const startIndex = updatedData.indexOf(key);
  59. if (startIndex !== -1) {
  60. const lineEndIndex = updatedData.indexOf("\n", startIndex);
  61. const lineToReplace = updatedData.substring(startIndex, lineEndIndex);
  62. updatedData = updatedData.replace(lineToReplace, `${key}${value}`);
  63. } else {
  64. console.warn(`未找到 ${key.trim()} 在文件中: ${filePath}`);
  65. }
  66. }
  67. if (data !== updatedData) {
  68. await fs.writeFile(filePath, updatedData, "utf8");
  69. console.log(`更新成功: ${filePath} 中的 APPID 已更新为 ${newAPPID}`);
  70. // console.log(`更新成功: ${filePath} 中的 LOGO 已更新为 ${newLOGO}`);
  71. // console.log(
  72. // `更新成功: ${filePath} 中的 HOME_LOGO 已更新为 ${newHOME_LOGO}`
  73. // );
  74. } else {
  75. console.log(`没有需要更新的内容: ${filePath}`);
  76. }
  77. } catch (error) {
  78. throw new Error(`更新配置文件失败: ${filePath}, 错误: ${error.message}`);
  79. }
  80. }
  81. // 更新项目配置
  82. async function updateProjectConfig(projectName) {
  83. const projectConfig = projectConfigs[projectName];
  84. if (!projectConfig) {
  85. throw new Error(`无效的项目名称: ${projectName}`);
  86. }
  87. // 更新 project.config.json 中的 appid
  88. const config = await readJsonFile(configFilePath);
  89. config.appid = projectConfig.miniAppId;
  90. await writeJsonFile(configFilePath, config);
  91. console.log(
  92. `更新成功: ${configFilePath} 中的 appid 已更新为 ${projectConfig.miniAppId}`
  93. );
  94. // 更新 dev.js 和 prod.js 中的 APPID
  95. await Promise.all([
  96. updateConfigInFile(
  97. devFilePath,
  98. projectConfig.appId,
  99. projectConfig.LOGO,
  100. projectConfig.HOME_LOGO
  101. ),
  102. updateConfigInFile(
  103. prodFilePath,
  104. projectConfig.appId,
  105. projectConfig.LOGO,
  106. projectConfig.HOME_LOGO
  107. ),
  108. ]);
  109. }
  110. program
  111. .command("project <projectName>")
  112. .description("设置项目")
  113. .action(async (projectName) => {
  114. try {
  115. await updateProjectConfig(projectName);
  116. console.log(
  117. "---------- 每次执行完mycli命令后,需要重新编译项目!!! ----------"
  118. );
  119. } catch (error) {
  120. console.error("操作过程中出错:", error.message);
  121. }
  122. });
  123. program.parse(process.argv);