function updateJsonWorkspace()

in packages/angular_devkit/core/src/workspace/json/writer.ts [207:364]


function updateJsonWorkspace(metadata: JsonWorkspaceMetadata): string {
  const data = new MagicString(metadata.raw);
  const indent = data.getIndentString();
  const removedCommas = new Set<number>();
  const nodeChanges = new Map<
    JsonAstNode | JsonAstKeyValue,
    (JsonAstNode | JsonAstKeyValue | string)[]
  >();

  for (const { op, path, node, value, type } of metadata.changes) {
    // targets/projects are typically large objects so always use multiline
    const multiline = node.start.line !== node.end.line || type !== 'json';
    const pathSegments = path.split('/');
    const depth = pathSegments.length - 1; // TODO: more complete analysis
    const propertyOrIndex = unescapeKey(pathSegments[depth]);
    const jsonValue = normalizeValue(value, type);
    if (op === 'add' && jsonValue === undefined) {
      continue;
    }

    // Track changes to the order/size of any modified objects/arrays
    let elements = nodeChanges.get(node);
    if (!elements) {
      if (node.kind === 'array') {
        elements = node.elements.slice();
        nodeChanges.set(node, elements);
      } else if (node.kind === 'object') {
        elements = node.properties.slice();
        nodeChanges.set(node, elements);
      } else {
        // keyvalue
        elements = [];
      }
    }

    switch (op) {
      case 'add':
        let contentPrefix = '';
        if (node.kind === 'object') {
          contentPrefix = `"${propertyOrIndex}": `;
        }

        const spacing = multiline ? '\n' + indent.repeat(depth) : ' ';
        const content = spacing + contentPrefix + stringify(jsonValue, multiline, depth, indent);

        // Additions are handled after analyzing all operations
        // This is mainly to support array operations which can occur at arbitrary indices
        if (node.kind === 'object') {
          // Object property additions are always added at the end for simplicity
          elements.push(content);
        } else {
          // Add place holders if adding an index past the length
          // An empty string is an impossible real value
          for (let i = elements.length; i < +propertyOrIndex; ++i) {
            elements[i] = '';
          }
          if (elements[+propertyOrIndex] === '') {
            elements[+propertyOrIndex] = content;
          } else {
            elements.splice(+propertyOrIndex, 0, content);
          }
        }
        break;
      case 'remove':
        let removalIndex = -1;
        if (node.kind === 'object') {
          removalIndex = elements.findIndex((e) => {
            return typeof e != 'string' && e.kind === 'keyvalue' && e.key.value === propertyOrIndex;
          });
        } else if (node.kind === 'array') {
          removalIndex = +propertyOrIndex;
        }
        if (removalIndex === -1) {
          continue;
        }

        const nodeToRemove = elements[removalIndex];
        if (typeof nodeToRemove === 'string') {
          // synthetic
          elements.splice(removalIndex, 1);
          continue;
        }

        if (elements.length - 1 === removalIndex) {
          // If the element is a terminal element remove the otherwise trailing comma
          const commaIndex = findPrecedingComma(nodeToRemove, data.original);
          if (commaIndex !== -1) {
            data.remove(commaIndex, commaIndex + 1);
            removedCommas.add(commaIndex);
          }
        }
        data.remove(
          findFullStart(nodeToRemove, data.original),
          findFullEnd(nodeToRemove, data.original),
        );
        elements.splice(removalIndex, 1);
        break;
      case 'replace':
        let nodeToReplace;
        if (node.kind === 'keyvalue') {
          nodeToReplace = node.value;
        } else if (node.kind === 'array') {
          nodeToReplace = elements[+propertyOrIndex];
          if (typeof nodeToReplace === 'string') {
            // Was already modified. This is already handled.
            continue;
          }
        } else {
          continue;
        }

        nodeChanges.delete(nodeToReplace);

        data.overwrite(
          nodeToReplace.start.offset,
          nodeToReplace.end.offset,
          stringify(jsonValue, multiline, depth, indent),
        );
        break;
    }
  }

  for (const [node, elements] of nodeChanges.entries()) {
    let parentPoint =
      1 + data.original.indexOf(node.kind === 'array' ? '[' : '{', node.start.offset);

    // Short-circuit for simple case
    if (elements.length === 1 && typeof elements[0] === 'string') {
      data.appendRight(parentPoint, elements[0]);
      continue;
    }

    // Combine adjecent element additions to minimize/simplify insertions
    const optimizedElements: typeof elements = [];
    for (let i = 0; i < elements.length; ++i) {
      const element = elements[i];
      if (typeof element === 'string' && i > 0 && typeof elements[i - 1] === 'string') {
        optimizedElements[optimizedElements.length - 1] += ',' + element;
      } else {
        optimizedElements.push(element);
      }
    }

    let prefixComma = false;
    for (const element of optimizedElements) {
      if (typeof element === 'string') {
        data.appendRight(parentPoint, (prefixComma ? ',' : '') + element);
      } else {
        parentPoint = findFullEnd(element, data.original);
        prefixComma = data.original[parentPoint - 1] !== ',' || removedCommas.has(parentPoint - 1);
      }
    }
  }

  const result = data.toString();

  return result;
}