UNPKG

@amplitude/ampli

Version:

Amplitude CLI

191 lines (190 loc) 8.99 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const chalk_1 = require("chalk"); const lodash_1 = require("lodash"); const TerminalWriter_1 = require("../stdout/TerminalWriter"); const generated_1 = require("../graphql/generated"); const getPropertyTypeString_1 = require("../util/property/getPropertyTypeString"); const icons_1 = require("../ui/icons"); const { bold, blueBright, greenBright, redBright, white, cyan, } = chalk_1.default; const stdout = new TerminalWriter_1.default(); const ComparisonResultColors = { [generated_1.ComparisonResult.Added]: bold, [generated_1.ComparisonResult.Removed]: bold, [generated_1.ComparisonResult.Updated]: bold, [generated_1.ComparisonResult.Conflict]: bold, [generated_1.ComparisonResult.NoChanges]: bold, }; const padding = ' '; const ComparisonResultSymbol = { [generated_1.ComparisonResult.Added]: greenBright(`${padding}[A]`), [generated_1.ComparisonResult.Removed]: redBright(`${padding}[D]`), [generated_1.ComparisonResult.Updated]: blueBright(`${padding}[U]`), [generated_1.ComparisonResult.Conflict]: redBright(`${padding}[C]`), [generated_1.ComparisonResult.NoChanges]: white(`${padding}[-]`), }; const ENTITY_DIFF_FIELDS = ['name', 'description']; const PROPERTY_DIFF_FIELDS = ['name', 'description', 'required', 'type', 'schema', 'kind']; function prettyPrint(obj) { switch (typeof obj) { case 'object': return JSON.stringify(obj) .replace(/\n/g, ''); default: return `${obj}`; } } function getObjectDiff(from = {}, to = {}, fields) { const diffItems = []; fields.forEach(field => { const fromValue = from ? from[field] : undefined; const toValue = to ? to[field] : undefined; const hasFromValue = !lodash_1.isEmpty(fromValue); const hasToValue = !lodash_1.isEmpty(toValue); let result = generated_1.ComparisonResult.Updated; if (!hasFromValue && hasToValue) { result = generated_1.ComparisonResult.Added; } else if (hasFromValue && !hasToValue) { result = generated_1.ComparisonResult.Removed; } if (!lodash_1.isEqual(fromValue, toValue)) { diffItems.push({ field: field === 'schema' ? 'rules' : field, old: field === 'type' ? getPropertyTypeString_1.default(fromValue) : fromValue, new: field === 'type' ? getPropertyTypeString_1.default(toValue) : toValue, result, }); } }); return diffItems; } function itemChangeInfo(diff) { const from = (diff.result === generated_1.ComparisonResult.Added) ? '' : prettyPrint(diff.old); const arrow = [generated_1.ComparisonResult.Added, generated_1.ComparisonResult.Removed].includes(diff.result) ? '' : ' → '; const to = (diff.result === generated_1.ComparisonResult.Removed) ? '' : prettyPrint(diff.new); return `${diff.field}: ${from}${arrow}${to}`; } function getSourceResult(comparison, source) { var _a; let sourceResult; (_a = comparison.sourceComparisons) === null || _a === void 0 ? void 0 : _a.forEach(sc => { var _a, _b; if (sc.result === generated_1.ComparisonResult.Added && ((_a = sc.origin) === null || _a === void 0 ? void 0 : _a.name) === source.name) { sourceResult = generated_1.ComparisonResult.Added; } else if (sc.result === generated_1.ComparisonResult.Removed && ((_b = sc.target) === null || _b === void 0 ? void 0 : _b.name) === source.name) { sourceResult = generated_1.ComparisonResult.Removed; } }); return sourceResult; } function entityInfo(comparison, source) { var _a, _b, _c, _d; const name = ((_a = comparison.event) === null || _a === void 0 ? void 0 : _a.name) || ((_b = comparison.template) === null || _b === void 0 ? void 0 : _b.name); const sourceResult = getSourceResult(comparison, source); const result = (comparison.result === generated_1.ComparisonResult.Updated && sourceResult) ? sourceResult : comparison.result; const itemDiffs = getObjectDiff((_c = comparison.objectComparison) === null || _c === void 0 ? void 0 : _c.target, (_d = comparison.objectComparison) === null || _d === void 0 ? void 0 : _d.origin, ENTITY_DIFF_FIELDS); const changeText = (result === generated_1.ComparisonResult.Updated && itemDiffs.length > 0) ? ` ${itemDiffs .map(diff => `\ ${ComparisonResultSymbol[diff.result]} ${icons_1.ICON_RETURN_ARROW} ${itemChangeInfo(diff)}`) .join('\n')}` : ''; const textStyle = ComparisonResultColors[result]; const symbol = ComparisonResultSymbol[result]; return `${symbol} ${textStyle(name)}${changeText}`; } function propertyInfo(property) { var _a, _b; const textStyle = ComparisonResultColors[property.result]; const symbol = ComparisonResultSymbol[property.result]; const propertyName = property.result === generated_1.ComparisonResult.Removed ? (_a = property.target) === null || _a === void 0 ? void 0 : _a.name : (_b = property.origin) === null || _b === void 0 ? void 0 : _b.name; const changeText = property.result === generated_1.ComparisonResult.Updated ? ` ${getObjectDiff(property.target, property.origin, PROPERTY_DIFF_FIELDS) .map(diff => `\ ${ComparisonResultSymbol[diff.result]} ${icons_1.ICON_RETURN_ARROW} ${itemChangeInfo(diff)}`) .join('\n')}` : ''; return `${symbol} ${icons_1.ICON_RETURN_ARROW} property: ${textStyle(propertyName || 'unknown')}${changeText}`; } function eventTemplateInfo(template) { var _a, _b; const textStyle = ComparisonResultColors[template.result]; const symbol = ComparisonResultSymbol[template.result]; const templateName = template.result === generated_1.ComparisonResult.Removed ? (_a = template.target) === null || _a === void 0 ? void 0 : _a.name : (_b = template.origin) === null || _b === void 0 ? void 0 : _b.name; const changeText = template.result === generated_1.ComparisonResult.Updated ? ` ${getObjectDiff(template.target, template.origin, ENTITY_DIFF_FIELDS) .map(diff => `\ ${ComparisonResultSymbol[diff.result]} ${icons_1.ICON_RETURN_ARROW} ${itemChangeInfo(diff)}`) .join('\n')}` : ''; return `${symbol} ${icons_1.ICON_RETURN_ARROW} property group: ${textStyle(templateName || 'unknown')}${changeText}`; } function TrackingPlanDiff(params) { const { changes, source, } = params; if (changes.changeCount < 1) { stdout.println(`${icons_1.ICON_SUCCESS} All events are up to date.`); return; } if (changes.changedTemplates.length > 0) { stdout.println(); stdout.println(cyan(`Property Groups:`)); changes.changedTemplates.forEach(template => { stdout.println(entityInfo(template, source)); if (template.result === generated_1.ComparisonResult.Updated) { if (template.propertyComparisons) { template.propertyComparisons.forEach(property => { stdout.println(propertyInfo(property)); }); } } }); } if (changes.changedEvents.length > 0) { stdout.println(); stdout.println(cyan(`Events:`)); changes.changedEvents.forEach(event => { stdout.println(entityInfo(event, source)); if (event.result === generated_1.ComparisonResult.Updated) { if (event.templateComparisons) { event.templateComparisons.forEach(template => { stdout.println(eventTemplateInfo(template)); }); } if (event.propertyComparisons) { event.propertyComparisons.forEach(property => { stdout.println(propertyInfo(property)); }); } } }); } const { changedProperties } = changes; if (changedProperties.length > 0) { stdout.println(); stdout.println(cyan(`Properties:`)); const changedPropertiesByKind = lodash_1.groupBy(changedProperties, p => { var _a, _b, _c; return (_b = (_a = p.target) === null || _a === void 0 ? void 0 : _a.kind) !== null && _b !== void 0 ? _b : (_c = p.origin) === null || _c === void 0 ? void 0 : _c.kind; }); Object.keys(generated_1.PropertyKind).forEach(kindName => { const properties = changedPropertiesByKind[generated_1.PropertyKind[kindName]]; if (properties) { stdout.println(` ${bold(kindName)}`); properties.forEach(property => { stdout.println(propertyInfo(property)); }); } }); } stdout.println(); } exports.default = TrackingPlanDiff;