Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix osx root path issues #171

Merged
merged 3 commits into from Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Expand Up @@ -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

Expand Down
16 changes: 16 additions & 0 deletions 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 = <T>(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));
}
8 changes: 4 additions & 4 deletions 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";
Expand Down Expand Up @@ -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)));
Expand Down
6 changes: 3 additions & 3 deletions 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
Expand All @@ -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 });
4 changes: 3 additions & 1 deletion test/tests/transformer/specific.test.ts
Expand Up @@ -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<import("./packages/pkg-b").PackageBType>`,
`export ${
tsVersion < 49 ? `declare ` : ""
}type ImportWithChildren = import("./packages/pkg-a").PassThru<import("./packages/pkg-b").PackageBType>`,
{ kind: ["dts"] }
);
});
Expand Down