Skip to content

Commit 0a3be81

Browse files
committed
fix sub-paths
1 parent 52f9c02 commit 0a3be81

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { mkdir, writeFile } from "fs/promises";
2+
import { tmpdir } from "os";
3+
import { describe, expect, it } from "vitest";
4+
import { subPaths } from "./sub-paths";
5+
import { pathToFileURL } from "url";
6+
7+
describe("sub-paths", () => {
8+
it("should retrieve all sub-paths of a directory", async () => {
9+
const tmpDir = tmpdir();
10+
11+
await mkdir(`${tmpDir}/test-dir/sub`, { recursive: true });
12+
13+
await writeFile(`${tmpDir}/test-dir/sub/file1.txt`, "file1");
14+
await writeFile(`${tmpDir}/test-dir/sub/file2.txt`, "file2");
15+
await writeFile(`${tmpDir}/test-dir/file3.txt`, "file3");
16+
await writeFile(`${tmpDir}/test-dir/file4.txt`, "file4");
17+
const result = await subPaths(pathToFileURL(`${tmpDir}/test-dir`));
18+
19+
expect(result).toEqual([
20+
{
21+
path: pathToFileURL(`${tmpDir}/test-dir/file3.txt`),
22+
relativePath: "file3.txt",
23+
},
24+
{
25+
path: pathToFileURL(`${tmpDir}/test-dir/file4.txt`),
26+
relativePath: "file4.txt",
27+
},
28+
29+
{
30+
path: pathToFileURL(`${tmpDir}/test-dir/sub/file1.txt`),
31+
relativePath: "sub/file1.txt",
32+
},
33+
{
34+
path: pathToFileURL(`${tmpDir}/test-dir/sub/file2.txt`),
35+
relativePath: "sub/file2.txt",
36+
},
37+
]);
38+
});
39+
});

packages/hub/src/utils/sub-paths.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { readdir, stat } from "node:fs/promises";
2+
import { fileURLToPath, pathToFileURL } from "node:url";
23

34
/**
45
* Recursively retrieves all sub-paths of a given directory up to a specified depth.
@@ -20,7 +21,7 @@ export async function subPaths(
2021
const files = await readdir(path, { withFileTypes: true });
2122
const ret: Array<{ path: URL; relativePath: string }> = [];
2223
for (const file of files) {
23-
const filePath = new URL(file.name, path);
24+
const filePath = pathToFileURL(fileURLToPath(path) + "/" + file.name);
2425
if (file.isDirectory()) {
2526
ret.push(
2627
...(await subPaths(filePath, maxDepth - 1)).map((subPath) => ({

packages/hub/vitest-browser.config.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default defineConfig({
77
...configDefaults.exclude,
88
"src/utils/FileBlob.spec.ts",
99
"src/utils/symlink.spec.ts",
10+
"src/utils/sub-paths.spec.ts",
1011
"src/lib/cache-management.spec.ts",
1112
"src/lib/download-file-to-cache-dir.spec.ts",
1213
"src/lib/snapshot-download.spec.ts",

0 commit comments

Comments
 (0)