NewIntentExtractor / scripts /alias-hooks.mjs
helo-ayush's picture
feat: deploy SIHJantaParty Realtime Chat & Events Server (Node 20 Docker)
78664f6
Raw History Blame Contribute Delete
1.4 kB
/**
* Resolves the `@/*` -> `src/*` TypeScript path alias for plain `node` runs.
*
* Node reads neither tsconfig.json nor its `paths`, so maintenance scripts that
* import app modules (`@/models/teams`) fail to resolve without this. Node 24
* strips the TypeScript itself; only the alias and the missing file extension
* need handling.
*
* Registered via `--import ./scripts/register-alias.mjs` in the npm scripts.
*/
import { existsSync } from "node:fs";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
const __dirname = import.meta.dirname ?? path.dirname(fileURLToPath(import.meta.url));
const SRC = path.resolve(__dirname, "..", "src");
// Extensionless imports need a suffix; try the same order tsc would.
const CANDIDATES = ["", ".ts", ".tsx", ".js", ".mjs", "/index.ts", "/index.tsx"];
export async function resolve(specifier, context, next) {
if (specifier === "server-only") {
return next("next/dist/compiled/server-only/empty", context);
}
if (!specifier.startsWith("@/")) return next(specifier, context);
const base = path.join(SRC, specifier.slice(2));
for (const ext of CANDIDATES) {
const candidate = base + ext;
if (existsSync(candidate)) {
return next(pathToFileURL(candidate).href, context);
}
}
throw new Error(
`[register-alias] could not resolve "${specifier}" under ${SRC}`
);
}