Skip to content

Commit

Permalink
feat: ✨ support for adding new web config added (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
WasiqB authored May 18, 2024
1 parent 5c74c7a commit 381d928
Show file tree
Hide file tree
Showing 13 changed files with 345 additions and 224 deletions.
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@
"url": "https://github.com/BoykaFramework/boyka-cli"
},
"devDependencies": {
"@eslint/js": "^9.2.0",
"@eslint/js": "^9.3.0",
"@release-it/conventional-changelog": "^8.0.1",
"@stylistic/eslint-plugin-js": "^2.1.0",
"@stylistic/eslint-plugin-ts": "^2.1.0",
"@tsconfig/node18": "^18.2.4",
"@types/node": "^20.12.11",
"@types/node": "^20.12.12",
"@types/yargs": "^17.0.32",
"@typescript-eslint/eslint-plugin": "^7.2.0",
"@typescript-eslint/parser": "^7.2.0",
"eslint": "^9.2.0",
"@typescript-eslint/eslint-plugin": "^7.9.0",
"@typescript-eslint/parser": "^7.9.0",
"eslint": "^9.3.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^5.1.3",
Expand All @@ -56,10 +56,10 @@
"prettier": "^3.2.5",
"release-it": "^17.1.1",
"typescript": "^5.4.5",
"typescript-eslint": "^7.8.0"
"typescript-eslint": "^7.9.0"
},
"dependencies": {
"@inquirer/prompts": "^5.0.2",
"@inquirer/prompts": "^5.0.4",
"chalk": "^5.3.0",
"nanospinner": "^1.1.0",
"yargs": "^17.7.2"
Expand Down
388 changes: 232 additions & 156 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

10 changes: 2 additions & 8 deletions src/commands/configure_cmds/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommandModule } from 'yargs';
import { epiLogMessage, errorMessage, failureMessage, helpMessage } from '../../utils/constants.js';
import { epiLogMessage, failureMessage, handleCommand } from '../../utils/constants.js';
import { handleAddApiConfig } from '../../handler/config/init/api.js';

export const apiCommand = {
Expand Down Expand Up @@ -29,12 +29,6 @@ export const apiCommand = {
.showHelpOnFail(true, failureMessage('API Config'))
.epilog(epiLogMessage),
handler: async (argv) => {
try {
await handleAddApiConfig(argv);
console.log(helpMessage);
} catch (error: any) {
console.error(errorMessage(error));
process.exit(1);
}
await handleCommand(handleAddApiConfig(argv));
},
} satisfies CommandModule;
22 changes: 2 additions & 20 deletions src/commands/configure_cmds/init.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
import { CommandModule } from 'yargs';
import {
capabilitiesHelpMessage,
epiLogMessage,
errorMessage,
failureMessage,
getTarget,
helpMessage,
} from '../../utils/constants.js';
import { epiLogMessage, failureMessage, handleCommand } from '../../utils/constants.js';
import { handleConfigInit } from '../../handler/config/init/init.js';
import { TargetProviders } from '../../types/enum-types.js';

export const initCommand = {
command: 'init',
Expand All @@ -26,16 +18,6 @@ export const initCommand = {
.showHelpOnFail(true, failureMessage('Init Config'))
.epilog(epiLogMessage),
handler: async (argv) => {
try {
await handleConfigInit(argv);
const target = getTarget();
if (target && target !== TargetProviders.LOCAL) {
console.log(capabilitiesHelpMessage);
}
console.log(helpMessage);
} catch (error: any) {
console.error(errorMessage(error));
process.exit(1);
}
await handleCommand(handleConfigInit(argv));
},
} satisfies CommandModule;
15 changes: 11 additions & 4 deletions src/commands/configure_cmds/web.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { CommandModule } from 'yargs';
import { epiLogMessage, failureMessage } from '../../utils/constants.js';
import { epiLogMessage, failureMessage, handleCommand } from '../../utils/constants.js';
import { handleAddWebConfig } from '../../handler/config/init/web.js';

export const webCommand = {
command: 'web [name]',
aliases: ['w'],
describe: 'Add Web Boyka-Framework Config file',
builder: (yargs) =>
yargs
.option('p', {
alias: ['path'],
describe: 'Path to the config file',
default: `${process.cwd()}/src/test/resources`,
type: 'string',
})
.positional('name', {
demandOption: true,
describe: 'Name of the Web config block',
Expand All @@ -16,12 +23,12 @@ export const webCommand = {
if (!argv.name) {
throw new Error('Web config name should be provided!');
}
return true;
})
.help('help')
.showHelpOnFail(true, failureMessage('Web config'))
.epilog(epiLogMessage),
handler: (argv) => {
// TODO: web handler
console.log(`Handle Web Config ${argv.name}...`);
handler: async (argv) => {
await handleCommand(handleAddWebConfig(argv));
},
} satisfies CommandModule;
12 changes: 3 additions & 9 deletions src/handler/config/init/api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getConfigName } from '../../../questions/inputs.js';
import { FrameworkSetting } from '../../../types/config-type.js';
import { updateApi } from '../../update/api.js';
import { defaultApiSetting } from '../../../types/default-type-values.js';
import { defaultNewApiSetting } from '../../../types/default-type-values.js';
import { ArgumentsCamelCase } from 'yargs';
import { createConfigFile, loadJSON } from '../../../utils/json.js';
import { configBlockExists } from '../../../utils/constants.js';
Expand All @@ -10,9 +10,7 @@ export const createApiSetting = async () => {
const configName = await getConfigName('API');
const frameworkSetting: FrameworkSetting = {
api: {
[configName]: {
...defaultApiSetting,
},
...defaultNewApiSetting(configName),
},
};
const api = frameworkSetting.api;
Expand All @@ -30,11 +28,7 @@ export const handleAddApiConfig = async (argv: ArgumentsCamelCase) => {
if (apiSetting[name]) {
throw new Error(configBlockExists('API', name));
}
const newApiSetting = {
[name]: {
...defaultApiSetting,
},
};
const newApiSetting = defaultNewApiSetting(name);
await updateApi(newApiSetting[name]);
settings.api = {
...apiSetting,
Expand Down
10 changes: 6 additions & 4 deletions src/handler/config/init/mobile.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { getConfigName } from '../../../questions/inputs.js';
import { FrameworkSetting } from '../../../types/config-type.js';
import { updateMobile } from '../../update/mobile.js';
import { defaultUiSetting, defaultMobileSetting } from '../../../types/default-type-values.js';
import { defaultUiSetting, defaultNewMobileSetting } from '../../../types/default-type-values.js';
import { defaultDelaySetting } from '../../../types/default-type-values.js';

export const createMobileSetting = async (platformType: string) => {
const configName = await getConfigName(platformType);
const frameworkSetting: FrameworkSetting = {
ui: {
delay: {
...defaultDelaySetting,
},
...defaultUiSetting,
mobile: {
[configName]: {
...defaultMobileSetting,
},
...defaultNewMobileSetting(configName),
},
},
};
Expand Down
41 changes: 36 additions & 5 deletions src/handler/config/init/web.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
import { getConfigName } from '../../../questions/inputs.js';
import { FrameworkSetting } from '../../../types/config-type.js';
import { FrameworkSetting, WebSetting } from '../../../types/config-type.js';
import { updateWeb } from '../../update/web.js';
import { defaultUiSetting, defaultWebSetting } from '../../../types/default-type-values.js';
import { defaultNewWebSetting, defaultUiSetting } from '../../../types/default-type-values.js';
import { ArgumentsCamelCase } from 'yargs';
import { createConfigFile, loadJSON } from '../../../utils/json.js';
import { configBlockExists } from '../../../utils/constants.js';

export const createWebSetting = async () => {
const configName = await getConfigName('Web');
const frameworkSetting: FrameworkSetting = {
ui: {
...defaultUiSetting,
web: {
[configName]: {
...defaultWebSetting,
},
...defaultNewWebSetting(configName),
},
},
};
const web = frameworkSetting.ui?.web;
if (web) await updateWeb(web[configName]);
return frameworkSetting;
};

export const handleAddWebConfig = async (argv: ArgumentsCamelCase) => {
const name = argv.name as string;
const path = argv.path as string;
const configPath = path === '.' ? process.cwd() : path;

const settings = loadJSON(configPath) as FrameworkSetting;
let uiSetting = settings.ui;
let webSetting: { [key: string]: WebSetting };
if (uiSetting) {
webSetting = uiSetting.web;
if (webSetting[name]) {
throw new Error(configBlockExists('Web', name));
}
} else {
uiSetting = {
...defaultUiSetting,
};
}
const newWebSetting = defaultNewWebSetting(name);
await updateWeb(newWebSetting[name]);
settings.ui = {
...uiSetting,
web: {
...webSetting,
...newWebSetting,
},
};
createConfigFile(configPath, settings, 'updated');
};
1 change: 1 addition & 0 deletions src/handler/update/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const updateWeb = async (config: WebSetting) => {
config.target = target as TargetProviders;
setTarget(config.target);
if (config.target !== TargetProviders.LOCAL) {
config.capabilities = {};
config.user_name = `\${env:${await getUserName()}}`;
config.password = `\${env:${await getPassword()}}`;
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/config-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export type DelaySetting = {

export type UiSetting = {
timeout: TimeoutSetting;
delay: DelaySetting;
delay?: DelaySetting;
logging: UiLogSetting;
screenshot: ScreenshotSetting;
web?: { [key: string]: WebSetting };
Expand Down
26 changes: 23 additions & 3 deletions src/types/default-type-values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,6 @@ export const defaultDelaySetting: DelaySetting = {
};

export const defaultUiSetting: UiSetting = {
delay: {
...defaultDelaySetting,
},
logging: {
...defaultUiLogSetting,
},
Expand Down Expand Up @@ -178,3 +175,26 @@ export const defaultMobileSetting: MobileSetting = {
server: defaultServerSetting,
device: defaultDeviceSetting,
};

export const defaultNewWebSetting = (configName: string): { [key: string]: WebSetting } => {
return {
[configName]: {
...defaultWebSetting,
},
};
};
export const defaultNewMobileSetting = (configName: string): { [key: string]: MobileSetting } => {
return {
[configName]: {
...defaultMobileSetting,
},
};
};

export const defaultNewApiSetting = (configName: string): { [key: string]: ApiSetting } => {
return {
[configName]: {
...defaultApiSetting,
},
};
};
14 changes: 7 additions & 7 deletions src/types/enum-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,17 @@ export enum ApplicationType {
}

export enum TestDataSource {
EXCEL,
EXCEL = 'EXCEL',
}

export enum PageLoadStrategy {
NONE,
EAGER,
NORMAL,
NONE = 'NONE',
EAGER = 'EAGER',
NORMAL = 'NORMAL',
}

export enum Speed {
FAST,
NORMAL,
SLOW,
FAST = 'FAST',
NORMAL = 'NORMAL',
SLOW = 'SLOW',
}
14 changes: 14 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,17 @@ Boyka config file does not exist at [${path}].
Create one by running command 'boyka config init'`,
);

export const handleCommand = async (handler: Promise<void>) => {
try {
await handler;
const target = getTarget();
if (target && target !== TargetProviders.LOCAL) {
console.log(capabilitiesHelpMessage);
}
console.log(helpMessage);
} catch (error: any) {
console.error(errorMessage(error));
process.exit(1);
}
};

0 comments on commit 381d928

Please sign in to comment.