How to use the @schematics/angular/utility/ast-utils.addImportToModule 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 codelab-fun / codelab / tools / schematics / slide / index.ts View on Github external
const sourceFile = ts.createSourceFile(
      modulePath,
      host.read(modulePath).toString('utf-8'),
      ts.ScriptTarget.Latest,
      true
    );

    let code = fs
      .readFileSync(join(__dirname, './files/code.bs'), 'utf-8')
      .toString();

    const componentName = classify(schema.name.split('/').pop()) + 'Component';

    code = code.replace('EmptyComponent', componentName);

    const moduleImportChanges = addImportToModule(
      sourceFile,
      modulePath,
      'SlidesModule',
      '@codelab/slides'
    );

    const routingImportChanges = addImportToModule(
      sourceFile,
      modulePath,
      'routes',
      null
    );

    const changes = [...moduleImportChanges, ...routingImportChanges];

    const recorder = host.beginUpdate(modulePath);
github OnsenUI / OnsenUI / bindings / angular2 / schematics / src / utils / index.ts View on Github external
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}`);
  }

  const changes = addImportToModule(moduleSource, modulePath, moduleName, src);
  const recorder = host.beginUpdate(modulePath);

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

  host.commitUpdate(recorder);
}
github DevExpress / devextreme-schematics / src / add-sample-views / index.ts View on Github external
return (host: Tree) => {
    const source = getSourceFile(host, routingPath);

    if (!source) {
      return host;
    }

    let changes;

    if (isView) {
      changes = addDeclarationToModule(source, routingPath, options.componentName, options.relativePath);
    } else {
      changes = addImportToModule(source, routingPath, options.componentName, options.relativePath);
    }

    return applyChanges(host, changes, routingPath);
  };
}
github ngx-meta / rules / libs / schematics / ng-add / index.ts View on Github external
return (host: Tree, context: SchematicContext) => {
    const animImport = ['BrowserAnimationsModule', '@angular/platform-browser/animations'];

    try {
      const modulePath = getAppModulePath(host, getMainProjectPath(host, options));
      const srcPath = getSourceFile(host, modulePath);

      if (!isImported(srcPath, 'MetaConfig, MetaUIRulesModule',
        '@ngx-metaui/rules')) {

        let changes = [
          ...addImportToModule(srcPath, modulePath, animImport[0], animImport[1]),
          ...addSymbolToNgModuleMetadata(srcPath, modulePath, 'imports',
            'MetaUIRulesModule.forRoot({})')];

        if (options.uiLib === 'prime-ng') {
          changes = [...changes, ...addSymbolToNgModuleMetadata(srcPath, modulePath,
            'imports',
            'PrimeNgRulesModule.forRoot()')];
        }

        const recorder = host.beginUpdate(modulePath);
        changes.forEach((change) => {
          if (change instanceof InsertChange) {
            recorder.insertLeft(change.pos, change.toAdd);
          }
        });
        host.commitUpdate(recorder);
github angular / components / src / cdk / schematics / utils / ast.ts View on Github external
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}`);
  }

  const changes = addImportToModule(moduleSource, modulePath, moduleName, src);
  const recorder = host.beginUpdate(modulePath);

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

  host.commitUpdate(recorder);
}
github NarikMe / narik-angular / projects / narik-cli / schematics / ng-add / index.ts View on Github external
export function addModuleImportToModule(
  host: Tree,
  modulePath: string,
  moduleName: string,
  src: string,
  isModuleImport: boolean = true
) {
  const moduleSource = getSourceFile(host, modulePath);

  if (!moduleSource) {
    throw new SchematicsException(`Module not found: ${modulePath}`);
  }
  let changes: Change[] = [];

  if (isModuleImport) {
    changes = addImportToModule(moduleSource, modulePath, moduleName, src);
  } else {
    if (!isImported(moduleSource, moduleName, src)) {
      const changeItem = insertImport(
        moduleSource,
        modulePath,
        moduleName,
        src
      );
      changes.push(changeItem);
    }
  }

  const recorder = host.beginUpdate(modulePath);

  changes.forEach((change: Change) => {
    if (change instanceof InsertChange) {
github nrwl / nx / packages / bazel / src / app / index.ts View on Github external
return (host: Tree) => {
    const modulePath = `${path}/app/app.module.ts`;
    const moduleSource = host.read(modulePath)!.toString('utf-8');
    const sourceFile = ts.createSourceFile(modulePath, moduleSource, ts.ScriptTarget.Latest, true);
    const importChanges = addImportToModule(sourceFile, modulePath, 'BrowserModule', '@angular/platform-browser');
    const bootstrapChanges = addBootstrapToModule(sourceFile, modulePath, 'AppComponent', './app.component');
    insert(host, modulePath, [...importChanges, ...bootstrapChanges]);
    return host;
  };
}
github akveo / nebular / schematics / playground-module / add-to-modules.ts View on Github external
function processRoutingModule(tree: Tree, modulePath: Path) {
  const moduleDeclarations = getClassWithDecorator(tree, modulePath, 'NgModule');
  if (!moduleDeclarations.length) {
    return;
  }

  const featureModulePath = findFeatureModule(tree, dirname(modulePath));
  if (!featureModulePath) {
    throw new SchematicsException(`Can't find module for routing module ${featureModulePath }.`);
  }

  const featureModuleSource = getSourceFile(tree, featureModulePath);
  const importString = importPath(featureModulePath, modulePath);
  for (const moduleDeclaration of moduleDeclarations) {
    const className = (moduleDeclaration.name as ts.Identifier).getText();
    const changes = addImportToModule(featureModuleSource, featureModulePath, className, importString);
    applyInsertChange(tree, featureModulePath, ...changes);
  }
}