|
| 1 | +// @ts-check |
| 2 | +import { existsSync } from "node:fs"; |
| 3 | +import { symlink } from "node:fs/promises"; |
| 4 | +import { dirname, join, resolve } from "node:path"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | +import { patch } from "ts-patch"; |
| 7 | +import { patch as patch1 } from "tsp1"; |
| 8 | +import { patch as patch2 } from "tsp2"; |
| 9 | + |
| 10 | +const __dirname = dirname(fileURLToPath(import.meta.url)); // https://stackoverflow.com/questions/46745014/alternative-for-dirname-in-node-js-when-using-es6-modules |
| 11 | + |
| 12 | +async function symlinkTsNode() { |
| 13 | + // we need to patch the root package node_modules in order for it to locate ts-node for the register tests. |
| 14 | + // installing ts-node as a depenency in the root is not an option since it would create two copies of ts-node |
| 15 | + // thus messing with the mocks in the tests |
| 16 | + const target = resolve(__dirname, "node_modules/ts-node"); |
| 17 | + const path = resolve(__dirname, "../node_modules/ts-node"); |
| 18 | + |
| 19 | + if (!existsSync(path)) await symlink(target, path, "junction"); |
| 20 | +} |
| 21 | + |
| 22 | +function patchTsModules() { |
| 23 | + /* ****************************************************************************************************************** * |
| 24 | + * Config |
| 25 | + * ****************************************************************************************************************** */ |
| 26 | + const rootDir = __dirname; |
| 27 | + const tsDirs = ["typescript-three", "typescript-four-seven", "typescript"]; |
| 28 | + /* ****************************************************************************************************************** * |
| 29 | + * Patch TS Modules |
| 30 | + * ****************************************************************************************************************** */ |
| 31 | + |
| 32 | + const baseDirs = new Map(); |
| 33 | + |
| 34 | + for (const tsDirName of tsDirs) { |
| 35 | + const mainDir = resolve(rootDir, "node_modules", tsDirName); |
| 36 | + if (!existsSync(join(mainDir, "lib-backup"))) baseDirs.set(tsDirName, mainDir); |
| 37 | + } |
| 38 | + |
| 39 | + // Patch discovered modules |
| 40 | + for (const [dirName, dir] of baseDirs) |
| 41 | + if (dirName === "typescript-three") patch1(["tsc.js", "typescript.js"], { basedir: dir }); |
| 42 | + else if (dirName === "typescript-four-seven") patch2(["tsc.js", "typescript.js"], { dir }); |
| 43 | + else patch(["tsc.js", "typescript.js"], { dir }); |
| 44 | +} |
| 45 | + |
| 46 | +patchTsModules(); |
| 47 | +await symlinkTsNode(); |
0 commit comments