How to use the @schematics/angular/utility/ast-utils.insertImport 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 NG-ZORRO / ng-zorro-antd / schematics / ng-add / setup-project / register-locale.ts View on Github external
const workspace = getWorkspace(host);
    const project = getProjectFromWorkspace(workspace, options.project);
    const appModulePath = getAppModulePath(host, getProjectMainFile(project));
    const moduleSource = getSourceFile(host, appModulePath);

    const locale = getCompatibleLocal(options);
    const localePrefix = locale.split('_')[ 0 ];

    const recorder = host.beginUpdate(appModulePath);

    const changes = [
      insertImport(moduleSource, appModulePath, 'NZ_I18N',
        'ng-zorro-antd/i18n'),
      insertImport(moduleSource, appModulePath, locale,
        'ng-zorro-antd/i18n'),
      insertImport(moduleSource, appModulePath, 'registerLocaleData',
        '@angular/common'),
      insertImport(moduleSource, appModulePath, localePrefix,
        `@angular/common/locales/${localePrefix}`, true),
      registerLocaleData(moduleSource, appModulePath, localePrefix),
      ...insertI18nTokenProvide(moduleSource, appModulePath, locale)
    ];

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

    host.commitUpdate(recorder);

    return host;
github NG-ZORRO / ng-zorro-antd / schematics / ng-add / setup-project / register-locale.ts View on Github external
return (host: Tree) => {
    const workspace = getWorkspace(host);
    const project = getProjectFromWorkspace(workspace, options.project);
    const appModulePath = getAppModulePath(host, getProjectMainFile(project));
    const moduleSource = getSourceFile(host, appModulePath);

    const locale = getCompatibleLocal(options);
    const localePrefix = locale.split('_')[ 0 ];

    const recorder = host.beginUpdate(appModulePath);

    const changes = [
      insertImport(moduleSource, appModulePath, 'NZ_I18N',
        'ng-zorro-antd/i18n'),
      insertImport(moduleSource, appModulePath, locale,
        'ng-zorro-antd/i18n'),
      insertImport(moduleSource, appModulePath, 'registerLocaleData',
        '@angular/common'),
      insertImport(moduleSource, appModulePath, localePrefix,
        `@angular/common/locales/${localePrefix}`, true),
      registerLocaleData(moduleSource, appModulePath, localePrefix),
      ...insertI18nTokenProvide(moduleSource, appModulePath, locale)
    ];

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

    host.commitUpdate(recorder);
github ng-matero / ng-matero / schematics / utils / build-component.ts View on Github external
function addImportDeclaration(host: Tree, modulePath: string, fileName: string, filePath: string) {
  const source = readIntoSourceFile(host, modulePath);
  const changes = insertImport(source as any, modulePath, fileName, filePath);
  const declarationRecorder = host.beginUpdate(modulePath);

  if (changes instanceof InsertChange) {
    declarationRecorder.insertLeft(changes.pos, changes.toAdd);
  }

  host.commitUpdate(declarationRecorder);
}
github nrwl / nx / packages / bazel / src / collection / lib / index.ts View on Github external
indexFilePath,
      indexSource,
      ts.ScriptTarget.Latest,
      true
    );
    const moduleSource = host.read(modulePath)!.toString('utf-8');
    const moduleSourceFile = ts.createSourceFile(
      modulePath,
      moduleSource,
      ts.ScriptTarget.Latest,
      true
    );
    const constName = `${toPropertyName(schema.name)}Routes`;

    insert(host, modulePath, [
      insertImport(
        moduleSourceFile,
        modulePath,
        'RouterModule, Route',
        '@angular/router'
      ),
      ...addImportToModule(moduleSourceFile, modulePath, `RouterModule`),
      ...addGlobal(
        moduleSourceFile,
        modulePath,
        `export const ${constName}: Route[] = [];`
      )
    ]);
    insert(host, indexFilePath, [
      ...addReexport(indexSourceFile, indexFilePath, moduleFileName, constName)
    ]);
    return host;
github ng-alain / delon / packages / schematics / utils / alain.ts View on Github external
function addImportToModule(host: Tree, filePath: string, symbolName: string, fileName: string) {
  const source = getSourceFile(host, filePath);
  const change = insertImport(source, filePath, symbolName, fileName) as InsertChange;
  const declarationRecorder = host.beginUpdate(filePath);
  declarationRecorder.insertLeft(change.pos, change.toAdd);
  host.commitUpdate(declarationRecorder);
}
github akveo / nebular / schematics / utils / routing.ts View on Github external
routingModulePath: Path,
  routes: ts.ArrayLiteralExpression,
  route: string,
  componentClass?: string,
  fileImportPath?: string,
): void {
  const source = getSourceFile(tree, routingModulePath);
  const alreadyInRoutes = routes.getFullText().includes(route);
  if (alreadyInRoutes) {
    return;
  }

  addArrayElement(tree, source, routes, route);

  if (componentClass && fileImportPath) {
    const importChange = insertImport(source, source.fileName, componentClass, fileImportPath);
    applyInsertChange(tree, normalize(source.fileName), importChange);
  }
}
github valor-software / ng2-charts / ng2-charts-schematics / src / ng2-charts-schematics / ng2-process-tree.ts View on Github external
  const changes = newImports.map(([a, b]) => insertImport(codeSource, codeAction.path, a, b));
  const recorder = tree.beginUpdate(codeAction.path);
github nrwl / nx / packages / bazel / src / collection / 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
    );
    insert(host, modulePath, [
      insertImport(sourceFile, modulePath, 'NxModule', '@nrwl/nx'),
      ...addImportToModule(sourceFile, modulePath, 'NxModule.forRoot()')
    ]);
    return host;
  };
}