From 0c291cfed419787625943c857e74f0183892a2c8 Mon Sep 17 00:00:00 2001 From: Lajos Meszaros Date: Fri, 1 Sep 2023 18:52:53 +0200 Subject: [PATCH] feat(cli): create command line runner for arx.exe that uses settings from .env --- package-lock.json | 4 ++++ package.json | 4 ++++ src/bin/cli.ts | 20 ++++++++++++++++++++ src/bin/rungame.ts | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 src/bin/cli.ts create mode 100644 src/bin/rungame.ts diff --git a/package-lock.json b/package-lock.json index 037f43d0..f43f0b1d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,12 +13,16 @@ "arx-header-size": "^2.2.0", "color-rgba": "^2.4.0", "dotenv": "^16.3.1", + "minimist-lite": "^2.2.1", "node-pkware": "^3.0.4", "seedrandom": "^3.0.5", "sharp": "^0.32.5", "sharp-bmp": "^0.1.5", "three": "^0.155.0" }, + "bin": { + "arx-level-generator": "dist/bin/cli.js" + }, "devDependencies": { "@trivago/prettier-plugin-sort-imports": "^4.2.0", "@types/color-rgba": "^2.1.0", diff --git a/package.json b/package.json index 08a45a6e..2204aba1 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,9 @@ "npm": ">=8.0.0", "node": ">=18.0.0" }, + "bin": { + "arx-level-generator": "dist/src/bin/cli.js" + }, "exports": { ".": { "default": "./dist/src/index.js", @@ -86,6 +89,7 @@ "arx-header-size": "^2.2.0", "color-rgba": "^2.4.0", "dotenv": "^16.3.1", + "minimist-lite": "^2.2.1", "node-pkware": "^3.0.4", "seedrandom": "^3.0.5", "sharp": "^0.32.5", diff --git a/src/bin/cli.ts b/src/bin/cli.ts new file mode 100644 index 00000000..2eee6f1e --- /dev/null +++ b/src/bin/cli.ts @@ -0,0 +1,20 @@ +#!/usr/bin/env -S node --enable-source-maps +import minimist from 'minimist-lite' +import { Settings } from '@src/Settings.js' +import { rungame } from './rungame.js' + +type AppArgs = { + rungame: boolean +} + +const args: AppArgs = minimist(process.argv.slice(2), { + boolean: ['rungame'], +}) + +const settings = new Settings() + +if (args.rungame) { + await rungame(settings) +} else { + console.info('[info] cli: available commands: "--rungame"') +} diff --git a/src/bin/rungame.ts b/src/bin/rungame.ts new file mode 100644 index 00000000..a7737e43 --- /dev/null +++ b/src/bin/rungame.ts @@ -0,0 +1,42 @@ +import { exec } from 'node:child_process' +import os from 'node:os' +import path from 'node:path' +import { promisify } from 'node:util' +import { Settings } from '@src/Settings.js' +import { fileExists } from '@src/helpers.js' + +export const rungame = async (settings: Settings) => { + const operatingSystem = os.platform() + + if (operatingSystem !== 'win32' && operatingSystem !== 'linux') { + console.error(`[error] rungame: unsupported platform (expected "win32" or "linux", but got "${operatingSystem}")`) + return + } + + const args = [`--loadlevel ${settings.levelIdx}`] + + let exeFile: string + switch (operatingSystem) { + case 'win32': + exeFile = path.resolve(settings.outputDir, 'arx.exe') + break + case 'linux': + exeFile = path.resolve(settings.outputDir, 'arx') + break + } + + if (!(await fileExists(exeFile))) { + console.error(`[error] rungame: executable not found at "${exeFile}"`) + return + } + + try { + const { stdout, stderr } = await promisify(exec)(`${exeFile} ${args.join(' ')}`) + console.log(stdout) + if (stderr !== '') { + console.error(stderr) + } + } catch (e: unknown) { + console.error(e) + } +}