0

I would like my Angular Schematic for ng add to automatically commit its changes to Git. Adding a Rule to commit the changes to Git after all my other Rules doesn't work properly because it executes too early and misses most of the changes:

export function ngAdd(): Rule {
  return chain([ 
    /* all my rules */
    gitCommit()
  ]);
}

function gitCommit(message: string): Rule {
  return () => {
    execSync(`git add .`).toString();
    execSync(`git commit -m "${message}"`).toString();
  }
}

It makes sense that this doesn't work because Rules make changes to the Tree and not directly on the file system. Therefore, the Rule returned by gitCommit() would execute before the changes from the previous Rules are applied to the file system.

Question

How can I run a task after the transformed Tree is applied to the file system within the Schematic?

Sam Herrmann
  • 6,293
  • 4
  • 31
  • 50
  • Just for clarification you want all the changes done by schematics to be commited in git? – Vimal Patel Jan 26 '22 at 16:58
  • 1
    That is correct. I am looking for the same experience as what you get with `ng new` where the Angular CLI creates an "initial commit" with everything it's created. After `ng new`, the developer can run my `ng add` schematic, with all changes again automatically added to Git in a separate commit. – Sam Herrmann Jan 26 '22 at 17:16
  • 1
    Check this, they are doing it using Task not rule. https://github.dev/angular/angular-cli/blob/fe746463f93899586dd0273b16cb12b7bde7f484/packages/schematics/angular/ng-new/index.ts#L90 – Vimal Patel Jan 26 '22 at 17:59

0 Answers0