From debed9c704e007f375328b5bfb48766a167898e9 Mon Sep 17 00:00:00 2001 From: Ru Chern CHONG Date: Sun, 2 Mar 2025 17:25:52 +0800 Subject: [PATCH] Refactor import paths to use scoped aliases --- apps/api/src/config/index.ts | 2 +- apps/api/src/lib/getCarsByFuelType.ts | 8 ++++---- apps/api/src/lib/getLatestMonth.ts | 2 +- apps/api/src/lib/getUniqueMonths.ts | 10 +++++----- apps/api/src/lib/groupMonthsByYear.ts | 2 +- .../__tests__/getTrailingTwelveMonths.test.ts | 2 +- apps/api/src/v1/index.ts | 4 ++-- apps/api/src/v1/routes/cars.ts | 18 +++++++++--------- apps/api/src/v1/routes/coe.ts | 12 ++++++------ apps/api/src/v1/routes/makes.ts | 10 +++++----- apps/api/src/v1/routes/months.ts | 4 ++-- apps/api/tsconfig.json | 6 +++--- apps/api/vitest.config.ts | 2 +- apps/updater/src/index.ts | 2 +- apps/updater/src/lib/updateCOE.ts | 4 ++-- apps/updater/src/lib/updateCOEPQP.ts | 4 ++-- apps/updater/src/lib/updateCars.ts | 6 +++--- apps/updater/src/lib/updater.ts | 17 ++++++++++------- apps/updater/src/trigger/update-cars.ts | 6 +++--- apps/updater/src/trigger/update-coe-pqp.ts | 6 +++--- apps/updater/src/trigger/update-coe.ts | 6 +++--- .../utils/__tests__/calculateChecksum.test.ts | 2 +- .../utils/__tests__/cleanSpecialChars.test.ts | 2 +- .../utils/__tests__/createUniqueKey.test.ts | 2 +- apps/updater/src/utils/createUpdateTask.ts | 9 ++++++--- apps/updater/src/utils/downloadFile.ts | 2 +- apps/updater/src/utils/redisCache.ts | 2 +- apps/updater/tsconfig.json | 10 ++-------- apps/updater/vitest.config.ts | 2 +- tsconfig.json | 1 + 30 files changed, 83 insertions(+), 82 deletions(-) diff --git a/apps/api/src/config/index.ts b/apps/api/src/config/index.ts index 402e8a5..c80e084 100644 --- a/apps/api/src/config/index.ts +++ b/apps/api/src/config/index.ts @@ -1,4 +1,4 @@ -import { FuelType } from "@/types"; +import { FuelType } from "@api/types"; export const CACHE_TTL = 24 * 60 * 60; diff --git a/apps/api/src/lib/getCarsByFuelType.ts b/apps/api/src/lib/getCarsByFuelType.ts index 6e4a1d1..9362d99 100644 --- a/apps/api/src/lib/getCarsByFuelType.ts +++ b/apps/api/src/lib/getCarsByFuelType.ts @@ -1,7 +1,7 @@ -import db from "@/config/db"; -import { getLatestMonth } from "@/lib/getLatestMonth"; -import type { FuelType } from "@/types"; -import getTrailingTwelveMonths from "@/utils/getTrailingTwelveMonths"; +import db from "@api/config/db"; +import { getLatestMonth } from "@api/lib/getLatestMonth"; +import type { FuelType } from "@api/types"; +import getTrailingTwelveMonths from "@api/utils/getTrailingTwelveMonths"; import { cars } from "@sgcarstrends/schema"; import { and, asc, between, desc, eq, ilike, or } from "drizzle-orm"; diff --git a/apps/api/src/lib/getLatestMonth.ts b/apps/api/src/lib/getLatestMonth.ts index 760764e..67ea75a 100644 --- a/apps/api/src/lib/getLatestMonth.ts +++ b/apps/api/src/lib/getLatestMonth.ts @@ -1,4 +1,4 @@ -import db from "@/config/db"; +import db from "@api/config/db"; import { max } from "drizzle-orm"; import type { PgTable } from "drizzle-orm/pg-core"; diff --git a/apps/api/src/lib/getUniqueMonths.ts b/apps/api/src/lib/getUniqueMonths.ts index a2f62ad..e0526c4 100644 --- a/apps/api/src/lib/getUniqueMonths.ts +++ b/apps/api/src/lib/getUniqueMonths.ts @@ -1,12 +1,12 @@ -import { CACHE_TTL } from "@/config"; -import db from "@/config/db"; -import redis from "@/config/redis"; +import { CACHE_TTL } from "@api/config"; +import db from "@api/config/db"; +import redis from "@api/config/redis"; import { desc, getTableName } from "drizzle-orm"; import type { PgTable } from "drizzle-orm/pg-core"; export const getUniqueMonths = async ( table: T, - key = "month" + key = "month", ) => { const tableName = getTableName(table); const CACHE_KEY = `${tableName}:months`; @@ -22,7 +22,7 @@ export const getUniqueMonths = async ( months = results.map(({ month }) => month); - await redis.sadd(CACHE_KEY, ...months); + await redis.sadd(CACHE_KEY, months); await redis.expire(CACHE_KEY, CACHE_TTL); } diff --git a/apps/api/src/lib/groupMonthsByYear.ts b/apps/api/src/lib/groupMonthsByYear.ts index e90ceb9..6bbdca8 100644 --- a/apps/api/src/lib/groupMonthsByYear.ts +++ b/apps/api/src/lib/groupMonthsByYear.ts @@ -1,5 +1,5 @@ export const groupMonthsByYear = ( - months: string[] + months: string[], ): { year: string; months: string[] }[] => { const groupedData: Record = {}; diff --git a/apps/api/src/utils/__tests__/getTrailingTwelveMonths.test.ts b/apps/api/src/utils/__tests__/getTrailingTwelveMonths.test.ts index 9f87839..82af9b4 100644 --- a/apps/api/src/utils/__tests__/getTrailingTwelveMonths.test.ts +++ b/apps/api/src/utils/__tests__/getTrailingTwelveMonths.test.ts @@ -1,4 +1,4 @@ -import getTrailingTwelveMonths from "@/utils/getTrailingTwelveMonths"; +import getTrailingTwelveMonths from "@api/utils/getTrailingTwelveMonths"; import { describe, expect, it } from "vitest"; describe("getTrailingTwelveMonths", () => { diff --git a/apps/api/src/v1/index.ts b/apps/api/src/v1/index.ts index 409a47d..872b31d 100644 --- a/apps/api/src/v1/index.ts +++ b/apps/api/src/v1/index.ts @@ -1,5 +1,5 @@ -import { getCarsByFuelType } from "@/lib/getCarsByFuelType"; -import { FuelType } from "@/types"; +import { getCarsByFuelType } from "@api/lib/getCarsByFuelType"; +import { FuelType } from "@api/types"; import { Hono } from "hono"; import { bearerAuth } from "hono/bearer-auth"; import { Resource } from "sst"; diff --git a/apps/api/src/v1/routes/cars.ts b/apps/api/src/v1/routes/cars.ts index e7f29bd..2e0c38e 100644 --- a/apps/api/src/v1/routes/cars.ts +++ b/apps/api/src/v1/routes/cars.ts @@ -1,12 +1,12 @@ -import { CACHE_TTL, HYBRID_REGEX } from "@/config"; -import db from "@/config/db"; -import redis from "@/config/redis"; -import { getLatestMonth } from "@/lib/getLatestMonth"; -import { getUniqueMonths } from "@/lib/getUniqueMonths"; -import { groupMonthsByYear } from "@/lib/groupMonthsByYear"; -import { CarQuerySchema, MonthsQuerySchema } from "@/schemas"; -import type { Make } from "@/types"; -import getTrailingTwelveMonths from "@/utils/getTrailingTwelveMonths"; +import { CACHE_TTL, HYBRID_REGEX } from "@api/config"; +import db from "@api/config/db"; +import redis from "@api/config/redis"; +import { getLatestMonth } from "@api/lib/getLatestMonth"; +import { getUniqueMonths } from "@api/lib/getUniqueMonths"; +import { groupMonthsByYear } from "@api/lib/groupMonthsByYear"; +import { CarQuerySchema, MonthsQuerySchema } from "@api/schemas"; +import type { Make } from "@api/types"; +import getTrailingTwelveMonths from "@api/utils/getTrailingTwelveMonths"; import { zValidator } from "@hono/zod-validator"; import { cars } from "@sgcarstrends/schema"; import { and, asc, between, desc, eq, ilike, sql } from "drizzle-orm"; diff --git a/apps/api/src/v1/routes/coe.ts b/apps/api/src/v1/routes/coe.ts index 95b98ea..eb8d411 100644 --- a/apps/api/src/v1/routes/coe.ts +++ b/apps/api/src/v1/routes/coe.ts @@ -1,9 +1,9 @@ -import { CACHE_TTL } from "@/config"; -import db from "@/config/db"; -import redis from "@/config/redis"; -import { getUniqueMonths } from "@/lib/getUniqueMonths"; -import { groupMonthsByYear } from "@/lib/groupMonthsByYear"; -import { type COE, COEQuerySchema, MonthsQuerySchema } from "@/schemas"; +import { CACHE_TTL } from "@api/config"; +import db from "@api/config/db"; +import redis from "@api/config/redis"; +import { getUniqueMonths } from "@api/lib/getUniqueMonths"; +import { groupMonthsByYear } from "@api/lib/groupMonthsByYear"; +import { type COE, COEQuerySchema, MonthsQuerySchema } from "@api/schemas"; import { zValidator } from "@hono/zod-validator"; import { coe, coePQP } from "@sgcarstrends/schema"; import { and, asc, desc, eq, gte, inArray, lte, max } from "drizzle-orm"; diff --git a/apps/api/src/v1/routes/makes.ts b/apps/api/src/v1/routes/makes.ts index d4ec4dc..905439e 100644 --- a/apps/api/src/v1/routes/makes.ts +++ b/apps/api/src/v1/routes/makes.ts @@ -1,8 +1,8 @@ -import { CACHE_TTL } from "@/config"; -import db from "@/config/db"; -import redis from "@/config/redis"; -import { MakeParamSchema, MakeQuerySchema } from "@/schemas"; -import type { Make } from "@/types"; +import { CACHE_TTL } from "@api/config"; +import db from "@api/config/db"; +import redis from "@api/config/redis"; +import { MakeParamSchema, MakeQuerySchema } from "@api/schemas"; +import type { Make } from "@api/types"; import { zValidator } from "@hono/zod-validator"; import { cars } from "@sgcarstrends/schema"; import { and, asc, desc, eq, ilike } from "drizzle-orm"; diff --git a/apps/api/src/v1/routes/months.ts b/apps/api/src/v1/routes/months.ts index 7d822c3..957d45f 100644 --- a/apps/api/src/v1/routes/months.ts +++ b/apps/api/src/v1/routes/months.ts @@ -1,5 +1,5 @@ -import { getLatestMonth } from "@/lib/getLatestMonth"; -import { LatestMonthQuerySchema } from "@/schemas"; +import { getLatestMonth } from "@api/lib/getLatestMonth"; +import { LatestMonthQuerySchema } from "@api/schemas"; import { zValidator } from "@hono/zod-validator"; import { cars, coe } from "@sgcarstrends/schema"; import { Hono } from "hono"; diff --git a/apps/api/tsconfig.json b/apps/api/tsconfig.json index 34f3b98..4950801 100644 --- a/apps/api/tsconfig.json +++ b/apps/api/tsconfig.json @@ -4,10 +4,10 @@ "baseUrl": ".", "moduleResolution": "Node", "allowSyntheticDefaultImports": true, + "resolveJsonModule": true, "paths": { - "@/*": ["src/*"] - }, - "resolveJsonModule": true + "@api/*": ["src/*"] + } }, "include": ["**/*.ts"] } diff --git a/apps/api/vitest.config.ts b/apps/api/vitest.config.ts index 4fe1bc1..3034b20 100644 --- a/apps/api/vitest.config.ts +++ b/apps/api/vitest.config.ts @@ -15,7 +15,7 @@ export default defineConfig({ }, resolve: { alias: { - "@": resolve(__dirname, "./src"), + "@api": resolve(__dirname, "./src"), }, }, }); diff --git a/apps/updater/src/index.ts b/apps/updater/src/index.ts index 127f591..0e3b70b 100644 --- a/apps/updater/src/index.ts +++ b/apps/updater/src/index.ts @@ -1,4 +1,4 @@ -import { updateCOEPQP } from "@/lib/updateCOEPQP"; +import { updateCOEPQP } from "@updater/lib/updateCOEPQP"; import { Hono } from "hono"; import { handle } from "hono/aws-lambda"; import { bearerAuth } from "hono/bearer-auth"; diff --git a/apps/updater/src/lib/updateCOE.ts b/apps/updater/src/lib/updateCOE.ts index eecd516..b4c1261 100644 --- a/apps/updater/src/lib/updateCOE.ts +++ b/apps/updater/src/lib/updateCOE.ts @@ -1,6 +1,6 @@ -import { LTA_DATAMALL_BASE_URL } from "@/config"; -import type { COE } from "@/types"; import { coe } from "@sgcarstrends/schema"; +import { LTA_DATAMALL_BASE_URL } from "@updater/config"; +import type { COE } from "@updater/types"; import { updater } from "./updater"; export const updateCOE = () => { diff --git a/apps/updater/src/lib/updateCOEPQP.ts b/apps/updater/src/lib/updateCOEPQP.ts index bd52e67..fc7df31 100644 --- a/apps/updater/src/lib/updateCOEPQP.ts +++ b/apps/updater/src/lib/updateCOEPQP.ts @@ -1,6 +1,6 @@ -import { LTA_DATAMALL_BASE_URL } from "@/config"; -import type { PQP } from "@/types"; import { coePQP } from "@sgcarstrends/schema"; +import { LTA_DATAMALL_BASE_URL } from "@updater/config"; +import type { PQP } from "@updater/types"; import { updater } from "./updater"; export const updateCOEPQP = () => { diff --git a/apps/updater/src/lib/updateCars.ts b/apps/updater/src/lib/updateCars.ts index 97a893d..c3a3622 100644 --- a/apps/updater/src/lib/updateCars.ts +++ b/apps/updater/src/lib/updateCars.ts @@ -1,7 +1,7 @@ -import { LTA_DATAMALL_BASE_URL } from "@/config"; -import type { Car } from "@/types"; -import { cleanSpecialChars } from "@/utils/cleanSpecialChars"; import { cars } from "@sgcarstrends/schema"; +import { LTA_DATAMALL_BASE_URL } from "@updater/config"; +import type { Car } from "@updater/types"; +import { cleanSpecialChars } from "@updater/utils/cleanSpecialChars"; import { updater } from "./updater"; export const updateCars = () => { diff --git a/apps/updater/src/lib/updater.ts b/apps/updater/src/lib/updater.ts index 3aba2af..9778425 100644 --- a/apps/updater/src/lib/updater.ts +++ b/apps/updater/src/lib/updater.ts @@ -1,11 +1,14 @@ import path from "node:path"; -import { AWS_LAMBDA_TEMP_DIR } from "@/config"; -import { db } from "@/db"; -import { calculateChecksum } from "@/utils/calculateChecksum"; -import { createUniqueKey } from "@/utils/createUniqueKey"; -import { downloadFile } from "@/utils/downloadFile"; -import { type CSVTransformOptions, processCSV } from "@/utils/processCSV"; -import { cacheChecksum, getCachedChecksum } from "@/utils/redisCache"; +import { AWS_LAMBDA_TEMP_DIR } from "@updater/config"; +import { db } from "@updater/db"; +import { calculateChecksum } from "@updater/utils/calculateChecksum"; +import { createUniqueKey } from "@updater/utils/createUniqueKey"; +import { downloadFile } from "@updater/utils/downloadFile"; +import { + type CSVTransformOptions, + processCSV, +} from "@updater/utils/processCSV"; +import { cacheChecksum, getCachedChecksum } from "@updater/utils/redisCache"; import { type Table, getTableName } from "drizzle-orm"; export interface UpdaterConfig { diff --git a/apps/updater/src/trigger/update-cars.ts b/apps/updater/src/trigger/update-cars.ts index 734fc49..6dffb0b 100644 --- a/apps/updater/src/trigger/update-cars.ts +++ b/apps/updater/src/trigger/update-cars.ts @@ -1,6 +1,6 @@ -import { schedulers } from "@/config/schedulers"; -import { updateCars } from "@/lib/updateCars"; -import { createUpdateTask } from "@/utils/createUpdateTask"; +import { schedulers } from "@updater/config/schedulers"; +import { updateCars } from "@updater/lib/updateCars"; +import { createUpdateTask } from "@updater/utils/createUpdateTask"; export const updateCarsTask = createUpdateTask( "cars", diff --git a/apps/updater/src/trigger/update-coe-pqp.ts b/apps/updater/src/trigger/update-coe-pqp.ts index 5d17685..021546b 100644 --- a/apps/updater/src/trigger/update-coe-pqp.ts +++ b/apps/updater/src/trigger/update-coe-pqp.ts @@ -1,6 +1,6 @@ -import { schedulers } from "@/config/schedulers"; -import { updateCOEPQP } from "@/lib/updateCOEPQP"; -import { createUpdateTask } from "@/utils/createUpdateTask"; +import { schedulers } from "@updater/config/schedulers"; +import { updateCOEPQP } from "@updater/lib/updateCOEPQP"; +import { createUpdateTask } from "@updater/utils/createUpdateTask"; export const updateCOEPQPTask = createUpdateTask( "coe-pqp", diff --git a/apps/updater/src/trigger/update-coe.ts b/apps/updater/src/trigger/update-coe.ts index 7dd95fb..8c114de 100644 --- a/apps/updater/src/trigger/update-coe.ts +++ b/apps/updater/src/trigger/update-coe.ts @@ -1,5 +1,5 @@ -import { schedulers } from "@/config/schedulers"; -import { updateCOE } from "@/lib/updateCOE"; -import { createUpdateTask } from "@/utils/createUpdateTask"; +import { schedulers } from "@updater/config/schedulers"; +import { updateCOE } from "@updater/lib/updateCOE"; +import { createUpdateTask } from "@updater/utils/createUpdateTask"; export const updateCOETask = createUpdateTask("coe", schedulers.coe, updateCOE); diff --git a/apps/updater/src/utils/__tests__/calculateChecksum.test.ts b/apps/updater/src/utils/__tests__/calculateChecksum.test.ts index 388c4f4..b0ae171 100644 --- a/apps/updater/src/utils/__tests__/calculateChecksum.test.ts +++ b/apps/updater/src/utils/__tests__/calculateChecksum.test.ts @@ -1,7 +1,7 @@ import crypto from "node:crypto"; import { unlink, writeFile } from "node:fs/promises"; import { join } from "node:path"; -import { calculateChecksum } from "@/utils/calculateChecksum"; +import { calculateChecksum } from "@updater/utils/calculateChecksum"; import { afterEach, beforeEach, describe, expect, it } from "vitest"; describe("calculateChecksum", () => { diff --git a/apps/updater/src/utils/__tests__/cleanSpecialChars.test.ts b/apps/updater/src/utils/__tests__/cleanSpecialChars.test.ts index 720734c..1cf83df 100644 --- a/apps/updater/src/utils/__tests__/cleanSpecialChars.test.ts +++ b/apps/updater/src/utils/__tests__/cleanSpecialChars.test.ts @@ -1,4 +1,4 @@ -import { cleanSpecialChars } from "@/utils/cleanSpecialChars"; +import { cleanSpecialChars } from "@updater/utils/cleanSpecialChars"; import { describe, expect, it } from "vitest"; describe("cleanSpecialChars", () => { diff --git a/apps/updater/src/utils/__tests__/createUniqueKey.test.ts b/apps/updater/src/utils/__tests__/createUniqueKey.test.ts index cd9c5bf..18e47cb 100644 --- a/apps/updater/src/utils/__tests__/createUniqueKey.test.ts +++ b/apps/updater/src/utils/__tests__/createUniqueKey.test.ts @@ -1,4 +1,4 @@ -import { createUniqueKey } from "@/utils/createUniqueKey"; +import { createUniqueKey } from "@updater/utils/createUniqueKey"; import { describe, expect, it } from "vitest"; describe("createUniqueKey", () => { diff --git a/apps/updater/src/utils/createUpdateTask.ts b/apps/updater/src/utils/createUpdateTask.ts index 5e0af22..bfeeb27 100644 --- a/apps/updater/src/utils/createUpdateTask.ts +++ b/apps/updater/src/utils/createUpdateTask.ts @@ -1,7 +1,10 @@ -import redis from "@/config/redis"; -import type { ScheduleOptions, SchedulerName } from "@/config/schedulers"; -import type { UpdaterResult } from "@/lib/updater"; import { AbortTaskRunError, logger, schedules } from "@trigger.dev/sdk/v3"; +import redis from "@updater/config/redis"; +import type { + ScheduleOptions, + SchedulerName, +} from "@updater/config/schedulers"; +import type { UpdaterResult } from "@updater/lib/updater"; type UpdaterFunction = () => Promise; diff --git a/apps/updater/src/utils/downloadFile.ts b/apps/updater/src/utils/downloadFile.ts index a808966..86cd63c 100644 --- a/apps/updater/src/utils/downloadFile.ts +++ b/apps/updater/src/utils/downloadFile.ts @@ -1,4 +1,4 @@ -import { AWS_LAMBDA_TEMP_DIR } from "@/config"; +import { AWS_LAMBDA_TEMP_DIR } from "@updater/config"; import AdmZip from "adm-zip"; export const downloadFile = async (url: string, csvFile?: string) => { diff --git a/apps/updater/src/utils/redisCache.ts b/apps/updater/src/utils/redisCache.ts index ba9dc2d..621ae25 100644 --- a/apps/updater/src/utils/redisCache.ts +++ b/apps/updater/src/utils/redisCache.ts @@ -1,4 +1,4 @@ -import redis from "@/config/redis"; +import redis from "@updater/config/redis"; /** * Caches a checksum value for a given file name diff --git a/apps/updater/tsconfig.json b/apps/updater/tsconfig.json index 0b285ad..dd07ebd 100644 --- a/apps/updater/tsconfig.json +++ b/apps/updater/tsconfig.json @@ -12,16 +12,10 @@ "declaration": true, "resolveJsonModule": true, "paths": { - "@/*": ["./src/*"] + "@updater/*": ["src/*"] }, "types": ["node"] }, - "include": [ - "src/**/*", - "drizzle.config.ts", - "sst.config.ts", - "sst-env.d.ts", - "trigger.config.ts" - ], + "include": ["*.ts"], "exclude": ["node_modules", "dist"] } diff --git a/apps/updater/vitest.config.ts b/apps/updater/vitest.config.ts index 31d36c7..298ee33 100644 --- a/apps/updater/vitest.config.ts +++ b/apps/updater/vitest.config.ts @@ -4,7 +4,7 @@ import { defineConfig } from "vitest/config"; export default defineConfig({ resolve: { alias: { - "@": path.resolve(__dirname, "./src"), + "@updater": path.resolve(__dirname, "./src"), }, }, test: { diff --git a/tsconfig.json b/tsconfig.json index 514379c..a068ec2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,7 @@ "target": "ESNext", "forceConsistentCasingInFileNames": true, "esModuleInterop": true, + "moduleResolution": "bundler", "skipLibCheck": true, "verbatimModuleSyntax": true, "paths": {