UNPKG

uni-packing-wgt

Version:

uniapp wgt多环境打包、调试、发布(七牛云)插件

202 lines (168 loc) 6.52 kB
#!/usr/bin/env node const fs = require("fs") const path = require('path') const execSync = require('child_process').execSync const compressing = require('compressing'); const {initConfig, configFilePath, configOutputFilePath, manifestPath,manifestTsPath} = require("./file-mgr") if (initConfig()) return ; const manifest = require(manifestPath) const config = require(configFilePath) const uploader = require("./upload-tools") const copyDirectory = require("./copy-directory") const {checkYiConfigExist, getConfigVersionInfo} = require("./server-mgr"); const {isEmpty,incrementVersion} = require("./utils") const appid = manifest.appid const args = process.argv.slice(2) const runDev = !args || args.length === 0 || (args && args.includes('-d')) const runBeta = (args && args.includes('-b')) const runRelease = (args && args.includes('-r')) const commands = [] if (runDev) commands.push('npm run build:app-plus-dev') if (runBeta) commands.push('npm run build:app-plus-beta') if (runRelease) commands.push('npm run build:app-plus-release') console.log(`======= run ${args} ========`) if (commands.length === 0){ console.error("no command") return } if (checkYiConfigExist()) { // 定制业务 if (commands.length > 1) { console.log("====== 不支持多环境同时执行 ========") return } getConfigVersionInfo().then((r) => { execute(r) }) }else { // 常规 execute() } function execute(versions) { let versionCode = 0 if (manifest.versionCode instanceof Number) { versionCode = manifest.versionCode; } else { versionCode = parseInt(manifest.versionCode); } let versionName = manifest.versionName console.log("manifest code: " + versionCode, "name: " + versionName) if (versions) { versions.push({versionName: versionName, versionCode: versionCode}) // 取出最大versionCode的对象 const maxVersion = versions.reduce((max, current) => { return current.versionCode >= max.versionCode ? current : max }) if (maxVersion){ versionName = maxVersion.versionName versionCode = maxVersion.versionCode } } // if (version && !isEmpty(version.versionName) && version.versionCode > 0 && manifest.versionCode < version.versionCode){ // code = version.versionCode // versionName = version.versionName // } if (config.isIncrementVersion) { versionCode = versionCode + 1 versionName = incrementVersion(versionName, config.versionLength) console.log("new code: " + versionCode, "name: " + versionName) changeVersion(versionCode, versionName) } let err = false const command = commands.join('\n') console.log("====== 开始生成资源包 =====") try { execSync(command, {encoding: 'utf-8'}) } catch (e) { err = true console.error("资源包生成异常", e) } if (err) return console.log("====== 资源包生成完成 =====") // wgt打包 let wgtInfos = [] commands.forEach((c) => { let targetPath = '' let wgtOutFile = '' if (c.includes("dev")) { targetPath = "./dist/dev/app/" wgtOutFile = `./dist/dev/${appid}.wgt` }else if (c.includes("beta")){ targetPath = "./dist/beta/app/" wgtOutFile = `./dist/beta/${appid}.wgt` }else if (c.includes("release")){ targetPath = "./dist/release/app/" wgtOutFile = `./dist/release/${appid}.wgt` } if (!!targetPath && !!wgtOutFile){ wgtInfos.push({targetPath, wgtOutFile}) } }) console.log("====== 资源包打包开始 =====") Promise.allSettled(wgtInfos.map(item => generateWgt(item))) .then((r) => { console.log(...r) console.log("====== 资源包打包完成 =====") if (config.uploadWgtPackage) { let files = r.map(v => v.value) uploader.uploadFiles(...files).finally(() => { console.log("====== 资源包上传完成 =====") }) } if (config.pkgCopyToNativeDir && fs.existsSync(configOutputFilePath)) { console.log("====== 资源包开始复制 =====") const output = require(configOutputFilePath) if (output.sourceDir && output.targetDir && output.sourceDir.length > 0 && output.targetDir.length > 0 && fs.existsSync(output.sourceDir)) { copyDirectory.copy(output.sourceDir, output.targetDir) console.log("====== 资源包复制任务完成 =====") }else { console.log("====== 检查输出输入路径是否配置 =====") } } }).catch((error) => { console.error("资源包打包异常", error) }) } function changeVersion(code, name) { // 更新版本号 if (fs.existsSync(manifestTsPath)){ let filePath = manifestTsPath const data = fs.readFileSync(filePath, "utf8") const modifiedData = data.replace(/versionName: '(.+?)'/, `versionName: '${name}'`).replace(/versionCode: '(\d+)'/, `versionCode: '${code}'`); fs.writeFileSync(filePath, modifiedData, "utf8") } manifest.versionCode = code.toString() manifest.versionName = name let replaceFiles = [{ path: manifestPath, name: 'manifest.json', content: JSON.stringify(manifest, null, 2) }] replaceFiles.forEach((file) => { fs.writeFileSync(file.path, file.content, {encoding: 'utf-8'}) }) } function generateWgt(target = {targetPath: "", wgtOutFile: ""}) { const targetPath = target.targetPath const wgtOutFile = target.wgtOutFile return new Promise((resolve, reject) => { if (!targetPath || !wgtOutFile) { reject("target path not found") return } const tarStream = new compressing.zip.Stream() let paths = fs.readdirSync(targetPath) paths.forEach((item) => { let filePath = path.join(targetPath, item) tarStream.addEntry(filePath) }) tarStream .pipe(fs.createWriteStream(wgtOutFile)) .on('error', (err) => { reject(`${wgtOutFile}: ${err.message}`) }) .on('finish', () => { resolve(`${wgtOutFile}`) }) }) }