From adb8b4549eb4defbb69ef2edac5250255663e960 Mon Sep 17 00:00:00 2001 From: Pabloader Date: Tue, 9 Jul 2024 12:51:37 +0000 Subject: [PATCH] Refactor directories layout --- {src => build}/assets/index.html | 0 {src/build => build}/audioPlugin.ts | 0 {src/build => build}/build.ts | 0 {src/build => build}/fontPlugin.ts | 0 {src/build => build}/html.ts | 4 ++-- {src/build => build}/imagePlugin.ts | 0 {src/build => build}/isGame.ts | 4 ++-- {src/build => build}/server.ts | 3 ++- {src/build => build}/wasmPlugin.ts | 0 package.json | 5 +++-- src/common/types.d.ts | 24 ------------------------ src/games/playground/awoo.wasm.ts | 4 ++++ src/games/playground/index.tsx | 3 ++- src/types.d.ts | 24 ++++++++++++++++++++++++ 14 files changed, 39 insertions(+), 32 deletions(-) rename {src => build}/assets/index.html (100%) rename {src/build => build}/audioPlugin.ts (100%) rename {src/build => build}/build.ts (100%) rename {src/build => build}/fontPlugin.ts (100%) rename {src/build => build}/html.ts (93%) rename {src/build => build}/imagePlugin.ts (100%) rename {src/build => build}/isGame.ts (78%) rename {src/build => build}/server.ts (88%) rename {src/build => build}/wasmPlugin.ts (100%) delete mode 100644 src/common/types.d.ts diff --git a/src/assets/index.html b/build/assets/index.html similarity index 100% rename from src/assets/index.html rename to build/assets/index.html diff --git a/src/build/audioPlugin.ts b/build/audioPlugin.ts similarity index 100% rename from src/build/audioPlugin.ts rename to build/audioPlugin.ts diff --git a/src/build/build.ts b/build/build.ts similarity index 100% rename from src/build/build.ts rename to build/build.ts diff --git a/src/build/fontPlugin.ts b/build/fontPlugin.ts similarity index 100% rename from src/build/fontPlugin.ts rename to build/fontPlugin.ts diff --git a/src/build/html.ts b/build/html.ts similarity index 93% rename from src/build/html.ts rename to build/html.ts index 5fca08c..4310e75 100644 --- a/src/build/html.ts +++ b/build/html.ts @@ -11,10 +11,10 @@ import { getGames } from './isGame'; import audioPlugin from './audioPlugin'; export async function buildHTML(game: string, production = false, portable = false) { - const html = await Bun.file(path.resolve(import.meta.dir, '..', 'assets', 'index.html')).text(); + const html = await Bun.file(path.resolve(import.meta.dir, 'assets', 'index.html')).text(); const bundle = await Bun.build({ outdir: '/tmp', - entrypoints: [path.resolve(import.meta.dir, '..', 'index.ts')], + entrypoints: [path.resolve(import.meta.dir, '..', 'src', 'index.ts')], sourcemap: production ? 'none' : 'inline', define: { global: 'window', diff --git a/src/build/imagePlugin.ts b/build/imagePlugin.ts similarity index 100% rename from src/build/imagePlugin.ts rename to build/imagePlugin.ts diff --git a/src/build/isGame.ts b/build/isGame.ts similarity index 78% rename from src/build/isGame.ts rename to build/isGame.ts index 081d277..5bd4825 100644 --- a/src/build/isGame.ts +++ b/build/isGame.ts @@ -4,7 +4,7 @@ import fs from 'fs/promises'; export async function isGame(name: string | null | undefined) { if (!name || name === 'index') return false; - const dir = path.resolve(import.meta.dir, '..', 'games', name); + const dir = path.resolve(import.meta.dir, '..', 'src', 'games', name); if (!await fs.exists(dir)) return false; @@ -14,7 +14,7 @@ export async function isGame(name: string | null | undefined) { } export async function getGames() { - const dir = path.resolve(import.meta.dir, '..', 'games'); + const dir = path.resolve(import.meta.dir, '..', 'src', 'games'); if (!await fs.exists(dir)) return []; const stat = await fs.stat(dir); diff --git a/src/build/server.ts b/build/server.ts similarity index 88% rename from src/build/server.ts rename to build/server.ts index be94617..f56c67f 100644 --- a/src/build/server.ts +++ b/build/server.ts @@ -29,8 +29,9 @@ Bun.serve({ } } catch (e) { console.error(e); + return new Response(`Error building HTML: ${e}`, { status: 500 }); } - return new Response(null, { status: 500 }); + return new Response(`Error building HTML`, { status: 500 }); default: console.log(`Pathname: ${pathname}`); return new Response(null, { status: 404 }); diff --git a/src/build/wasmPlugin.ts b/build/wasmPlugin.ts similarity index 100% rename from src/build/wasmPlugin.ts rename to build/wasmPlugin.ts diff --git a/package.json b/package.json index 6ae119e..d57b119 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,11 @@ { "name": "tsgames", "module": "index.ts", + "version": "1.0.0", "type": "module", "scripts": { - "start": "bun --hot src/build/server.ts", - "build": "bun src/build/build.ts" + "start": "bun --hot build/server.ts", + "build": "bun build/build.ts" }, "dependencies": { "classnames": "2.5.1", diff --git a/src/common/types.d.ts b/src/common/types.d.ts deleted file mode 100644 index 3b7d897..0000000 --- a/src/common/types.d.ts +++ /dev/null @@ -1,24 +0,0 @@ -declare module "*.png" { - const image: HTMLImageElement; - export default image; -} -declare module "*.jpg" { - const image: HTMLImageElement; - export default image; -} -declare module "*.jpeg" { - const image: HTMLImageElement; - export default image; -} -declare module "*.wav" { - const audio: HTMLAudioElement; - export default audio; -} -declare module "*.mp3" { - const audio: HTMLAudioElement; - export default audio; -} -declare module "*.ogg" { - const audio: HTMLAudioElement; - export default audio; -} \ No newline at end of file diff --git a/src/games/playground/awoo.wasm.ts b/src/games/playground/awoo.wasm.ts index 1845c70..7b6cc4f 100644 --- a/src/games/playground/awoo.wasm.ts +++ b/src/games/playground/awoo.wasm.ts @@ -1,3 +1,7 @@ export function awoo(a: i32, b: i32): void { console.log((a * b).toString()); +} + +export function testTemplate(a: i32, f: f64): string { + return `${a} + ${f} = ${a + f}`; } \ No newline at end of file diff --git a/src/games/playground/index.tsx b/src/games/playground/index.tsx index 76f67b6..06f7225 100644 --- a/src/games/playground/index.tsx +++ b/src/games/playground/index.tsx @@ -1,9 +1,10 @@ -import { awoo } from "./awoo.wasm"; +import { awoo, testTemplate } from "./awoo.wasm"; import { render } from "preact"; import attack from './assets/attack.wav'; export default function main() { awoo(42, 69); + console.log(testTemplate(420, 69)); render(