index.ts 4.7 KB

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