diff --git a/package.json b/package.json index bccdecc3..24656ced 100644 --- a/package.json +++ b/package.json @@ -87,9 +87,12 @@ "@oclif/plugin-update" ], "hooks": { - "init": "./dist/hooks/npm-update-notifier.js", - "prerun": "./dist/hooks/prerun.js", - "finally": "./dist/hooks/finally.js" + "init": [ + "./dist/hooks/init/00_npm-update-notifier.js", + "./dist/hooks/init/01_initialize_amplitude.js" + ], + "prerun": "./dist/hooks/prerun/prerun.js", + "finally": "./dist/hooks/finally/finally.js" }, "topicSeparator": " ", "macos": { diff --git a/src/commands/scan/eol.ts b/src/commands/scan/eol.ts index 46e3b827..6a6c122d 100644 --- a/src/commands/scan/eol.ts +++ b/src/commands/scan/eol.ts @@ -52,6 +52,7 @@ export default class ScanEol extends Command { default: false, description: `Save the generated SBOM as ${filenamePrefix}.sbom.json in the scanned directory`, }), + version: Flags.version(), }; public async run(): Promise { diff --git a/src/hooks/finally.ts b/src/hooks/finally.ts deleted file mode 100644 index 546c8f9a..00000000 --- a/src/hooks/finally.ts +++ /dev/null @@ -1,19 +0,0 @@ -import type { Hook } from '@oclif/core'; -import ora from 'ora'; -import { track } from '../service/analytics.svc.ts'; - -const hook: Hook<'finally'> = async (opts) => { - const spinner = ora().start('Cleaning up'); - const event = track('CLI Session Ended', (context) => ({ - cli_version: context.cli_version, - ended_at: new Date(), - })).promise; - - if (!opts.argv.includes('--help')) { - await event; - } - - spinner.stop(); -}; - -export default hook; diff --git a/src/hooks/finally/finally.ts b/src/hooks/finally/finally.ts new file mode 100644 index 00000000..c769ac6a --- /dev/null +++ b/src/hooks/finally/finally.ts @@ -0,0 +1,25 @@ +import type { Hook } from '@oclif/core'; +import ora, { type Ora } from 'ora'; +import { track } from '../../service/analytics.svc.ts'; + +const hook: Hook<'finally'> = async (opts) => { + const isHelpOrVersionCmd = opts.argv.includes('--help') || opts.argv.includes('--version'); + + let spinner: Ora | undefined; + + if (!isHelpOrVersionCmd) { + spinner = ora().start('Cleaning up'); + } + + const event = track('CLI Session Ended', (context) => ({ + cli_version: context.cli_version, + ended_at: new Date(), + })).promise; + + if (!isHelpOrVersionCmd) { + await event; + spinner?.stop(); + } +}; + +export default hook; diff --git a/src/hooks/npm-update-notifier.ts b/src/hooks/init/00_npm-update-notifier.ts similarity index 94% rename from src/hooks/npm-update-notifier.ts rename to src/hooks/init/00_npm-update-notifier.ts index a57cce08..e24442f1 100644 --- a/src/hooks/npm-update-notifier.ts +++ b/src/hooks/init/00_npm-update-notifier.ts @@ -1,7 +1,7 @@ import type { Hook } from '@oclif/core'; import updateNotifier, { type UpdateInfo } from 'update-notifier'; -import pkg from '../../package.json' with { type: 'json' }; -import { debugLogger } from '../service/log.svc.ts'; +import pkg from '../../../package.json' with { type: 'json' }; +import { debugLogger } from '../../service/log.svc.ts'; const updateNotifierHook: Hook.Init = async (options) => { debugLogger('pkg.version', pkg.version); diff --git a/src/hooks/prerun.ts b/src/hooks/init/01_initialize_amplitude.ts similarity index 65% rename from src/hooks/prerun.ts rename to src/hooks/init/01_initialize_amplitude.ts index 50b9c72b..866e0c56 100644 --- a/src/hooks/prerun.ts +++ b/src/hooks/init/01_initialize_amplitude.ts @@ -1,9 +1,8 @@ import { parseArgs } from 'node:util'; import type { Hook } from '@oclif/core'; -import debug from 'debug'; -import { initializeAnalytics, track } from '../service/analytics.svc.ts'; +import { initializeAnalytics, track } from '../../service/analytics.svc.ts'; -const hook: Hook<'prerun'> = async (opts) => { +const hook: Hook.Init = async () => { const args = parseArgs({ allowPositionals: true, strict: false }); initializeAnalytics(); track('CLI Command Submitted', (context) => ({ @@ -14,11 +13,6 @@ const hook: Hook<'prerun'> = async (opts) => { cli_version: context.cli_version, started_at: context.started_at, })); - - // If JSON flag is enabled, silence debug logging - if (opts.Command.prototype.jsonEnabled()) { - debug.disable(); - } }; export default hook; diff --git a/src/hooks/prerun/prerun.ts b/src/hooks/prerun/prerun.ts new file mode 100644 index 00000000..f6d90b2a --- /dev/null +++ b/src/hooks/prerun/prerun.ts @@ -0,0 +1,11 @@ +import type { Hook } from '@oclif/core'; +import debug from 'debug'; + +const hook: Hook<'prerun'> = async (opts) => { + // If JSON flag is enabled, silence debug logging + if (opts.Command.prototype.jsonEnabled()) { + debug.disable(); + } +}; + +export default hook; diff --git a/test/hooks/npm-update-notifier.test.ts b/test/hooks/npm-update-notifier.test.ts index 82acd0cf..c26fa044 100644 --- a/test/hooks/npm-update-notifier.test.ts +++ b/test/hooks/npm-update-notifier.test.ts @@ -1,6 +1,6 @@ import assert from 'node:assert'; import { describe, it } from 'node:test'; -import { getDistTag, handleUpdate } from '../../src/hooks/npm-update-notifier.ts'; +import { getDistTag, handleUpdate } from '../../src/hooks/init/00_npm-update-notifier'; describe('getDistTag', () => { it('should return beta for beta versions', () => {