UNPKG

@sanity/migrate

Version:

Tooling for running data migrations on Sanity.io projects

148 lines (147 loc) • 5.83 kB
import { isatty } from 'node:tty'; import { convertToTree, formatTree, maxKeyLength } from '@sanity/cli-core/tree'; import { chalk } from '@sanity/cli-core/ux'; var isTty = isatty(1); export function prettyFormat(param) { var _param_indentSize = param.indentSize, indentSize = _param_indentSize === void 0 ? 0 : _param_indentSize, migration = param.migration, subject = param.subject; return (Array.isArray(subject) ? subject : [ subject ]).map(function(subjectEntry) { if (subjectEntry.type === 'transaction') { return [ [ badge('transaction', 'info'), subjectEntry.id === undefined ? null : chalk.underline(subjectEntry.id) ].filter(Boolean).join(' '), indent(prettyFormat({ indentSize: indentSize, migration: migration, subject: subjectEntry.mutations })) ].join('\n\n'); } return prettyFormatMutation({ indentSize: indentSize, migration: migration, subject: subjectEntry }); }).join('\n\n'); } function encodeItemRef(ref) { return typeof ref === 'number' ? ref : ref._key; } function badgeStyle(variant) { var styles = { destructive: chalk.bgRed.black.bold, incremental: chalk.bgGreen.black.bold, info: chalk.bgWhite.black, maybeDestructive: chalk.bgYellow.black.bold }; return styles[variant]; } function badge(label, variant) { if (!isTty) { return "[".concat(label, "]"); } return badgeStyle(variant)(" ".concat(label, " ")); } var mutationImpact = { create: 'incremental', createIfNotExists: 'incremental', createOrReplace: 'maybeDestructive', delete: 'destructive', patch: 'maybeDestructive' }; function documentId(mutation) { if ('id' in mutation) { return mutation.id; } if ('document' in mutation) { return mutation.document._id; } return undefined; } var listFormatter = new Intl.ListFormat('en-US', { type: 'disjunction' }); function mutationHeader(mutation, migration) { var _migration_documentTypes; var mutationType = badge(mutation.type, mutationImpact[mutation.type]); var documentType = 'document' in mutation || migration.documentTypes ? badge('document' in mutation ? mutation.document._type : listFormatter.format((_migration_documentTypes = migration.documentTypes) !== null && _migration_documentTypes !== void 0 ? _migration_documentTypes : []), 'info') : null; // TODO: Should we list documentType when a mutation can be yielded for any document type? return [ mutationType, documentType, chalk.underline(documentId(mutation)) ].filter(Boolean).join(' '); } function prettyFormatMutation(param) { var _param_indentSize = param.indentSize, indentSize = _param_indentSize === void 0 ? 0 : _param_indentSize, migration = param.migration, subject = param.subject; var _subject_options; var lock = 'options' in subject ? chalk.cyan("(if revision==".concat((_subject_options = subject.options) === null || _subject_options === void 0 ? void 0 : _subject_options.ifRevision, ")")) : ''; var header = [ mutationHeader(subject, migration), lock ].join(' '); var padding = ' '.repeat(indentSize); if (subject.type === 'create' || subject.type === 'createIfNotExists' || subject.type === 'createOrReplace') { return [ header, '\n', indent(JSON.stringify(subject.document, null, 2), indentSize) ].join(''); } if (subject.type === 'patch') { var tree = convertToTree(subject.patches.flat()); var paddingLength = Math.max(maxKeyLength(tree.children) + 2, 30); return [ header, '\n', formatTree({ getMessage: function(patch) { return formatPatchMutation(patch); }, indent: padding, node: tree.children, paddingLength: paddingLength }) ].join(''); } return header; } function formatPatchMutation(patch) { var op = patch.op; var formattedType = chalk.bold(op.type); if (op.type === 'unset') { return "".concat(chalk.red(formattedType), "()"); } if (op.type === 'diffMatchPatch') { return "".concat(chalk.yellow(formattedType), "(").concat(op.value, ")"); } if (op.type === 'inc' || op.type === 'dec') { return "".concat(chalk.yellow(formattedType), "(").concat(op.amount, ")"); } if (op.type === 'set') { return "".concat(chalk.yellow(formattedType), "(").concat(JSON.stringify(op.value), ")"); } if (op.type === 'setIfMissing') { return "".concat(chalk.green(formattedType), "(").concat(JSON.stringify(op.value), ")"); } if (op.type === 'insert') { return "".concat(chalk.green(formattedType), "(").concat(op.position, ", ").concat(encodeItemRef(op.referenceItem), ", ").concat(JSON.stringify(op.items), ")"); } if (op.type === 'replace') { return "".concat(chalk.yellow(formattedType), "(").concat(encodeItemRef(op.referenceItem), ", ").concat(JSON.stringify(op.items), ")"); } if (op.type === 'truncate') { return "".concat(chalk.red(formattedType), "(").concat(op.startIndex, ", ").concat(op.endIndex, ")"); } throw new Error("Invalid operation type: ".concat(op.type)); } function indent(subject) { var size = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : 2; var padding = ' '.repeat(size); return subject.split('\n').map(function(line) { return padding + line; }).join('\n'); }