Skip to content

Commit

Permalink
feat: add sigint handler (#77)
Browse files Browse the repository at this point in the history
* feat: add sigint handler

* fix: missing BaseCommand usage
  • Loading branch information
rpidanny authored Jul 4, 2024
1 parent 7c75fbf commit 2f45278
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
28 changes: 28 additions & 0 deletions src/base.command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {

public async init(): Promise<void> {
await super.init()
this.initSigIntHandler()

const { args, flags } = await this.parse({
flags: this.ctor.flags,
baseFlags: (super.ctor as typeof BaseCommand).baseFlags,
Expand Down Expand Up @@ -111,4 +113,30 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
})
}
}

private initSigIntHandler(): void {
process.on('SIGINT', async () => {
const { customData } = this.config

if (customData) {
const { metadata, mixpanel, startTime } = customData
const now = new Date()

const endTime = performance.now()
const durationMs = endTime - (startTime || 0)

mixpanel.track(
Metric.CommandSigInt,
{
...metadata,
duration: prettyMilliseconds(durationMs),
endTime: moment(now).format('YYYY-MM-DD HH:mm:ss'),
},
() => {
process.exit(0)
},
)
}
})
}
}
4 changes: 2 additions & 2 deletions src/commands/config/set.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Command } from '@oclif/core'
import fs from 'fs-extra'
import path from 'path'

import { BaseCommand } from '../../base.command.js'
import { CONFIG_FILE_NAME } from '../../config/constants.js'
import { ConfigSchema, TConfig } from '../../config/schema.js'
import uiInput from '../../utils/ui/input.js'

export default class SetConfig extends Command {
export default class SetConfig extends BaseCommand<typeof SetConfig> {
static description = 'Set config'

static examples = ['<%= config.bin %> <%= command.id %>']
Expand Down
1 change: 1 addition & 0 deletions src/utils/analytics/metric.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export enum Metric {
CommandRun = 'command_run',
CommandComplete = 'command_complete',
CommandError = 'command_error',
CommandSigInt = 'command_sigint',
}

0 comments on commit 2f45278

Please sign in to comment.