mirror of
https://gitcode.com/github-mirrors/react-native-update-cli.git
synced 2025-09-16 09:41:38 +08:00
Use table for listApp
This commit is contained in:
55
src/app.js
55
src/app.js
@@ -4,12 +4,9 @@
|
||||
|
||||
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!`);
|
||||
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;
|
||||
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;
|
||||
}
|
||||
@@ -64,9 +75,11 @@ export async function chooseApp(platform) {
|
||||
|
||||
export const commands = {
|
||||
createApp: async function ({ options }) {
|
||||
const name = options.name || await question('App Name:');
|
||||
const name = options.name || (await question('App Name:'));
|
||||
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 });
|
||||
console.log(`Created app ${id}`);
|
||||
await this.selectApp({
|
||||
@@ -88,7 +101,9 @@ export const commands = {
|
||||
listApp(platform);
|
||||
},
|
||||
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;
|
||||
|
||||
let updateInfo = {};
|
||||
@@ -96,7 +111,9 @@ 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;
|
||||
}
|
||||
}
|
||||
@@ -105,6 +122,10 @@ export const commands = {
|
||||
appId: id,
|
||||
appKey,
|
||||
};
|
||||
fs.writeFileSync('update.json', JSON.stringify(updateInfo, null, 4), 'utf8');
|
||||
fs.writeFileSync(
|
||||
'update.json',
|
||||
JSON.stringify(updateInfo, null, 4),
|
||||
'utf8',
|
||||
);
|
||||
},
|
||||
}
|
||||
};
|
||||
|
Reference in New Issue
Block a user