// 此文件用于小程序发布和提交体验版本
// 命令行配置文档为 https://developers.weixin.qq.com/miniprogram/dev/devtools/cli.html#%E8%87%AA%E5%8A%A8%E9%A2%84%E8%A7%88
const fs = require('fs')
const cp = require("child_process")
const CONFIG = {
CLI_PATH: 'D:/tool/Tencent/微信web开发者工具/cli.bat',
PROJECT_PATH: 'E:/coocaa/mp-tvpai'
}
// open IDE
function init() {
return new Promise((resolve, reject) => {
cp.execFile(CONFIG.CLI_PATH, ['-o', CONFIG.PROJECT_PATH], (error, stdout, stderr) => {
if (error) {
console.log('Failed to start IDE and open the project.')
reject(error);
}
resolve();
})
})
}
// 修改环境变量
function changeEnv(version) {
let text = ""
return new Promise((resolve, reject) => {
if(version.indexOf('beta') >= 0) {
text = "const env = 'dev'\n module.exports = env" // 测试环境
} else {
text = "const env = 'prod'\n module.exports = env" // 正式环境
}
fs.writeFile("./config/env.js", text, error => {
if (error) {
reject(`修改环境变量失败,原因是:${error.message}`)
}
resolve()
});
})
}
// 上传
function upload(version, comment) {
console.log('正在上传,请稍等片刻...')
comment = comment || (version.indexOf('beta') >= 0 ? '测试环境' : '正式环境')
return new Promise((resolve, reject) => {
const cmd = `cli -u ${version}@${CONFIG.PROJECT_PATH} --upload-desc ${comment}`
cp.execFile(CONFIG.CLI_PATH, cmd.split(' '), (error, stdout, stderr) => {
if (error) {
console.log('Failed to start IDE and open the project.')
reject(error);
} else {
resolve();
}
})
})
}
function makeUp(params) {
return new Promise((resolve, reject) => {
let version = ''
let comment = ''
let isBeta = false
params.forEach((item, index) => {
try {
if(item == '-v') {
version = params[index + 1]
}
if(item == '-m') {
comment = params[index + 1]
}
if(item == '--beta') {
isBeta = true
}
} catch (error) {
reject(error)
}
})
if(isBeta) {
version = `beta${version}`
}
if(!version) {
reject('请输入版本号')
}
console.log('版本号:', version)
console.log('环境:', isBeta ? '测试' : '正式')
console.log('备注:', comment || '无')
resolve({ version, comment })
})
}
async function main() {
try {
// 获取窗口参数
const params = process.argv.slice(2)
let { version, comment } = await makeUp(params);
await init();
await changeEnv(version);
await upload(version, comment)
console.log('上传完成')
} catch (error) {
console.log('\nerror:', error)
}
}
main();
执行命令:
node deploy.js --beta -v 1.0.0 -m 备注