Add frontend app with terminal UI, sandbox integration, and asset handling.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
91049379f7
commit
b841c2d1f6
19 files changed
+800
-13
No files matched your search
@@ -0,0 +1,52 @@
|
||||
import { existsSync, readdirSync, readFileSync } from 'node:fs';
|
||||
import { extname, join, relative, sep } from 'node:path';
|
||||
|
||||
export type Asset = {
|
||||
body: Buffer;
|
||||
type: string;
|
||||
/** Vite puts a content hash into every file name below assets/. */
|
||||
immutable: boolean;
|
||||
};
|
||||
|
||||
const TYPES: Record<string, string> = {
|
||||
'.html': 'text/html; charset=utf-8',
|
||||
'.js': 'text/javascript; charset=utf-8',
|
||||
'.css': 'text/css; charset=utf-8',
|
||||
'.svg': 'image/svg+xml',
|
||||
'.png': 'image/png',
|
||||
'.ico': 'image/x-icon',
|
||||
'.woff': 'font/woff',
|
||||
'.woff2': 'font/woff2',
|
||||
'.txt': 'text/plain; charset=utf-8',
|
||||
};
|
||||
|
||||
function walk(directory: string): string[]
|
||||
{
|
||||
return readdirSync(directory, { withFileTypes: true }).flatMap((entry) =>
|
||||
{
|
||||
const path = join(directory, entry.name);
|
||||
if (entry.isDirectory()) return walk(path);
|
||||
return entry.isFile() ? [path] : [];
|
||||
});
|
||||
}
|
||||
|
||||
/** Loads the built frontend into memory, keyed by URL path. The index page is also served at "/". */
|
||||
export function loadAssets(directory: string): Map<string, Asset>
|
||||
{
|
||||
const assets = new Map<string, Asset>();
|
||||
if (!existsSync(directory)) return assets;
|
||||
|
||||
for (const file of walk(directory))
|
||||
{
|
||||
const path = `/${relative(directory, file).split(sep).join('/')}`;
|
||||
assets.set(path, {
|
||||
body: readFileSync(file),
|
||||
type: TYPES[extname(file)] ?? 'application/octet-stream',
|
||||
immutable: path.startsWith('/assets/'),
|
||||
});
|
||||
}
|
||||
|
||||
const index = assets.get('/index.html');
|
||||
if (index) assets.set('/', index);
|
||||
return assets;
|
||||
}
|
||||
Reference in new issue
Block a user