Commit 68641ec7 authored by Merzough Münker's avatar Merzough Münker
Browse files

feat: add ng-add logic to update project files

parent dca873d4
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -6,6 +6,9 @@
  },
  "allowedNonPeerDependencies": [
    "@rxap/schematics-utilities",
    "@rxap/schematics-ts-morph",
    "@angular-devkit/schematics",
    "ts-morph",
    "tslib"
  ]
}
+5 −2
Original line number Diff line number Diff line
@@ -40,6 +40,9 @@
  },
  "dependencies": {
    "@rxap/schematics-utilities": "^12.1.0",
    "tslib": "^2.2.0"
    "@angular-devkit/schematics": "^12.1.1",
    "tslib": "^2.2.0",
    "ts-morph": "^11.0.0",
    "@rxap/schematics-ts-morph": "^12.1.0"
  }
}
+93 −4
Original line number Diff line number Diff line
import { Rule } from '@angular-devkit/schematics';
import { InstallPeerDependencies } from '@rxap/schematics-utilities';
import {
  Rule,
  Tree,
  chain,
  SchematicsException
} from '@angular-devkit/schematics';
import {
  InstallPeerDependencies,
  GetProjectSourceRoot,
  UpdateAngularJson
} from '@rxap/schematics-utilities';
import { NgAddSchema } from './schema';
import {
  Project,
  QuoteKind,
  IndentationText
} from 'ts-morph';
import {
  AddDir,
  ApplyTsMorphProject,
  AddToArray
} from '@rxap/schematics-ts-morph';
import { join } from 'path';

export default function(): Rule {
  return InstallPeerDependencies();
export default function(options: NgAddSchema): Rule {
  return (host: Tree) => {

    const rules: Rule[] = [
      InstallPeerDependencies()
    ];

    if (options.project) {

      const projectSourceRoot = GetProjectSourceRoot(host, options.project);
      const project           = new Project({
        useInMemoryFileSystem: true,
        manipulationSettings:  { quoteKind: QuoteKind.Single, indentationText: IndentationText.TwoSpaces }
      });
      AddDir(host.getDir(projectSourceRoot), project);

      const mainSourceFile = project.getSourceFile('/main.ts');

      if (!mainSourceFile) {
        throw new SchematicsException(`Could not find the main.ts source file in '[projectSourceRoot]/main.ts'`);
      }

      AddToArray(
        mainSourceFile,
        'setup',
        'ConfigService.Load()',
        'Promise<any>[]'
      );

      mainSourceFile.addImportDeclarations([
        {
          namedImports:    [ 'ConfigService' ],
          moduleSpecifier: '@rxap/config'
        }
      ]);

      rules.push(ApplyTsMorphProject(project, projectSourceRoot));

      const configFilePath = join(projectSourceRoot, 'config.json');

      rules.push(UpdateAngularJson(angular => {

        const p = angular.projects.get(options.project!);

        if (p) {
          const buildTarget = p.targets.get('build');
          if (buildTarget) {
            if (!buildTarget.options.assets) {
              buildTarget.options.assets = [];
            }
            const assets: string[] = buildTarget.options.assets;
            if (!assets.includes(configFilePath)) {
              assets.push(configFilePath);
            }
          }
        }

      }));

      rules.push(tree => {
        if (!tree.exists(configFilePath)) {
          tree.create(configFilePath, '{}');
        }
      });

    }

    return chain(rules);

  };
}
+1 −0
Original line number Diff line number Diff line
export interface NgAddSchema {
  project?: string;
}
+9 −1
Original line number Diff line number Diff line
@@ -2,6 +2,14 @@
  "$schema": "http://json-schema.org/draft-07/schema",
  "$id": "config-ng-add",
  "type": "object",
  "properties": {},
  "properties": {
    "project": {
      "type": "string",
      "description": "The project where the environment feature should be added",
      "$default": {
        "$source": "projectName"
      }
    }
  },
  "required": []
}