// @ts-ignore
const fs = require( 'fs');
// @ts-ignore
const ci = require ('miniprogram-ci');
// @ts-ignore
const path = require ('path');

// @ts-ignore
const envType = process.argv[process.argv.length - 1];  // 获取传入的环境变量名称

const WX_DESC = envType == 'production' ? '正式环境' : '测试环境'        // 备注

; // @ts-ignore
(async () => {
    // @ts-ignore
    const manifest = path.resolve(__dirname, './src/manifest.json')
    // @ts-ignore
    const manifestConfig = JSON.parse(fs.readFileSync(manifest).toString())
    const appId = manifestConfig['mp-weixin'].appid
    const versionName = manifestConfig.versionName || '1.0.0'

    const project = new ci.Project({
        appid: appId,
        type: 'miniProgram',
        // @ts-ignore
        projectPath: path.join(__dirname, './dist/build/mp-weixin'),  // 获取打包的路径
        // @ts-ignore
        privateKeyPath: path.join(__dirname, `./keys/private.${appId}.key`), // 你要上传的小程序APPid
        ignores: ['node_modules/**/*'],
    })
    await ci.upload({
        project,
        version: versionName,
        desc: WX_DESC,
        robot: 1,
        setting: {
            es7: true,
            minifyJS: true,  // 压缩 JS 代码
            minify: true     // 压缩所有代码，对应小程序开发者工具的 "压缩代码"
        },
        // @ts-ignore
        onProgressUpdate: console.log,
    })
})()
