'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.commands = undefined; var _api = require('./api'); var _utils = require('./utils'); var _app = require('./app'); var _package = require('./package'); var _compareVersions = require('compare-versions'); async function showVersion(appId, offset) { const { data, count } = await (0, _api.get)(`/app/${appId}/version/list`); console.log(`Offset ${offset}`); for (const version of data) { let packageInfo = version.packages.slice(0, 3).map(v => v.name).join(', '); const count = version.packages.length; if (count > 3) { packageInfo += `...and ${count - 3} more`; } if (count === 0) { packageInfo = `(no package)`; } else { packageInfo = `[${packageInfo}]`; } console.log(`${version.id}) ${version.hash.slice(0, 8)} ${version.name} ${packageInfo}`); } return data; } async function listVersions(appId) { let offset = 0; while (true) { await showVersion(appId, offset); const cmd = await (0, _utils.question)('page Up/page Down/Begin/Quit(U/D/B/Q)'); switch (cmd.toLowerCase()) { case 'u': offset = Math.max(0, offset - 10); break; case 'd': offset += 10; break; case 'b': offset = 0; break; case 'q': return; } } } async function chooseVersion(appId) { let offset = 0; while (true) { const data = await showVersion(appId, offset); const cmd = await (0, _utils.question)('Enter versionId or page Up/page Down/Begin(U/D/B)'); switch (cmd.toLowerCase()) { case 'U': offset = Math.max(0, offset - 10); break; case 'D': offset += 10; break; case 'B': offset = 0; break; default: { const v = data.find(v => v.id === (cmd | 0)); if (v) { return v; } } } } } const commands = exports.commands = { publish: async function ({ args, options }) { const fn = args[0]; const { name, description, metaInfo } = options; if (!fn || !fn.endsWith('.ppk')) { throw new Error('使用方法: pushy publish ppk后缀文件 --platform ios|android'); } const platform = (0, _app.checkPlatform)(options.platform || (await (0, _utils.question)('平台(ios/android):'))); const { appId } = await (0, _app.getSelectedApp)(platform); const { hash } = await (0, _api.uploadFile)(fn); const { id } = await (0, _api.post)(`/app/${appId}/version/create`, { name: name || (await (0, _utils.question)('输入版本名称: ')) || '(未命名)', hash, description: description || (await (0, _utils.question)('输入版本描述:')), metaInfo: metaInfo || (await (0, _utils.question)('输入自定义的 meta info:')) }); // TODO local diff (0, _utils.saveToLocal)(fn, `${appId}/ppk/${id}.ppk`); console.log(`已成功上传新热更包(id: ${id})`); const v = await (0, _utils.question)('是否现在将此热更应用到原生包上?(Y/N)'); if (v.toLowerCase() === 'y') { await this.update({ args: [], options: { versionId: id, platform } }); } }, versions: async function ({ options }) { const platform = (0, _app.checkPlatform)(options.platform || (await (0, _utils.question)('平台(ios/android):'))); const { appId } = await (0, _app.getSelectedApp)(platform); await listVersions(appId); }, update: async function ({ args, options }) { const platform = (0, _app.checkPlatform)(options.platform || (await (0, _utils.question)('平台(ios/android):'))); const { appId } = await (0, _app.getSelectedApp)(platform); const versionId = options.versionId || (await chooseVersion(appId)).id; let pkgId; let pkgVersion = options.packageVersion; let minPkgVersion = options.minPackageVersion; let maxPkgVersion = options.maxPackageVersion; if (pkgVersion) { pkgVersion = pkgVersion.trim(); const { data } = await (0, _api.get)(`/app/${appId}/package/list?limit=1000`); const pkg = data.find(d => d.name === pkgVersion); if (pkg) { pkgId = pkg.id; } else { throw new Error(`未查询到匹配原生版本:${pkgVersion}`); } } if (minPkgVersion) { minPkgVersion = String(minPkgVersion).trim(); const { data } = await (0, _api.get)(`/app/${appId}/package/list?limit=1000`); const pkgs = data.filter(d => (0, _compareVersions.compare)(d.name, minPkgVersion, '>=')); if (pkgs.length === 0) { throw new Error(`未查询到 >= ${minPkgVersion} 的原生版本`); } for (const pkg of pkgs) { await (0, _api.put)(`/app/${appId}/package/${pkg.id}`, { versionId }); console.log(`已将版本 ${versionId} 绑定到原生版本 ${pkg.name}`); } console.log(`操作完成,共已绑定 ${pkgs.length} 个原生版本`); return; } if (maxPkgVersion) { maxPkgVersion = String(maxPkgVersion).trim(); const { data } = await (0, _api.get)(`/app/${appId}/package/list?limit=1000`); const pkgs = data.filter(d => (0, _compareVersions.compare)(d.name, maxPkgVersion, '<=')); if (pkgs.length === 0) { throw new Error(`未查询到 <= ${maxPkgVersion} 的原生版本`); } for (const pkg of pkgs) { await (0, _api.put)(`/app/${appId}/package/${pkg.id}`, { versionId }); console.log(`已将版本 ${versionId} 绑定到原生版本 ${pkg.name}`); } console.log(`操作完成,共已绑定 ${pkgs.length} 个原生版本`); return; } if (!pkgId) { pkgId = options.packageId || (await (0, _package.choosePackage)(appId)).id; } if (!pkgId) { throw new Error('请提供 packageId 或 packageVersion 参数'); } await (0, _api.put)(`/app/${appId}/package/${pkgId}`, { versionId }); console.log('操作成功'); }, updateVersionInfo: async function ({ args, options }) { const platform = (0, _app.checkPlatform)(options.platform || (await (0, _utils.question)('平台(ios/android):'))); const { appId } = await (0, _app.getSelectedApp)(platform); const versionId = options.versionId || (await chooseVersion(appId)).id; const updateParams = {}; options.name && (updateParams.name = options.name); options.description && (updateParams.description = options.description); options.metaInfo && (updateParams.metaInfo = options.metaInfo); await (0, _api.put)(`/app/${appId}/version/${versionId}`, updateParams); console.log('操作成功'); } };