/*
 * @Descripttion: 微信小程序上传脚本
 * @Company: 思宏科技
 * @version: v1.0
 * @Author: HJ
 * @Date: 2021-08-13 15:43:11
 * @LastEditors: OBKoro1
 * @LastEditTime: 2022-01-10 18:14:56
 */
const qs = require('qs')
const ci = require('miniprogram-ci')
const path = require('path')
const config = require('../../weixin.upload')
const dotenv = require('dotenv')

const { CONFIG_ENV } = process.env

const env = dotenv.config({
  path: path.join(__dirname, '../../', `.env.${CONFIG_ENV}`)
})

console.log(env)

// eslint-disable-next-line space-before-function-paren
;(async () => {
  const { npm_package_buildConfig_weixin_desc } = process.env

  const results = process.argv.find(item => item.includes('version='))
  let version = '1.0.0'
  if (results) {
    version = qs.parse(results).version
  }

  const project = new ci.Project({
    appid: env.parsed.APPID,
    type: 'miniProgram',
    projectPath: path.resolve(__dirname, '../../dist/build/mp-weixin-pack'),
    privateKeyPath: path.join(__dirname, `/private.${env.parsed.APPID}.key`),
    ignores: ['node_modules/**/*']
  })
  const uploadResult = await ci.upload({
    project,
    version,
    desc: npm_package_buildConfig_weixin_desc,
    setting: {
      es6: true,
      es7: true,
      es11: true,
      minifyJS: true,
      minifyWXML: true,
      minifyWXSS: true,
      minify: true
    },
    onProgressUpdate: console.log,
    ...config
  })
  console.log('上传结果', uploadResult)
})()
