Skip to content

Commit b1a72fa

Browse files
committed
get updates working
1 parent 8d48215 commit b1a72fa

File tree

4 files changed

+65
-26
lines changed

4 files changed

+65
-26
lines changed

src/lib/client/adapters/filesystem/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export async function create(stubs) {
3838
await new Promise((f) => setTimeout(f, 100)); // wait for chokidar
3939
},
4040

41-
/** @param {import('$lib/types').Stub[]} stubs */
41+
/** @param {import('$lib/types').FileStub[]} stubs */
4242
async update(stubs) {
4343
await fetch(`/backend/${id}`, {
4444
method: 'put',

src/lib/client/adapters/webcontainer/index.js

Lines changed: 62 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,10 @@ export async function create(stubs) {
1818
fulfil(base);
1919
});
2020

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+
});
3125

3226
const code = await install.onExit;
3327

@@ -36,34 +30,79 @@ export async function create(stubs) {
3630
return;
3731
}
3832

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'] });
4834
});
4935

36+
let current = stubs;
37+
5038
return {
5139
base,
5240

5341
/** @param {import('$lib/types').Stub[]} stubs */
5442
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+
5563
// TODO
56-
await new Promise((f) => setTimeout(f, 100)); // wait for chokidar
64+
await new Promise((f) => setTimeout(f, 200)); // wait for chokidar
5765
},
5866

59-
/** @param {import('$lib/types').Stub[]} stubs */
67+
/** @param {import('$lib/types').FileStub[]} stubs */
6068
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+
61100
// TODO
62-
await new Promise((f) => setTimeout(f, 100)); // wait for chokidar
101+
await new Promise((f) => setTimeout(f, 200)); // wait for chokidar
63102
},
64103

65104
async destroy() {
66-
// TODO
105+
vm.teardown();
67106
}
68107
};
69108
}

src/lib/types/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export type Stub = FileStub | DirectoryStub;
1919
export interface Adapter {
2020
base: string;
2121
reset(files: Array<Stub>): Promise<void>;
22-
update(files: Array<Stub>): Promise<void>;
22+
update(file: Array<FileStub>): Promise<void>;
2323
destroy(): Promise<void>;
2424
}
2525

src/routes/tutorial/[slug]/index.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@
145145
146146
const actual = new Map();
147147
148-
await adapter.update(stubs);
148+
await adapter.reset(stubs);
149149
await get_transformed_modules(adapter.base, section.scope.prefix, stubs, actual);
150150
151151
for (const [name, transformed] of expected.entries()) {

0 commit comments

Comments
 (0)