Skip to content

Commit 44f4406

Browse files
authored
feat: Adding version with ctp (#362)
1 parent 93a27ae commit 44f4406

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ ctp -i '[
7070
Usage: ctp [options]
7171
7272
Options:
73-
-i, --input <value> input string
74-
-s, --stdin read input from stdin
73+
-v, --version output the current version
74+
-i, --input <value> input string
75+
-s, --stdin read input from stdin
7576
-t, --tableOptions <value> table options in JSON format
76-
-h, --help display help for command
77+
-h, --help display help for command
7778
```
7879

7980
## License

index.test.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import { runCLI } from './index';
44
import printTableFromInp from './src/service';
55

66
// Mock dependencies
7-
jest.mock('fs');
87
jest.mock('./src/service');
98
jest.mock('commander', () => {
109
const mockCommand = {
1110
option: jest.fn().mockReturnThis(),
11+
version: jest.fn().mockReturnThis(),
1212
parse: jest.fn().mockReturnThis(),
1313
opts: jest.fn().mockReturnValue({}),
1414
};
@@ -23,18 +23,36 @@ describe('CLI', () => {
2323
mockConsoleLog = jest.spyOn(console, 'log').mockImplementation();
2424

2525
// Reset mocks
26-
(fs.readFileSync as jest.Mock).mockReset();
2726
(printTableFromInp as jest.Mock).mockReset();
2827

2928
// Reset Commander mock
3029
const mockCommand = new Command();
3130
(mockCommand.opts as jest.Mock).mockReturnValue({});
31+
(mockCommand.version as jest.Mock).mockReset();
32+
(mockCommand.option as jest.Mock).mockReset();
33+
(mockCommand.parse as jest.Mock).mockReset();
34+
35+
// Set up default mock returns
36+
(mockCommand.version as jest.Mock).mockReturnValue(mockCommand);
37+
(mockCommand.option as jest.Mock).mockReturnValue(mockCommand);
38+
(mockCommand.parse as jest.Mock).mockReturnValue(mockCommand);
3239
});
3340

3441
afterEach(() => {
3542
jest.restoreAllMocks();
3643
});
3744

45+
it('should configure version command with package version', () => {
46+
const mockCommand = new Command();
47+
runCLI(['node', 'index.js']);
48+
49+
expect(mockCommand.version).toHaveBeenCalledWith(
50+
expect.any(String),
51+
'-v, --version',
52+
'output the current version'
53+
);
54+
});
55+
3856
it('should handle input option', () => {
3957
const input = '[{"id":1,"name":"John"}]';
4058
const mockCommand = new Command();
@@ -60,12 +78,17 @@ describe('CLI', () => {
6078
const input = '[{"id":1,"name":"John"}]';
6179
const mockCommand = new Command();
6280
(mockCommand.opts as jest.Mock).mockReturnValue({ stdin: true });
63-
(fs.readFileSync as jest.Mock).mockReturnValue({ toString: () => input });
81+
82+
// Mock fs.readFileSync only for this test
83+
const mockReadFileSync = jest.spyOn(fs, 'readFileSync')
84+
.mockReturnValue(Buffer.from(input));
6485

6586
runCLI(['node', 'index.js', '-s']);
6687

67-
expect(fs.readFileSync).toHaveBeenCalledWith(0);
88+
expect(mockReadFileSync).toHaveBeenCalledWith(0);
6889
expect(printTableFromInp).toHaveBeenCalledWith(input, undefined);
90+
91+
mockReadFileSync.mockRestore();
6992
});
7093

7194
it('should show error when no input option is provided', () => {

index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
#!/usr/bin/env node
22
import { Command } from 'commander';
33
import * as fs from 'fs';
4+
import * as path from 'path';
45

56
import printTableFromInp from './src/service';
67

8+
// Read package.json to get version
9+
const packageJson = JSON.parse(
10+
fs.readFileSync(path.join(__dirname, 'package.json'), 'utf8')
11+
);
12+
713
export function runCLI(argv: string[] = process.argv) {
814
const program = new Command();
915

1016
program
17+
.version(packageJson.version, '-v, --version', 'output the current version')
1118
.option('-i, --input <value>', 'input string')
1219
.option('-s, --stdin', 'read input from stdin')
1320
.option('-t, --tableOptions <value>', 'table options in JSON format')

0 commit comments

Comments
 (0)