How to use the @nrwl/workspace.insert function in @nrwl/workspace

To help you get started, weโ€™ve selected a few @nrwl/workspace 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 nrwl / nx / packages / angular / src / schematics / ngrx / rules / add-exports-barrel.ts View on Github external
const buffer = host.read(indexFilePath);
      if (!!buffer) {
        // AST to 'index.ts' barrel for the public API
        const indexSource = buffer!.toString('utf-8');
        const indexSourceFile = ts.createSourceFile(
          indexFilePath,
          indexSource,
          ts.ScriptTarget.Latest,
          true
        );

        // Public API for the feature interfaces, selectors, and facade
        const { fileName } = names(options.name);
        const statePath = `./lib/${options.directory}/${fileName}`;

        insert(host, indexFilePath, [
          ...addGlobal(
            indexSourceFile,
            indexFilePath,
            exportBarrels
              ? `import * as ${className}Actions from '${statePath}.actions';`
              : `export * from '${statePath}.actions';`
          ),
          ...addGlobal(
            indexSourceFile,
            indexFilePath,
            exportBarrels
              ? `import * as ${className}Feature from '${statePath}.reducer';`
              : `export * from '${statePath}.reducer';`
          ),
          ...addGlobal(
            indexSourceFile,
github nrwl / nx / packages / angular / src / schematics / ngrx / rules / add-imports-to-module.ts View on Github external
const storeRouterModule = 'StoreRouterConnectingModule.forRoot()';

    // InsertImport [symbol,source] value pairs
    const nxModuleImport = ['NxModule', '@nrwl/angular'];
    const storeModule = ['StoreModule', '@ngrx/store'];
    const effectsModule = ['EffectsModule', '@ngrx/effects'];
    const storeDevTools = ['StoreDevtoolsModule', '@ngrx/store-devtools'];
    const environment = ['environment', '../environments/environment'];
    const storeRouter = ['StoreRouterConnectingModule', '@ngrx/router-store'];

    // this is just a heuristic
    const hasRouter = sourceText.indexOf('RouterModule') > -1;
    const hasNxModule = sourceText.includes('NxModule.forRoot()');

    if (context.options.onlyEmptyRoot || context.options.minimal) {
      insert(host, modulePath, [
        addImport.apply(this, storeModule),
        addImport.apply(this, effectsModule),
        addImport.apply(this, storeDevTools),
        addImport.apply(this, environment),
        ...(hasRouter ? [addImport.apply(this, storeRouter)] : []),
        ...addImportToModule(source, modulePath, storeForRoot),
        ...addImportToModule(source, modulePath, effectsForEmptyRoot),
        ...addImportToModule(source, modulePath, devTools),
        ...(hasRouter
          ? addImportToModule(source, modulePath, storeRouterModule)
          : [])
      ]);
    } else {
      let common = [
        addImport.apply(this, storeModule),
        addImport.apply(this, effectsModule),
github nrwl / nx / packages / angular / src / schematics / library / library.ts View on Github external
return (host: Tree) => {
    const moduleSource = host.read(options.modulePath)!.toString('utf-8');
    const sourceFile = ts.createSourceFile(
      options.modulePath,
      moduleSource,
      ts.ScriptTarget.Latest,
      true
    );
    insert(host, options.modulePath, [
      insertImport(
        sourceFile,
        options.modulePath,
        'RouterModule',
        '@angular/router'
      ),
      ...addImportToModule(
        sourceFile,
        options.modulePath,
        `
        RouterModule.forChild([
        /* {path: '', pathMatch: 'full', component: InsertYourComponentHere} */
       ]) `
      )
    ]);
    return host;
github nrwl / nx / packages / angular / src / schematics / library / library.ts View on Github external
}').then(module => module.${options.moduleName})}`
      )
    ]);

    const tsConfig = findClosestTsConfigApp(host, options.parentModule);
    if (tsConfig) {
      const tsConfigAppSource = host.read(tsConfig)!.toString('utf-8');
      const tsConfigAppFile = ts.createSourceFile(
        tsConfig,
        tsConfigAppSource,
        ts.ScriptTarget.Latest,
        true
      );

      const offset = offsetFromRoot(path.dirname(tsConfig));
      insert(host, tsConfig, [
        ...addIncludeToTsConfig(
          tsConfig,
          tsConfigAppFile,
          `\n    , "${offset}${options.projectRoot}/src/index.ts"\n`
        )
      ]);
    } else {
      // we should warn the user about not finding the config
    }

    return host;
  };
}
github nrwl / nx / packages / schematics / migrations / update-8-0-0 / update-8-0-0.ts View on Github external
const value = node
        .getText(sourceFile)
        .substr(1, node.getText(sourceFile).length - 2);
      if (value === '@nrwl/builders/plugins/jest/resolver') {
        changes.push(
          new ReplaceChange(
            'jest.config.js',
            node.getStart(sourceFile),
            node.getText(sourceFile),
            `'@nrwl/jest/plugins/resolver'`
          )
        );
      }
    }
  });
  insert(host, 'jest.config.js', changes);
};
github nrwl / nx / packages / react / src / schematics / application / application.ts View on Github external
function addRouterToComponent(host: Tree) {
          const appPath = join(
            options.appProjectRoot,
            `src/app/${options.fileName}.tsx`
          );
          const appFileContent = host.read(appPath).toString('utf-8');
          const appSource = ts.createSourceFile(
            appPath,
            appFileContent,
            ts.ScriptTarget.Latest,
            true
          );

          insert(host, appPath, addInitialRoutes(appPath, appSource, context));
        },
        addDepsToPackageJson(
github nrwl / nx / packages / angular / src / schematics / library / library.ts View on Github external
return (host: Tree) => {
    const moduleSource = host.read(options.modulePath)!.toString('utf-8');
    const moduleSourceFile = ts.createSourceFile(
      options.modulePath,
      moduleSource,
      ts.ScriptTarget.Latest,
      true
    );
    const constName = `${toPropertyName(options.fileName)}Routes`;

    insert(host, options.modulePath, [
      insertImport(
        moduleSourceFile,
        options.modulePath,
        'RouterModule, Route',
        '@angular/router'
      ),
      ...addImportToModule(
        moduleSourceFile,
        options.modulePath,
        `RouterModule`
      ),
      ...addGlobal(
        moduleSourceFile,
        options.modulePath,
        `export const ${constName}: Route[] = [];`
      )
github nrwl / nx / packages / react / src / schematics / library / library.ts View on Github external
function addBrowserRouterToMain(host: Tree) {
        const { content, source } = readComponent(host, options.appMain);
        const isRouterPresent = content.match(/react-router-dom/);
        if (!isRouterPresent) {
          insert(
            host,
            options.appMain,
            addBrowserRouter(options.appMain, source, context)
          );
        }
      },
      function addInitialAppRoutes(host: Tree) {
github nrwl / nx / packages / angular / src / schematics / downgrade-module / downgrade-module.ts View on Github external
return (host: Tree) => {
    const { modulePath, moduleSource, moduleClassName } = readBootstrapInfo(
      host,
      options.project
    );
    insert(host, modulePath, [
      ...addMethod(moduleSource, modulePath, {
        className: moduleClassName,
        methodHeader: 'ngDoBootstrap(): void',
        body: ``
      }),
      ...removeFromNgModule(moduleSource, modulePath, 'bootstrap')
    ]);
    return host;
  };
}
github nrwl / nx / packages / angular / src / schematics / upgrade-module / upgrade-module.ts View on Github external
return (host: Tree) => {
    const { moduleClassName, modulePath, moduleSource } = readBootstrapInfo(
      host,
      options.project
    );

    insert(host, modulePath, [
      insertImport(
        moduleSource,
        modulePath,
        `configure${toClassName(options.name)}, upgradedComponents`,
        `../${toFileName(options.name)}-setup`
      ),
      insertImport(
        moduleSource,
        modulePath,
        'UpgradeModule',
        '@angular/upgrade/static'
      ),
      ...addImportToModule(moduleSource, modulePath, 'UpgradeModule'),
      ...addDeclarationToModule(
        moduleSource,
        modulePath,