@angular-devkit/schematics#forEach TypeScript Examples

The following examples show how to use @angular-devkit/schematics#forEach. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: templates.ts    From cli with Apache License 2.0 6 votes vote down vote up
function overwriteIfExists(host: Tree): Rule {
  return forEach((fileEntry: FileEntry) => {
    if (host.exists(fileEntry.path)) {
      host.overwrite(fileEntry.path, fileEntry.content);
      return null;
    }
    return fileEntry;
  });
}
Example #2
Source File: index.ts    From angular-miniprogram with MIT License 6 votes vote down vote up
export default function (options: CommonHookOptions) {
  options.schematicPath = __dirname;
  return (tree: Tree, context: SchematicContext) => {
    return chain([
      getAngularSubDirRuleFactory(options),
      mergeSourceRuleFactory(options),
      changeStructuralDirectiveRuleFactory(options),
      removeNgComponentOutletRuleFactory(),
      removeI18nRuleFactory(),
      forEach((entry) => {
        if (
          entry.path.startsWith('/src/library/common') &&
          entry.path.endsWith('.ts')
        ) {
          return {
            content: Buffer.from(
              entry.content
                .toString()
                .replace(/@angular\/common/g, `angular-miniprogram/common`)
            ),
            path: entry.path,
          };
        }
        return entry;
      }),
    ]);
  };
}
Example #3
Source File: index.ts    From open-source with MIT License 5 votes vote down vote up
export default function (options: ComponentOptions): Rule {
   return async (host: Tree) => {
     const workspace = await getWorkspace(host);
     const project = workspace.projects.get(options.project as string);

     if (options.path === undefined && project) {
       options.path = buildDefaultPath(project);
     }

     if (options.prefix === undefined && project) {
       options.prefix = project.prefix || '';
     }

     options.module = findModuleFromOptions(host, options);

     const parsedPath = parseName(options.path as string, options.name);
     options.name = parsedPath.name;
     options.path = parsedPath.path;
     options.selector =
       options.selector || buildSelector(options, (project && project.prefix) || '');

     validateName(options.name);
     validateHtmlSelector(options.selector);

     const templateSource = apply(url('./files'), [
       options.skipTests ? filter((path) => !path.endsWith('.spec.ts.template')) : noop(),
       applyTemplates({
         ...strings,
         'if-flat': (s: string) => (options.flat ? '' : s),
         ...options,
       }),
       !options.type
         ? forEach(((file) => {
             return file.path.includes('..')
               ? {
                   content: file.content,
                   path: file.path.replace('..', '.'),
                 }
               : file;
           }) as FileOperator)
         : noop(),
       move(parsedPath.path),
     ]);

     return chain([
       addDeclarationToNgModule(options),
       mergeWith(templateSource),
       options.lintFix ? applyLintFix(options.path) : noop(),
     ]);
   };
 }