import deepEqual from "deep-equal" ; import { diff , formatters } from "jsondiffpatch" ; import { DiffResult , Operation } from "../types/core" ; import log from "./logger" ; const DIRECTUS_TABLE_PREFIX = "directus_" ; param item export const isObject = ( item ) : boolean => item === Object ( item ) || Array . isArray ( item ) ; param table export const isDirectusTable = ( table ) : boolean => table . startsWith ( DIRECTUS_TABLE_PREFIX ) ; param collection2 param comparer export const missingFilter = < TItem > ( collection2 , comparer ) => ( item ) : boolean => ! collection2 . some ( comparer ( item ) ) ; export const capitalize = ( value ) : string => value . charAt ( 0 ) . toUpperCase ( ) . concat ( value . slice ( 1 ) ) ; param collection1 param collection2 param comparer export const getDiff = < TItem > ( collection1 , collection2 , comparer ) : DiffResult < TItem > [ ] => { return collection1 . reduce ( ( result , item ) => { const relatedItem = collection2 . find ( comparer ( item ) ) ; if ( ! relatedItem || deepEqual ( item , relatedItem ) ) { return result ; } result . push ( { oldItem : relatedItem , newItem : item , } ) ; return result ; } , [ ] as DiffResult < TItem > [ ] ) ; } ; param diffResults param operation export const logDiff = ( diffResults , operation ) : void => { Object . entries ( diffResults ) . map ( ( [ entity , diffs ] ) => { log . message ( operation , `${ capitalize ( entity ) } to ${ operation }` ) ; if ( diffs . length === 0 ) { log . plain ( "No changes detected." ) ; return ; } if ( operation === "update" ) { ( diffs as DiffResult [ ] ) . map ( ( { newItem , oldItem } ) => log . plain ( formatters . console . format ( diff ( oldItem , newItem ) , oldItem ) ) ) ; } else { ( diffs as unknown [ ] ) . map ( item => { log . plain ( item ) ; } ) ; } } ) ; } ;