How to use the @schematics/angular/utility/ast-utils.addProviderToModule function in @schematics/angular

To help you get started, we’ve selected a few @schematics/angular examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DevExpress / devextreme-schematics / src / add-layout / index.ts View on Github external
const modifyRoutingModule = (host: Tree, routingModulePath: string) => {
  // TODO: Try to use the isolated host to generate the result string
  let source = getSourceFile(host, routingModulePath)!;
  const importChange = insertImport(source, routingModulePath, 'LoginFormComponent', './shared/components');
  const providerChanges = addProviderToModule(source, routingModulePath, 'AuthGuardService', './shared/services');
  applyChanges(host, [ importChange, ...providerChanges], routingModulePath);

  source = getSourceFile(host, routingModulePath)!;
  const routes = findRoutesInSource(source)!;
  if (!hasComponentInRoutes(routes, 'login-form')) {
    const loginFormRoute = getRoute('login-form');
    insertItemToArray(host, routingModulePath, routes, loginFormRoute);
  }
};
github NativeScript / nativescript-schematics / src / migrate-module / index.ts View on Github external
const addProvider = (providerClassName: string, providerPath: string) => (tree: Tree) => {
  const nsModulePath = addExtension(moduleInfo.modulePath, nsext);
  
  // check if the {N} version of the @NgModule exists
  if (!tree.exists(nsModulePath)) {
    throw new SchematicsException(
      `Module file [${nsModulePath}] doesn't exist.` +
      `Create it if you want the schematic to add ${moduleInfo.className} to its module providers,` +
      `or if you just want to update the component without updating its module, ` +
      `then rerun this command with --skip-module flag`,
    );
  }

  // Get the changes required to update the @NgModule
  const changes = addProviderToModule(
    getSourceFile(tree, nsModulePath),
    // nsModulePath, // <- this doesn't look like it is in use
    '',
    providerClassName,
    providerPath,
    // findRelativeImportPath(nsModulePath, providerPath)
  );
    
  // Save changes
  const recorder = tree.beginUpdate(nsModulePath);
  changes.forEach((change: InsertChange) =>
    recorder.insertRight(change.pos, change.toAdd),
  );
  tree.commitUpdate(recorder);
};
github Alorel / ngforage / projects / ngforage / schematics / ng-add / setup.ts View on Github external
const {modulePath, appModule} = loadAppModule(tree, options.project);

    const moduleFileContent = tree.read(modulePath);
    if (!moduleFileContent) {
      throw new SchematicsException(`Could not read app module ${modulePath}`);
    }

    const confPath = join(dirname(modulePath), Strings.CFG_FILE);

    if (!tree.exists(confPath)) {
      tree.create(confPath, fs.readFileSync(join(__dirname, 'files', Strings.CFG_FILE)));
    } else {
      context.logger.info('Skipping ngforage.config.ts - already exists');
    }

    const prov = addProviderToModule(appModule, modulePath, 'NGFORAGE_CONFIG_PROVIDER', './ngforage.config')
      .filter((v): v is InsertChange => v instanceof InsertChange);
    if (prov.length) {
      const recorder = tree.beginUpdate(modulePath);
      for (const p of prov) {
        recorder.insertLeft(p.pos, p.toAdd);
      }

      tree.commitUpdate(recorder);
    }
  };
}
github NarikMe / narik-angular / projects / narik-cli / schematics / ng-add / index.ts View on Github external
return (host: Tree) => {
    const workspace = getWorkspace(host);
    const project = workspace.projects[workspace.defaultProject!];
    const modulePath = getAppModulePath(host, getProjectMainFile(project));
    const moduleSource = getSourceFile(host, modulePath);

    for (const provide of provids) {
      if (provide.uiKey && provide.uiKey !== ui) {
        continue;
      }
      const changes = addProviderToModule(
        moduleSource,
        modulePath,
        provide.key,
        provide.link
      );

      const recorder = host.beginUpdate(modulePath);

      changes.forEach((change: Change) => {
        if (change instanceof InsertChange) {
          recorder.insertLeft(change.pos, change.toAdd);
        }
      });

      host.commitUpdate(recorder);
    }
github DevExpress / devextreme-schematics / src / add-layout / index.ts View on Github external
return (source: SourceFile) => {
      return addProviderToModule(source, appModulePath, importName, path);
    };
  };
github intershop / intershop-pwa / schematics / src / utils / registration.js View on Github external
return host => {
        const source = filesystem_1.readIntoSourceFile(host, options.module);
        const relativePath = options.moduleImportPath
            ? find_module_1.buildRelativePath(options.module, options.moduleImportPath)
            : undefined;
        const declarationChanges = ast_utils_1.addProviderToModule(source, options.module, options.artifactName, relativePath);
        const declarationRecorder = host.beginUpdate(options.module);
        for (const change of declarationChanges) {
            if (change instanceof change_1.InsertChange) {
                declarationRecorder.insertLeft(change.pos, change.toAdd);
            }
        }
        host.commitUpdate(declarationRecorder);
        return host;
    };
}
github akveo / nebular / schematics / playground-module / add-to-modules.ts View on Github external
function processService(tree: Tree, servicePath: Path): void {
  const modulePath = findFeatureModule(tree, dirname(servicePath));
  if (!modulePath) {
    throw new SchematicsException(`Can't find module for service ${servicePath}.`);
  }
  const serviceDeclarations = getClassWithDecorator(tree, servicePath, 'Injectable');

  for (const service of serviceDeclarations) {
    const serviceClassName = (service.name as ts.Identifier).getText();
    const importString = importPath(modulePath, servicePath);
    const source = getSourceFile(tree, servicePath);
    const changes = addProviderToModule(source, modulePath, serviceClassName, importString);

    applyInsertChange(tree, modulePath, ...changes);
  }
}