关于微信小程序多环境切换appid,导致混乱,实现脚本自动打包
1.在全局建立config文件夹,建立对应的环境变量,根据打包命令的不同,导出对应的appid;
2.在项目中新建ci文件夹,在创建build.js
build.js
import fs from 'node:fs';
import readline from 'node:readline';
import { execSync } from 'node:child_process';
import ci from 'miniprogram-ci';
import { sit, uat, pl } from '../config/index.js';
const r = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
// 获取当前 node 环境变量
const NODE_ENV = process.env.NODE_ENV;
console.log('发布环境:', NODE_ENV);
r.question('请输入发版备注:', async desc => {
if (NODE_ENV === 'sit') {
upload('sit', sit, desc);
} else if (NODE_ENV === 'uat') {
upload('uat', uat, desc);
} else if (NODE_ENV === 'pl') {
upload('pl', pl, desc);
} else if (NODE_ENV === 'all') {
console.log('先发布 sit 环境');
upload('sit', sit, desc, true);
} else {
console.error('请指定环境变量 NODE_ENV 的值为 sit / uat / pl');
}
r.close();
});
/**
* 上传代码到微信小程序平台
* @param {string} env 环境
* @param {object} config 配置
* @param {string} desc 备注
* @param {boolean} isALl 是否发布全部环境
*/
async function upload(env, config, desc = 'update', isALl = false) {
let versionFile = fs.readFileSync('./ci/version.js', 'utf-8');
// 修改对应的版本号
let sitVersion = versionFile.match(/sit: '(.*)',/)[1];
let uatVersion = versionFile.match(/uat: '(.*)',/)[1];
let plVersion = versionFile.match(/pl: '(.*)',/)[1];
// 当前版本
const currentVersion = env === 'sit' ? sitVersion : env === 'uat' ? uatVersion : plVersion;
// console.log('当前版本号:', currentVersion);
// 下一个版本
const newVersion = getNextVersion(currentVersion);
const { appid, privateKeyPath } = config;
const project = new ci.Project({
appid,
type: 'miniProgram',
projectPath: process.cwd(),
privateKeyPath,
ignores: ['node_modules/**/*'],
});
// 在有需要的时候构建npm
// await ci.packNpm(project, {
// ignores: ['pack_npm_ignore_list'],
// reporter: infos => {
// // console.log(infos);
// },
// });
console.log('开始上传,请等待...');
let fileSum = 0;
const printProgressFunc = () => {
fileSum++;
process.stdout.moveCursor(0, -1); // 将光标移动到上一行
process.stdout.clearLine(); // 清除当前行
process.stdout.cursorTo(0); // 将光标移动到行首
process.stdout.clearLine(); // 清除当前行
process.stdout.cursorTo(0); // 将光标移动到行首
process.stdout.write(`已上传文件数: ${fileSum}\n`);
};
await ci.upload({
project,
version: newVersion,
desc,
setting: { es6: true, es7: true, minifyJS: true, minifyWXML: true, minifyWXSS: true, minify: true },
onProgressUpdate: printProgressFunc,
robot: env === 'sit' ? 1 : env === 'uat' ? 2 : 3,
});
console.log(env + ' 上传成功 ' + currentVersion + ' ---> ' + newVersion);
// 完成之后,将对应的版本号写入到 ci/version.js 文件中
if (env === 'sit') {
sitVersion = newVersion;
}
if (env === 'uat') {
uatVersion = newVersion;
}
if (env === 'pl') {
plVersion = newVersion;
}
// 写入新的版本号
versionFile = versionFile.replace(/sit: '(.*)',/, `sit: '${sitVersion}',`);
versionFile = versionFile.replace(/uat: '(.*)',/, `uat: '${uatVersion}',`);
versionFile = versionFile.replace(/pl: '(.*)',/, `pl: '${plVersion}',`);
fs.writeFileSync('./ci/version.js', versionFile);
console.log(env + ' 新版本号文件写入成功');
if (isALl) {
console.log('开始发布下一个环境');
if (env === 'sit') {
upload('uat', uat, desc, true);
}
if (env === 'uat') {
upload('pl', pl, desc);
}
} else {
commitVersion();
}
}
/**
* 获取给定版本号的下一个版本号
* @param {String} version
* @returns {String}
* @example
* getNextVersion('1.1.1') => '1.1.2'
*/
function getNextVersion(version) {
const versionArr = version.split('.');
const lastNum = versionArr.pop();
versionArr.push(Number(lastNum) + 1);
return versionArr.join('.');
}
/**
* 提交 ./ci/version.js 文件到仓库
* @param {String} desc 提交备注
*/
function commitVersion(desc = '发布后更新版本文件') {
execSync(`git add ./ci/version.js`);
execSync(`git commit -m "${desc}"`);
execSync(`git push`);
console.log('版本号文件提交成功');
}
3.登录微信公众平台,找到小程序代码上传,获取密钥,放在ci文件夹中;