1
0
mirror of https://gitcode.com/github-mirrors/react-native-update-cli.git synced 2025-09-16 09:41:38 +08:00
Code Issues Packages Projects Releases Wiki Activity GitHub Gitee

Use table for listApp

This commit is contained in:
sunnylqm
2021-01-20 23:29:31 +08:00
parent 59fad93832
commit 7f1dcbb571

View File

@@ -2,14 +2,11 @@
* Created by tdzl2003 on 2/13/16.
*/
import {question} from './utils';
import { question } from './utils';
import fs from 'fs';
const Table = require('tty-table');
const {
post,
get,
doDelete,
} = require('./api');
const { post, get, doDelete } = require('./api');
const validPlatforms = {
ios: 1,
@@ -20,28 +17,42 @@ export function checkPlatform(platform) {
if (!validPlatforms[platform]) {
throw new Error(`Invalid platform '${platform}'`);
}
return platform
return platform;
}
export function getSelectedApp(platform) {
checkPlatform(platform);
if (!fs.existsSync('update.json')){
throw new Error(`App not selected. run 'pushy selectApp --platform ${platform}' first!`);
if (!fs.existsSync('update.json')) {
throw new Error(
`App not selected. run 'pushy selectApp --platform ${platform}' first!`,
);
}
const updateInfo = JSON.parse(fs.readFileSync('update.json', 'utf8'));
if (!updateInfo[platform]) {
throw new Error(`App not selected. run 'pushy selectApp --platform ${platform}' first!`);
throw new Error(
`App not selected. run 'pushy selectApp --platform ${platform}' first!`,
);
}
return updateInfo[platform];
}
export async function listApp(platform){
const {data} = await get('/app/list');
const list = platform?data.filter(v=>v.platform===platform):data;
export async function listApp(platform) {
const { data } = await get('/app/list');
const list = platform ? data.filter((v) => v.platform === platform) : data;
const header = [
{ value: 'App Id' },
{ value: 'App Name' },
{ value: 'Platform' },
];
const rows = [];
for (const app of list) {
console.log(`${app.id}) ${app.name}(${app.platform})`);
rows.push([app.id, app.name, app.platform]);
}
console.log(Table(header, rows).render());
if (platform) {
console.log(`\nTotal ${list.length} ${platform} apps`);
} else {
@@ -55,7 +66,7 @@ export async function chooseApp(platform) {
while (true) {
const id = await question('Enter appId:');
const app = list.find(v=>v.id === (id|0));
const app = list.find((v) => v.id === (id | 0));
if (app) {
return app;
}
@@ -63,19 +74,21 @@ export async function chooseApp(platform) {
}
export const commands = {
createApp: async function ({options}) {
const name = options.name || await question('App Name:');
const {downloadUrl} = options;
const platform = checkPlatform(options.platform || await question('Platform(ios/android):'));
const {id} = await post('/app/create', {name, platform});
createApp: async function ({ options }) {
const name = options.name || (await question('App Name:'));
const { downloadUrl } = options;
const platform = checkPlatform(
options.platform || (await question('Platform(ios/android):')),
);
const { id } = await post('/app/create', { name, platform });
console.log(`Created app ${id}`);
await this.selectApp({
args: [id],
options: {platform, downloadUrl},
options: { platform, downloadUrl },
});
},
deleteApp: async function ({args, options}) {
const {platform} = options;
deleteApp: async function ({ args, options }) {
const { platform } = options;
const id = args[0] || chooseApp(platform);
if (!id) {
console.log('Canceled');
@@ -83,12 +96,14 @@ export const commands = {
await doDelete(`/app/${id}`);
console.log('Ok.');
},
apps: async function ({options}){
const {platform} = options;
apps: async function ({ options }) {
const { platform } = options;
listApp(platform);
},
selectApp: async function({args, options}) {
const platform = checkPlatform(options.platform || await question('Platform(ios/android):'));
selectApp: async function ({ args, options }) {
const platform = checkPlatform(
options.platform || (await question('Platform(ios/android):')),
);
const id = args[0] || (await chooseApp(platform)).id;
let updateInfo = {};
@@ -96,15 +111,21 @@ export const commands = {
try {
updateInfo = JSON.parse(fs.readFileSync('update.json', 'utf8'));
} catch (e) {
console.error('Failed to parse file `update.json`. Try to remove it manually.');
console.error(
'Failed to parse file `update.json`. Try to remove it manually.',
);
throw e;
}
}
const {appKey} = await get(`/app/${id}`);
const { appKey } = await get(`/app/${id}`);
updateInfo[platform] = {
appId: id,
appKey,
};
fs.writeFileSync('update.json', JSON.stringify(updateInfo, null, 4), 'utf8');
fs.writeFileSync(
'update.json',
JSON.stringify(updateInfo, null, 4),
'utf8',
);
},
}
};