Skip to content

Commit

Permalink
feat: added type aliases (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zain-ul-din committed Apr 27, 2024
1 parent 48f0cf5 commit 2db520e
Show file tree
Hide file tree
Showing 20 changed files with 88 additions and 65 deletions.
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"main": "index.js",
"license": "MIT",
"scripts": {
"dev": "ts-node ./src/index.ts",
"dev": "ts-node -r tsconfig-paths/register ./src/index.ts",
"format": "prettier --write ./src ./scripts",
"prepare": "husky",
"purge": "node ./scripts/purge.js"
Expand All @@ -20,6 +20,7 @@
"cheerio": "^1.0.0-rc.12",
"dotenv": "^16.4.5",
"ofetch": "^1.3.3",
"picocolors": "^1.0.0"
"picocolors": "^1.0.0",
"tsconfig-paths": "^4.2.0"
}
}
4 changes: 2 additions & 2 deletions src/crawlers/Crawler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {FetchOptions, ofetch} from "ofetch";
import request_headers from "../static/request_headers.json";
import request_headers from "#/static/request_headers.json";
import {EventEmitter} from "stream";
import {ENV} from "../constants";
import {ENV} from "#/constants";

type CrawlerEvents = "crawl";

Expand Down
13 changes: 6 additions & 7 deletions src/crawlers/MetaDataCrawler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Crawler from "./Crawler";
import Parser from "../parsers/Parser";
import HTMLOptionsType from "../types/HTMLOptionsType";
import MetaDataType from "../types/MetaDataType";
import TimetableRequestPayload from "../types/TimetableRequestPayload";
import {promisify} from "../lib/util";
import Parser from "#/parsers/Parser";
import HTMLOptionsType from "#/types/HTMLOptionsType";
import MetaDataType from "#/types/MetaDataType";
import TimetableRequestPayload from "#/types/TimetableRequestPayload";
import {promisify} from "#/lib";

interface MetaDataCrawlerParams {
parser: Parser<HTMLOptionsType>;
Expand All @@ -22,7 +22,7 @@ export interface MetaDataCrawlerReturnType {
* @description It's called metadata since metadata is `a set of data that describes and gives information about other data`.
* In this case, it is describing about the timetable data.
*/
class MetaDataCrawler extends Crawler<MetaDataCrawlerReturnType> {
export class MetaDataCrawler extends Crawler<MetaDataCrawlerReturnType> {
/**
* Semesters page endpoint
* @returns static html page
Expand Down Expand Up @@ -141,4 +141,3 @@ class MetaDataCrawler extends Crawler<MetaDataCrawlerReturnType> {
}
}

export default MetaDataCrawler;
11 changes: 5 additions & 6 deletions src/crawlers/TimetableCrawler.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import Parser from "../parsers/Parser";
import TimetableDocType from "../types/TimetableDocType";
import TimetableRequestPayload from "../types/TimetableRequestPayload";
import TimetableType from "../types/TimetableType";
import Parser from "#/parsers/Parser";
import TimetableDocType from "#/types/TimetableDocType";
import TimetableRequestPayload from "#/types/TimetableRequestPayload";
import TimetableType from "#/types/TimetableType";
import Crawler from "./Crawler";

interface TimetableCrawlerParams {
timetableParser: Parser<TimetableType>;
}

class TimetableCrawler extends Crawler<TimetableDocType> {
export class TimetableCrawler extends Crawler<TimetableDocType> {
private static readonly END_POINT = `https://timetable.lgu.edu.pk/Semesters/semester_info/SEMESTER_TIMETABLE.php`;

private timetableParser: Parser<TimetableType>;
Expand Down Expand Up @@ -36,4 +36,3 @@ class TimetableCrawler extends Crawler<TimetableDocType> {
}
}

export default TimetableCrawler;
6 changes: 2 additions & 4 deletions src/crawlers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
import MetaDataCrawler from "./MetaDataCrawler";
import TimetableCrawler from "./TimetableCrawler";

export {MetaDataCrawler, TimetableCrawler};
export * from "./MetaDataCrawler";
export * from "./TimetableCrawler";
18 changes: 8 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import {ERRORS} from "./constants";
import Worker from "./lib/worker";
import TimetableRepository from "./lib/TimetableRepository";
import {logOnCrawlTimetable} from "./lib/logger";
import { Worker, TimetableRepository, logOnCrawlTimetable } from "#/lib";
import { ERRORS } from "#/constants";

new Worker()

/*
Callback invoked when metadata is available
*/
Callback invoked when metadata is available
*/
.onCrawlMetaData(({metaData, timeTableRequestPayloads}) => {
if (timeTableRequestPayloads.length == 0) throw ERRORS.INVALID_COOKIE;
TimetableRepository.writeMetaData(metaData);
})

/*
Callback invoked when timetable is crawled
*/
Callback invoked when timetable is crawled
*/
.onCrawlTimetable(logOnCrawlTimetable)

/*
Callback invoked when all timetables are crawled
*/
Callback invoked when all timetables are crawled
*/
.onFinish((allTimetables) =>
TimetableRepository.writeTimetables(allTimetables)
.writeTeachersTimetable(allTimetables)
Expand Down
15 changes: 7 additions & 8 deletions src/lib/TimetableRepository.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import {writeDB} from "../local-db";
import {hashStr} from "../local-db/cipher";
import MetaDataType from "../types/MetaDataType";
import TimetableDocType from "../types/TimetableDocType";
import {writeDB, hashStr} from "#/local-db";
import MetaDataType from "#/types/MetaDataType";
import TimetableDocType from "#/types/TimetableDocType";
import {computeRoomTimetable, computeRooms, computeTeacherTimetable, computeTeachers} from "./computes";
import {isJsonString} from "./util";
import {WEEK_DAYS_NAME} from "../constants";
import TimetableType from "../types/TimetableType";
import {WEEK_DAYS_NAME} from "#/constants";
import TimetableType from "#/types/TimetableType";

/**
* Repository for managing timetable data.
*/
class TimetableRepository {
export class TimetableRepository {
/**
* Writes metadata to the database.
* @param metaData The metadata to write.
Expand Down Expand Up @@ -128,4 +127,4 @@ class TimetableRepository {
}
}

export default TimetableRepository;

4 changes: 2 additions & 2 deletions src/lib/computes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
// of requests made to the official website.
// **************************************************************************************************

import TimetableDocType from "../types/TimetableDocType";
import {WEEK_DAYS_NAME} from "../constants";
import TimetableDocType from "#/types/TimetableDocType";
import {WEEK_DAYS_NAME} from "#/constants";

/**
* Computes the list of unique teachers from the provided timetables.
Expand Down
6 changes: 6 additions & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export * from "./TimetableRepository";
export * from "./computes";
export * from "./logger";
export * from "./util";
export * from "./worker";

2 changes: 1 addition & 1 deletion src/lib/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import TimetableDocType from "../types/TimetableDocType";
import TimetableDocType from "#/types/TimetableDocType";
import pc from "picocolors";

/**
Expand Down
16 changes: 7 additions & 9 deletions src/lib/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,20 @@

import os from "os";
import {chunkifyArray, clamp, promisify} from "./util";
import TimetableRequestPayload from "../types/TimetableRequestPayload";
import TimetableRequestPayload from "#/types/TimetableRequestPayload";
import cluster from "cluster";
import {MetaDataCrawler, TimetableCrawler} from "../crawlers";
import TimetableParser from "../parsers/TimetableParser";
import TimetableType from "../types/TimetableType";
import OptionsParser from "../parsers/OptionsParser";
import {MetaDataCrawler, TimetableCrawler} from "#/crawlers";
import {TimetableParser, OptionsParser} from "#/parsers";
import TimetableType from "#/types/TimetableType";
import {EventEmitter} from "stream";
import {MetaDataCrawlerReturnType} from "../crawlers/MetaDataCrawler";
import TimetableDocType from "../types/TimetableDocType";
import {MetaDataCrawlerReturnType} from "#/crawlers";
import TimetableDocType from "#/types/TimetableDocType";

/**
* Worker class manages the process of crawling and processing timetable data using
* a cluster of worker processes to improve performance.
*/
class Worker {
export class Worker {
/** The number of available CPU cores */
private availableCores: number;

Expand Down Expand Up @@ -150,4 +149,3 @@ class Worker {
}
}

export default Worker;
2 changes: 1 addition & 1 deletion src/local-db/cipher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Encoding, createCipheriv, createDecipheriv, createHash} from "crypto";
import {ENV} from "../constants";
import {ENV} from "#/constants";
import assert from "assert";

// Define the cipher algorithm and the encoding for encrypted data
Expand Down
8 changes: 4 additions & 4 deletions src/local-db/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import initLocalDB from "./initDB";
import {writeDB} from "./writeDB";
import {readDB} from "./readDB";
import {initLocalDB} from "./initDB";

initLocalDB();

export {writeDB, readDB};
export * from "./cipher";
export * from "./readDB";
export * from "./writeDB";
5 changes: 3 additions & 2 deletions src/local-db/initDB.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {existsSync, mkdirSync} from "fs";
import {DB_PATH, LOCAL_DB_PATH} from "./paths";
import {ENV} from "../constants";
import {ENV} from "#/constants";

/**
* Initializes the local database directory.
* If the local database directory does not exist and the environment is development, it creates the directory.
* It also creates the main database directory if it does not exist.
*/
export default function initLocalDB() {
export function initLocalDB() {
if (!existsSync(LOCAL_DB_PATH) && ENV.NODE_ENV === "development") mkdirSync(LOCAL_DB_PATH);
if (!existsSync(DB_PATH)) mkdirSync(DB_PATH);
}

2 changes: 1 addition & 1 deletion src/local-db/types/FileSchema.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {CIPHER_ALGO, ENCRYPTED_DATA_ENCODING} from "../cipher";
import {CIPHER_ALGO, ENCRYPTED_DATA_ENCODING} from "#/cipher";

/**
* Represents the schema of a JSON file.
Expand Down
2 changes: 1 addition & 1 deletion src/local-db/writeDB.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {writeFileSync} from "fs";
import {DB_PATH, LOCAL_DB_PATH} from "./paths";
import {ENV} from "../constants";
import {ENV} from "#/constants";
import {CIPHER_ALGO, ENCRYPTED_DATA_ENCODING, encrypt, hashStr} from "./cipher";
import FileSchema from "./types/FileSchema";
import {isSame} from "./util";
Expand Down
5 changes: 2 additions & 3 deletions src/parsers/OptionsParser.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {load} from "cheerio";
import HTMLOptionsType from "../types/HTMLOptionsType";
import HTMLOptionsType from "#/types/HTMLOptionsType";
import Parser from "./Parser";

/**
* Parses dropdown options from HTML content using Cheerio.
*/
class OptionsParser extends Parser<HTMLOptionsType> {
export class OptionsParser extends Parser<HTMLOptionsType> {
/**
* Parses dropdown options from HTML content.
* @param html The raw HTML content to parse.
Expand All @@ -28,4 +28,3 @@ class OptionsParser extends Parser<HTMLOptionsType> {
}
}

export default OptionsParser;
3 changes: 1 addition & 2 deletions src/parsers/TimetableParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Parser from "./Parser";
/**
* Parsers timetable data from HTML content.
*/
class TimetableParser extends Parser {
export class TimetableParser extends Parser {
/**
* Parses timetable data from raw HTML content.
* @param rawContent The raw HTML content to parse.
Expand Down Expand Up @@ -74,4 +74,3 @@ class TimetableParser extends Parser {
}
}

export default TimetableParser;
2 changes: 2 additions & 0 deletions src/parsers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./OptionsParser";
export * from "./TimetableParser";
24 changes: 24 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,21 @@ husky@^9.0.11:
resolved "https://registry.yarnpkg.com/husky/-/husky-9.0.11.tgz#fc91df4c756050de41b3e478b2158b87c1e79af9"
integrity sha512-AB6lFlbwwyIqMdHYhwPe+kjOC3Oc5P3nThEoW/AaO2BX3vJDjWPFxYLxokUZOo6RNX20He3AaT8sESs9NJcmEw==

json5@^2.2.2:
version "2.2.3"
resolved "https://registry.yarnpkg.com/json5/-/json5-2.2.3.tgz#78cd6f1a19bdc12b73db5ad0c61efd66c1e29283"
integrity sha512-XmOWe7eyHYH14cLdVPoyg+GOH3rYX++KpzrylJwSW98t3Nk+U8XOl8FWKOgwtzdb8lXGf6zYwDUzeHMWfxasyg==

make-error@^1.1.1:
version "1.3.6"
resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2"
integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==

minimist@^1.2.6:
version "1.2.8"
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c"
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==

node-fetch-native@^1.4.0:
version "1.6.2"
resolved "https://registry.npmjs.org/node-fetch-native/-/node-fetch-native-1.6.2.tgz"
Expand Down Expand Up @@ -236,6 +246,11 @@ prettier@^3.2.5:
resolved "https://registry.npmjs.org/prettier/-/prettier-3.2.5.tgz"
integrity sha512-3/GWa9aOC0YeD7LUfvOG2NiDyhOWRvt1k+rcKhOuYnMY24iiCphgneUfJDyFXd6rZCAnuLBv6UeAULtrhT/F4A==

strip-bom@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3.0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3"
integrity sha512-vavAMRXOgBVNF6nyEEmL3DBK19iRpDcoIwW+swQ+CbGiu7lju6t+JklA1MHweoWtadgt4ISVUsXLyDq34ddcwA==

ts-node@^10.9.2:
version "10.9.2"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.9.2.tgz#70f021c9e185bccdca820e26dc413805c101c71f"
Expand All @@ -255,6 +270,15 @@ ts-node@^10.9.2:
v8-compile-cache-lib "^3.0.1"
yn "3.1.1"

tsconfig-paths@^4.2.0:
version "4.2.0"
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-4.2.0.tgz#ef78e19039133446d244beac0fd6a1632e2d107c"
integrity sha512-NoZ4roiN7LnbKn9QqE1amc9DJfzvZXxF4xDavcOWt1BPkdx+m+0gJuPM+S0vCe7zTJMYUP0R8pO2XMr+Y8oLIg==
dependencies:
json5 "^2.2.2"
minimist "^1.2.6"
strip-bom "^3.0.0"

typescript@^5.4.2:
version "5.4.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.4.2.tgz#0ae9cebcfae970718474fe0da2c090cad6577372"
Expand Down

0 comments on commit 2db520e

Please sign in to comment.