How to use the @ngrx/schematics/schematics-core.ReplaceChange function in @ngrx/schematics

To help you get started, we’ve selected a few @ngrx/schematics 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 ngrx / platform / modules / schematics / src / container / index.ts View on Github external
stm => stm.kind === ts.SyntaxKind.ClassDeclaration
    );
    const component = componentClass as ts.ClassDeclaration;
    const componentConstructor = component.members.find(
      member => member.kind === ts.SyntaxKind.Constructor
    );
    const cmpCtr = componentConstructor as ts.ConstructorDeclaration;
    const { pos } = cmpCtr;
    const stateType = options.state
      ? `fromStore.${options.stateInterface}`
      : 'any';
    const constructorText = cmpCtr.getText();
    const [start, end] = constructorText.split('()');
    const storeText = `private store: Store<${stateType}>`;
    const storeConstructor = [start, `(${storeText})`, end].join('');
    const constructorUpdate = new ReplaceChange(
      componentPath,
      pos,
      `  ${constructorText}\n\n`,
      `\n\n  ${storeConstructor}`
    );

    const changes = [storeImport, stateImport, constructorUpdate];
    const recorder = host.beginUpdate(componentPath);

    for (const change of changes) {
      if (change instanceof InsertChange) {
        recorder.insertLeft(change.pos, change.toAdd);
      } else if (change instanceof ReplaceChange) {
        recorder.remove(pos, change.oldText.length);
        recorder.insertLeft(change.order, change.newText);
      }