diff --git a/README.md b/README.md index 7876f21..0a25743 100644 --- a/README.md +++ b/README.md @@ -10,26 +10,14 @@ pnpm install ``` -### Boot the Infrastructure (Redis Server) - -```bash -pnpm run dev:infra -``` - ### Run the Program ```bash pnpm run dev ``` -### Cleanup Docker Infrastructure - -```bash -pnpm run dev:infra:stop -``` - -### To see if you have a Docker container currently running +### To see if you have any containers currently running ```bash -docker ps +docker compose ps ``` diff --git a/docker-compose.yml b/docker-compose.yml index fe30a4a..46607fb 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -8,5 +8,16 @@ services: command: redis-server --appendonly yes restart: unless-stopped + mongodb: + image: mongodb/mongodb-community-server:6.0-ubi8 + ports: + - "27017:27017" + environment: + - MONGO_INITDB_ROOT_USERNAME=devuser + - MONGO_INITDB_ROOT_PASSWORDdevpassword123= + volumes: + - mongodb_data:/data/db + volumes: redis_data: + mongodb_data: diff --git a/package.json b/package.json index cd97897..0c97886 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "husky": "^8.0.0", "typescript": "^5.0.2" }, + "type": "module", "scripts": { "lint": "eslint . --ext .ts,.vue", "lint:fix": "eslint . --fix --ext .ts,.vue", @@ -23,9 +24,7 @@ "prettier:fix": "prettier . --write", "format": "pnpm prettier:fix && pnpm lint:fix", "prepare": "husky install", - "dev": "pnpm --stream -r dev", - "dev:infra": "docker-compose up -d redis", - "dev:infra:stop": "docker-compose down redis" + "dev": "node ./scripts/wrapper.js ./scripts/boot.py" }, "dependencies": { "@element-plus/icons-vue": "^2.3.1", diff --git a/scripts/boot.py b/scripts/boot.py new file mode 100755 index 0000000..bdabcd8 --- /dev/null +++ b/scripts/boot.py @@ -0,0 +1,35 @@ +import subprocess +import sys + +def cleanup() -> None: + """ + Shuts down any docker infrastructure that is currently running. Does not propagate any errors, only error messages if there are issues shutting down the infrastructure. + """ + + print("Shutting down docker infrastructure...") + try: + subprocess.run(["docker", "compose", "down"], check=True) + print("Docker infrastructure stopped successfully!") + except subprocess.CalledProcessError: + print("Error shutting down docker infrastructure!") + raise subprocess.CalledProcessError() + +def start_infrastructure() -> None: + """ + Starts the Docker infrastructure such that both redis and mongodb containers are launched. Does not propagate any errors, only prints an error message and than exits with an error code. + """ + + print("Booting up redis and mongodb...") + try: + subprocess.run(["docker", "compose", "up", "-d"], check=True) + except subprocess.CalledProcessError: + print("Error starting docker infrastructure!") + raise subprocess.CalledProcessError() + +start_infrastructure() +try: + subprocess.run([sys.executable, "scripts/start.py"], check=True) +except (KeyboardInterrupt, subprocess.CalledProcessError): + pass +finally: + cleanup() \ No newline at end of file diff --git a/scripts/start.py b/scripts/start.py new file mode 100755 index 0000000..379d841 --- /dev/null +++ b/scripts/start.py @@ -0,0 +1,9 @@ +import subprocess + +def init() -> None: + try: + subprocess.run(["pnpm", "--stream", "-r", "dev"]) + except subprocess.CalledProcessError: + print("Error starting application!") + +init() \ No newline at end of file diff --git a/scripts/wrapper.js b/scripts/wrapper.js new file mode 100755 index 0000000..45bd8df --- /dev/null +++ b/scripts/wrapper.js @@ -0,0 +1,35 @@ +#!/usr/bin/env node + +import { spawn } from "child_process"; +import { platform } from "process"; + +const pythonCmd = platform === "win32" ? "python" : "python3"; +const scriptPath = process.argv[2]; + +if (!scriptPath) { + console.error("Usage: node run-python.js "); + process.exit(1); +} + +console.log(`Running ${scriptPath} with ${pythonCmd}...`); + +const pythonProcess = spawn(pythonCmd, [scriptPath], { + stdio: "inherit", +}); + +pythonProcess.on("error", (error) => { + console.error(`Failed to start Python: ${error.message}`); + process.exit(1); +}); + +pythonProcess.on("close", (code) => { + process.exit(code || 0); +}); + +process.on("SIGINT", () => { + pythonProcess.kill("SIGINT"); +}); + +process.on("SIGTERM", () => { + pythonProcess.kill("SIGTERM"); +});