0

I'm writting a CLI using Oclif, and i try to exec a Custom Schematics that i build but if i launch separated using "ng add" command, the schematics ask correctly. If i launch schematic from Oclif, it don't ask anything.

Examples:

Works: In terminal: ng add D:/projects/schematics/ams-front-schematics

Don't works:

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)););
  }
}

Run command function only exec: npmRun 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();
      }
    });
  });
}

1 Answers1

0

You can do that simply by integrating angular schematics cli @angular-devkit/schematics-cli into your oclif project.

  1. install angular schematics cli in your project by npm i @angular-devkit/schematics-cli
  2. then install angular schematics in your project by npm i @schematics/angular
  3. and after that update your main command file according to the below snippet. and simply run your cli by ./bin/run from project dir

Note: For testing you have to run this command from angular project

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;