From 990d3fa79fbb8f000451fb829ce6ca73b1fd13b1 Mon Sep 17 00:00:00 2001 From: Tomi Alu Date: Fri, 25 Oct 2024 15:17:19 +0100 Subject: [PATCH 1/2] feat(command): return package version for version option Co-authored-by: Rakesh Arunachalam --- src/cli.ts | 7 +++++++ src/util/print/printWelcome.ts | 5 +++++ tsconfig.json | 3 ++- 3 files changed, 14 insertions(+), 1 deletion(-) 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/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, From 511df6f7d19ae04ca2ed14de2d6fc34a842df4c3 Mon Sep 17 00:00:00 2001 From: Tomi Alu Date: Fri, 25 Oct 2024 15:40:00 +0100 Subject: [PATCH 2/2] test: assert that function is called with right arguments --- src/util/print/__tests__/printWelcome.test.ts | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) 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); + }); + }); });