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