How to use the @schematics/angular/utility/change.InsertChange 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 NG-ZORRO / ng-zorro-antd / schematics / ng-add / setup-project / theming.ts View on Github external
// unnecessary path segments and windows backslash delimiters.
    const customThemePath = normalize(join(project.sourceRoot, defaultCustomThemeFilename));

    if (host.exists(customThemePath)) {
      console.log();
      console.warn(chalk.yellow(`Cannot create a custom NG-ZORRO theme because
          ${chalk.bold(customThemePath)} already exists. Skipping custom theme generation.`));
      return;
    }

    host.create(customThemePath, themeContent);
    addThemeStyleToTarget(project, 'build', host, customThemePath, workspace);
    return;
  }

  const insertion = new InsertChange(stylesPath, 0, themeContent);
  const recorder = host.beginUpdate(stylesPath);

  recorder.insertLeft(insertion.pos, insertion.toAdd);
  host.commitUpdate(recorder);
}
github NativeScript / nativescript-schematics / src / ast-utils.ts View on Github external
position = expr.getEnd() - 1;
      toInsert = `  ${metadataField}: [${symbolName}],\n`;
    } else {
      node = expr.properties[expr.properties.length - 1];
      position = node.getEnd();
      // Get the indentation of the last element, if any.
      const text = node.getFullText(source);
      const matches = text.match(/^\r?\n\s*/);
      if (matches.length > 0) {
        toInsert = `,${matches[0]}${metadataField}: ${symbolName},`;
      } else {
        toInsert = `, ${metadataField}: ${symbolName},`;
      }
    }

    return [new InsertChange(componentPath, position, toInsert)];
  }

  const assignment = matchingProperties[0] as ts.PropertyAssignment;

  // If it's not an array, keep the original value.
  if (assignment.initializer.kind !== ts.SyntaxKind.ArrayLiteralExpression) {
    return [];
  }

  const arrLiteral = assignment.initializer as ts.ArrayLiteralExpression;
  if (arrLiteral.elements.length == 0) {
    // Forward the property.
    node = arrLiteral;
  } else {
    node = arrLiteral.elements;
  }
github nrwl / nx / packages / schematics / src / utility / ast-utils.ts View on Github external
}
    }
  } else if (node.kind == ts.SyntaxKind.ArrayLiteralExpression) {
    // We found the field but it's empty. Insert it just before the `]`.
    position--;
    toInsert = `${expression}`;
  } else {
    // Get the indentation of the last element, if any.
    const text = node.getFullText(source);
    if (text.match(/^\r?\n/)) {
      toInsert = `,${text.match(/^\r?\n(\r?)\s+/)[0]}${expression}`;
    } else {
      toInsert = `, ${expression}`;
    }
  }
  const insert = new InsertChange(ngModulePath, position, toInsert);
  return [insert];
}
github NativeScript / nativescript-schematics / src / ast-utils.ts View on Github external
}
  } else if (node.kind == ts.SyntaxKind.ArrayLiteralExpression) {
    // We found the field but it's empty. Insert it just before the `]`.
    position--;
    toInsert = `${symbolName}`;
  } else {
    // Get the indentation of the last element, if any.
    const text = node.getFullText(source);
    if (text.match(/^\r?\n/)) {
      toInsert = `,${text.match(/^\r?\n(\r?)\s+/)[0]}${symbolName},`;
    } else {
      toInsert = `, ${symbolName},`;
    }
  }

  return [new InsertChange(componentPath, position, toInsert)];
}
github ionic-team / angular-toolkit / schematics / page / index.ts View on Github external
if (ts.isVariableDeclaration(declaration) && declaration.initializer && declaration.name.getText() === 'routes') {
        const node = declaration.initializer.getChildAt(1);
        const lastRouteNode = node.getLastToken();

        if (!lastRouteNode) {
          return [];
        }

        const changes: Change[] = [];
        let trailingCommaFound = false;

        if (lastRouteNode.kind === ts.SyntaxKind.CommaToken) {
          trailingCommaFound = true;
        } else {
          changes.push(new InsertChange(ngModulePath, lastRouteNode.getEnd(), ','));
        }

        changes.push(new InsertChange(ngModulePath, lastRouteNode.getEnd() + 1, `  {\n    path: '${routePath}',\n    loadChildren: () => import('${routeLoadChildren}').then( m => m.${ngModuleName})\n  }${trailingCommaFound ? ',' : ''}\n`));

        return changes;
      }
    }
  }

  return [];
}
github ng-alain / delon / packages / schematics / utils / alain.ts View on Github external
export function addValueToVariable(host: Tree, filePath: string, variableName: string, text: string, needWrap = true) {
  const source = getSourceFile(host, filePath);
  const node = findNode(source, ts.SyntaxKind.Identifier, variableName);
  if (!node) {
    throw new SchematicsException(`Could not find any [${variableName}] variable in path '${filePath}'.`);
  }
  const arr = (node.parent as any).initializer as ts.ArrayLiteralExpression;

  const change = new InsertChange(
    filePath,
    arr.end - 1,
    `${arr.elements && arr.elements.length > 0 ? ',' : ''}${needWrap ? '\n  ' : ''}${text}`,
  );

  const declarationRecorder = host.beginUpdate(filePath);
  declarationRecorder.insertLeft(change.pos, change.toAdd);
  host.commitUpdate(declarationRecorder);
}
github ionic-team / angular-toolkit / schematics / page / index.ts View on Github external
const lastRouteNode = node.getLastToken();

        if (!lastRouteNode) {
          return [];
        }

        const changes: Change[] = [];
        let trailingCommaFound = false;

        if (lastRouteNode.kind === ts.SyntaxKind.CommaToken) {
          trailingCommaFound = true;
        } else {
          changes.push(new InsertChange(ngModulePath, lastRouteNode.getEnd(), ','));
        }

        changes.push(new InsertChange(ngModulePath, lastRouteNode.getEnd() + 1, `  {\n    path: '${routePath}',\n    loadChildren: () => import('${routeLoadChildren}').then( m => m.${ngModuleName})\n  }${trailingCommaFound ? ',' : ''}\n`));

        return changes;
      }
    }
  }

  return [];
}
github ngx-meta / rules / libs / schematics / ng-generate / m-page / index.ts View on Github external
try {
      const pathToUserRules = normalize(options.path + '/rules/user-rules.ts');
      const path = getSourceFile(host, pathToUserRules);
      const tsClass = strings.classify(options.modelClass);

      const exportList = getSourceNodes(path)
        .find(n => n.kind === ts.SyntaxKind.SyntaxList);

      if (exportList) {
        const exports = exportList.getChildren();

        const lastExport = exports[exports.length - 1];


        const rec = `\n\n/** Auto generated  export */\nexport * from './ts/${tsClass}OSS';`;
        const change = new InsertChange(pathToUserRules, lastExport.getEnd(), rec);

        const declarationRecorder = host.beginUpdate(pathToUserRules);
        declarationRecorder.insertLeft(change.pos, change.toAdd);
        host.commitUpdate(declarationRecorder);

      }

      return host;
    } catch (e) {
      context.logger.log('warn',
        `✅️ Failed to add new export to the user-rules.ts ${e}`);
    }
  };
}
github ng-alain / delon / packages / schematics / utils / html.ts View on Github external
export function addHtmlToBody(host: Tree, project: Project, html: string) {
  const { indexPath, src } = getIndexHtmlContent(host, project);

  if (src.indexOf(html) === -1) {
    const node = getTag(host, src, 'body');
    const insertion = new InsertChange(indexPath, node.endOffset, html);
    const recorder = host.beginUpdate(indexPath);
    recorder.insertLeft(insertion.pos, insertion.toAdd);
    host.commitUpdate(recorder);
  }
}