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

Feat: support pushing app by app id instead of app version id #45

Merged
merged 5 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
44 changes: 44 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
function makeModuleNameMapper(srcPath, tsconfigPath) {
// Get paths from tsconfig
const tsConfig = require(tsconfigPath);
const {paths} = tsConfig.compilerOptions;

const aliases = {};

// Iterate over paths and convert them into moduleNameMapper format
Object.entries(paths).forEach(([item, itemPaths]) => {
const key = `^${item.replace('/*', '/(.*)')}$`;
const {jestPath, basePath} = srcPath[itemPaths[0].split('/')[1]];
const path = paths[item][0].replace(basePath, '').replace('*', '$1');
aliases[key] = jestPath + '/' + path;
});
return aliases;
}

const TS_CONFIG_PATH = './tsconfig.json';
const SRC_PATH_MAPPING = {
src: {jestPath: '<rootDir>/src', basePath: './src/'},
test: {jestPath: '<rootDir>/test', basePath: './test/'},
};

module.exports = {
moduleNameMapper: makeModuleNameMapper(SRC_PATH_MAPPING, TS_CONFIG_PATH),
preset: 'ts-jest',
testEnvironment: 'node',
setupFiles: ['<rootDir>/test/test-setup.ts'],
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
coverageReporters: ['json', 'lcov'],
coveragePathIgnorePatterns: [
'/node_modules/',
'<rootDir>/src/types',
'<rootDir>/src/dotenv-override.ts',
'<rootDir>/src/utils/validation-utils.ts',
],
clearMocks: true,
globals: {
'ts-jest': {
tsconfig: '<rootDir>/test/jest.tsconfig.json',
},
},
testTimeout: 10000,
};
15 changes: 0 additions & 15 deletions jest.config.js

This file was deleted.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"lint:fix": "npm run lint -- --fix",
"postpack": "shx rm -f oclif.manifest.json",
"posttest": "yarn lint",
"test": "mocha --forbid-only \"test/**/*.test.ts\"",
"test": "jest",
"prepack": "yarn build && oclif manifest && oclif readme",
"version": "oclif readme && git add README.md",
"prettier:check": "prettier --check ./src",
Expand Down Expand Up @@ -79,6 +79,7 @@
"@types/glob": "^8.0.0",
"@types/inquirer": "^9.0.3",
"@types/inquirer-autocomplete-prompt": "^3.0.0",
"@types/jest": "^29.5.6",
"@types/mocha": "^10.0.1",
"@types/node": "^18.11.9",
"@types/parse-gitignore": "^1.0.0",
Expand All @@ -95,10 +96,12 @@
"eslint-plugin-n": "^15.5.1",
"eslint-plugin-prettier": "5.0.0",
"eslint-plugin-unicorn": "^47.0.0",
"jest": "^29.7.0",
"mocha": "^10.2.0",
"oclif": "^3.9.1",
"prettier": "^3.0.0",
"shx": "^0.3.3",
"ts-jest": "^29.1.1",
"tsc-alias": "^1.8.6",
"tslib": "^2.4.1",
"tsx": "^3.12.7",
Expand Down
24 changes: 20 additions & 4 deletions src/commands/code/push.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Flags } from '@oclif/core';
import { Listr } from 'listr2';

import { AuthenticatedCommand } from 'commands-base/authenticated-command';
import { APP_VERSION_ID_TO_ENTER } from 'consts/messages';
import { APP_ID_TO_ENTER, APP_VERSION_ID_TO_ENTER } from 'consts/messages';
import { defaultVersionByAppId } from 'services/app-versions-service';
import { DynamicChoicesService } from 'services/dynamic-choices-service';
import {
buildAssetToDeployTask,
Expand All @@ -16,22 +17,26 @@ import logger from 'utils/logger';
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,
};

export default class Push extends AuthenticatedCommand {
DEBUG_TAG = 'code_push';
static description = 'Push your project to get hosted on monday-code.';

static examples = [
'<%= config.bin %> <%= command.id %> -d PROJECT DIRECTORY PATH -i APP_VERSION_ID_TO_PUSH',
'<%= config.bin %> <%= command.id %> -i APP_VERSION_ID_TO_PUSH',
'<%= config.bin %> <%= command.id %> -a APP_ID_TO_PUSH',
];

static flags = Push.serializeFlags({
directoryPath: Flags.string({
char: 'd',
description: MESSAGES.directory,
}),
appId: Flags.string({
char: 'a',
description: MESSAGES.appId,
}),
appVersionId: Flags.integer({
char: 'i',
aliases: ['v'],
Expand All @@ -40,16 +45,27 @@ export default class Push extends AuthenticatedCommand {
});

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

public async run(): Promise<void> {
const { flags } = await this.parse(Push);
let appVersionId;

const appId = flags.appId;
if (appId) {
const latestDraftVersion = await defaultVersionByAppId(Number(appId));
if (!latestDraftVersion) throw new Error('No draft version found for the given app id.');
appVersionId = latestDraftVersion.id;
} else {
appVersionId = flags.appVersionId;
}

let appVersionId = flags.appVersionId;
if (!appVersionId) {
const appAndAppVersion = await DynamicChoicesService.chooseAppAndAppVersion();
appVersionId = appAndAppVersion.appVersionId;
}

logger.debug(`push code to appVersionId: ${appVersionId}`, this.DEBUG_TAG);
this.preparePrintCommand(this, { appVersionId, directoryPath: flags.directoryPath });
const tasks = new Listr<PushCommandTasksContext>(
[
Expand Down
4 changes: 2 additions & 2 deletions src/services/api-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { ZodObject } from 'zod/lib/types';

import { CONFIG_KEYS } from 'consts/config';
import { ACCESS_TOKEN_NOT_FOUND } from 'consts/messages';
import { ConfigService } from 'services/config-service.js';
import { getAppsDomain } from 'services/env-service.js';
import { ConfigService } from 'services/config-service';
import { getAppsDomain } from 'services/env-service';
import { HttpError } from 'types/errors';
import { BaseErrorResponse, BaseResponseHttpMetaData, ExecuteParams } from 'types/services/api-service';
import { wrapInBox } from 'utils/cli-utils';
Expand Down
52 changes: 52 additions & 0 deletions src/services/app-versions-service.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// eslint-disable-next-line node/no-extraneous-import,n/no-extraneous-import
import { describe, expect, it, jest } from '@jest/globals';

import { APP_VERSION_STATUS } from 'consts/app-versions';
import * as appVersionService from 'services/app-versions-service';

const appId = 1;

const appWithDraftsVersions = [
{
id: 1,
name: 'name',
versionNumber: 'versionNumber',
appId: appId,
status: APP_VERSION_STATUS.DRAFT,
},
{
id: 2,
name: 'name',
versionNumber: 'versionNumber',
appId: appId,
status: APP_VERSION_STATUS.DRAFT,
},
];

const appWithLiveVersion = [
{
id: 1,
name: 'name',
versionNumber: 'versionNumber',
appId: appId,
status: APP_VERSION_STATUS.LIVE,
},
];

const mockedListAppVersionsByAppId = jest.spyOn(appVersionService, 'listAppVersionsByAppId');

describe('AppVersionsService', () => {
describe('defaultVersionByAppId', () => {
it('should return the latest draft version for the app', async () => {
mockedListAppVersionsByAppId.mockResolvedValue(appWithDraftsVersions);
const defaultVersion = await appVersionService.defaultVersionByAppId(appId);
expect(defaultVersion?.id).toEqual(2);
});

it('should return the latest draft version for the app', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename

mockedListAppVersionsByAppId.mockResolvedValue(appWithLiveVersion);
const defaultVersion = await appVersionService.defaultVersionByAppId(appId);
expect(defaultVersion?.id).not.toBeDefined();
});
});
});
7 changes: 7 additions & 0 deletions src/services/app-versions-service.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { APP_VERSION_STATUS } from 'consts/app-versions';
import { listAppVersionsByAppIdUrl } from 'consts/urls';
import { execute } from 'services/api-service';
import { listAppVersionsSchema } from 'services/schemas/app-versions-schemas';
Expand Down Expand Up @@ -28,3 +29,9 @@ export const listAppVersionsByAppId = async (appId: AppId): Promise<Array<AppVer
throw new Error('Failed to list app versions.');
}
};

export const defaultVersionByAppId = async (appId: AppId): Promise<AppVersion | undefined> => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add unit test

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;
};
7 changes: 7 additions & 0 deletions test/jest.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"types": ["node", "jest"],
"sourceMap": true
}
}
6 changes: 6 additions & 0 deletions test/test-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
process.on('unhandledRejection', (err) => {
console.log(err);
throw new Error(
"Got unhandled rejection (see above)! maybe you missed await on 'expect(..).rejects...' or 'expect(..).resolves...' ?"
);
});
37 changes: 26 additions & 11 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,33 @@
"target": "ES2022",
"lib": [
"ES2022",
"DOM" /*Added it to support PUSHER-JS lib*/
"DOM"
],
"paths": {
"src/*": ["./src/*"],
"consts/*": ["./src/consts/*"],
"services/*": ["./src/services/*"],
"types/*": ["./src/types/*"],
"errors/*": ["./src/errors/*"],
"utils/*": ["./src/utils/*"],
"commands/*": ["./src/commands/*"],
"commands-base/*": ["./src/commands-base/*"]
"src/*": [
"./src/*"
],
"consts/*": [
"./src/consts/*"
],
"services/*": [
"./src/services/*"
],
"types/*": [
"./src/types/*"
],
"errors/*": [
"./src/errors/*"
],
"utils/*": [
"./src/utils/*"
],
"commands/*": [
"./src/commands/*"
],
"commands-base/*": [
"./src/commands-base/*"
]
}
},
"include": [
Expand All @@ -33,7 +49,6 @@
"node_modules",
"bin",
"dist",
"__tests__",
"**/*.test.ts"
"__tests__"
]
}
Loading
Loading