2

Angular schematics has some tasks. I want to create my own task to run with the script executor. An example of how angular does this.

At the moment I just spawn predefined tasks at the end of the schematic.

Giovanni Bassi
  • 739
  • 6
  • 18
teabagp
  • 33
  • 3

2 Answers2

4

I was able to register an executor, but it is not supported, as I am using a private field. Here is what you need to do:

const host = <NodeModulesEngineHost>(<any>context.engine)._host; // this line is not supported
host.registerTaskExecutor<YourFactoryOptions>({
  name: "your-executor-name",
  create: (opt) => import('../path/to/executor').then(mod => mod.default(opt))
});

You can see on Github how to create the task executor registration, and then it is later actually registered here.

Giovanni Bassi
  • 739
  • 6
  • 18
0

I thought about going down this path, but since adding your own tasks isn't supported at the moment, I decided to just create a simple schematic that wraps the method I wanted to call and use the RunSchematicTask. That solved my problem and felt a bit safer, although it did result in a bit of boilerplate / clutter in the schematics directory.

As an example, here's the simple schematic:

export function simpleSchematic(options: any): Rule {
  return async () => {
    await someAsyncMethod(options);
  }
}

And here's how I invoked it in the "parent" schematic:

export function parentSchematic(options: any): Rule {
  return chain([
    someRuleCreator(options),
    (_tree: Tree, context: SchematicContext) => {
      context.addTask(new RunSchematicTask('simple-schematic', {})
    }
  ]);
}

Note that I did have to add the simple-schematic entry to the collection.json, but I didn't provide that in my example above.

tvsbrent
  • 2,350
  • 1
  • 10
  • 9