diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..243833a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,15 @@ +node_modules/ +dist/ +output/ +.vscode +.dockerignore +Dockerfile +*.zip +*.rar +*.tgz +*.txt +*.json + +!package.json +!package-lock.json +!tsconfig.json diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..9d30af8 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,32 @@ +FROM node:20.15.0-alpine AS base +RUN mkdir -p /opt/app +WORKDIR /opt/app +RUN addgroup -S user && adduser -S user -G user +RUN chown -R user:user /opt/app +COPY package*.json ./ + +# BUILD TARGET +FROM base AS build +RUN npm install +COPY . ./ +RUN npm run transpile + +# DEVELOPMENT TARGET PROFILE +FROM base AS development +RUN npm install +COPY . ./ +RUN mkdir /opt/app/node_modules/.vite \ + && chown -R user:user /opt/app/node_modules/.vite +USER user +EXPOSE 9229 +CMD ["sh"] + +# PRODUCTION TARGET PROFILE +FROM base AS production +ENV NODE_ENV=production +COPY --from=build /opt/app/dist /opt/app/dist +COPY package*.json ./ +RUN npm install && npm prune --production +USER user +EXPOSE 9229 +CMD ["sh"] diff --git a/README.md b/README.md index 20e8563..4ebec11 100644 --- a/README.md +++ b/README.md @@ -23,6 +23,8 @@ Solutions for [Advent of Code](https://adventofcode.com/) event puzzles. ## Usage +Using Node + 1. Run a TypeScript file inside the **/src** directory. For example: ``` @@ -35,12 +37,44 @@ Solutions for [Advent of Code](https://adventofcode.com/) event puzzles. ``` 3. See the [Available Scripts](#available-scripts) section for more information. +## Alternate Usage + +Using Docker + +- Build the image + ``` + docker compose -f docker-compose.dev.yml build + ``` + +- Transpile the TypeScript files to JavaScript (PowerShell) + ``` + docker run -it -v ${pwd}:/opt/app -v /opt/app/node_modules --rm weaponsforge/adventofcode:dev npm run transpile + ``` + +- Run tests (PowerShell) + ``` + docker run -it -v ${pwd}:/opt/app -v /opt/app/node_modules --rm weaponsforge/adventofcode:dev npm test + ``` + +- Watch TS file updates: Use available scripts - `npm run watch`, `npm run watch:docker:win` + ``` + docker run -it -v ${pwd}:/opt/app -v /opt/app/node_modules --rm weaponsforge/adventofcode:dev + ``` + ## Available Scripts ### `npm run dev` Runs `vitest` in watch mode, watching file changes and errors to files linked with `*.test.ts` files. +### `npm run watch` + +Watches file changes in `.ts` files using the `tsc --watch` option. + +### `npm run watch:docker:win` + +Watches file changes in `.ts` files using the `tsc --watch` option with `dynamicPriorityPolling` in Docker containers running in Windows WSL2. + ### `npm run dev:debug` Runs the sample TS script. diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml new file mode 100644 index 0000000..42cdfd6 --- /dev/null +++ b/docker-compose.dev.yml @@ -0,0 +1,15 @@ +services: + weaponsforge.adventofcode-dev: + container_name: weaponsforge-adventofcode-dev + image: weaponsforge/adventofcode:dev + build: + context: . + dockerfile: Dockerfile + target: development + volumes: + - .:/opt/app + - /opt/app/node_modules + ports: + - "9229:9229" + stdin_open: true + tty: true diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml new file mode 100644 index 0000000..d3ed5a9 --- /dev/null +++ b/docker-compose.prod.yml @@ -0,0 +1,15 @@ +services: + weaponsforge.adventofcode: + container_name: weaponsforge-adventofcode + image: weaponsforge/adventofcode:latest + build: + context: . + dockerfile: Dockerfile + target: production + volumes: + - ./output:/opt/app/output + - /opt/app/node_modules + ports: + - "9229:9229" + stdin_open: true + tty: true diff --git a/package.json b/package.json index 7cf8b00..90b78d4 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,8 @@ "dev": "vitest", "dev:debug": "vite-node src/sample/sample.ts", "transpile": "tsc -p tsconfig.json && tsc-alias", + "watch": "tsc -p tsconfig.json --watch", + "watch:docker:win": "tsc -p tsconfig.json --watch --watchFile dynamicPriorityPolling --watchDirectory dynamicPriorityPolling", "lint": "eslint \"src/**/*.ts\" *.mjs *.ts", "lint:fix": "eslint \"src/**/*.ts\" *.mjs *.ts --fix", "test": "vitest run" diff --git a/tsconfig.json b/tsconfig.json index e059dab..4e8a5a1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -38,6 +38,10 @@ ] } }, + "watchOptions": { + // "watchFile": "dynamicPriorityPolling" + "excludeDirectories": ["**/node_modules", "dist"] + }, "include": ["src/**/*.ts"], "exclude": ["node_modules"] }