Skip to content

Commit

Permalink
Create cli.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
kroese authored Sep 24, 2024
1 parent 2870cd2 commit 3e7a315
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions source/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env tsx
import process from 'node:process'

import arg from 'arg'
import camelcaseKeys from 'camelcase-keys'

import {cliClient} from './modules/cli-client.js'
import Umbreld, {type UmbreldOptions} from './index.js'

// Quick trpc client for testing
if (process.argv.includes('client')) {
const clientIndex = process.argv.indexOf('client')
const query = process.argv[clientIndex + 1]
const args = process.argv.slice(clientIndex + 2)

await cliClient({query, args})
process.exit(0)
}

const showHelp = () =>
console.log(`
Usage
$ umbreld
Options
--help Shows this help message
--data-directory Your Umbrel data directory
--port The port to listen on
--log-level The logging intensity: silent|normal|verbose
--default-app-store-repo The default app store repository
Examples
$ umbreld --data-directory ~/umbrel
`)

const args = camelcaseKeys(
arg({
'--help': Boolean,
'--data-directory': String,
'--port': Number,
'--log-level': String,
'--default-app-store-repo': String,
}),
)

if (args.help) {
showHelp()
process.exit(0)
}

// TODO: Validate these args are valid
const umbreld = new Umbreld(args as UmbreldOptions)

process.once('uncaughtException', async () => {
umbreld.logger.log(`Received signal, shutting down cleanly...`)
await umbreld.stop()
process.exit(0)
})

process.once('SIGINT', () => { throw new Error('SIGINT') })
process.once('SIGTERM', () => { throw new Error('SIGTERM') })

try {
await umbreld.start()
} catch (error) {
console.error(process.env.NODE_ENV === 'production' ? (error as Error).message : error)
process.exit(1)
}

0 comments on commit 3e7a315

Please sign in to comment.