CoE CLI 開發新增命令 (已取代)

注意

CoE CLI 已被取代,並將於未來版本中移除。 使用 Power Platform 專案安裝精靈來安裝和管理您的 ALM Accelerator for Power Platform 專案。

若要新增範例命令,您可以使用以下命令來範本化 TypeScript 命令和 Jest JavaScript 測試框架單元測試的初始設定。

cd coe-cli
coe cli add -n sample

將命令連接至命令列

當您完成新命令的單元測試後,請執行下列工作:

  1. 查看 https://www.npmjs.com/package/commander 命令、選項。

  2. 更新 commands.ts 以包括新的命令或子命令。

    • 將檔案頂端匯入檔案。
    import { SampleArguments, SampleCommand} from './sample';
    
    • 新增模擬注入的函數。
        createSampleCommand: () => SampleCommand
    
    • 在建構函式中建立命令。
           this.createSampleCommand = () => new SampleCommand
    
    • 新增函數。
        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)
                });
        }
    
    • 將新命令註冊到 init 函數。
            this.AddSampleCommand(program);
    
  3. 更新 commands.spec.ts 以包括單元測試。

    • 包括命令的參考。
    import { SampleCommand } from '../../src/commands/sample'
    
    • 新增 Jest 測試組。
    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. 使用新的變更執行單元測試。

    npm run test