forked from stackblitz/tutorialkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathButtonWriteToFile.tsx
More file actions
41 lines (33 loc) · 998 Bytes
/
ButtonWriteToFile.tsx
File metadata and controls
41 lines (33 loc) · 998 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import tutorialStore from 'tutorialkit:store';
import { webcontainer } from 'tutorialkit:core';
interface Props {
filePath: string;
newContent: string;
// default to 'store'
access?: 'store' | 'webcontainer';
testId?: string;
}
export function ButtonWriteToFile({ filePath, newContent, access = 'store', testId = 'write-to-file' }: Props) {
async function writeFile() {
switch (access) {
case 'webcontainer': {
const webcontainerInstance = await webcontainer;
const folderPath = filePath.split('/').slice(0, -1).join('/');
if (folderPath) {
await webcontainerInstance.fs.mkdir(folderPath, { recursive: true });
}
await webcontainerInstance.fs.writeFile(filePath, newContent);
return;
}
case 'store': {
tutorialStore.updateFile(filePath, newContent);
return;
}
}
}
return (
<button data-testid={testId} onClick={writeFile}>
Write to File
</button>
);
}