@@ -18,16 +18,10 @@ export async function create(stubs) {
18
18
fulfil ( base ) ;
19
19
} ) ;
20
20
21
- const install = await vm . run (
22
- {
23
- command : 'turbo' ,
24
- args : [ 'install' ]
25
- } ,
26
- {
27
- stdout : ( data ) => console . log ( data ) ,
28
- stderr : ( data ) => console . error ( data )
29
- }
30
- ) ;
21
+ const install = await vm . run ( {
22
+ command : 'turbo' ,
23
+ args : [ 'install' ]
24
+ } ) ;
31
25
32
26
const code = await install . onExit ;
33
27
@@ -36,34 +30,79 @@ export async function create(stubs) {
36
30
return ;
37
31
}
38
32
39
- console . log ( 'installation succeeded' ) ;
40
-
41
- await vm . run (
42
- { command : 'turbo' , args : [ 'run' , 'dev' ] } ,
43
- {
44
- stdout : ( data ) => console . log ( data ) ,
45
- stderr : ( data ) => console . error ( data )
46
- }
47
- ) ;
33
+ await vm . run ( { command : 'turbo' , args : [ 'run' , 'dev' ] } ) ;
48
34
} ) ;
49
35
36
+ let current = stubs ;
37
+
50
38
return {
51
39
base,
52
40
53
41
/** @param {import('$lib/types').Stub[] } stubs */
54
42
async reset ( stubs ) {
43
+ const old = new Set ( current . filter ( ( stub ) => stub . type === 'file' ) . map ( ( stub ) => stub . name ) ) ;
44
+ current = stubs ;
45
+
46
+ for ( const stub of stubs ) {
47
+ if ( stub . type === 'file' ) {
48
+ old . delete ( stub . name ) ;
49
+ }
50
+ }
51
+
52
+ for ( const file of old ) {
53
+ // TODO this fails with a cryptic error
54
+ // index.svelte:155 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'rmSync')
55
+ // at Object.rm (webcontainer.e2e246a845f9e80283581d6b944116e399af6950.js:6:121171)
56
+ // at MessagePort._0x4ec3f4 (webcontainer.e2e246a845f9e80283581d6b944116e399af6950.js:6:110957)
57
+ // at MessagePort.nrWrapper (headless:5:29785)
58
+ await vm . fs . rm ( file ) ;
59
+ }
60
+
61
+ await vm . loadFiles ( convert_stubs_to_tree ( stubs ) ) ;
62
+
55
63
// TODO
56
- await new Promise ( ( f ) => setTimeout ( f , 100 ) ) ; // wait for chokidar
64
+ await new Promise ( ( f ) => setTimeout ( f , 200 ) ) ; // wait for chokidar
57
65
} ,
58
66
59
- /** @param {import('$lib/types').Stub [] } stubs */
67
+ /** @param {import('$lib/types').FileStub [] } stubs */
60
68
async update ( stubs ) {
69
+ /** @type {import('@webcontainer/api').FileSystemTree } */
70
+ const root = { } ;
71
+
72
+ for ( const stub of stubs ) {
73
+ let tree = root ;
74
+
75
+ const path = stub . name . split ( '/' ) . slice ( 1 ) ;
76
+ const basename = /** @type {string } */ ( path . pop ( ) ) ;
77
+
78
+ for ( const part of path ) {
79
+ if ( ! tree [ part ] ) {
80
+ /** @type {import('@webcontainer/api').FileSystemTree } */
81
+ const directory = { } ;
82
+
83
+ tree [ part ] = {
84
+ directory
85
+ } ;
86
+ }
87
+
88
+ tree = /** @type {import('@webcontainer/api').DirectoryEntry } */ ( tree [ part ] ) . directory ;
89
+ }
90
+
91
+ tree [ basename ] = {
92
+ file : {
93
+ contents : stub . text ? stub . contents : base64 . toByteArray ( stub . contents )
94
+ }
95
+ } ;
96
+ }
97
+
98
+ await vm . loadFiles ( root ) ;
99
+
61
100
// TODO
62
- await new Promise ( ( f ) => setTimeout ( f , 100 ) ) ; // wait for chokidar
101
+ await new Promise ( ( f ) => setTimeout ( f , 200 ) ) ; // wait for chokidar
63
102
} ,
64
103
65
104
async destroy ( ) {
66
- // TODO
105
+ vm . teardown ( ) ;
67
106
}
68
107
} ;
69
108
}
0 commit comments