Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@ import * as fs from 'fs';

import printTableFromInp from './src/service';

const program = new Command();
export function runCLI(argv: string[] = process.argv) {
const program = new Command();

program
.option('-i, --input <value>', 'input string')
.option('-s, --stdin', 'read input from stdin')
.option('-t, --tableOptions <value>', 'table options in JSON format')
.parse(process.argv);
program
.option('-i, --input <value>', 'input string')
.option('-s, --stdin', 'read input from stdin')
.option('-t, --tableOptions <value>', 'table options in JSON format')
.parse(argv);

const options = program.opts();
const options = program.opts();

if (options.input) {
console.log('program.input', options.input);
printTableFromInp(options.input, options.tableOptions);
} else if (options.stdin) {
printTableFromInp(fs.readFileSync(0).toString(), options.tableOptions);
} else {
console.log('Error: Cant detect input option');
if (options.input) {
console.log('program.input', options.input);
printTableFromInp(options.input, options.tableOptions);
} else if (options.stdin) {
printTableFromInp(fs.readFileSync(0).toString(), options.tableOptions);
} else {
console.log('Error: Cant detect input option');
}
}

// Run CLI if this file is executed directly
if (require.main === module) {
runCLI();
}
6 changes: 6 additions & 0 deletions jestconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
"collectCoverage": true,
"collectCoverageFrom": [
"src/**/*.ts",
"index.ts",
"!**/*.d.ts",
"!**/node_modules/**"
],
"preset": "ts-jest",
"testMatch": ["**/test/**/*.[jt]s?(x)", "**/?(*.)+(spec|test).[tj]s?(x)"]
}
77 changes: 77 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { Command } from 'commander';
import * as fs from 'fs';
import { runCLI } from '../index';
import printTableFromInp from '../src/service';

// Mock dependencies
jest.mock('fs');
jest.mock('../src/service');
jest.mock('commander', () => {
const mockCommand = {
option: jest.fn().mockReturnThis(),
parse: jest.fn().mockReturnThis(),
opts: jest.fn().mockReturnValue({}),
};
return { Command: jest.fn(() => mockCommand) };
});

describe('CLI', () => {
let mockConsoleLog: jest.SpyInstance;

beforeEach(() => {
// Mock console.log
mockConsoleLog = jest.spyOn(console, 'log').mockImplementation();

// Reset mocks
(fs.readFileSync as jest.Mock).mockReset();
(printTableFromInp as jest.Mock).mockReset();

// Reset Commander mock
const mockCommand = new Command();
(mockCommand.opts as jest.Mock).mockReturnValue({});
});

afterEach(() => {
jest.restoreAllMocks();
});

it('should handle input option', () => {
const input = '[{"id":1,"name":"John"}]';
const mockCommand = new Command();
(mockCommand.opts as jest.Mock).mockReturnValue({ input });

runCLI(['node', 'index.js', '-i', input]);

expect(mockConsoleLog).toHaveBeenCalledWith('program.input', input);
expect(printTableFromInp).toHaveBeenCalledWith(input, undefined);
});

it('should handle input with table options', () => {
const input = '[{"id":1,"name":"John"}]';
const tableOptions = '{"title":"Test"}';
const mockCommand = new Command();
(mockCommand.opts as jest.Mock).mockReturnValue({ input, tableOptions });

runCLI(['node', 'index.js', '-i', input, '-t', tableOptions]);

expect(mockConsoleLog).toHaveBeenCalledWith('program.input', input);
expect(printTableFromInp).toHaveBeenCalledWith(input, tableOptions);
});

it('should handle stdin option', () => {
const input = '[{"id":1,"name":"John"}]';
const mockCommand = new Command();
(mockCommand.opts as jest.Mock).mockReturnValue({ stdin: true });
(fs.readFileSync as jest.Mock).mockReturnValue({ toString: () => input });

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

expect(fs.readFileSync).toHaveBeenCalledWith(0);
expect(printTableFromInp).toHaveBeenCalledWith(input, undefined);
});

it('should show error when no input option is provided', () => {
runCLI(['node', 'index.js']);
expect(mockConsoleLog).toHaveBeenCalledWith('Error: Cant detect input option');
});
});