Skip to content

Commit

Permalink
chore: re-write some files to typescript - part 3 (#489)
Browse files Browse the repository at this point in the history
* chore: rename static report builder to typescript

* chore: rename plugin-events to typescript

* chore: re-write plugin-events to typescript

* chore: re-write local-images-saver to typescript

* chore: re-write browser constants to typescript

* chore: rename base tests tree builder to typescript

* chore: re-write base tests tree builder to typescript

* chore: rename static tests tree builder to typescript

* chore: re-write static tests tree builder to typescript

* chore: rename db-utils/common to typescript

* chore: re-write db-utils/common to typescript

* chore: rename db-utils/server to typescript

* chore: re-write db-utils/server to typescript

* chore: rename plugin-api to typescript

* chore: re-write plugin-api to typescript

* chore: re-write static report builder to typescript

* chore: re-write create-workers to typescript

* chore: re-write cli-commands/index to typescript

* chore: re-write defaults constants to typescript

* chore: re-write save-formats constants to typescript

* chore: re-write custom-gui-asserts to typescript

* chore: re-write config to typescript

* chore: re-write plugin-adapter to typescript

* chore: re-write suite-adapter to typescript

* chore: fix cliCommands imports
  • Loading branch information
shadowusr authored Aug 23, 2023
1 parent 76b8fe3 commit 7779714
Show file tree
Hide file tree
Showing 63 changed files with 1,048 additions and 821 deletions.
2 changes: 1 addition & 1 deletion docs/en/html-reporter-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ A database instance is passed to the event handler.
### Usage example

```javascript
const parseConfig = require('./config');
const {parseConfig} = require('./config');

module.exports = (hermione, opts) => {
const pluginConfig = parseConfig(opts);
Expand Down
2 changes: 1 addition & 1 deletion docs/ru/html-reporter-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ hermione.htmlReporter.on(hermione.htmlReporter.events.DATABASE_CREATED, (db) =>
### Пример использования

```javascript
const parseConfig = require('./config');
const {parseConfig} = require('./config');

module.exports = (hermione, opts) => {
const pluginConfig = parseConfig(opts);
Expand Down
4 changes: 2 additions & 2 deletions hermione.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

const os = require('os');
const PQueue = require('p-queue');
const PluginAdapter = require('./lib/plugin-adapter');
const createWorkers = require('./lib/workers/create-workers');
const {PluginAdapter} = require('./lib/plugin-adapter');
const {createWorkers} = require('./lib/workers/create-workers');

let workers;

Expand Down
4 changes: 3 additions & 1 deletion lib/cli-commands/gui.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict';

const {cliCommands} = require('.');
const runGui = require('../gui').default;
const Api = require('../gui/api');
const {GUI: commandName} = require('./');

const {GUI: commandName} = cliCommands;

module.exports = (program, pluginConfig, hermione) => {
// must be executed here because it adds `gui` field in `gemini` and `hermione tool`,
Expand Down
6 changes: 2 additions & 4 deletions lib/cli-commands/index.js → lib/cli-commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict';

module.exports = {
export const cliCommands = {
GUI: 'gui',
MERGE_REPORTS: 'merge-reports',
REMOVE_UNUSED_SCREENS: 'remove-unused-screens'
};
} as const;
4 changes: 3 additions & 1 deletion lib/cli-commands/merge-reports.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict';

const {MERGE_REPORTS: commandName} = require('./');
const {cliCommands} = require('.');
const mergeReports = require('../merge-reports');
const {logError} = require('../server-utils');

const {MERGE_REPORTS: commandName} = cliCommands;

module.exports = (program, pluginConfig, hermione) => {
program
.command(`${commandName} [paths...]`)
Expand Down
4 changes: 3 additions & 1 deletion lib/cli-commands/remove-unused-screens/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ const chalk = require('chalk');
const filesize = require('filesize');
const Promise = require('bluebird');

const {REMOVE_UNUSED_SCREENS: commandName} = require('..');
const {cliCommands} = require('..');
const {getTestsFromFs, findScreens, askQuestion, identifyOutdatedScreens, identifyUnusedScreens, removeScreens} = require('./utils');
const {DATABASE_URLS_JSON_NAME, LOCAL_DATABASE_NAME} = require('../../constants/database');
const {logger} = require('../../common-utils');

const {REMOVE_UNUSED_SCREENS: commandName} = cliCommands;

// TODO: remove hack after add ability to add controllers from plugin in silent mode
function proxyHermione() {
const proxyHandler = {
Expand Down
20 changes: 10 additions & 10 deletions lib/common-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import crypto from 'crypto';
import {pick} from 'lodash';
import url from 'url';
import axios, {AxiosRequestConfig} from 'axios';
import {SUCCESS, FAIL, ERROR, SKIPPED, UPDATED, IDLE, RUNNING, QUEUED} from './constants/test-statuses';
import {SUCCESS, FAIL, ERROR, SKIPPED, UPDATED, IDLE, RUNNING, QUEUED, TestStatus} from './constants';

import {UNCHECKED, INDETERMINATE, CHECKED} from './constants/checked-statuses';
export const getShortMD5 = (str: string): string => {
return crypto.createHash('md5').update(str, 'ascii').digest('hex').substr(0, 7);
};

const statusPriority: string[] = [
const statusPriority: TestStatus[] = [
// non-final
RUNNING, QUEUED,

Expand All @@ -19,15 +19,15 @@ const statusPriority: string[] = [

export const logger = pick(console, ['log', 'warn', 'error']);

export const isSuccessStatus = (status: string): boolean => status === SUCCESS;
export const isFailStatus = (status: string): boolean => status === FAIL;
export const isIdleStatus = (status: string): boolean => status === IDLE;
export const isRunningStatus = (status: string): boolean => status === RUNNING;
export const isErroredStatus = (status: string): boolean => status === ERROR;
export const isSkippedStatus = (status: string): boolean => status === SKIPPED;
export const isUpdatedStatus = (status: string): boolean => status === UPDATED;
export const isSuccessStatus = (status: TestStatus): boolean => status === SUCCESS;
export const isFailStatus = (status: TestStatus): boolean => status === FAIL;
export const isIdleStatus = (status: TestStatus): boolean => status === IDLE;
export const isRunningStatus = (status: TestStatus): boolean => status === RUNNING;
export const isErroredStatus = (status: TestStatus): boolean => status === ERROR;
export const isSkippedStatus = (status: TestStatus): boolean => status === SKIPPED;
export const isUpdatedStatus = (status: TestStatus): boolean => status === UPDATED;

export const determineStatus = (statuses: string[]): string | null => {
export const determineStatus = (statuses: TestStatus[]): TestStatus | null => {
if (!statuses.length) {
return SUCCESS;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
'use strict';
import {isUndefined, isArray, isEmpty, isFunction, isPlainObject, isString} from 'lodash';
import CustomGuiControlTypes from '../gui/constants/custom-gui-control-types';

const {isUndefined, isArray, isEmpty, isFunction, isPlainObject, isString} = require('lodash');
const SUPPORTED_CONTROL_TYPES: string[] = Object.values(CustomGuiControlTypes);

const SUPPORTED_CONTROL_TYPES = Object.values(require('../gui/constants/custom-gui-control-types'));

const assertSectionGroupType = (context, type) => {
const assertSectionGroupType = (context: string, type: unknown): void => {
if (isUndefined(type)) {
throw new Error(`${context} must contain field "type"`);
}
Expand All @@ -16,7 +15,7 @@ const assertSectionGroupType = (context, type) => {
}
};

const assertSectionGroupControls = (context, controls) => {
const assertSectionGroupControls = (context: string, controls: unknown): void => {
if (isUndefined(controls)) {
throw new Error(`${context} must contain field "controls"`);
}
Expand All @@ -26,14 +25,14 @@ const assertSectionGroupControls = (context, controls) => {
if (isEmpty(controls)) {
throw new Error(`${context} must contain non-empty array in the field "controls"`);
}
controls.forEach((control) => {
controls.forEach((control: unknown) => {
if (!isPlainObject(control)) {
throw new Error(`${context} must contain objects in the array "controls"`);
}
});
};

const assertSectionGroupAction = (context, action) => {
const assertSectionGroupAction = (context: string, action: unknown): void => {
if (isUndefined(action)) {
throw new Error(`${context} must contain field "action"`);
}
Expand All @@ -42,34 +41,35 @@ const assertSectionGroupAction = (context, action) => {
}
};

const assertSectionGroup = (sectionName, group, groupIndex) => {
const assertSectionGroup = (sectionName: string, group: unknown, groupIndex: number): void => {
const context = `customGui["${sectionName}"][${groupIndex}]`;

if (!isPlainObject(group)) {
throw new Error(`${context} must be plain object, but got ${typeof group}`);
}

assertSectionGroupType(context, group.type);
assertSectionGroupControls(context, group.controls);
assertSectionGroupAction(context, group.action);
const groupObj = group as Record<string, unknown>;

assertSectionGroupType(context, groupObj.type);
assertSectionGroupControls(context, groupObj.controls);
assertSectionGroupAction(context, groupObj.action);
};

const assertSection = (section, sectionName) => {
const assertSection = (section: unknown, sectionName: string): void => {
if (!isArray(section)) {
throw new Error(`customGui["${sectionName}"] must be an array, but got ${typeof section}`);
}
section.forEach((group, groupIndex) => assertSectionGroup(sectionName, group, groupIndex));
section.forEach((group: unknown, groupIndex: number) => assertSectionGroup(sectionName, group, groupIndex));
};

const assertCustomGui = (customGui) => {
export const assertCustomGui = (customGui: unknown): void => {
if (!isPlainObject(customGui)) {
throw new Error(`"customGui" option must be plain object, but got ${typeof customGui}`);
}
for (const sectionName in customGui) {
assertSection(customGui[sectionName], sectionName);
}
};

module.exports = {
assertCustomGui
const customGuiObj = customGui as Record<string, unknown>;

for (const sectionName in customGuiObj) {
assertSection(customGuiObj[sectionName], sectionName);
}
};
Loading

0 comments on commit 7779714

Please sign in to comment.