chore: lint

This commit is contained in:
karishmas6
2024-06-08 00:46:59 +05:30
parent 920ebe8bce
commit 7e1e8f2920

View File

@@ -19,7 +19,7 @@ import { browserPool } from "../../server";
interface PersistedGeneratedData { interface PersistedGeneratedData {
lastUsedSelector: string; lastUsedSelector: string;
lastIndex: number|null; lastIndex: number | null;
lastAction: string; lastAction: string;
} }
@@ -44,7 +44,7 @@ export class WorkflowGenerator {
* The socket used to communicate with the client. * The socket used to communicate with the client.
* @private * @private
*/ */
private socket : Socket; private socket: Socket;
/** /**
* The public constructor of the WorkflowGenerator. * The public constructor of the WorkflowGenerator.
@@ -100,9 +100,9 @@ export class WorkflowGenerator {
}); });
socket.on('new-recording', () => this.workflowRecord = { socket.on('new-recording', () => this.workflowRecord = {
workflow: [], workflow: [],
} ); });
socket.on('activeIndex', (data) => this.generatedData.lastIndex = parseInt(data)); socket.on('activeIndex', (data) => this.generatedData.lastIndex = parseInt(data));
socket.on('decision', async ({pair, actionType, decision}) => { socket.on('decision', async ({ pair, actionType, decision }) => {
const id = browserPool.getActiveBrowserId(); const id = browserPool.getActiveBrowserId();
if (id) { if (id) {
const activeBrowser = browserPool.getRemoteBrowser(id); const activeBrowser = browserPool.getRemoteBrowser(id);
@@ -148,7 +148,7 @@ export class WorkflowGenerator {
* @private * @private
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
private addPairToWorkflowAndNotifyClient = async(pair: WhereWhatPair, page: Page) => { private addPairToWorkflowAndNotifyClient = async (pair: WhereWhatPair, page: Page) => {
let matched = false; let matched = false;
// validate if a pair with the same where conditions is already present in the workflow // validate if a pair with the same where conditions is already present in the workflow
if (pair.where.selectors && pair.where.selectors[0]) { if (pair.where.selectors && pair.where.selectors[0]) {
@@ -197,7 +197,7 @@ export class WorkflowGenerator {
} }
} }
this.socket.emit('workflow', this.workflowRecord); this.socket.emit('workflow', this.workflowRecord);
logger.log('info',`Workflow emitted`); logger.log('info', `Workflow emitted`);
}; };
/** /**
@@ -235,14 +235,14 @@ export class WorkflowGenerator {
* @param page The page to use for obtaining the needed data. * @param page The page to use for obtaining the needed data.
* @returns {Promise<void>} * @returns {Promise<void>}
*/ */
public onChangeUrl = async(newUrl: string, page: Page) => { public onChangeUrl = async (newUrl: string, page: Page) => {
this.generatedData.lastUsedSelector = ''; this.generatedData.lastUsedSelector = '';
const pair: WhereWhatPair = { const pair: WhereWhatPair = {
where: { url: this.getBestUrl(page.url()) }, where: { url: this.getBestUrl(page.url()) },
what: [ what: [
{ {
action: 'goto', action: 'goto',
args: [newUrl], args: [newUrl],
} }
], ],
} }
@@ -284,7 +284,7 @@ export class WorkflowGenerator {
*/ */
public customAction = async (action: CustomActions, settings: any, page: Page) => { public customAction = async (action: CustomActions, settings: any, page: Page) => {
const pair: WhereWhatPair = { const pair: WhereWhatPair = {
where: { url: this.getBestUrl(page.url())}, where: { url: this.getBestUrl(page.url()) },
what: [{ what: [{
action, action,
args: settings ? Array.isArray(settings) ? settings : [settings] : [], args: settings ? Array.isArray(settings) ? settings : [settings] : [],
@@ -297,7 +297,8 @@ export class WorkflowGenerator {
lastData: { lastData: {
selector: this.generatedData.lastUsedSelector, selector: this.generatedData.lastUsedSelector,
action: this.generatedData.lastAction, action: this.generatedData.lastAction,
} }); }
});
} else { } else {
await this.addPairToWorkflowAndNotifyClient(pair, page); await this.addPairToWorkflowAndNotifyClient(pair, page);
} }
@@ -337,7 +338,7 @@ export class WorkflowGenerator {
logger.log('debug', `pair ${index}: Added to workflow file.`); logger.log('debug', `pair ${index}: Added to workflow file.`);
} else if (index < this.workflowRecord.workflow.length && index >= 0) { } else if (index < this.workflowRecord.workflow.length && index >= 0) {
this.workflowRecord.workflow.splice( this.workflowRecord.workflow.splice(
this.workflowRecord.workflow.length - index , 0, pair); this.workflowRecord.workflow.length - index, 0, pair);
} else { } else {
logger.log('error', `Add pair ${index}: Index out of range.`); logger.log('error', `Add pair ${index}: Index out of range.`);
} }
@@ -362,7 +363,7 @@ export class WorkflowGenerator {
* @param socket The socket to be used for communication. * @param socket The socket to be used for communication.
* @returns void * @returns void
*/ */
public updateSocket = (socket: Socket) : void => { public updateSocket = (socket: Socket): void => {
this.socket = socket; this.socket = socket;
this.registerEventHandlers(socket); this.registerEventHandlers(socket);
}; };
@@ -374,15 +375,15 @@ export class WorkflowGenerator {
* @returns {WorkflowFile} * @returns {WorkflowFile}
*/ */
private removeAllGeneratedFlags = (workflow: WorkflowFile): WorkflowFile => { private removeAllGeneratedFlags = (workflow: WorkflowFile): WorkflowFile => {
for (let i = 0; i < workflow.workflow.length; i++) { for (let i = 0; i < workflow.workflow.length; i++) {
if ( if (
workflow.workflow[i].what[0] && workflow.workflow[i].what[0] &&
workflow.workflow[i].what[0].action === 'flag' && workflow.workflow[i].what[0].action === 'flag' &&
workflow.workflow[i].what[0].args?.includes('generated')) { workflow.workflow[i].what[0].args?.includes('generated')) {
workflow.workflow[i].what.splice(0, 1); workflow.workflow[i].what.splice(0, 1);
}
} }
return workflow; }
return workflow;
}; };
/** /**
@@ -440,7 +441,7 @@ export class WorkflowGenerator {
JSON.stringify({ recording_meta: this.recordingMeta, recording }, null, 2) JSON.stringify({ recording_meta: this.recordingMeta, recording }, null, 2)
); );
} }
catch (e) { catch (e) {
const { message } = e as Error; const { message } = e as Error;
logger.log('warn', `Cannot save the file to the local file system`) logger.log('warn', `Cannot save the file to the local file system`)
} }
@@ -456,7 +457,7 @@ export class WorkflowGenerator {
* @private * @private
* @returns {Promise<string|null>} * @returns {Promise<string|null>}
*/ */
private generateSelector = async (page:Page, coordinates:Coordinates, action: ActionType) => { private generateSelector = async (page: Page, coordinates: Coordinates, action: ActionType) => {
const elementInfo = await getElementInformation(page, coordinates); const elementInfo = await getElementInformation(page, coordinates);
const bestSelector = getBestSelectorForAction( const bestSelector = getBestSelectorForAction(
{ {
@@ -495,7 +496,7 @@ export class WorkflowGenerator {
* @param fromNavBar Whether the navigation is from the simulated browser's navbar or not. * @param fromNavBar Whether the navigation is from the simulated browser's navbar or not.
* @returns void * @returns void
*/ */
public notifyUrlChange = (url:string) => { public notifyUrlChange = (url: string) => {
if (this.socket) { if (this.socket) {
this.socket.emit('urlChanged', url); this.socket.emit('urlChanged', url);
} }
@@ -530,7 +531,7 @@ export class WorkflowGenerator {
//it's safe to always add a go back action to the first rule in the workflow //it's safe to always add a go back action to the first rule in the workflow
this.workflowRecord.workflow[0].what.push({ this.workflowRecord.workflow[0].what.push({
action: 'goBack', action: 'goBack',
args: [{waitUntil: 'commit'}], args: [{ waitUntil: 'commit' }],
}); });
this.notifyUrlChange(newUrl); this.notifyUrlChange(newUrl);
this.socket.emit('workflow', this.workflowRecord); this.socket.emit('workflow', this.workflowRecord);
@@ -548,7 +549,7 @@ export class WorkflowGenerator {
//it's safe to always add a go forward action to the first rule in the workflow //it's safe to always add a go forward action to the first rule in the workflow
this.workflowRecord.workflow[0].what.push({ this.workflowRecord.workflow[0].what.push({
action: 'goForward', action: 'goForward',
args: [{waitUntil: 'commit'}], args: [{ waitUntil: 'commit' }],
}); });
this.notifyUrlChange(newUrl); this.notifyUrlChange(newUrl);
this.socket.emit('workflow', this.workflowRecord); this.socket.emit('workflow', this.workflowRecord);
@@ -572,7 +573,7 @@ export class WorkflowGenerator {
const haveSameUrl = this.workflowRecord.workflow const haveSameUrl = this.workflowRecord.workflow
.filter((p, index) => { .filter((p, index) => {
if (p.where.url === pair.where.url) { if (p.where.url === pair.where.url) {
possibleOverShadow.push({index: index, isOverShadowing: false}); possibleOverShadow.push({ index: index, isOverShadowing: false });
return true; return true;
} else { } else {
return false; return false;
@@ -616,7 +617,7 @@ export class WorkflowGenerator {
break; break;
} else { } else {
// add new selector to the where part of the overshadowing pair // add new selector to the where part of the overshadowing pair
this.workflowRecord.workflow[index].where.selectors?.push(selector); this.workflowRecord.workflow[index].where.selectors?.push(selector);
} }
} }
} }
@@ -641,7 +642,7 @@ export class WorkflowGenerator {
*/ */
private getBestUrl = (url: string) => { private getBestUrl = (url: string) => {
const parsedUrl = new URL(url); const parsedUrl = new URL(url);
const protocol = parsedUrl.protocol === 'https:' || parsedUrl.protocol === 'http:' ? `${parsedUrl.protocol}//`: parsedUrl.protocol; const protocol = parsedUrl.protocol === 'https:' || parsedUrl.protocol === 'http:' ? `${parsedUrl.protocol}//` : parsedUrl.protocol;
const regex = new RegExp(/(?=.*[A-Z])/g) const regex = new RegExp(/(?=.*[A-Z])/g)
// remove all params with uppercase letters, they are most likely dynamically generated // remove all params with uppercase letters, they are most likely dynamically generated
// also escapes all regex characters from the params // also escapes all regex characters from the params
@@ -670,7 +671,7 @@ export class WorkflowGenerator {
* Returns parameters if present in the workflow or null. * Returns parameters if present in the workflow or null.
* @param workflow The workflow to be checked. * @param workflow The workflow to be checked.
*/ */
private checkWorkflowForParams = (workflow: WorkflowFile): string[]|null => { private checkWorkflowForParams = (workflow: WorkflowFile): string[] | null => {
// for now the where condition cannot have any params, so we're checking only what part of the pair // for now the where condition cannot have any params, so we're checking only what part of the pair
// where only the args part of what condition can have a parameter // where only the args part of what condition can have a parameter
for (const pair of workflow.workflow) { for (const pair of workflow.workflow) {
@@ -715,15 +716,17 @@ export class WorkflowGenerator {
// when more than one press action is present, add a type action // when more than one press action is present, add a type action
pair.what.splice(index - input.actionCounter, input.actionCounter, { pair.what.splice(index - input.actionCounter, input.actionCounter, {
action: 'type', action: 'type',
args: [input.selector, input.value], }, { args: [input.selector, input.value],
}, {
action: 'waitForLoadState', action: 'waitForLoadState',
args: ['networkidle'], }); args: ['networkidle'],
});
} }
} }
for (const pair of workflow.workflow) { for (const pair of workflow.workflow) {
pair.what.forEach( (condition, index) => { pair.what.forEach((condition, index) => {
if (condition.action === 'press') { if (condition.action === 'press') {
if (condition.args && condition.args[1]) { if (condition.args && condition.args[1]) {
if (!input.selector) { if (!input.selector) {
@@ -741,7 +744,7 @@ export class WorkflowGenerator {
action: 'waitForLoadState', action: 'waitForLoadState',
args: ['networkidle'], args: ['networkidle'],
}) })
input = {selector: '', value: '', actionCounter: 0}; input = { selector: '', value: '', actionCounter: 0 };
} }
} else { } else {
pushTheOptimizedAction(pair, index); pushTheOptimizedAction(pair, index);
@@ -756,7 +759,7 @@ export class WorkflowGenerator {
if (input.value.length !== 0) { if (input.value.length !== 0) {
pushTheOptimizedAction(pair, index); pushTheOptimizedAction(pair, index);
// clear the input // clear the input
input = {selector: '', value: '', actionCounter: 0}; input = { selector: '', value: '', actionCounter: 0 };
} }
} }
}); });
@@ -767,7 +770,7 @@ export class WorkflowGenerator {
/** /**
* Returns workflow params from the stored metadata. * Returns workflow params from the stored metadata.
*/ */
public getParams = (): string[]|null => { public getParams = (): string[] | null => {
return this.checkWorkflowForParams(this.workflowRecord); return this.checkWorkflowForParams(this.workflowRecord);
} }