Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create app command #82

Merged
merged 4 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mondaycom/apps-cli",
"version": "2.3.0",
"version": "2.3.1",
"description": "A cli tool to manage apps (and monday-code projects) in monday.com",
"author": "monday.com Apps Team",
"type": "module",
Expand Down
21 changes: 21 additions & 0 deletions src/commands/app/__tests__/create.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import AppCreate from 'commands/app/create';
import { buildMockFlags, getStdout, mockRequestResolvedValueOnce } from 'test/cli-test-utils';

describe('app:create', () => {
const mockAppCreateResponse = {
app: {
id: 1_000_000_001,
name: 'New App by CLI',
state: 'active',
kind: 'private',
},
};

it('should create app successfully', async () => {
const mockPushFlags = buildMockFlags(AppCreate, { name: 'New App by CLI' });
mockRequestResolvedValueOnce(mockAppCreateResponse);
await AppCreate.run(mockPushFlags);
const stdout = getStdout();
expect(stdout).toContain('App created successfully: New App by CLI (id: 1000000001)');
});
});
6 changes: 3 additions & 3 deletions src/commands/app/__tests__/list.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AppList from 'commands/app/list';
import { getStderr, getStdout, mockRequestResolvedValueOnce } from 'test/cli-test-utils';
import { getStdout, mockRequestResolvedValueOnce } from 'test/cli-test-utils';

describe('app:list', () => {
const mockAppListResponse = {
Expand All @@ -26,7 +26,7 @@ describe('app:list', () => {
it('should print message if no apps', async () => {
mockRequestResolvedValueOnce({ apps: [] });
await AppList.run();
const stderr = getStderr();
expect(stderr).toContain('No apps found');
const stdout = getStdout();
expect(stdout).toContain('No apps found');
});
});
39 changes: 39 additions & 0 deletions src/commands/app/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { Flags } from '@oclif/core';

import { AuthenticatedCommand } from 'commands-base/authenticated-command';
import { createApp } from 'services/apps-service';
import logger from 'utils/logger';

export default class AppCreate extends AuthenticatedCommand {
maorb-dev marked this conversation as resolved.
Show resolved Hide resolved
static description = 'Create an app.';

static withPrintCommand = false;

static examples = ['<%= config.bin %> <%= command.id %>', '<%= config.bin %> <%= command.id %> -n NEW_APP_NAME'];

static flags = AppCreate.serializeFlags({
name: Flags.string({
char: 'n',
description: 'Name your new app.',
required: false,
}),
});

DEBUG_TAG = 'app_create';

public async run(): Promise<void> {
try {
const { flags } = await this.parse(AppCreate);
const { name } = flags;

logger.debug(`invoking create app: name=${name}`, this.DEBUG_TAG);
const app = await createApp({ name });
logger.success(`App created successfully: ${app.name} (id: ${app.id})`);
} catch (error: any) {
logger.debug(error, this.DEBUG_TAG);

// need to signal to the parent process that the command failed
process.exit(1);
}
}
}
4 changes: 4 additions & 0 deletions src/consts/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ export const listAppsUrl = (): string => {
return BASE_APPS_URL;
};

export const createAppUrl = (): string => {
return BASE_APPS_URL;
};

export const listAppVersionsByAppIdUrl = (appId: AppId): string => {
return `${BASE_APPS_URL}/${appId}/versions`;
};
Expand Down
30 changes: 27 additions & 3 deletions src/services/apps-service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { listAppsUrl } from 'consts/urls';
import { createAppUrl, listAppsUrl } from 'consts/urls';
import { execute } from 'services/api-service';
import { listAppSchema } from 'services/schemas/apps-service-schemas';
import { createAppSchema, listAppSchema } from 'services/schemas/apps-service-schemas';
import { HttpError } from 'types/errors';
import { HttpMethodTypes } from 'types/services/api-service';
import { App, ListAppResponse } from 'types/services/apps-service';
import { App, CreateAppResponse, ListAppResponse } from 'types/services/apps-service';
import { appsUrlBuilder } from 'utils/urls-builder';

export const listApps = async (): Promise<Array<App>> => {
Expand All @@ -28,3 +28,27 @@ export const listApps = async (): Promise<Array<App>> => {
throw new Error('Failed to list apps.');
}
};

export const createApp = async (body?: { name?: string }): Promise<App> => {
try {
const path = createAppUrl();
const url = appsUrlBuilder(path);
const response = await execute<CreateAppResponse>(
{
url,
headers: { Accept: 'application/json' },
method: HttpMethodTypes.POST,
body,
},
createAppSchema,
);

return response.app;
} catch (error: any) {
if (error instanceof HttpError) {
throw error;
}

throw new Error('Failed to create app.');
}
};
6 changes: 6 additions & 0 deletions src/services/schemas/apps-service-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ export const listAppSchema = z
apps: z.array(appSchema),
})
.merge(baseResponseHttpMetaDataSchema);

export const createAppSchema = z
.object({
app: appSchema,
})
.merge(baseResponseHttpMetaDataSchema);
3 changes: 2 additions & 1 deletion src/types/services/apps-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { z } from 'zod';

import { appSchema, listAppSchema } from 'services/schemas/apps-service-schemas';
import { appSchema, createAppSchema, listAppSchema } from 'services/schemas/apps-service-schemas';

export type App = z.infer<typeof appSchema>;
export type ListAppResponse = z.infer<typeof listAppSchema>;
export type CreateAppResponse = z.infer<typeof createAppSchema>;
Loading