Skip to content

Commit

Permalink
fix: OSX paths can be rewritten improperly (fixes LeDDGroup#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradennapier authored and nonara committed Jan 5, 2023
1 parent 04fd73c commit 8b663a3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
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

0 comments on commit 8b663a3

Please sign in to comment.