-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.js
115 lines (95 loc) · 3.25 KB
/
sync.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { storyblokClient } from './storyblok'
import { log, createIndex, deleteIndex, createBulkOperations, transformId, transformStory, cacheInvalidate } from './helpers'
function indexStories ({ db, stories = [] }) {
const bulkOps = createBulkOperations(stories)
return db.bulk({
body: bulkOps
})
}
async function syncStories ({ db, page = 1, perPage = 100, environment = null }) {
const { data: { stories }, total } = await storyblokClient.get('cdn/stories', {
page,
per_page: perPage,
resolve_links: 'url'
})
const newStories = stories.filter(story => {
if (!environment) {
return true
}
const environments = story.tag_list.filter(tag => tag.startsWith('publish-')).map(tag => tag.replace('publish-', ''))
return environments.length === 0 || environments.includes(environment)
}).map(story => {
const fullSlug = story.full_slug.replace(/^\/|\/$/g, '')
return {
...story,
full_slug: fullSlug,
real_path: fullSlug.substr(0, 1) === '/' ? fullSlug : `/${fullSlug}`,
folder: fullSlug.lastIndexOf('/') !== -1 ? fullSlug.substring(0, fullSlug.lastIndexOf('/')) : null
}
})
const promise = indexStories({ db, stories: newStories })
const lastPage = Math.ceil((total / perPage))
if (page < lastPage) {
page += 1
return syncStories({ db, page, perPage, environment })
}
return promise
}
const fullSync = async (db, config) => {
log('Syncing published stories!')
await db.indices.delete(deleteIndex())
await db.indices.create(createIndex())
await syncStories({ db, perPage: config.storyblok.perPage, environment: config.storyblok.environment })
}
const handleHook = async (db, config, params) => {
const cv = Date.now() // bust cache
const { story_id: id, action } = params
switch (action) {
case 'published':
const { data: { story } } = await storyblokClient.get(`cdn/stories/${id}`, {
cv,
resolve_links: 'url'
})
const environment = config.storyblok.environment
if (environment) {
const environments = story.tag_list.filter(tag => tag.startsWith('publish-')).map(tag => tag.replace('publish-', ''))
if (environments.length !== 0 && !environments.includes(environment)) {
const searchStory = transformId(id)
const response = await db.exists(searchStory)
if (response.statusCode === 200) {
await db.delete(searchStory)
log(`Unpublished ${story.full_slug}`)
break
} else {
log(`Skipped ${story.full_slug}`)
return
}
}
}
const publishedStory = transformStory(story)
await db.index(publishedStory)
log(`Published ${story.full_slug}`)
break
case 'unpublished':
const unpublishedStory = transformId(id)
await db.delete(unpublishedStory)
log(`Unpublished ${id}`)
break
case 'branch_deployed':
await fullSync(db, config)
break
default:
break
}
await cacheInvalidate(config.storyblok)
}
const seedDatabase = async (db, config) => {
try {
await db.ping()
await fullSync(db, config)
log('Stories synced!')
} catch (error) {
log('Stories not synced!')
}
}
export { syncStories, fullSync, handleHook, seedDatabase }