publish-weapp.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. const ci = require('miniprogram-ci')
  2. const path = require('path')
  3. const { execSync } = require('child_process')
  4. // 小程序配置
  5. const MINI_CONFIGS = {
  6. enterprise: {
  7. name: '企业小程序',
  8. appid: 'wx1e791ed2e0229eb8',
  9. projectPath: path.resolve(__dirname, '../mini'),
  10. privateKeyPath: path.resolve(__dirname, '../keys/private.wx1e791ed2e0229eb8.key'),
  11. buildCmd: 'cd mini && pnpm build:weapp',
  12. distPath: 'dist/weapp',
  13. },
  14. talent: {
  15. name: '人才小程序',
  16. appid: 'wx3c47dbce1ea7d43c',
  17. projectPath: path.resolve(__dirname, '../mini-talent'),
  18. privateKeyPath: path.resolve(__dirname, '../keys/private.wx3c47dbce1ea7d43c.key'),
  19. buildCmd: 'cd mini-talent && pnpm build:weapp',
  20. distPath: 'dist/weapp',
  21. },
  22. }
  23. /**
  24. * 构建小程序
  25. */
  26. function buildMiniProject(config) {
  27. console.log(`\n🔨 正在构建 ${config.name}...`)
  28. try {
  29. execSync(config.buildCmd, { stdio: 'inherit' })
  30. console.log(`✅ ${config.name} 构建完成`)
  31. } catch (error) {
  32. console.error(`❌ ${config.name} 构建失败`)
  33. throw error
  34. }
  35. }
  36. /**
  37. * 上传小程序到微信服务器
  38. */
  39. async function uploadMiniProject(config, options) {
  40. const { version, desc, robot = 1 } = options
  41. console.log(`\n📤 正在上传 ${config.name}...`)
  42. console.log(` 版本: ${version}`)
  43. console.log(` 描述: ${desc}`)
  44. const project = new ci.Project({
  45. appid: config.appid,
  46. type: 'miniProgram',
  47. projectPath: config.projectPath,
  48. privateKeyPath: config.privateKeyPath,
  49. ignores: ['node_modules/**/*'],
  50. })
  51. try {
  52. const result = await ci.upload({
  53. project,
  54. version,
  55. desc,
  56. setting: {
  57. useProjectConfig: true,
  58. es7: true,
  59. minify: true,
  60. minifyJS: true,
  61. minifyWXML: true,
  62. minifyWXSS: true,
  63. autoPrefixWXSS: true,
  64. },
  65. onProgressUpdate: (progress) => {
  66. process.stdout.write(`\r 上传进度: ${progress}%`)
  67. },
  68. robot,
  69. })
  70. console.log(`\n✅ ${config.name} 上传成功!`)
  71. console.log(` 时间: ${result.time}`)
  72. console.log(` 版本: ${result.version}`)
  73. console.log(` 请前往小程序后台查看: https://mp.weixin.qq.com/`)
  74. } catch (error) {
  75. console.error(`\n❌ ${config.name} 上传失败`)
  76. console.error(error.message)
  77. throw error
  78. }
  79. }
  80. /**
  81. * 预览小程序(生成二维码)
  82. */
  83. async function previewMiniProject(config, options) {
  84. const { desc, qrcodePath, robot = 1 } = options
  85. console.log(`\n👀 正在生成 ${config.name} 预览二维码...`)
  86. const project = new ci.Project({
  87. appid: config.appid,
  88. type: 'miniProgram',
  89. projectPath: config.projectPath,
  90. privateKeyPath: config.privateKeyPath,
  91. ignores: ['node_modules/**/*'],
  92. })
  93. try {
  94. const result = await ci.preview({
  95. project,
  96. desc,
  97. setting: { useProjectConfig: true },
  98. qrcodeFormat: 'image',
  99. qrcodeOutputDest: qrcodePath,
  100. onProgressUpdate: (progress) => {
  101. process.stdout.write(`\r 生成进度: ${progress}%`)
  102. },
  103. robot,
  104. })
  105. console.log(`\n✅ 预览二维码已生成: ${qrcodePath}`)
  106. } catch (error) {
  107. console.error(`\n❌ 预览二维码生成失败`)
  108. console.error(error.message)
  109. throw error
  110. }
  111. }
  112. /**
  113. * 主函数
  114. */
  115. async function main() {
  116. const args = process.argv.slice(2)
  117. const [miniType, action = 'upload'] = args
  118. // 解析参数
  119. const options = {
  120. version: process.env.VERSION || '1.0.0',
  121. desc: process.env.DESC || '自动化发布',
  122. robot: parseInt(process.env.ROBOT || '1'),
  123. }
  124. if (!miniType || !MINI_CONFIGS[miniType]) {
  125. console.error('❌ 请指定小程序类型: enterprise 或 talent')
  126. console.error('示例: node scripts/publish-weapp.js enterprise')
  127. process.exit(1)
  128. }
  129. const config = MINI_CONFIGS[miniType]
  130. try {
  131. // 构建项目
  132. buildMiniProject(config)
  133. // 执行操作
  134. if (action === 'upload') {
  135. await uploadMiniProject(config, options)
  136. } else if (action === 'preview') {
  137. await previewMiniProject(config, {
  138. desc: options.desc,
  139. qrcodePath: path.resolve(__dirname, `../qrcode-${miniType}.jpg`),
  140. robot: options.robot,
  141. })
  142. } else {
  143. console.error(`❌ 未知操作: ${action}`)
  144. process.exit(1)
  145. }
  146. } catch (error) {
  147. process.exit(1)
  148. }
  149. }
  150. main()