Skip to content

Commit

Permalink
fix: fix osx root path issues
Browse files Browse the repository at this point in the history
fixes so all paths dont get rewritten to `../../../../../../../users/...` on osx 

fixes LeDDGroup#167
  • Loading branch information
bradennapier committed Dec 15, 2022
1 parent 04fd73c commit 419dbe6
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/utils/resolve-module-name.ts
@@ -1,6 +1,7 @@
import { VisitorContext } from "../types";
import { isBaseDir, isURL, maybeAddRelativeLocalPrefix } from "./general-utils";
import * as path from "path";
import { realpathSync } from "fs";
import { removeFileExtension, removeSuffix, ResolvedModuleFull, SourceFile } from "typescript";
import { getOutputDirForSourceFile } from "./ts-helpers";

Expand Down Expand Up @@ -116,6 +117,18 @@ function getResolvedSourceFile(context: VisitorContext, fileName: string): Sourc
// region: Utils
/* ****************************************************************************************************************** */

function tryRealpathNative(value) {
try {
return realpathSync.native(value)
} catch {
return value
}
}

function nativeRelativePath(from, to) {
return path.relative(tryRealpathNative(from), tryRealpathNative(to))
}

/**
* Resolve a module name
*/
Expand Down Expand Up @@ -167,12 +180,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 419dbe6

Please sign in to comment.