Skip to content

Commit ff16678

Browse files
authored
Check import.meta.resolve, use require.resolve as a fallback
1 parent 1e97d10 commit ff16678

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

_packages/native-preview/lib/getExePath.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from "node:fs";
2+
import module from "node:module";
23
import path from "node:path";
34
import { fileURLToPath } from "node:url";
45

@@ -22,21 +23,21 @@ export default function getExePath() {
2223
// We're actually running from an installed package.
2324
const platformPackageName = "@typescript/" + expectedPackage;
2425
try {
25-
// v20.6.0, v18.19.0
26-
const packageJson = import.meta.resolve(platformPackageName + "/package.json");
27-
const packageJsonPath = fileURLToPath(packageJson);
28-
exeDir = path.join(path.dirname(packageJsonPath), "lib");
29-
}
30-
catch (e) {
31-
// Failed to resolve: this might be due to an older Node version that doesn't support import.meta.resolve.
32-
if (e instanceof TypeError && e.message?.includes("resolve is not a function")) {
26+
if (typeof import.meta.resolve === "undefined") {
3327
// v16.20.1
34-
exeDir = path.resolve(__dirname, "..", "node_modules", platformPackageName, "lib");
28+
const require = module.createRequire(import.meta.url);
29+
const packageJson = require.resolve(platformPackageName + "/package.json");
30+
exeDir = path.join(path.dirname(packageJson), "lib");
3531
} else {
36-
throw new Error("Unable to resolve " + platformPackageName + ". Either your platform is unsupported, or you are missing the package on disk.");
32+
// v20.6.0, v18.19.0
33+
const packageJson = import.meta.resolve(platformPackageName + "/package.json");
34+
const packageJsonPath = fileURLToPath(packageJson);
35+
exeDir = path.join(path.dirname(packageJsonPath), "lib");
3736
}
37+
} catch (e) {
38+
console.error(e);
39+
throw new Error("Unable to resolve " + platformPackageName + ". Either your platform is unsupported, or you are missing the package on disk.");
3840
}
39-
4041
}
4142

4243
const exe = path.join(exeDir, "tsgo" + (process.platform === "win32" ? ".exe" : ""));
@@ -47,4 +48,3 @@ export default function getExePath() {
4748

4849
return exe;
4950
}
50-

0 commit comments

Comments
 (0)