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