diff --git a/src/cli.ts b/src/cli.ts index 09daa43..34a8e67 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,6 +1,9 @@ import { program } from 'commander'; import buildAction from './util/buildAction'; import printWelcome from './util/print/printWelcome'; +import packageJson from '../package.json'; + +const typedPackageJson = packageJson; export default function runCli() { program @@ -62,6 +65,10 @@ export default function runCli() { ) .action(buildAction(import('./commands/notifications'))); + program.version( + `The current version of Belt is: ${typedPackageJson.version}`, + ); + printWelcome(); program.parse(); } diff --git a/src/util/print/__tests__/printWelcome.test.ts b/src/util/print/__tests__/printWelcome.test.ts index 55b7f19..612203e 100644 --- a/src/util/print/__tests__/printWelcome.test.ts +++ b/src/util/print/__tests__/printWelcome.test.ts @@ -1,8 +1,30 @@ -import { test, vi } from 'vitest'; +import { test, vi, describe, expect, beforeEach } from 'vitest'; +import chalk from 'chalk'; import printWelcome from '../printWelcome'; -vi.mock('../../print', () => ({ default: vi.fn() })); +const consoleSpy = vi.spyOn(console, 'log'); -test('doesnt error', () => { - printWelcome(); +describe('printWelcome', () => { + beforeEach(() => { + consoleSpy.mockClear(); + }); + + test('prints introduction message', () => { + process.argv = ['node', 'script.js', 'create']; + printWelcome(); + expect(console.log).toHaveBeenCalledWith(chalk.bold('\n\n\tšŸ‘– Belt šŸ‘–\n')); + }); + + describe('does not print introduction message for version', () => { + test('works for --version', () => { + process.argv = ['node', 'script.js', '--version']; + printWelcome(); + expect(consoleSpy.mock.calls.length).toBe(0); + }); + test('works for -V', () => { + process.argv = ['node', 'script.js', '-V']; + printWelcome(); + expect(consoleSpy.mock.calls.length).toBe(0); + }); + }); }); diff --git a/src/util/print/printWelcome.ts b/src/util/print/printWelcome.ts index b1156de..367c766 100644 --- a/src/util/print/printWelcome.ts +++ b/src/util/print/printWelcome.ts @@ -2,6 +2,11 @@ import chalk from 'chalk'; import print from '../print'; export default function printWelcome() { + const showVersionCommand = + process.argv.includes('--version') || process.argv.includes('-V'); + if (showVersionCommand) { + return; + } print(chalk.bold('\n\n\tšŸ‘– Belt šŸ‘–\n')); print( 'Perform project setup and redundant tasks\n without your pants falling down!\n\n', diff --git a/tsconfig.json b/tsconfig.json index c32294a..6a75fd1 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,7 +10,8 @@ "outDir": "./build", "skipLibCheck": true, "types": ["node"], - "typeRoots": ["./src/types", "./node_modules/@types"] + "typeRoots": ["./src/types", "./node_modules/@types"], + "resolveJsonModule": true }, "ts-node": { "esm": true,