Skip to content

Commit 25b1755

Browse files
committed
+ FileManager.tsx: auto-chmod
1 parent 2df79cb commit 25b1755

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/components/FileManager.tsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { useStore } from '@nanostores/react'
2+
import tutorialStore from 'tutorialkit:store';
3+
import { useRef, useEffect } from 'react';
4+
import { webcontainer } from 'tutorialkit:core';
5+
import type { WebContainer } from '@webcontainer/api';
6+
7+
export function FileManager() {
8+
const files = useStore(tutorialStore.files);
9+
const processedFiles = useRef(new Set<string>());
10+
11+
async function chmodx(wc: WebContainer, path: string) {
12+
const process = await wc.spawn('chmod', ['+x', path]);
13+
14+
const exitCode = await process.exit;
15+
16+
if (exitCode !== 0) {
17+
console.error(`failed to chmox +x ${path}: `, exitCode)
18+
} else {
19+
console.log(`updated permissions for: ${path}`)
20+
}
21+
}
22+
23+
useEffect(() => {
24+
if (!files) return;
25+
26+
(async () => {
27+
const wc = await webcontainer;
28+
29+
Object.entries(files).forEach(([_, fd]) => {
30+
const dir = fd.path.split('/').filter(Boolean).slice(-2, -1)[0];
31+
if (dir === "bin" && !processedFiles.current.has(fd.path)) {
32+
processedFiles.current = new Set([...processedFiles.current, fd.path]);
33+
chmodx(wc, '/home/tutorial' + fd.path);
34+
}
35+
});
36+
})();
37+
}, [files]);
38+
39+
return null;
40+
}

src/components/TopBar.astro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
import path from 'node:path';
33
import { ShellConfigurator } from './ShellConfigurator';
4+
import { FileManager } from './FileManager';
45
56
const logo = path.join(import.meta.env.BASE_URL, 'logo.svg');
67
const logoDark = path.join(import.meta.env.BASE_URL, 'logo-dark.svg');
@@ -36,3 +37,4 @@ const logoDark = path.join(import.meta.env.BASE_URL, 'logo-dark.svg');
3637
</nav>
3738

3839
<ShellConfigurator client:only />
40+
<FileManager client:only />

0 commit comments

Comments
 (0)