@ibiz/cli

埃必致内部命令行工具

Usage no npm install needed!

<script type="module">
  import ibizCli from 'https://cdn.skypack.dev/@ibiz/cli';
</script>

README

iBiz命令行工具

目录说明

|-- actions                与命令匹配的对应处理行为
|-- bin                    入口目录
|-- commands               命令声明与命令注册
|-- core                   核心包,基本工具类与接口
|-- tools                  工具包
|   `-- gulp               打包清理gulp插件
|-- LICENSE
|-- README.md
|-- gulpfile.js
|-- package.json
|-- tsconfig.json
`-- yarn.lock

本地开发

// 在项目根目录
yarn

// 启动项目
yarn dev

// 本地全局安装,成功后可在命令行任意目录使用
npm link

扩展

1. 新增命令

  1. 以新增link命令为例

  2. 在commands目录下新建需要新增的命令link/link.ts文件。

  3. link命令类继承CommandBase类

    export class LinkCommand extends CommandBase { }
    
  4. 实现抽象方法load,用于注册命令

    protected load(program: CommanderStatic): void {
        program
        .command('link')
        .arguments('<source> <target>')
        .description('建立软链接', {
            source: '需要链接的「文件、文件夹」',
            target: '链接至的目标目录',
        })
        .usage('<source> <target>')
        .option('-f, --force', '当指向的目标文件存在时,删除原有[文件 or 文件夹]并建立软链接!')
        .action(async (source: string, target: string, options: Record<string, string | boolean>, _command: Command) => {
            const input: Input = {};
            input.source = { name: 'source', value: source };
            input.target = { name: 'target', value: target };
            const opts: Input = {};
            opts.force = { name: 'force', value: options.force };
            try {
            await this.action.handle(input, opts);
            } catch (err) {
            if (isError(err)) {
                log.error('', err.message);
                process.exit(10);
            } else {
                process.exit(0);
            }
            }
        });
    }
    
  5. 在commands/loader/loader.ts的load方法中进行命令注册

    public static load(program: CommanderStatic): void {
        new LinkCommand(program);
        new LinkModelCommand(program);
        this.handleInvalidCommand(program);
    }
    
  6. 实现getAction抽象方法注册行为。

2. 补充命令处理行为

  1. 补充link的处理行为

  2. 在actions目录下新建link/link.ts行为处理文件

  3. link行为处理继承ActionBase

    export class LinkAction extends ActionBase
    
  4. 实现handle抽象方法,用于处理命令。

    async handle(input: Input, options: Input, _extraFlags?: string[]): Promise<void> {
        const { source, target } = input;
        const { force } = options;
        linkDir(source.value, target.value, force.value);
    }