index.ts 5.4 KB

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