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

feat: add ng-add logic to update project files

parent 0e293e61
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
@@ -35,6 +35,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"
  }
}
+91 −4
Original line number Diff line number Diff line
import { Rule } from '@angular-devkit/schematics';
import { InstallPeerDependencies } from '@rxap/schematics-utilities';
import {
  Rule,
  chain,
  Tree,
  SchematicsException
} from '@angular-devkit/schematics';
import {
  InstallPeerDependencies,
  GetProjectSourceRoot
} from '@rxap/schematics-utilities';
import { NgAddSchema } from './schema';
import {
  Project,
  QuoteKind,
  IndentationText
} from 'ts-morph';
import {
  AddDir,
  ApplyTsMorphProject,
  AddNgModuleProvider,
  AddToArray
} from '@rxap/schematics-ts-morph';

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 appModelSourceFile = project.getSourceFile('/app/app.module.ts');

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

      AddNgModuleProvider(
        appModelSourceFile,
        {
          provide:  'RXAP_ENVIRONMENT',
          useValue: 'environment'
        },
        [
          {
            namedImports:    [ 'RXAP_ENVIRONMENT' ],
            moduleSpecifier: '@rxap/environment'
          },
          {
            namedImports:    [ 'environment' ],
            moduleSpecifier: '../environments/environment'
          }
        ]
      );

      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',
        'UpdateEnvironment(environment)',
        'Promise<any>[]'
      );

      mainSourceFile.addImportDeclarations([
        {
          namedImports:    [ 'UpdateEnvironment' ],
          moduleSpecifier: '@rxap/environment'
        },
        {
          namedImports:    [ 'environment' ],
          moduleSpecifier: './environments/environment'
        }
      ]);

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

    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": "environment-ng-add",
  "type": "object",
  "properties": {},
  "properties": {
    "project": {
      "type": "string",
      "description": "The project where the environment feature should be added",
      "$default": {
        "$source": "projectName"
      }
    }
  },
  "required": []
}