public formatObjectDiff()

in packages/@aws-cdk/cloudformation-diff/lib/format.ts [268:306]


  public formatObjectDiff(oldObject: any, newObject: any, linePrefix: string) {
    if ((typeof oldObject !== typeof newObject) || Array.isArray(oldObject) || typeof oldObject === 'string' || typeof oldObject === 'number') {
      if (oldObject !== undefined && newObject !== undefined) {
        if (typeof oldObject === 'object' || typeof newObject === 'object') {
          const oldStr = JSON.stringify(oldObject, null, 2);
          const newStr = JSON.stringify(newObject, null, 2);
          const diff = _diffStrings(oldStr, newStr, this.context);
          for (let i = 0; i < diff.length; i++) {
            this.print('%s   %s %s', linePrefix, i === 0 ? '└─' : '  ', diff[i]);
          }
        } else {
          this.print('%s   ├─ %s %s', linePrefix, REMOVAL, this.formatValue(oldObject, chalk.red));
          this.print('%s   └─ %s %s', linePrefix, ADDITION, this.formatValue(newObject, chalk.green));
        }
      } else if (oldObject !== undefined /* && newObject === undefined */) {
        this.print('%s   └─ %s', linePrefix, this.formatValue(oldObject, chalk.red));
      } else /* if (oldObject === undefined && newObject !== undefined) */ {
        this.print('%s   └─ %s', linePrefix, this.formatValue(newObject, chalk.green));
      }
      return;
    }
    const keySet = new Set(Object.keys(oldObject));
    Object.keys(newObject).forEach(k => keySet.add(k));
    const keys = new Array(...keySet).filter(k => !deepEqual(oldObject[k], newObject[k])).sort();
    const lastKey = keys[keys.length - 1];
    for (const key of keys) {
      const oldValue = oldObject[key];
      const newValue = newObject[key];
      const treePrefix = key === lastKey ? '└' : '├';
      if (oldValue !== undefined && newValue !== undefined) {
        this.print('%s   %s─ %s %s:', linePrefix, treePrefix, this.changeTag(oldValue, newValue), chalk.blue(`.${key}`));
        this.formatObjectDiff(oldValue, newValue, `${linePrefix}   ${key === lastKey ? ' ' : '│'}`);
      } else if (oldValue !== undefined /* && newValue === undefined */) {
        this.print('%s   %s─ %s Removed: %s', linePrefix, treePrefix, REMOVAL, chalk.blue(`.${key}`));
      } else /* if (oldValue === undefined && newValue !== undefined */ {
        this.print('%s   %s─ %s Added: %s', linePrefix, treePrefix, ADDITION, chalk.blue(`.${key}`));
      }
    }
  }