Skip to content

Commit d5b448d

Browse files
committed
fix(cli): rename resolveViteBin to resolveViteBinFromPackageJsonPath and update related usages
1 parent 297c3eb commit d5b448d

2 files changed

Lines changed: 18 additions & 8 deletions

File tree

packages/cli/src/bin/tsed-build.spec.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import {mkdtemp, rm, writeFile} from "node:fs/promises";
22
import {tmpdir} from "node:os";
33
import path from "node:path";
4-
import {pathToFileURL} from "node:url";
54

65
import {describe, expect, it} from "vitest";
76

8-
import {resolveViteBin} from "./tsed-build.js";
7+
import {resolveViteBinFromPackageJsonPath} from "./tsed-build.js";
98

109
async function withTempDir(run: (cwd: string) => Promise<void>) {
1110
const cwd = await mkdtemp(path.join(tmpdir(), "tsed-build-"));
@@ -23,7 +22,7 @@ describe("tsed-build", () => {
2322
const packageJsonPath = path.join(cwd, "package.json");
2423
await writeFile(packageJsonPath, JSON.stringify({bin: {vite: "bin/vite.js"}}));
2524

26-
const viteBin = await resolveViteBin(async () => pathToFileURL(packageJsonPath).href);
25+
const viteBin = await resolveViteBinFromPackageJsonPath(packageJsonPath);
2726

2827
expect(viteBin).toEqual(path.join(cwd, "bin/vite.js"));
2928
});
@@ -34,7 +33,7 @@ describe("tsed-build", () => {
3433
const packageJsonPath = path.join(cwd, "package.json");
3534
await writeFile(packageJsonPath, JSON.stringify({bin: "bin/vite.js"}));
3635

37-
const viteBin = await resolveViteBin(async () => pathToFileURL(packageJsonPath).href);
36+
const viteBin = await resolveViteBinFromPackageJsonPath(packageJsonPath);
3837

3938
expect(viteBin).toEqual(path.join(cwd, "bin/vite.js"));
4039
});
@@ -45,7 +44,7 @@ describe("tsed-build", () => {
4544
const packageJsonPath = path.join(cwd, "package.json");
4645
await writeFile(packageJsonPath, JSON.stringify({name: "vite"}));
4746

48-
await expect(resolveViteBin(async () => pathToFileURL(packageJsonPath).href)).rejects.toThrowError(
47+
await expect(resolveViteBinFromPackageJsonPath(packageJsonPath)).rejects.toThrowError(
4948
"Unable to resolve Vite CLI binary from vite/package.json"
5049
);
5150
});

packages/cli/src/bin/tsed-build.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,15 @@ function runNode(cmd: string, args: string[]) {
2323
});
2424
}
2525

26-
type ResolveFn = (specifier: string) => string | Promise<string>;
26+
function toPath(value: string) {
27+
if (value.startsWith("file://")) {
28+
return fileURLToPath(value);
29+
}
30+
31+
return value;
32+
}
2733

28-
export async function resolveViteBin(resolve: ResolveFn = import.meta.resolve) {
29-
const packageJsonPath = fileURLToPath(await resolve("vite/package.json"));
34+
export async function resolveViteBinFromPackageJsonPath(packageJsonPath: string) {
3035
const packageJson = JSON.parse(await readFile(packageJsonPath, "utf-8")) as {
3136
bin?: string | Record<string, string>;
3237
};
@@ -39,6 +44,12 @@ export async function resolveViteBin(resolve: ResolveFn = import.meta.resolve) {
3944
return path.resolve(path.dirname(packageJsonPath), binRelativePath);
4045
}
4146

47+
export async function resolveViteBin() {
48+
const packageJsonPath = toPath(await import.meta.resolve("vite/package.json"));
49+
50+
return resolveViteBinFromPackageJsonPath(packageJsonPath);
51+
}
52+
4253
export async function build(rawArgs: string[] = process.argv.slice(2)) {
4354
const viteBin = await resolveViteBin();
4455
await runNode(process.execPath, [viteBin, "build", ...rawArgs]);

0 commit comments

Comments
 (0)