diff --git a/src/base.command.ts b/src/base.command.ts index 64cb898..b04245b 100644 --- a/src/base.command.ts +++ b/src/base.command.ts @@ -45,6 +45,8 @@ export abstract class BaseCommand extends Command { public async init(): Promise { await super.init() + this.initSigIntHandler() + const { args, flags } = await this.parse({ flags: this.ctor.flags, baseFlags: (super.ctor as typeof BaseCommand).baseFlags, @@ -111,4 +113,30 @@ export abstract class BaseCommand 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) + }, + ) + } + }) + } } diff --git a/src/commands/config/set.ts b/src/commands/config/set.ts index fb18103..aeee9dd 100644 --- a/src/commands/config/set.ts +++ b/src/commands/config/set.ts @@ -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 { static description = 'Set config' static examples = ['<%= config.bin %> <%= command.id %>'] diff --git a/src/utils/analytics/metric.ts b/src/utils/analytics/metric.ts index 88c6375..5391923 100644 --- a/src/utils/analytics/metric.ts +++ b/src/utils/analytics/metric.ts @@ -2,4 +2,5 @@ export enum Metric { CommandRun = 'command_run', CommandComplete = 'command_complete', CommandError = 'command_error', + CommandSigInt = 'command_sigint', }