Skip to content

Commit

Permalink
Feat: sort apps and versions by created date, filter only drafts for …
Browse files Browse the repository at this point in the history
…push
  • Loading branch information
Your Full Name committed Oct 30, 2023
1 parent 01bb8d5 commit 738520d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
15 changes: 11 additions & 4 deletions src/commands/code/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -43,26 +44,32 @@ export default class Push extends AuthenticatedCommand {
aliases: ['v'],
description: MESSAGES.appVersionId,
}),
force: Flags.boolean({
char: 'f',
description: MESSAGES.force,
}),
});

static args = {};
DEBUG_TAG = 'code_push';

public async run(): Promise<void> {
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;
}

Expand Down
7 changes: 5 additions & 2 deletions src/services/app-versions-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ export const listAppVersionsByAppId = async (appId: AppId): Promise<Array<AppVer
}
};

export const defaultVersionByAppId = async (appId: AppId): Promise<AppVersion | undefined> => {
export const defaultVersionByAppId = async (appId: AppId, useLiveVersion = false): Promise<AppVersion | undefined> => {
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;
};
4 changes: 2 additions & 2 deletions src/services/dynamic-choices-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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 };
Expand Down

0 comments on commit 738520d

Please sign in to comment.