nitayneeman / schematics-utilities

🛠️ Useful exported utilities for working with Schematics
https://nitayneeman.github.io/schematics-utilities
MIT License
87 stars 9 forks source link

addModuleImportToRootModule doesn't affect the appModule #23

Open TheNemus opened 4 years ago

TheNemus commented 4 years ago

KO in 2.0.0 and 2.0.1 OK in 1.1.3

Here is my rule factory function

import {
  Rule, Tree/*, SchematicContext*/
} from '@angular-devkit/schematics';
import { addModuleImportToRootModule, getProjectFromWorkspace } from 'schematics-utilities';
import { getWorkspace } from '@schematics/angular/utility/config';

export function ngAdd(options: any): Rule {
  return (host: Tree/*, context: SchematicContext*/) => {
      // get the workspace config of the consuming project
      // i.e. angular.json file
      const workspace = getWorkspace(host);

      // identify the project config which is using our library
      // or default to the default project in consumer workspace
      const project = getProjectFromWorkspace(
        workspace,
        options.project || workspace.defaultProject
      );
      // inject our module into the current main module of the selected project
      addModuleImportToRootModule(
        // tree to modify
        host,
        // Module name to insert
        'AngularCoreModule',
        // project name for import statement
        '@corp/angular-core',
        // project to be modified
        project
      );

      // return updated tree
      return host;
  };
}

Here is what I got in console

Installing packages for tooling via npm. Installed packages for tooling via npm. UPDATE src/app/app.module.ts (1333 bytes)

But my app.module.ts is absolutely the same as before.

I could downgrade to 1.1.3 but I need to set configuration for my modules, but then I got this issue:

https://github.com/nitayneeman/schematics-utilities/issues/7

What am I supposed to do?

P.S. Where I can find a comprehensive list of the API your package expoes? Thank you?

Antarus66 commented 4 years ago

Hi there! I have the same issue and would be grateful if you help.

raul1991 commented 4 years ago

is there any update on this @nitayneeman

yodi-n commented 4 years ago

Hi, I have the same issue. I created a ng-add schematics for an NG8 lib and whenever I call addModuleImportToRootModule, it doesn't change my appModule

I did some digging and found the problem here

export function addModuleImportToModule(host: Tree, modulePath: string, moduleName: string, src: string) {
  const moduleSource = getSourceFile(host, modulePath);

  if (!moduleSource) {
    throw new SchematicsException(`Module not found: ${modulePath}`);
  }

  //this is fine
  const changes = addImportToModule(<any>moduleSource, modulePath, moduleName, src);
  const recorder = host.beginUpdate(modulePath);

  changes.forEach(change => {
    if (change instanceof InsertChange) { // <= this always returns false
      recorder.insertLeft(change.pos, change.toAdd);
    }
  });

  host.commitUpdate(recorder);
}

the changes array contains the correct changes (the file import and the import in the ngModule) but this check : if (change instanceof InsertChange) always returns false. If I remove this check, it works as expected.

raul1991 commented 4 years ago

Either cast it explicitly or use angular/schematics not from utilities.

Read it here

yodi-n commented 4 years ago

I made it work using @schematics/angular, but it's still a bug IMO. I cannot cast it myself as the code that is faulty is in the schematics-utilities library.