Skip to content

Commit b9ce88a

Browse files
committed
allow registering commands
1 parent 8455411 commit b9ce88a

File tree

4 files changed

+92
-78
lines changed

4 files changed

+92
-78
lines changed

src/cli.ts

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,3 @@
1-
import { Command } from 'commander'
2-
import { existsSync, mkdirSync } from 'fs'
3-
import { parseAndRemoveWildcardOptions } from '~/utils'
4-
import { StaxConfig } from '~/types'
5-
import { registerCommands } from '~/commands'
6-
import { version } from '../package.json'
7-
import * as path from 'path'
8-
import tmp from 'tmp'
1+
import { runProgram } from './program'
92

10-
function buildArgs() {
11-
let [ args, overrides ] = parseAndRemoveWildcardOptions(process.argv, '--stax.')
12-
const commandSeparator = args.indexOf('--')
13-
14-
if (commandSeparator >= 0) {
15-
const command = args.slice(commandSeparator+1).join(' ')
16-
args = args.slice(0, commandSeparator)
17-
args.push(command)
18-
}
19-
20-
if (args.includes('-v') || args.includes('--version')) {
21-
console.log(version)
22-
process.exit(0)
23-
}
24-
25-
return [ args, overrides ]
26-
}
27-
28-
function runProgram() {
29-
const program = new Command()
30-
const [ args, overrides ] = buildArgs()
31-
32-
registerCommands(program, overrides as unknown as StaxConfig)
33-
program.name('stax')
34-
program.parse(args as string[])
35-
}
36-
37-
function init() {
38-
process.env.STAX_HOME = path.join(process.env.HOME, '.stax')
39-
process.env.STAX_HOST_SERVICES = path.join(process.env.STAX_HOME, 'host-services')
40-
41-
if (!existsSync(process.env.STAX_HOME)) mkdirSync(process.env.STAX_HOME)
42-
if (!existsSync(process.env.STAX_HOST_SERVICES)) mkdirSync(process.env.STAX_HOST_SERVICES)
43-
44-
process.on('SIGINT', () => { tmp.setGracefulCleanup(); process.exit() })
45-
46-
process.on('unhandledRejection', (reason: any, promise: Promise<any>) => {
47-
/*
48-
workaround to fix what seems like an input problem with bun. triggers every time when you press
49-
enter multiple times during a prompt.
50-
51-
TypeError: undefined is not an object
52-
at <anonymous> (native)
53-
at <anonymous> (native)
54-
at <anonymous> (native)
55-
at <anonymous> (native)
56-
at <anonymous> (native)
57-
at handleResult (native:36:41)
58-
at handleNativeReadableStreamPromiseResult2 (native:7:60)
59-
at processTicksAndRejections (native:7:39)
60-
at spawnSync (unknown)
61-
at spawnSync (node:child_process:233:22)
62-
63-
this work around causes some input to get ignored and you'll have to press your
64-
keys multiple times for it to register but it's better than crashing.
65-
*/
66-
if (reason instanceof TypeError && reason.message === 'undefined is not an object' && reason.stack?.includes('handleResult'))
67-
return
68-
69-
if (reason instanceof Error) {
70-
console.error(reason.stack)
71-
process.exit(1)
72-
}
73-
})
74-
75-
tmp.setGracefulCleanup()
76-
}
77-
78-
init()
793
runProgram()

src/commands/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import Stax from '~/stax'
2525

2626
const DEFAULT_CONTEXT_NAME = 'stax'
2727

28-
export function registerCommands(program: Command, overrides: StaxConfig) {
28+
export function registerCommands(program: Command, overrides: StaxConfig, additionalCommands: Function[] = []) {
2929
const stax = new Stax(DEFAULT_CONTEXT_NAME)
3030

3131
registerAliasCommand(program, stax)
@@ -49,4 +49,7 @@ export function registerCommands(program: Command, overrides: StaxConfig) {
4949
registerUpCommand(program, stax)
5050
registerLogoCommand(program)
5151
registerAutoCompleteCommand(program)
52+
53+
if (additionalCommands?.length > 0)
54+
additionalCommands.forEach(command => command(program, stax))
5255
}

src/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import Xaml from './xaml'
2+
import App from './app'
3+
import Container from './container'
4+
import docker from './docker'
5+
import settings from './settings'
6+
import Stax from './stax'
7+
import { runProgram } from './program'
8+
9+
export { Xaml, App, Container, docker, settings, Stax, runProgram }

src/program.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { Command } from 'commander'
2+
import { existsSync, mkdirSync } from 'fs'
3+
import { parseAndRemoveWildcardOptions } from '~/utils'
4+
import { StaxConfig } from '~/types'
5+
import { registerCommands } from '~/commands'
6+
import { version } from '../package.json'
7+
import * as path from 'path'
8+
import tmp from 'tmp'
9+
10+
function buildArgs() {
11+
let [ args, overrides ] = parseAndRemoveWildcardOptions(process.argv, '--stax.')
12+
const commandSeparator = args.indexOf('--')
13+
14+
if (commandSeparator >= 0) {
15+
const command = args.slice(commandSeparator+1).join(' ')
16+
args = args.slice(0, commandSeparator)
17+
args.push(command)
18+
}
19+
20+
if (args.includes('-v') || args.includes('--version')) {
21+
console.log(version)
22+
process.exit(0)
23+
}
24+
25+
return [ args, overrides ]
26+
}
27+
28+
function init() {
29+
process.env.STAX_HOME = path.join(process.env.HOME, '.stax')
30+
process.env.STAX_HOST_SERVICES = path.join(process.env.STAX_HOME, 'host-services')
31+
32+
if (!existsSync(process.env.STAX_HOME)) mkdirSync(process.env.STAX_HOME)
33+
if (!existsSync(process.env.STAX_HOST_SERVICES)) mkdirSync(process.env.STAX_HOST_SERVICES)
34+
35+
process.on('SIGINT', () => { tmp.setGracefulCleanup(); process.exit() })
36+
37+
process.on('unhandledRejection', (reason: any, promise: Promise<any>) => {
38+
/*
39+
workaround to fix what seems like an input problem with bun. triggers every time when you press
40+
enter multiple times during a prompt.
41+
42+
TypeError: undefined is not an object
43+
at <anonymous> (native)
44+
at <anonymous> (native)
45+
at <anonymous> (native)
46+
at <anonymous> (native)
47+
at <anonymous> (native)
48+
at handleResult (native:36:41)
49+
at handleNativeReadableStreamPromiseResult2 (native:7:60)
50+
at processTicksAndRejections (native:7:39)
51+
at spawnSync (unknown)
52+
at spawnSync (node:child_process:233:22)
53+
54+
this work around causes some input to get ignored and you'll have to press your
55+
keys multiple times for it to register but it's better than crashing.
56+
*/
57+
if (reason instanceof TypeError && reason.message === 'undefined is not an object' && reason.stack?.includes('handleResult'))
58+
return
59+
60+
if (reason instanceof Error) {
61+
console.error(reason.stack)
62+
process.exit(1)
63+
}
64+
})
65+
66+
tmp.setGracefulCleanup()
67+
}
68+
69+
export function runProgram(additionalCommands: Function[] = []) {
70+
const program = new Command()
71+
const [ args, overrides ] = buildArgs()
72+
73+
init()
74+
registerCommands(program, overrides as unknown as StaxConfig, additionalCommands)
75+
program.name('stax')
76+
program.parse(args as string[])
77+
}
78+

0 commit comments

Comments
 (0)