Skip to content

Commit

Permalink
feat(cli): create command line runner for arx.exe that uses settings …
Browse files Browse the repository at this point in the history
…from .env
  • Loading branch information
meszaros-lajos-gyorgy committed Sep 1, 2023
1 parent 006677e commit 0c291cf
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
4 changes: 4 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
20 changes: 20 additions & 0 deletions src/bin/cli.ts
Original file line number Diff line number Diff line change
@@ -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"')
}
42 changes: 42 additions & 0 deletions src/bin/rungame.ts
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 0c291cf

Please sign in to comment.