Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
dangowans committed Nov 20, 2023
1 parent cc1f55f commit 0e4e57b
Show file tree
Hide file tree
Showing 31 changed files with 8,680 additions and 8,651 deletions.
16 changes: 8 additions & 8 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": ["./tsconfig.json"],
"ecmaVersion": 2022,
"sourceType": "module"
},
"extends": ["eslint-config-cityssm"]
"root": true,
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": ["./tsconfig.json"],
"ecmaVersion": 2022,
"sourceType": "module"
},
"extends": ["eslint-config-cityssm"]
}
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
*.ejs
*.js
10 changes: 5 additions & 5 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"trailingComma": "none",
"tabWidth": 4,
"semi": true,
"singleQuote": false,
"printWidth": 100
}
"tabWidth": 2,
"semi": false,
"singleQuote": true,
"bracketSpacing": true
}
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
[![Code Climate maintainability](https://img.shields.io/codeclimate/maintainability/cityssm/wsib-clearance-check)](https://codeclimate.com/github/cityssm/wsib-clearance-check)
[![Code Climate coverage](https://img.shields.io/codeclimate/coverage/cityssm/wsib-clearance-check)](https://codeclimate.com/github/cityssm/wsib-clearance-check)
[![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/cityssm/wsib-clearance-check/coverage.yml)](https://github.com/cityssm/wsib-clearance-check/actions/workflows/coverage.yml)
[![Snyk Vulnerabilities for GitHub Repo](https://img.shields.io/snyk/vulnerabilities/github/cityssm/wsib-clearance-check)](https://app.snyk.io/org/cityssm/project/18c6a1c4-1d7a-4161-85e4-003bfe84a57f)

A tool to scrape the clearance certificate status from the
[WSIB Online Services Clearance Certificate Website](https://onlineservices.wsib.on.ca/EClearanceWeb/eclearance/start).
Expand Down
88 changes: 0 additions & 88 deletions browser-global.ts

This file was deleted.

2 changes: 1 addition & 1 deletion browser-global.d.ts → browserGlobal.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Browser } from "puppeteer";
import type { Browser } from 'puppeteer';
export declare const setHeadless: (headlessStatus: boolean) => void;
export declare const pageTimeoutMillis = 90000;
export declare function getBrowserGlobal(): Promise<Browser>;
Expand Down
22 changes: 10 additions & 12 deletions browser-global.js → browserGlobal.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { setIntervalAsync } from "set-interval-async/dynamic";
import { clearIntervalAsync } from "set-interval-async";
import puppeteer from "puppeteer";
import puppeteer from 'puppeteer';
import { clearIntervalAsync } from 'set-interval-async';
import { setIntervalAsync } from 'set-interval-async/dynamic';
let headless = true;
export const setHeadless = (headlessStatus) => {
headless = headlessStatus;
Expand All @@ -12,19 +12,17 @@ let browserGlobal;
let browserGlobalInitializedTime = 0;
let browserGlobalTimer;
function isBrowserGlobalReady() {
if (browserGlobal && browserGlobalInitializedTime + browserGlobalExpiryMillis > Date.now()) {
return true;
}
return false;
return Boolean(browserGlobal !== undefined &&
browserGlobalInitializedTime + browserGlobalExpiryMillis > Date.now());
}
export async function getBrowserGlobal() {
if (!isBrowserGlobalReady()) {
await cleanUpBrowserGlobal();
keepBrowserGlobalAlive();
browserGlobal = await puppeteer.launch({
headless,
headless: headless ? 'new' : false,
timeout: browserStartupTimeoutMillis,
args: ["--lang-en-CA,en"]
args: ['--lang-en-CA,en']
});
keepBrowserGlobalAlive();
browserGlobalTimer = setIntervalAsync(cleanUpBrowserGlobal, browserGlobalExpiryMillis);
Expand All @@ -42,14 +40,14 @@ export async function cleanUpBrowserGlobal(useForce = false) {
try {
await browserGlobal.close();
}
catch (_a) {
catch {
}
browserGlobal = undefined;
if (browserGlobalTimer) {
try {
clearIntervalAsync(browserGlobalTimer);
await clearIntervalAsync(browserGlobalTimer);
}
catch (_b) {
catch {
}
browserGlobalTimer = undefined;
}
Expand Down
90 changes: 90 additions & 0 deletions browserGlobal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import type { Browser } from 'puppeteer'
import puppeteer from 'puppeteer'
import { clearIntervalAsync } from 'set-interval-async'
import { setIntervalAsync } from 'set-interval-async/dynamic'

/*
* Headless Debug Setting
*/

let headless = true

export const setHeadless = (headlessStatus: boolean): void => {
headless = headlessStatus
}

/*
* Browser Global
*/

export const pageTimeoutMillis = 90_000
const browserStartupTimeoutMillis = 3 * 60_000

const browserGlobalExpiryMillis =
Math.max(browserStartupTimeoutMillis, pageTimeoutMillis) + 10_000

let browserGlobal: Browser | undefined
let browserGlobalInitializedTime = 0
let browserGlobalTimer

function isBrowserGlobalReady(): boolean {
return Boolean(
browserGlobal !== undefined &&
browserGlobalInitializedTime + browserGlobalExpiryMillis > Date.now()
)
}

export async function getBrowserGlobal(): Promise<Browser> {
if (!isBrowserGlobalReady()) {
await cleanUpBrowserGlobal()

keepBrowserGlobalAlive()

browserGlobal = await puppeteer.launch({
headless: headless ? 'new' : false,
timeout: browserStartupTimeoutMillis,
args: ['--lang-en-CA,en']
})

keepBrowserGlobalAlive()

browserGlobalTimer = setIntervalAsync(
cleanUpBrowserGlobal,
browserGlobalExpiryMillis
)
}

return browserGlobal as Browser
}

export function keepBrowserGlobalAlive(): void {
browserGlobalInitializedTime = Date.now()
}

export async function cleanUpBrowserGlobal(useForce = false): Promise<void> {
if (useForce) {
browserGlobalInitializedTime = 0
}

if (!isBrowserGlobalReady()) {
try {
await browserGlobal.close()
} catch {
// ignore
}

browserGlobal = undefined

if (browserGlobalTimer) {
try {
await clearIntervalAsync(browserGlobalTimer)
} catch {
// ignore
}

browserGlobalTimer = undefined
}

browserGlobalInitializedTime = 0
}
}
4 changes: 2 additions & 2 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getClearanceByAccountNumber, cleanUpBrowser } from "./index.js";
import { getClearanceByAccountNumber, cleanUpBrowser } from './index.js';
const cli = async () => {
const accountNumbers = process.argv[2].split(",");
const accountNumbers = process.argv[2].split(',');
for (const accountNumber of accountNumbers) {
const results = await getClearanceByAccountNumber(accountNumber);
console.log(results);
Expand Down
18 changes: 9 additions & 9 deletions cli.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { getClearanceByAccountNumber, cleanUpBrowser } from "./index.js";
import { getClearanceByAccountNumber, cleanUpBrowser } from './index.js'

const cli = async () => {
const accountNumbers = process.argv[2].split(",");
const accountNumbers = process.argv[2].split(',')

for (const accountNumber of accountNumbers) {
const results = await getClearanceByAccountNumber(accountNumber);
console.log(results);
}
for (const accountNumber of accountNumbers) {
const results = await getClearanceByAccountNumber(accountNumber)
console.log(results)
}

await cleanUpBrowser();
};
await cleanUpBrowser()
}

cli();
cli()
26 changes: 13 additions & 13 deletions config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
export const clearanceStart_url = "https://clearances.wsib.ca/Clearances/eclearance/start";
export const clearanceStart_searchFormSelector = "#TOKENSimpleSearchForm";
export const clearanceStart_searchFieldSelector = "#simpleAccountNumbersTOKEN";
export const clearanceStart_url = 'https://clearances.wsib.ca/Clearances/eclearance/start';
export const clearanceStart_searchFormSelector = '#TOKENSimpleSearchForm';
export const clearanceStart_searchFieldSelector = '#simpleAccountNumbersTOKEN';
export const clearanceResult_certificateLinkSelector = "#eClearanceWorkspaceTargSubDivFormXX .fancytable a[rel='eClearanceWorkspaceContent'][href^='GCSearchCertDet']";
export const clearanceResult_certificateBadStandingSelector = "#eClearanceWorkspaceTargSubDivFormXX .fancytable .badstanding";
export const clearanceResult_defaultErrorMessage = "Clearance certificate link not found.";
export const certificate_tableSelector = "#eClearanceWorkspaceDivForm .fancytable";
export const certificateField_contractorLegalTradeName = "Contractor Legal / Trade Name";
export const certificateField_contractorAddress = "Contractor Address";
export const certificateField_naicsCodes = "Contractor NAICS Code and Code Description";
export const certificateField_clearanceCertificateNumber = "Clearance certificate number";
export const certificateField_validityPeriod = "Validity period (dd-mmm-yyyy)";
export const certificateField_principalLegalTradeName = "Principal Legal / Trade Name";
export const certificateField_principalAddress = "Principal Address";
export const clearanceResult_certificateBadStandingSelector = '#eClearanceWorkspaceTargSubDivFormXX .fancytable .badstanding';
export const clearanceResult_defaultErrorMessage = 'Clearance certificate link not found.';
export const certificate_tableSelector = '#eClearanceWorkspaceDivForm .fancytable';
export const certificateField_contractorLegalTradeName = 'Contractor Legal / Trade Name';
export const certificateField_contractorAddress = 'Contractor Address';
export const certificateField_naicsCodes = 'Contractor NAICS Code and Code Description';
export const certificateField_clearanceCertificateNumber = 'Clearance certificate number';
export const certificateField_validityPeriod = 'Validity period (dd-mmm-yyyy)';
export const certificateField_principalLegalTradeName = 'Principal Legal / Trade Name';
export const certificateField_principalAddress = 'Principal Address';
35 changes: 21 additions & 14 deletions config.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
// Search Form

export const clearanceStart_url = "https://clearances.wsib.ca/Clearances/eclearance/start";
export const clearanceStart_url =
'https://clearances.wsib.ca/Clearances/eclearance/start'

export const clearanceStart_searchFormSelector = "#TOKENSimpleSearchForm";
export const clearanceStart_searchFieldSelector = "#simpleAccountNumbersTOKEN";
export const clearanceStart_searchFormSelector = '#TOKENSimpleSearchForm'
export const clearanceStart_searchFieldSelector = '#simpleAccountNumbersTOKEN'

// Search Results

export const clearanceResult_certificateLinkSelector =
"#eClearanceWorkspaceTargSubDivFormXX .fancytable a[rel='eClearanceWorkspaceContent'][href^='GCSearchCertDet']";
"#eClearanceWorkspaceTargSubDivFormXX .fancytable a[rel='eClearanceWorkspaceContent'][href^='GCSearchCertDet']"
export const clearanceResult_certificateBadStandingSelector =
"#eClearanceWorkspaceTargSubDivFormXX .fancytable .badstanding";
export const clearanceResult_defaultErrorMessage = "Clearance certificate link not found.";
'#eClearanceWorkspaceTargSubDivFormXX .fancytable .badstanding'
export const clearanceResult_defaultErrorMessage =
'Clearance certificate link not found.'

// Certificate

export const certificate_tableSelector = "#eClearanceWorkspaceDivForm .fancytable";
export const certificate_tableSelector =
'#eClearanceWorkspaceDivForm .fancytable'

export const certificateField_contractorLegalTradeName = "Contractor Legal / Trade Name";
export const certificateField_contractorAddress = "Contractor Address";
export const certificateField_naicsCodes = "Contractor NAICS Code and Code Description";
export const certificateField_clearanceCertificateNumber = "Clearance certificate number";
export const certificateField_validityPeriod = "Validity period (dd-mmm-yyyy)";
export const certificateField_principalLegalTradeName = "Principal Legal / Trade Name";
export const certificateField_principalAddress = "Principal Address";
export const certificateField_contractorLegalTradeName =
'Contractor Legal / Trade Name'
export const certificateField_contractorAddress = 'Contractor Address'
export const certificateField_naicsCodes =
'Contractor NAICS Code and Code Description'
export const certificateField_clearanceCertificateNumber =
'Clearance certificate number'
export const certificateField_validityPeriod = 'Validity period (dd-mmm-yyyy)'
export const certificateField_principalLegalTradeName =
'Principal Legal / Trade Name'
export const certificateField_principalAddress = 'Principal Address'
Loading

0 comments on commit 0e4e57b

Please sign in to comment.