การพัฒนา CoE CLI การเพิ่มคำสั่งใหม่ (เลิกใช้แล้ว)

Note

CoE CLI ถูกยกเลิกการใช้งานและจะไม่มีการเพิ่มคุณลักษณะใหม่ ปัญหาจะไม่ได้รับการตรวจทานหรือแก้ไขอีกต่อไป

ถ้าคุณระบุปัญหาด้านความปลอดภัยที่อาจเกิดขึ้น โปรดรายงานปัญหานั้นไปยัง Microsoft Security Response Center

นอกเหนือจาก ศูนย์การจัดการแพลตฟอร์ม Power ให้พิจารณาใช้ Microsoft Power Platform CLIPower Platform API, Power Platform inventory API และ Power Platform สําหรับตัวเชื่อมต่อ V2 ของผู้ดูแลระบบ

ในการเพิ่มคําสั่งตัวอย่างใหม่ ให้ใช้คําสั่งต่อไปนี้เพื่อเทมเพลตการตั้งค่าเริ่มต้นของคําสั่ง 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
      
    • สร้างคำสั่งในฟังก์ชัน Constructor

      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