From 2db520e8bebc848686a840725f0fa4846b43717e Mon Sep 17 00:00:00 2001 From: zainuldeen <78583049+Zain-ul-din@users.noreply.github.com> Date: Sat, 27 Apr 2024 23:57:56 +0500 Subject: [PATCH] feat: added type aliases (#5) --- package.json | 5 +++-- src/crawlers/Crawler.ts | 4 ++-- src/crawlers/MetaDataCrawler.ts | 13 ++++++------- src/crawlers/TimetableCrawler.ts | 11 +++++------ src/crawlers/index.ts | 6 ++---- src/index.ts | 18 ++++++++---------- src/lib/TimetableRepository.ts | 15 +++++++-------- src/lib/computes.ts | 4 ++-- src/lib/index.ts | 6 ++++++ src/lib/logger.ts | 2 +- src/lib/worker.ts | 16 +++++++--------- src/local-db/cipher.ts | 2 +- src/local-db/index.ts | 8 ++++---- src/local-db/initDB.ts | 5 +++-- src/local-db/types/FileSchema.d.ts | 2 +- src/local-db/writeDB.ts | 2 +- src/parsers/OptionsParser.ts | 5 ++--- src/parsers/TimetableParser.ts | 3 +-- src/parsers/index.ts | 2 ++ yarn.lock | 24 ++++++++++++++++++++++++ 20 files changed, 88 insertions(+), 65 deletions(-) create mode 100644 src/lib/index.ts create mode 100644 src/parsers/index.ts diff --git a/package.json b/package.json index 4e75f5da7..0b099c356 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" } } diff --git a/src/crawlers/Crawler.ts b/src/crawlers/Crawler.ts index c39180ffd..673f6773f 100644 --- a/src/crawlers/Crawler.ts +++ b/src/crawlers/Crawler.ts @@ -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"; diff --git a/src/crawlers/MetaDataCrawler.ts b/src/crawlers/MetaDataCrawler.ts index 57401218f..ef3f1e028 100644 --- a/src/crawlers/MetaDataCrawler.ts +++ b/src/crawlers/MetaDataCrawler.ts @@ -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; @@ -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 { +export class MetaDataCrawler extends Crawler { /** * Semesters page endpoint * @returns static html page @@ -141,4 +141,3 @@ class MetaDataCrawler extends Crawler { } } -export default MetaDataCrawler; diff --git a/src/crawlers/TimetableCrawler.ts b/src/crawlers/TimetableCrawler.ts index 95c831d45..3485c9c27 100644 --- a/src/crawlers/TimetableCrawler.ts +++ b/src/crawlers/TimetableCrawler.ts @@ -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; } -class TimetableCrawler extends Crawler { +export class TimetableCrawler extends Crawler { private static readonly END_POINT = `https://timetable.lgu.edu.pk/Semesters/semester_info/SEMESTER_TIMETABLE.php`; private timetableParser: Parser; @@ -36,4 +36,3 @@ class TimetableCrawler extends Crawler { } } -export default TimetableCrawler; diff --git a/src/crawlers/index.ts b/src/crawlers/index.ts index 655e07485..f07598e5d 100644 --- a/src/crawlers/index.ts +++ b/src/crawlers/index.ts @@ -1,4 +1,2 @@ -import MetaDataCrawler from "./MetaDataCrawler"; -import TimetableCrawler from "./TimetableCrawler"; - -export {MetaDataCrawler, TimetableCrawler}; +export * from "./MetaDataCrawler"; +export * from "./TimetableCrawler"; diff --git a/src/index.ts b/src/index.ts index abc0f5027..bc80489fd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) diff --git a/src/lib/TimetableRepository.ts b/src/lib/TimetableRepository.ts index 436485751..36253a45a 100644 --- a/src/lib/TimetableRepository.ts +++ b/src/lib/TimetableRepository.ts @@ -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. @@ -128,4 +127,4 @@ class TimetableRepository { } } -export default TimetableRepository; + diff --git a/src/lib/computes.ts b/src/lib/computes.ts index 5509cc6b4..b5c550705 100644 --- a/src/lib/computes.ts +++ b/src/lib/computes.ts @@ -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. diff --git a/src/lib/index.ts b/src/lib/index.ts new file mode 100644 index 000000000..32a5f1b0a --- /dev/null +++ b/src/lib/index.ts @@ -0,0 +1,6 @@ +export * from "./TimetableRepository"; +export * from "./computes"; +export * from "./logger"; +export * from "./util"; +export * from "./worker"; + diff --git a/src/lib/logger.ts b/src/lib/logger.ts index fe826c06a..25d4d7ed0 100644 --- a/src/lib/logger.ts +++ b/src/lib/logger.ts @@ -1,4 +1,4 @@ -import TimetableDocType from "../types/TimetableDocType"; +import TimetableDocType from "#/types/TimetableDocType"; import pc from "picocolors"; /** diff --git a/src/lib/worker.ts b/src/lib/worker.ts index 4d46179cc..8eea14ed6 100644 --- a/src/lib/worker.ts +++ b/src/lib/worker.ts @@ -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; @@ -150,4 +149,3 @@ class Worker { } } -export default Worker; diff --git a/src/local-db/cipher.ts b/src/local-db/cipher.ts index 9a9fa42bb..7b9603b40 100644 --- a/src/local-db/cipher.ts +++ b/src/local-db/cipher.ts @@ -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 diff --git a/src/local-db/index.ts b/src/local-db/index.ts index 94e1a1640..9fee5e6c1 100644 --- a/src/local-db/index.ts +++ b/src/local-db/index.ts @@ -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"; diff --git a/src/local-db/initDB.ts b/src/local-db/initDB.ts index e4738ef6e..4dc34989e 100644 --- a/src/local-db/initDB.ts +++ b/src/local-db/initDB.ts @@ -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); } + diff --git a/src/local-db/types/FileSchema.d.ts b/src/local-db/types/FileSchema.d.ts index 8da1075f9..f5bfde70d 100644 --- a/src/local-db/types/FileSchema.d.ts +++ b/src/local-db/types/FileSchema.d.ts @@ -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. diff --git a/src/local-db/writeDB.ts b/src/local-db/writeDB.ts index 134151067..bcf056d38 100644 --- a/src/local-db/writeDB.ts +++ b/src/local-db/writeDB.ts @@ -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"; diff --git a/src/parsers/OptionsParser.ts b/src/parsers/OptionsParser.ts index 92f5e2f52..2e6ab772a 100644 --- a/src/parsers/OptionsParser.ts +++ b/src/parsers/OptionsParser.ts @@ -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 { +export class OptionsParser extends Parser { /** * Parses dropdown options from HTML content. * @param html The raw HTML content to parse. @@ -28,4 +28,3 @@ class OptionsParser extends Parser { } } -export default OptionsParser; diff --git a/src/parsers/TimetableParser.ts b/src/parsers/TimetableParser.ts index 06a7e480d..24ec2ddec 100644 --- a/src/parsers/TimetableParser.ts +++ b/src/parsers/TimetableParser.ts @@ -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. @@ -74,4 +74,3 @@ class TimetableParser extends Parser { } } -export default TimetableParser; diff --git a/src/parsers/index.ts b/src/parsers/index.ts new file mode 100644 index 000000000..d8f757447 --- /dev/null +++ b/src/parsers/index.ts @@ -0,0 +1,2 @@ +export * from "./OptionsParser"; +export * from "./TimetableParser"; diff --git a/yarn.lock b/yarn.lock index dcaa2c95c..75d94c610 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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"