1
0
mirror of https://gitcode.com/github-mirrors/react-native-update-cli.git synced 2025-11-22 16:26:10 +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

@@ -4,12 +4,9 @@
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;
} }
@@ -64,9 +75,11 @@ 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(
options.platform || (await question('Platform(ios/android):')),
);
const { id } = await post('/app/create', { name, platform }); const { id } = await post('/app/create', { name, platform });
console.log(`Created app ${id}`); console.log(`Created app ${id}`);
await this.selectApp({ await this.selectApp({
@@ -88,7 +101,9 @@ export const commands = {
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,7 +111,9 @@ 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;
} }
} }
@@ -105,6 +122,10 @@ export const commands = {
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',
);
}, },
} };