【问题标题】:How to execute a angular schematics from Oclif如何从 Oclif 执行角度示意图
【发布时间】:2020-06-07 12:40:24
【问题描述】:

我正在使用 Oclif 编写 CLI,我尝试执行我构建的自定义原理图,但如果我使用“ng add”命令分开启动,原理图会正确询问。如果我从 Oclif 启动原理图,它什么都不问。

例子:

作品:在终端:ng add D:/projects/schematics/ams-front-schematics

无效:

export default class New extends Command {
  static description = 'Generate new project based in archetype';

  static args: Parser.args.IArg[] = [ { name: 'PROJECT_NAME', required: true } ];

  private answers: any;

  async run(): Promise<any> {
    const { args }: { args: Parser.args.Output } = this.parse(New);
    const name: string = args.PROJECT_NAME;

    process.setMaxListeners(0);
    require('events').EventEmitter.defaultMaxListeners = 100;
    await runCommand(`ng add D:/projects/schematics/ams-front-schematics`, {}, (...args: any[]) => this.log(...args)););
  }
}

仅运行命令函数 exec:npmRun 库。

export function runCommand(commands: string, options: any = {}, debug: (...args: any[]) => void) {
  return new Promise(resolve => {
    debug('command', commands);
    npmRun.exec(commands, options, (err: any, stdout: any, stderr: any) => {
      debug('err', err);
      debug('stdout', stdout);
      debug('stderr', stderr);
      if (err) {
        debug(stderr);
        debug('End', err);
        resolve();
      } else {
        debug(stdout);
        debug('End', true);
        resolve();
      }
    });
  });
}

【问题讨论】:

    标签: angular command-line-interface angular-schematics oclif


    【解决方案1】:

    您只需将 Angular 原理图 cli @angular-devkit/schematics-cli 集成到您的 ocif 项目中即可。

    1. 通过 npm i @angular-devkit/schematics-cli 在您的项目中安装 Angular 原理图 cli
    2. 然后通过 npm i @schematics/angular 在您的项目中安装角度示意图
    3. 然后根据下面的 sn-p 更新您的主命令文件。只需从项目目录中通过 ./bin/run 运行您的 cli

    注意:要进行测试,您必须从 Angular 项目运行此命令

    import { Command } from "@oclif/command";
    import { main } from "@angular-devkit/schematics-cli/bin/schematics";
    
    class AngularSchematicsCli extends Command {
      ...
      async run() {
        ...
        await main({
          args: ["@schematics/angular:component"],
        });
        
        // You can customise this args according to angular schematics 
        // args: ["@schematics/angular:component", "--name=test"],
      }
    }
    
    export = AngularSchematicsCli;

    【讨论】:

      猜你喜欢
      • 2018-12-26
      • 2019-08-23
      • 2020-08-19
      • 2018-08-04
      • 2019-04-22
      • 2019-04-21
      • 2019-11-02
      • 2018-12-24
      • 2020-01-27
      相关资源
      最近更新 更多