Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addModuleImportToRootModule doesn't affect the appModule #23

Open
TheNemus opened this issue Feb 26, 2020 · 5 comments
Open

addModuleImportToRootModule doesn't affect the appModule #23

TheNemus opened this issue Feb 26, 2020 · 5 comments

Comments

@TheNemus
Copy link

TheNemus commented Feb 26, 2020

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:

#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
Copy link

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

@raul1991
Copy link

is there any update on this @nitayneeman

@yodi-n
Copy link

yodi-n commented May 28, 2020

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
Copy link

raul1991 commented May 28, 2020

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

Read it here

@yodi-n
Copy link

yodi-n commented May 28, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants