Skip to content

Commit 0ff48d7

Browse files
bradennapierRon Spickenagel
authored and
Ron Spickenagel
committed
fix: OSX paths can be rewritten improperly (fixes #167)
1 parent 04fd73c commit 0ff48d7

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/utils/general-utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,30 @@
11
import url from "url";
22
import path from "path";
3+
import { realpathSync } from "fs";
34

45
/* ****************************************************************************************************************** *
56
* General Utilities & Helpers
67
* ****************************************************************************************************************** */
78

89
export const isURL = (s: string): boolean => !!s && (!!url.parse(s).host || !!url.parse(s).hostname);
10+
911
export const cast = <T>(v: any): T => v;
12+
1013
export const isBaseDir = (baseDir: string, testDir: string): boolean => {
1114
const relative = path.relative(baseDir, testDir);
1215
return relative ? !relative.startsWith("..") && !path.isAbsolute(relative) : true;
1316
};
17+
1418
export const maybeAddRelativeLocalPrefix = (p: string) => (p[0] === "." ? p : `./${p}`);
19+
20+
export function tryRealpathNative(value: string) {
21+
try {
22+
return realpathSync.native(value);
23+
} catch {
24+
return value;
25+
}
26+
}
27+
28+
export function nativeRelativePath(from: string, to: string) {
29+
return path.relative(tryRealpathNative(from), tryRealpathNative(to));
30+
}

src/utils/resolve-module-name.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { VisitorContext } from "../types";
2-
import { isBaseDir, isURL, maybeAddRelativeLocalPrefix } from "./general-utils";
2+
import { isBaseDir, isURL, maybeAddRelativeLocalPrefix, nativeRelativePath } from "./general-utils";
33
import * as path from "path";
44
import { removeFileExtension, removeSuffix, ResolvedModuleFull, SourceFile } from "typescript";
55
import { getOutputDirForSourceFile } from "./ts-helpers";
@@ -167,12 +167,12 @@ export function resolveModuleName(context: VisitorContext, moduleName: string):
167167

168168
/* Remove base dirs to make relative to root */
169169
if (fileRootDir && moduleRootDir) {
170-
srcFileOutputDir = path.relative(fileRootDir, srcFileOutputDir);
171-
moduleFileOutputDir = path.relative(moduleRootDir, moduleFileOutputDir);
170+
srcFileOutputDir = nativeRelativePath(fileRootDir, srcFileOutputDir);
171+
moduleFileOutputDir = nativeRelativePath(moduleRootDir, moduleFileOutputDir);
172172
}
173173
}
174174

175-
const outputDir = path.relative(srcFileOutputDir, moduleFileOutputDir);
175+
const outputDir = nativeRelativePath(srcFileOutputDir, moduleFileOutputDir);
176176

177177
/* Compose final output path */
178178
const outputPath = maybeAddRelativeLocalPrefix(tsInstance.normalizePath(path.join(outputDir, outputBaseName)));

0 commit comments

Comments
 (0)