Angular Schematic - Change execution scope or directory after calling the ng-new schematic

466 views Asked by At

I have an angular schematic where I create a new project, then call ng-add to add a package to that new project.

It looks like this:

export function createProject(options) {
  return () => {
    return chain([
     externalSchematic('@schematics/angular', 'ng-new', options),
     installPackages(options)
    ]);
  }
}

function installPackages(options) {
  return (tree, ctx) => {
    const taskId = ctx.addTask(new NodePackageInstallTask({
            packageName: 'my-package',
            workingDirectory: options.name
        }));

    ctx.addTask(new RunSchematicTask('addExternals', options), [taskId]);
  }
}

export function addExternals(options) {
  return () => {
    return externalSchematic('my-package', 'ng-add', {
      ...options
    }, {
      scope: options.name
    });
  }
}

To summarize:

  1. Generate a new angular project
  2. Install the package that I want to call ng-add
  3. After npm install is completed, call the ng-add schematic.

This works unless the ng-add schematic I call in step 3 also calls an external schematic. If it does, then it completely loses the scope. If options.name equals 'my-app', I would expect that the scope argument would result in all commands from my external schematic to be called from the 'my-app' folder. But that doesn't seem to be happening.

When you generate a new angular project in a schematic, is there a way to change the execution scope for the following commands to be in the newly created folder? Or is there a way to cd into that folder for subsequent commands?

0

There are 0 answers