1
0
Code Issues Pull Requests Actions Packages Projects Releases Wiki Activity GitHub Gitee
react-native-update-cli/lib/app.js

128 lines
3.7 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.commands = undefined;
exports.checkPlatform = checkPlatform;
exports.getSelectedApp = getSelectedApp;
exports.listApp = listApp;
exports.chooseApp = chooseApp;
var _utils = require('./utils');
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _ttyTable = require('tty-table');
var _ttyTable2 = _interopRequireDefault(_ttyTable);
var _api = require('./api');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
const validPlatforms = {
ios: 1,
android: 1
};
function checkPlatform(platform) {
if (!validPlatforms[platform]) {
throw new Error(`无法识别的平台 '${platform}'`);
}
return platform;
}
function getSelectedApp(platform) {
checkPlatform(platform);
if (!_fs2.default.existsSync('update.json')) {
throw new Error(`App not selected. run 'pushy selectApp --platform ${platform}' first!`);
}
const updateInfo = JSON.parse(_fs2.default.readFileSync('update.json', 'utf8'));
if (!updateInfo[platform]) {
throw new Error(`App not selected. run 'pushy selectApp --platform ${platform}' first!`);
}
return updateInfo[platform];
}
async function listApp(platform) {
const { data } = await (0, _api.get)('/app/list');
const list = platform ? data.filter(v => v.platform === platform) : data;
const header = [{ value: '应用 id' }, { value: '应用名称' }, { value: '平台' }];
const rows = [];
for (const app of list) {
rows.push([app.id, app.name, app.platform]);
}
console.log((0, _ttyTable2.default)(header, rows).render());
if (platform) {
console.log(`\共 ${list.length} ${platform} 个应用`);
} else {
console.log(`\共 ${list.length} 个应用`);
}
return list;
}
async function chooseApp(platform) {
const list = await listApp(platform);
while (true) {
const id = await (0, _utils.question)('输入应用 id:');
const app = list.find(v => v.id === (id | 0));
if (app) {
return app;
}
}
}
const commands = exports.commands = {
createApp: async function ({ options }) {
const name = options.name || (await (0, _utils.question)('应用名称:'));
const { downloadUrl } = options;
const platform = checkPlatform(options.platform || (await (0, _utils.question)('平台(ios/android):')));
const { id } = await (0, _api.post)('/app/create', { name, platform });
console.log(`已成功创建应用id: ${id}`);
await this.selectApp({
args: [id],
options: { platform, downloadUrl }
});
},
deleteApp: async function ({ args, options }) {
const { platform } = options;
const id = args[0] || chooseApp(platform);
if (!id) {
console.log('已取消');
}
await (0, _api.doDelete)(`/app/${id}`);
console.log('操作成功');
},
apps: async function ({ options }) {
const { platform } = options;
listApp(platform);
},
selectApp: async function ({ args, options }) {
const platform = checkPlatform(options.platform || (await (0, _utils.question)('平台(ios/android):')));
const id = args[0] ? parseInt(args[0]) : (await chooseApp(platform)).id;
let updateInfo = {};
if (_fs2.default.existsSync('update.json')) {
try {
updateInfo = JSON.parse(_fs2.default.readFileSync('update.json', 'utf8'));
} catch (e) {
console.error('Failed to parse file `update.json`. Try to remove it manually.');
throw e;
}
}
const { appKey } = await (0, _api.get)(`/app/${id}`);
updateInfo[platform] = {
appId: id,
appKey
};
_fs2.default.writeFileSync('update.json', JSON.stringify(updateInfo, null, 4), 'utf8');
}
};