const ci = require('miniprogram-ci'); const fs = require('fs'); const path = require('path'); // 接收主进程传递过来的参数 process.on('message', async (data) => { let {appid, name, version, desc, env, appindex} = data await setProjectConfig(appid); await setConfig(appid); const project = new ci.Project({ appid: appid, type: 'miniProgram', projectPath: path.resolve(__dirname, '../'), privateKeyPath: path.resolve(__dirname, `../destroy/keys/private.${appid}.key`), ignores: ['node_modules/**/*', 'destroy/**/*'], }); // 上传 const uploadResult = await ci.upload({ project, version: version, desc: desc, setting: { es6: true, minifyJS: true, minifyWXML: true, minifyWXSS: true, minify: true }, onProgressUpdate: (e) => { // console.log('onProcessUpdate', e) if (e._status == "done" && e._msg == "upload") { console.log(`${appid}--${name}--${env} 上传完成`); // config.appindex += 1; } }, }); process.send('success'); process.exit(); }); // 设置 project.config.json const setProjectConfig = async (appid) => { // 读取和替换的文件路径 const project_config = '../project.config.json'; const promise = new Promise((resolve, reject) => { // 读取 project.config.json fs.readFile( path.join(__dirname, project_config), 'utf8', (err, data) => { if (err) throw err; let json = JSON.parse(data); // 替换appid json.appid = appid; // console.log('project.config.json:', json) // 改写 project.config.json 中 appid fs.writeFile( path.join(__dirname, project_config), JSON.stringify(json, null, 4), (err) => { if (err) throw err; resolve(); } ); } ); }); return promise; } // 设置config.js const setConfig = async (appid) => { // 读取和替换的文件路径 const config_file = '../config/config.js'; const promise = new Promise((resolve, reject) => { // 读取 config.js fs.readFile( path.join(__dirname, config_file), 'utf8', (err, data) => { if (err) throw err; let configjs = data; const current_appid = appid; // 替换文件中当前小程序的appid const regex = new RegExp('(current_appid\\s*=\\s*["\']).*?(["\'])', 'g'); configjs = configjs.replace(regex, 'current_appid = \'' + current_appid + '\''); // console.log(configjs) // 改写 config.js 中 current_appid fs.writeFile( path.join(__dirname, config_file), configjs, (err) => { if (err) throw err; resolve(); } ); } ); }); return promise; }