CoE CLI development adding a new command

To add a new sample command, you can use the following command to template the initial set-up of the TypeScript command and the Jest JavaScript testing framework unit test.

cd coe-cli
coe cli add -n sample

Connecting the command to the command line

Once you have the unit test completed for your new command, perform these tasks:

  1. Review https://www.npmjs.com/package/commander on commands, options.

  2. Update commands.ts to include a new command or sub command.

    • Import your files at the top of the file.
    import { SampleArguments, SampleCommand} from './sample';
    
    • Add a function for mock injection.
        createSampleCommand: () => SampleCommand
    
    • Create the command in the constructor function.
           this.createSampleCommand = () => new SampleCommand
    
    • Add the function.
        AddSampleCommand(program: commander.Command) {
            var run = program.command('sample')
                .description('A new sample command')
                .option('-c, --comment <comment>', 'The comment for the command')
                .action(async (options: any) : Promise<void> => {
                    let args = new SampleArguments();
                    args.comment = options.comment;
                    let command = this.createSampleCommand();
                    await command.execute(args)
                });
        }
    
    • Register the new command to init function.
            this.AddSampleCommand(program);
    
  3. Update commands.spec.ts to include the unit tests.

    • Include a reference to the command.
    import { SampleCommand } from '../../src/commands/sample'
    
    • Add a set of Jest tests.
    describe('Sample', () => {
        test('Execute', async () => {
            // Arrange
            var commands = new CoeCliCommands();
            let mockSampleCommand = mock<SampleCommand>(); 
    
            commands.createSampleCommand = () => { return mockSampleCommand }
    
            mockSampleCommand.execute.mockResolvedValue()
    
            // Act
            await commands.execute(['node', 'commands.spec', 'sample', '-c', 'Some comment'])
    
            // Assert
            expect(mockSampleCommand.execute).toHaveBeenCalled()
        })
    });
    
  4. Run the unit tests with the new changes.

    npm run test