From 8b663a36ff0f10c4aef4e625c9ceebb324f0efaa Mon Sep 17 00:00:00 2001 From: Braden Napier Date: Thu, 15 Dec 2022 15:36:37 -0500 Subject: [PATCH 1/3] fix: OSX paths can be rewritten improperly (fixes #167) --- src/utils/general-utils.ts | 16 ++++++++++++++++ src/utils/resolve-module-name.ts | 8 ++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/utils/general-utils.ts b/src/utils/general-utils.ts index 89652cb..2456ffa 100755 --- a/src/utils/general-utils.ts +++ b/src/utils/general-utils.ts @@ -1,14 +1,30 @@ import url from "url"; import path from "path"; +import { realpathSync } from "fs"; /* ****************************************************************************************************************** * * General Utilities & Helpers * ****************************************************************************************************************** */ export const isURL = (s: string): boolean => !!s && (!!url.parse(s).host || !!url.parse(s).hostname); + export const cast = (v: any): T => v; + export const isBaseDir = (baseDir: string, testDir: string): boolean => { const relative = path.relative(baseDir, testDir); return relative ? !relative.startsWith("..") && !path.isAbsolute(relative) : true; }; + export const maybeAddRelativeLocalPrefix = (p: string) => (p[0] === "." ? p : `./${p}`); + +export function tryRealpathNative(value: string) { + try { + return realpathSync.native(value); + } catch { + return value; + } +} + +export function nativeRelativePath(from: string, to: string) { + return path.relative(tryRealpathNative(from), tryRealpathNative(to)); +} diff --git a/src/utils/resolve-module-name.ts b/src/utils/resolve-module-name.ts index b31158b..7abf35d 100755 --- a/src/utils/resolve-module-name.ts +++ b/src/utils/resolve-module-name.ts @@ -1,5 +1,5 @@ import { VisitorContext } from "../types"; -import { isBaseDir, isURL, maybeAddRelativeLocalPrefix } from "./general-utils"; +import { isBaseDir, isURL, maybeAddRelativeLocalPrefix, nativeRelativePath } from "./general-utils"; import * as path from "path"; import { removeFileExtension, removeSuffix, ResolvedModuleFull, SourceFile } from "typescript"; import { getOutputDirForSourceFile } from "./ts-helpers"; @@ -167,12 +167,12 @@ export function resolveModuleName(context: VisitorContext, moduleName: string): /* Remove base dirs to make relative to root */ if (fileRootDir && moduleRootDir) { - srcFileOutputDir = path.relative(fileRootDir, srcFileOutputDir); - moduleFileOutputDir = path.relative(moduleRootDir, moduleFileOutputDir); + srcFileOutputDir = nativeRelativePath(fileRootDir, srcFileOutputDir); + moduleFileOutputDir = nativeRelativePath(moduleRootDir, moduleFileOutputDir); } } - const outputDir = path.relative(srcFileOutputDir, moduleFileOutputDir); + const outputDir = nativeRelativePath(srcFileOutputDir, moduleFileOutputDir); /* Compose final output path */ const outputPath = maybeAddRelativeLocalPrefix(tsInstance.normalizePath(path.join(outputDir, outputBaseName))); From 46f4588fe7b845b49d282f04a22fefd9d1ffdc21 Mon Sep 17 00:00:00 2001 From: Ron S Date: Thu, 5 Jan 2023 17:05:28 -0500 Subject: [PATCH 2/3] style: Formatting fixes --- test/prepare.js | 6 +++--- test/tests/transformer/specific.test.ts | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/test/prepare.js b/test/prepare.js index 3cb9ba7..99ebc6a 100755 --- a/test/prepare.js +++ b/test/prepare.js @@ -1,7 +1,7 @@ const fs = require("fs"); const path = require("path"); const tsPatch = require("ts-patch"); -const tsp1 = require('tsp1'); +const tsp1 = require("tsp1"); /* ****************************************************************************************************************** * * Config @@ -22,6 +22,6 @@ for (const tsDirName of tsDirs) { } // Patch discovered modules -for (const [ dirName, dir ] of baseDirs) - if (dirName === 'typescript-three') tsp1.patch(["tsc.js", "typescript.js"], { basedir: dir }) +for (const [dirName, dir] of baseDirs) + if (dirName === "typescript-three") tsp1.patch(["tsc.js", "typescript.js"], { basedir: dir }); else tsPatch.patch(["tsc.js", "typescript.js"], { dir }); diff --git a/test/tests/transformer/specific.test.ts b/test/tests/transformer/specific.test.ts index a74321f..a0f215a 100755 --- a/test/tests/transformer/specific.test.ts +++ b/test/tests/transformer/specific.test.ts @@ -239,7 +239,9 @@ describe(`Specific Tests`, () => { (!skipDts && tsVersion >= 38 ? test : test.skip)(`Resolves nested imports`, () => { expect(subPackagesFile).transformedMatches( - `export ${tsVersion < 49 ? `declare ` : ''}type ImportWithChildren = import("./packages/pkg-a").PassThru`, + `export ${ + tsVersion < 49 ? `declare ` : "" + }type ImportWithChildren = import("./packages/pkg-a").PassThru`, { kind: ["dts"] } ); }); From 6c1aeefa1af5c4df0ad284a342173a7d3688949b Mon Sep 17 00:00:00 2001 From: Ron S Date: Thu, 5 Jan 2023 17:07:34 -0500 Subject: [PATCH 3/3] build(ci): Updated publish script --- .github/workflows/publish.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 6b5d633..a8c6374 100755 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -38,7 +38,7 @@ jobs: run: yarn build - name: Test - run: yarn test + run: yarn test --runInBand=false --maxWorkers=2 --workerIdleMemoryLimit=1700MB # https://github.com/facebook/jest/issues/11956 env: CI: true