upload.wx.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const ci = require('miniprogram-ci');
  2. const fs = require('fs');
  3. const path = require('path');
  4. // 接收主进程传递过来的参数
  5. process.on('message', async (data) => {
  6. let {appid, name, version, desc, env, appindex} = data
  7. await setProjectConfig(appid);
  8. await setConfig(appid);
  9. const project = new ci.Project({
  10. appid: appid,
  11. type: 'miniProgram',
  12. projectPath: path.resolve(__dirname, '../'),
  13. privateKeyPath: path.resolve(__dirname,
  14. `../destroy/keys/private.${appid}.key`),
  15. ignores: ['node_modules/**/*', 'destroy/**/*'],
  16. });
  17. // 上传
  18. const uploadResult = await ci.upload({
  19. project,
  20. version: version,
  21. desc: desc,
  22. setting: {
  23. es6: true,
  24. minifyJS: true,
  25. minifyWXML: true,
  26. minifyWXSS: true,
  27. minify: true
  28. },
  29. onProgressUpdate: (e) => {
  30. // console.log('onProcessUpdate', e)
  31. if (e._status == "done" && e._msg == "upload") {
  32. console.log(`${appid}--${name}--${env} 上传完成`);
  33. // config.appindex += 1;
  34. }
  35. },
  36. });
  37. process.send('success');
  38. process.exit();
  39. });
  40. // 设置 project.config.json
  41. const setProjectConfig = async (appid) => {
  42. // 读取和替换的文件路径
  43. const project_config = '../project.config.json';
  44. const promise = new Promise((resolve, reject) => {
  45. // 读取 project.config.json
  46. fs.readFile(
  47. path.join(__dirname, project_config),
  48. 'utf8',
  49. (err, data) => {
  50. if (err) throw err;
  51. let json = JSON.parse(data);
  52. // 替换appid
  53. json.appid = appid;
  54. // console.log('project.config.json:', json)
  55. // 改写 project.config.json 中 appid
  56. fs.writeFile(
  57. path.join(__dirname, project_config),
  58. JSON.stringify(json, null, 4),
  59. (err) => {
  60. if (err) throw err;
  61. resolve();
  62. }
  63. );
  64. }
  65. );
  66. });
  67. return promise;
  68. }
  69. // 设置config.js
  70. const setConfig = async (appid) => {
  71. // 读取和替换的文件路径
  72. const config_file = '../config/config.js';
  73. const promise = new Promise((resolve, reject) => {
  74. // 读取 config.js
  75. fs.readFile(
  76. path.join(__dirname, config_file),
  77. 'utf8',
  78. (err, data) => {
  79. if (err) throw err;
  80. let configjs = data;
  81. const current_appid = appid;
  82. // 替换文件中当前小程序的appid
  83. const regex = new RegExp('(current_appid\\s*=\\s*["\']).*?(["\'])', 'g');
  84. configjs = configjs.replace(regex, 'current_appid = \'' + current_appid + '\'');
  85. // console.log(configjs)
  86. // 改写 config.js 中 current_appid
  87. fs.writeFile(
  88. path.join(__dirname, config_file),
  89. configjs,
  90. (err) => {
  91. if (err) throw err;
  92. resolve();
  93. }
  94. );
  95. }
  96. );
  97. });
  98. return promise;
  99. }