Skip to content

Commit 13ff6fc

Browse files
committed
refactor: move fs API methods to own class
1 parent 97fb8de commit 13ff6fc

File tree

2 files changed

+75
-63
lines changed

2 files changed

+75
-63
lines changed

src/fixtures/file-system.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { commands } from "@vitest/browser/context";
2+
import type {
3+
FileSystemTree,
4+
BufferEncoding,
5+
WebContainer,
6+
} from "@webcontainer/api";
7+
8+
export class FileSystem {
9+
/** @internal */
10+
protected get _instance(): WebContainer {
11+
throw new Error("_instance should be overwritte");
12+
}
13+
14+
/**
15+
* Mount file directory into WebContainer.
16+
* `string` arguments are considered paths that are relative to [`root`](https://vitest.dev/config/#root)
17+
*/
18+
async mount(filesOrPath: string | FileSystemTree) {
19+
if (typeof filesOrPath === "string") {
20+
filesOrPath = await commands.readDirectory(filesOrPath);
21+
}
22+
23+
return await this._instance.mount(filesOrPath as FileSystemTree);
24+
}
25+
26+
/**
27+
* WebContainer's [`readFile`](https://webcontainers.io/guides/working-with-the-file-system#readfile) method.
28+
*/
29+
async readFile(path: string, encoding: BufferEncoding = "utf8") {
30+
return this._instance.fs.readFile(path, encoding);
31+
}
32+
33+
/**
34+
* WebContainer's [`writeFile`](https://webcontainers.io/guides/working-with-the-file-system#writefile) method.
35+
*/
36+
async writeFile(path: string, data: string, encoding = "utf8") {
37+
return this._instance.fs.writeFile(path, data, { encoding });
38+
}
39+
40+
/**
41+
* WebContainer's [`rename`](https://webcontainers.io/guides/working-with-the-file-system#rename) method.
42+
*/
43+
async rename(oldPath: string, newPath: string) {
44+
return this._instance.fs.rename(oldPath, newPath);
45+
}
46+
47+
/**
48+
* WebContainer's [`mkdir`](https://webcontainers.io/guides/working-with-the-file-system#mkdir) method.
49+
*/
50+
async mkdir(path: string) {
51+
return this._instance.fs.mkdir(path);
52+
}
53+
54+
/**
55+
* WebContainer's [`readdir`](https://webcontainers.io/guides/working-with-the-file-system#readdir) method.
56+
*/
57+
async readdir(path: string) {
58+
return this._instance.fs.readdir(path);
59+
}
60+
61+
/**
62+
* WebContainer's [`rm`](https://webcontainers.io/guides/working-with-the-file-system#rm) method.
63+
*/
64+
async rm(path: string) {
65+
return this._instance.fs.rm(path);
66+
}
67+
}

src/fixtures/webcontainer.ts

Lines changed: 8 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import { commands } from "@vitest/browser/context";
2-
import {
3-
type BufferEncoding,
4-
type FileSystemTree,
5-
WebContainer as WebContainerApi,
6-
} from "@webcontainer/api";
7-
8-
export class WebContainer {
1+
import { WebContainer as WebContainerApi } from "@webcontainer/api";
2+
import { FileSystem } from "./file-system";
3+
4+
export class WebContainer extends FileSystem {
95
/** @internal */
106
private _instancePromise?: WebContainerApi;
117

@@ -16,12 +12,15 @@ export class WebContainer {
1612
private _onExit: (() => Promise<unknown>)[] = [];
1713

1814
constructor() {
15+
super();
16+
1917
this._isReady = WebContainerApi.boot({}).then((instance) => {
2018
this._instancePromise = instance;
2119
});
2220
}
2321

24-
private get _instance(): WebContainerApi {
22+
/** @internal */
23+
protected get _instance(): WebContainerApi {
2524
if (!this._instancePromise) {
2625
throw new Error(
2726
"Webcontainer is not yet ready, make sure to call wait() after creation",
@@ -43,18 +42,6 @@ export class WebContainer {
4342
});
4443
}
4544

46-
/**
47-
* Mount file directory into WebContainer.
48-
* `string` arguments are considered paths that are relative to [`root`](https://vitest.dev/config/#root)
49-
*/
50-
async mount(filesOrPath: string | FileSystemTree) {
51-
if (typeof filesOrPath === "string") {
52-
filesOrPath = await commands.readDirectory(filesOrPath);
53-
}
54-
55-
return await this._instance.mount(filesOrPath as FileSystemTree);
56-
}
57-
5845
/** @internal */
5946
async teardown() {
6047
await Promise.all(this._onExit.map((fn) => fn()));
@@ -97,46 +84,4 @@ export class WebContainer {
9784

9885
return output.trim();
9986
}
100-
101-
/**
102-
* WebContainer's [`readFile`](https://webcontainers.io/guides/working-with-the-file-system#readfile) method.
103-
*/
104-
async readFile(path: string, encoding: BufferEncoding = "utf8") {
105-
return this._instance.fs.readFile(path, encoding);
106-
}
107-
108-
/**
109-
* WebContainer's [`writeFile`](https://webcontainers.io/guides/working-with-the-file-system#writefile) method.
110-
*/
111-
async writeFile(path: string, data: string, encoding = "utf8") {
112-
return this._instance.fs.writeFile(path, data, { encoding });
113-
}
114-
115-
/**
116-
* WebContainer's [`rename`](https://webcontainers.io/guides/working-with-the-file-system#rename) method.
117-
*/
118-
async rename(oldPath: string, newPath: string) {
119-
return this._instance.fs.rename(oldPath, newPath);
120-
}
121-
122-
/**
123-
* WebContainer's [`mkdir`](https://webcontainers.io/guides/working-with-the-file-system#mkdir) method.
124-
*/
125-
async mkdir(path: string) {
126-
return this._instance.fs.mkdir(path);
127-
}
128-
129-
/**
130-
* WebContainer's [`readdir`](https://webcontainers.io/guides/working-with-the-file-system#readdir) method.
131-
*/
132-
async readdir(path: string) {
133-
return this._instance.fs.readdir(path);
134-
}
135-
136-
/**
137-
* WebContainer's [`rm`](https://webcontainers.io/guides/working-with-the-file-system#rm) method.
138-
*/
139-
async rm(path: string) {
140-
return this._instance.fs.rm(path);
141-
}
14287
}

0 commit comments

Comments
 (0)