0

我正在使用 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();
      }
    });
  });
}
4

1 回答 1

0

你可以简单地通过将角度原理图 cli @angular-devkit/schematics-cli集成到你的oclif项目中来做到这一点。

  1. 通过npm i @angular-devkit/schematics-cli在您的项目中安装Angular 原理图 cli
  2. 然后通过npm i @schematics/angular在您的项目中安装角度示意图
  3. 然后根据以下代码段更新您的主命令文件。只需从项目目录中通过./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;

于 2020-08-28T06:19:07.467 回答