如何从 Oclif 执行 angular 原理图

How to execute a angular schematics from Oclif

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

运行 命令函数只执行:npm运行 library.

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 原理图 cli @angular-devkit/schematics-cli 集成到您的 oclif 项目中即可。

  1. 通过 npm i @angular-devkit/schematics-cli
  2. 在您的项目中安装 angular schematics cli
  3. 然后通过 npm i @schematics/angular
  4. 在您的项目中安装 angular 原理图
  5. 然后根据以下代码片段更新您的主命令文件。并简单地 运行 你的 cli by ./bin/run 来自项目目录

注意:为了测试你必须运行这个来自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;