@angular-devkit/schematics#UpdateRecorder TypeScript Examples

The following examples show how to use @angular-devkit/schematics#UpdateRecorder. 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: json-utils.ts    From fab-menu with MIT License 7 votes vote down vote up
export function appendPropertyInAstObject(
  recorder: UpdateRecorder,
  node: JsonAstObject,
  propertyName: string,
  value: JsonValue,
  indent: number
) {
  return originalAppendPropertyInAstObject(recorder, node, propertyName, value, indent);
}
Example #2
Source File: json-utils.ts    From fab-menu with MIT License 7 votes vote down vote up
export function insertPropertyInAstObjectInOrder(
  recorder: UpdateRecorder,
  node: JsonAstObject,
  propertyName: string,
  value: JsonValue,
  indent: number
) {
  return originalInsertPropertyInAstObjectInOrder(recorder, node, propertyName, value, indent);
}
Example #3
Source File: json-utils.ts    From angular-git-info with MIT License 7 votes vote down vote up
export function appendPropertyInAstObject(
  recorder: UpdateRecorder,
  node: JsonAstObject,
  propertyName: string,
  value: JsonValue,
  indent: number,
) {
  const indentStr = _buildIndent(indent);

  if (node.properties.length > 0) {
    // Insert comma.
    const last = node.properties[node.properties.length - 1];
    recorder.insertRight(last.start.offset + last.text.replace(/\s+$/, '').length, ',');
  }

  recorder.insertLeft(
    node.end.offset - 1,
    '  '
    + `"${propertyName}": ${JSON.stringify(value, null, 2).replace(/\n/g, indentStr)}`
    + indentStr.slice(0, -2),
  );
}
Example #4
Source File: json-utils.ts    From angular-git-info with MIT License 7 votes vote down vote up
export function appendValueInAstArray(
  recorder: UpdateRecorder,
  node: JsonAstArray,
  value: JsonValue,
  indent = 4,
) {
  const indentStr = _buildIndent(indent);

  if (node.elements.length > 0) {
    // Insert comma.
    const last = node.elements[node.elements.length - 1];
    recorder.insertRight(last.start.offset + last.text.replace(/\s+$/, '').length, ',');
  }

  recorder.insertLeft(
    node.end.offset - 1,
    '  '
    + JSON.stringify(value, null, 2).replace(/\n/g, indentStr)
    + indentStr.slice(0, -2),
  );
}
Example #5
Source File: change.ts    From router with MIT License 6 votes vote down vote up
export function createChangeRecorder(
  tree: Tree,
  path: string,
  changes: Change[]
): UpdateRecorder {
  const recorder = tree.beginUpdate(path);
  for (const change of changes) {
    if (change instanceof InsertChange) {
      recorder.insertLeft(change.pos, change.toAdd);
    } else if (change instanceof RemoveChange) {
      recorder.remove(change.pos, change.end - change.pos);
    } else if (change instanceof ReplaceChange) {
      recorder.remove(change.pos, change.oldText.length);
      recorder.insertLeft(change.pos, change.newText);
    }
  }
  return recorder;
}
Example #6
Source File: json-utils.ts    From fab-menu with MIT License 6 votes vote down vote up
export function removePropertyInAstObject(recorder: UpdateRecorder, node: JsonAstObject, propertyName: string) {
  return originalRemovePropertyInAstObject(recorder, node, propertyName);
}
Example #7
Source File: json-utils.ts    From fab-menu with MIT License 6 votes vote down vote up
export function appendValueInAstArray(recorder: UpdateRecorder, node: JsonAstArray, value: JsonValue, indent = 4) {
  return originalAppendValueInAstArray(recorder, node, value, indent);
}
Example #8
Source File: change.ts    From edit-in-place with MIT License 6 votes vote down vote up
export function createChangeRecorder(tree: Tree, path: string, changes: Change[]): UpdateRecorder {
  const recorder = tree.beginUpdate(path);
  for (const change of changes) {
    if (change instanceof InsertChange) {
      recorder.insertLeft(change.pos, change.toAdd);
    } else if (change instanceof RemoveChange) {
      recorder.remove(change.pos, change.end - change.pos);
    } else if (change instanceof ReplaceChange) {
      recorder.remove(change.pos, change.oldText.length);
      recorder.insertLeft(change.pos, change.newText);
    }
  }
  return recorder;
}
Example #9
Source File: json-utils.ts    From angular-git-info with MIT License 6 votes vote down vote up
export function insertPropertyInAstObjectInOrder(
  recorder: UpdateRecorder,
  node: JsonAstObject,
  propertyName: string,
  value: JsonValue,
  indent: number,
) {

  if (node.properties.length === 0) {
    appendPropertyInAstObject(recorder, node, propertyName, value, indent);

    return;
  }

  // Find insertion info.
  let insertAfterProp: JsonAstKeyValue | null = null;
  let prev: JsonAstKeyValue | null = null;
  let isLastProp = false;
  const last = node.properties[node.properties.length - 1];
  for (const prop of node.properties) {
    if (prop.key.value > propertyName) {
      if (prev) {
        insertAfterProp = prev;
      }
      break;
    }
    if (prop === last) {
      isLastProp = true;
      insertAfterProp = last;
    }
    prev = prop;
  }

  if (isLastProp) {
    appendPropertyInAstObject(recorder, node, propertyName, value, indent);

    return;
  }

  const indentStr = _buildIndent(indent);

  const insertIndex = insertAfterProp === null
    ? node.start.offset + 1
    : insertAfterProp.end.offset + 1;

  recorder.insertRight(
    insertIndex,
    `${indentStr}`
    + `"${propertyName}": ${JSON.stringify(value, null, 2).replace(/\n/g, indentStr)}`
    + ',',
  );
}