Search code examples
angularnrwlnrwl-nxangular-schematics

nrwl/nx schematics create angular app and configure angular json


I'm working in a nrwl/nx monorepo. My applications are having a custom configuration in angular.json. For example the output path were customized. Now i want to write my own schematic, which will configure my project.

My problem is, I don't know how to write a schematic which can change properties in angular.json. I need help.

Best wishes.


Solution

  • This in one option to change the output path in angular json via nx-workspace schematic:

    function updateOutputPath(name: string, prefix: string): Rule {
      return (host: Tree) => {
        const angularJson = JSON.parse(host.read('angular.json').toString());
        const appOutputPath = angularJson.projects[name].architect.build;
        const appPrefix = angularJson.projects[name];
        appPrefix.prefix = `${name}`;
        appOutputPath.options.outputPath = `dist/${prefix}/static`;
        angularJson.projects[name] = prefix;
        angularJson.projects[name].architect.build = appOutputPath;
        host.overwrite('angular.json', JSON.stringify(angularJson));
      }
    }