From cccd62b8ad94a7122963ee00ac4ff048abb79c7a Mon Sep 17 00:00:00 2001 From: Thibaud Date: Sat, 21 Mar 2026 10:03:59 +0100 Subject: [PATCH] fix: improve npm scripts and fix localhost detection --- .github/workflows/main.yml | 5 +---- .gitignore | 3 ++- README.md | 18 +++++++----------- client.ts | 7 +++---- common.ts | 2 -- package.json | 7 +++++-- serve.py | 8 ++------ tsconfig.json | 6 ++++-- 8 files changed, 24 insertions(+), 32 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 443436e..28191c7 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -15,10 +15,7 @@ jobs: - name: Install and Build run: | npm ci - npx tsc - mkdir dist - mv *.js dist/ - mv *.html dist/ + npm run build - name: Setup Pages id: pages uses: actions/configure-pages@v5 diff --git a/.gitignore b/.gitignore index f5f2ba6..08dc7b0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ -*.js +dist/ + diff --git a/README.md b/README.md index eb191dc..3fc1cfc 100644 --- a/README.md +++ b/README.md @@ -1,21 +1,17 @@ # Tic tac toe websockets -Two players tic-tac-joe implemented with an HTML canvas and websockets +Two players tic-tac-joe implemented with an HTML canvas and websockets -![](screenshot.png) +![Screenshot](screenshot.png) ## How to run -``` -// compilation of .mts files in watch mode -$ tsc -w - +```shell +$ npm run build:watch // serve static files (localhost:8000) -$ python serve.py - +$ npm run serve // run server -$ node server.mjs +$ npm run start:watch ``` -Go to http://localhost:8000 - +Go to [](http://localhost:8000) diff --git a/client.ts b/client.ts index f7d919b..8997be5 100644 --- a/client.ts +++ b/client.ts @@ -4,10 +4,9 @@ const ANIMATE_DURATION = 500; // ms const GRID_PADDING = 10; const MESSAGE_PADDING = 20; -let address = "ws://localhost:1234"; -if (window.location.hostname !== "localhost") { - address = "wss://tic-tac-toe-ws-production.up.railway.app"; -} +const address = ["127.0.0.1", "localhost"].includes(window.location.hostname) + ? "ws://localhost:1234" + : "wss://tic-tac-toe-ws-production.up.railway.app"; const ws = new WebSocket(address); interface Point { diff --git a/common.ts b/common.ts index 5688f47..225337b 100644 --- a/common.ts +++ b/common.ts @@ -1,5 +1,3 @@ -export type MessageKind = "click" | "hello" | "update" | "endgame" | "reset" | "spectate"; - export type Message = | { kind: "click", data: Click } | { kind: "hello", data: Hello } diff --git a/package.json b/package.json index 1c65dae..583d5b6 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,11 @@ "main": "index.js", "type": "module", "scripts": { - "build": "tsc", - "start": "node server.js" + "build": "tsc && cp index.html dist/", + "build:watch": "tsc --watch && cp index.html dist/", + "start": "node dist/server.js", + "start:watch": "node --watch dist/server.js", + "serve": "python serve.py" }, "author": "", "license": "ISC", diff --git a/serve.py b/serve.py index cfbe93e..b42d784 100644 --- a/serve.py +++ b/serve.py @@ -1,11 +1,7 @@ import http.server +from functools import partial -HandlerClass = http.server.SimpleHTTPRequestHandler - -# Patch in the correct extensions -HandlerClass.extensions_map['.js'] = 'text/javascript' -HandlerClass.extensions_map['.mjs'] = 'text/javascript' - +HandlerClass = partial(http.server.SimpleHTTPRequestHandler, directory="dist/") # Run the server (like `python -m http.server` does) http.server.test(HandlerClass, port=8000) diff --git a/tsconfig.json b/tsconfig.json index 0f760cc..f586201 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -55,7 +55,7 @@ // "sourceMap": true, /* Create source map files for emitted JavaScript files. */ // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */ // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */ - // "outDir": "./", /* Specify an output folder for all emitted files. */ + "outDir": "./dist", /* Specify an output folder for all emitted files. */ // "removeComments": true, /* Disable emitting comments. */ // "noEmit": true, /* Disable emitting files from a compilation. */ // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */ @@ -104,5 +104,7 @@ /* Completeness */ // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */ "skipLibCheck": true /* Skip type checking all .d.ts files. */ - } + }, + "include": ["./**/*.ts"], + "exclude": ["node_modules", "dist"] }