From 738520d43570a914d8efd576ff393c2cfde290e7 Mon Sep 17 00:00:00 2001 From: Your Full Name Date: Mon, 30 Oct 2023 13:44:16 +0200 Subject: [PATCH] Feat: sort apps and versions by created date, filter only drafts for push --- src/commands/code/push.ts | 15 +++++++++++---- src/services/app-versions-service.ts | 7 +++++-- src/services/dynamic-choices-service.ts | 4 ++-- 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/commands/code/push.ts b/src/commands/code/push.ts index 817fbce..653b159 100644 --- a/src/commands/code/push.ts +++ b/src/commands/code/push.ts @@ -19,6 +19,7 @@ const MESSAGES = { directory: 'Directory path of you project in your machine. If not included will use the current working directory.', appVersionId: APP_VERSION_ID_TO_ENTER, appId: APP_ID_TO_ENTER, + force: 'Force push to live version', }; export default class Push extends AuthenticatedCommand { @@ -43,6 +44,10 @@ export default class Push extends AuthenticatedCommand { aliases: ['v'], description: MESSAGES.appVersionId, }), + force: Flags.boolean({ + char: 'f', + description: MESSAGES.force, + }), }); static args = {}; @@ -50,19 +55,21 @@ export default class Push extends AuthenticatedCommand { public async run(): Promise { const { flags } = await this.parse(Push); - let appVersionId; + let appVersionId: number | undefined; const appId = flags.appId; + const force = flags.force; if (appId) { - const latestDraftVersion = await defaultVersionByAppId(Number(appId)); - if (!latestDraftVersion) throw new Error('No draft version found for the given app id.'); + const latestDraftVersion = await defaultVersionByAppId(Number(appId), force); + if (!latestDraftVersion) throw new Error('No editable version found for the given app id.'); appVersionId = latestDraftVersion.id; } else { appVersionId = flags.appVersionId; } if (!appVersionId) { - const appAndAppVersion = await DynamicChoicesService.chooseAppAndAppVersion([APP_VERSION_STATUS.DRAFT]); + const allowedStatuses = force ? [APP_VERSION_STATUS.DRAFT, APP_VERSION_STATUS.LIVE] : [APP_VERSION_STATUS.DRAFT]; + const appAndAppVersion = await DynamicChoicesService.chooseAppAndAppVersion(allowedStatuses); appVersionId = appAndAppVersion.appVersionId; } diff --git a/src/services/app-versions-service.ts b/src/services/app-versions-service.ts index 9af1430..79e8c18 100644 --- a/src/services/app-versions-service.ts +++ b/src/services/app-versions-service.ts @@ -31,8 +31,11 @@ export const listAppVersionsByAppId = async (appId: AppId): Promise => { +export const defaultVersionByAppId = async (appId: AppId, useLiveVersion = false): Promise => { const appVersions = await listAppVersionsByAppId(appId); const latestVersion = appVersions.sort((a, b) => b.id - a.id)[0]; - return latestVersion.status === APP_VERSION_STATUS.DRAFT ? latestVersion : undefined; + const allowedStatuses = useLiveVersion + ? [APP_VERSION_STATUS.LIVE, APP_VERSION_STATUS.DRAFT] + : [APP_VERSION_STATUS.DRAFT]; + return allowedStatuses.includes(latestVersion.status) ? latestVersion : undefined; }; diff --git a/src/services/dynamic-choices-service.ts b/src/services/dynamic-choices-service.ts index 07a6e4e..8c89ee6 100644 --- a/src/services/dynamic-choices-service.ts +++ b/src/services/dynamic-choices-service.ts @@ -20,7 +20,7 @@ export const DynamicChoicesService = { return selectedAppId; }, - async chooseAppVersion(appId: number, filterByStatus?: [APP_VERSION_STATUS]) { + async chooseAppVersion(appId: number, filterByStatus?: APP_VERSION_STATUS[]) { let appVersions = await listAppVersionsByAppId(appId); if (filterByStatus) { appVersions = appVersions.filter(appVersion => filterByStatus.includes(appVersion.status)); @@ -42,7 +42,7 @@ export const DynamicChoicesService = { return selectedAppVersionId; }, - async chooseAppAndAppVersion(filterByStatus?: [APP_VERSION_STATUS]) { + async chooseAppAndAppVersion(filterByStatus?: APP_VERSION_STATUS[]) { const appId = await this.chooseApp(); const appVersionId = await this.chooseAppVersion(appId, filterByStatus); return { appId, appVersionId };