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+
3+ import { FileSystem } from "./file-system" ;
4+ import { ProcessWrap } from "./process" ;
5+
6+ export class WebContainer extends FileSystem {
97 /** @internal */
108 private _instancePromise ?: WebContainerApi ;
119
@@ -16,12 +14,15 @@ export class WebContainer {
1614 private _onExit : ( ( ) => Promise < unknown > ) [ ] = [ ] ;
1715
1816 constructor ( ) {
17+ super ( ) ;
18+
1919 this . _isReady = WebContainerApi . boot ( { } ) . then ( ( instance ) => {
2020 this . _instancePromise = instance ;
2121 } ) ;
2222 }
2323
24- private get _instance ( ) : WebContainerApi {
24+ /** @internal */
25+ protected get _instance ( ) : WebContainerApi {
2526 if ( ! this . _instancePromise ) {
2627 throw new Error (
2728 "Webcontainer is not yet ready, make sure to call wait() after creation" ,
@@ -43,18 +44,6 @@ export class WebContainer {
4344 } ) ;
4445 }
4546
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-
5847 /** @internal */
5948 async teardown ( ) {
6049 await Promise . all ( this . _onExit . map ( ( fn ) => fn ( ) ) ) ;
@@ -68,75 +57,18 @@ export class WebContainer {
6857
6958 /**
7059 * Run command inside WebContainer.
71- * Returns the output of the command .
60+ * See [`runCommand` documentation](https://github.com/stackblitz/webcontainer-test#runcommand) for usage examples .
7261 */
73- async runCommand ( command : string , args : string [ ] = [ ] ) {
74- let output = "" ;
75-
76- const process = await this . _instance . spawn ( command , args , { output : true } ) ;
77-
78- process . output . pipeTo (
79- new WritableStream ( {
80- write ( data ) {
81- output += data ;
82- } ,
83- } ) ,
62+ runCommand (
63+ command : string ,
64+ args : string [ ] = [ ] ,
65+ ) : PromiseLike < string > & ProcessWrap {
66+ const proc = new ProcessWrap (
67+ this . _instance . spawn ( command , args , { output : true } ) ,
8468 ) ;
8569
86- // make sure any long-living processes are terminated before teardown, e.g. "npm run dev" commands
87- this . _onExit . push ( ( ) => {
88- // @ts -ignore -- internal
89- if ( process . _process != null ) {
90- process . kill ( ) ;
91- }
92-
93- return process . exit ;
94- } ) ;
95-
96- await process . exit ;
70+ this . _onExit . push ( ( ) => proc . exit ( ) ) ;
9771
98- return output . trim ( ) ;
99- }
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 ) ;
72+ return proc ;
14173 }
14274}
0 commit comments