-
Notifications
You must be signed in to change notification settings - Fork 394
/
getReferenceWeakener.js
48 lines (40 loc) · 1.18 KB
/
getReferenceWeakener.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import through2 from 'through2'
import {extractWithPath} from '@sanity/mutator'
import {get} from 'lodash'
export default options => {
const {targetDataset, importId} = options
let importMapCount = 0
return through2.obj(function weakenReferences(doc, enc, cb) {
const refs = findStrongRefs(doc)
// Make strong references weak
refs.forEach(item => {
item.ref._weak = true
})
this.push(doc)
if (refs.length > 0) {
this.push({
_id: `${targetDataset}.sanity.`,
_type: 'sanity.importmap',
importId: importId,
importMapNumber: ++importMapCount,
documentId: doc._id,
refs: refs.map(item => serializePath(item.path))
})
}
cb()
})
}
function findStrongRefs(doc) {
return extractWithPath('..[_ref]', doc)
.map(match => match.path.slice(0, -1))
.map(path => ({path, ref: get(doc, path)}))
.filter(item => item.ref._weak !== true)
}
function serializePath(path) {
return path.reduce((target, part, i) => {
const isIndex = typeof part === 'number'
const seperator = i === 0 ? '' : '.'
const add = isIndex ? `[${part}]` : `${seperator}${part}`
return `${target}${add}`
}, '')
}