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
Files
波仔糕 e98bcf504f cli modular refactor (#16)
* add logic to support SENTRY_PROPERTIES parameter

* remove update.json and meta.json files in ppk

* udpapte

* refactor modles

* update

* add package-module file

* update

* update readme file

* modifu cli.json file

* fix command issues

* improve version workflow logic

* udpate

* update

* update

* update

* udpate

* udpate

* add example

* update readme file

* udpate version

* change logic to use pushy command uniformly
2025-07-24 11:46:20 +08:00

139 lines
3.5 KiB
TypeScript

import fs from 'fs';
import Table from 'tty-table';
import { question } from './utils';
import { doDelete, get, post } from './api';
import type { Platform } from './types';
import { t } from './utils/i18n';
const validPlatforms = ['ios', 'android', 'harmony'];
export async function getPlatform(platform?: string) {
return assertPlatform(
platform || (await question(t('platformQuestion'))),
) as Platform;
}
export function assertPlatform(platform: string) {
if (!validPlatforms.includes(platform)) {
throw new Error(t('unsupportedPlatform', { platform }));
}
return platform;
}
export function getSelectedApp(platform: Platform) {
assertPlatform(platform);
if (!fs.existsSync('update.json')) {
throw new Error(t('appNotSelected', { platform }));
}
const updateInfo = JSON.parse(fs.readFileSync('update.json', 'utf8'));
if (!updateInfo[platform]) {
throw new Error(t('appNotSelected', { platform }));
}
return updateInfo[platform];
}
export async function listApp(platform: Platform | '' = '') {
const { data } = await get('/app/list');
const list = platform ? data.filter((v) => v.platform === platform) : data;
const header = [
{ value: t('appId') },
{ value: t('appName') },
{ value: t('platform') },
];
const rows = [];
for (const app of list) {
rows.push([app.id, app.name, app.platform]);
}
console.log(Table(header, rows).render());
console.log(`\n${t('totalApps', { count: list.length, platform })}`);
return list;
}
export async function chooseApp(platform: Platform) {
const list = await listApp(platform);
while (true) {
const id = await question(t('enterAppIdQuestion'));
const app = list.find((v) => v.id === Number(id));
if (app) {
return app;
}
}
}
export const appCommands = {
createApp: async function ({
options,
}: {
options: { name: string; downloadUrl: string; platform: Platform };
}) {
const name = options.name || (await question(t('appNameQuestion')));
const { downloadUrl } = options;
const platform = await getPlatform(options.platform);
const { id } = await post('/app/create', { name, platform, downloadUrl });
console.log(t('createAppSuccess', { id }));
await this.selectApp({
args: [id],
options: { platform },
});
},
deleteApp: async ({
args,
options,
}: {
args: string[];
options: { platform: Platform };
}) => {
const { platform } = options;
const id = args[0] || chooseApp(platform);
if (!id) {
console.log(t('cancelled'));
}
await doDelete(`/app/${id}`);
console.log(t('operationSuccess'));
},
apps: async ({ options }: { options: { platform: Platform } }) => {
const { platform } = options;
listApp(platform);
},
selectApp: async ({
args,
options,
}: {
args: string[];
options: { platform: Platform };
}) => {
const platform = await getPlatform(options.platform);
const id = args[0]
? Number.parseInt(args[0])
: (await chooseApp(platform)).id;
let updateInfo: Partial<
Record<Platform, { appId: number; appKey: string }>
> = {};
if (fs.existsSync('update.json')) {
try {
updateInfo = JSON.parse(fs.readFileSync('update.json', 'utf8'));
} catch (e) {
console.error(t('failedToParseUpdateJson'));
throw e;
}
}
const { appKey } = await get(`/app/${id}`);
updateInfo[platform] = {
appId: id,
appKey,
};
fs.writeFileSync(
'update.json',
JSON.stringify(updateInfo, null, 4),
'utf8',
);
},
};