Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/ignitor/ace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ export class AceProcess {

await this.#configureCallback(app)

/**
* Register terminating callback BEFORE handling the command.
* This ensures the callback is registered even if a staysAlive
* command calls app.terminate() during its execution.
*/
app.terminating(() => {
const mainCommand = kernel.getMainCommand()
if (mainCommand?.staysAlive) {
process.exitCode = mainCommand.exitCode
}
})

/**
* Handle command line args
*/
Expand All @@ -106,10 +118,6 @@ export class AceProcess {
if (!mainCommand || !mainCommand.staysAlive) {
process.exitCode = kernel.exitCode
await app.terminate()
} else {
app.terminating(() => {
process.exitCode = mainCommand.exitCode
})
}
}
}
42 changes: 42 additions & 0 deletions tests/ignitor/ace_process.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,48 @@ test.group('Ignitor | Ace process', (group) => {
assert.equal(ignitor.getApp()?.getState(), 'terminated')
})

test('set process.exitCode when staysAlive command calls app.terminate() during execution', async ({
cleanup,
assert,
}) => {
cleanup(async () => {
process.exitCode = undefined
await ignitor.terminate()
})

const ignitor = new IgnitorFactory()
.merge({
rcFileContents: {
providers: [() => import('../../providers/app_provider.js')],
},
})
.withCoreConfig()
.create(BASE_URL)

class Greet extends BaseCommand {
static commandName: string = 'greet'
static options = {
staysAlive: true,
}

async run() {
this.exitCode = 1
await this.app.terminate()
}
}

await ignitor
.ace()
.configure(async (app) => {
const kernel = await app.container.make('ace')
kernel.addLoader(new ListLoader([Greet]))
})
.handle(['greet'])

assert.equal(process.exitCode, 1)
assert.equal(ignitor.getApp()?.getState(), 'terminated')
})

test('switch app environment to repl when running repl command', async ({ cleanup, assert }) => {
cleanup(async () => {
await ignitor.terminate()
Expand Down
Loading