-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontentful.js
47 lines (40 loc) · 1.26 KB
/
contentful.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
const fs = require('fs')
const path = require('path')
const { SPACE_ID = '', ACCESS_TOKEN = '' } = process.env
const FILE_NAME = 'contentfulEntries.json'
const LIMIT = 800
const entries = { items: [], includes: { Asset: [] } }
const getEntries = (limit = LIMIT, skip = 0) =>
fetch(
`https://cdn.contentful.com/spaces/${SPACE_ID}/environments/master/entries?access_token=${ACCESS_TOKEN}&limit=${limit}&skip=${skip}`
)
.then((res) => res.json())
.then((res) => {
console.log(`Data fetched - limit: ${limit}; skip: ${skip} `)
entries.items.push(...(res?.items || []))
entries.includes.Asset.push(...(res?.includes?.Asset || []))
if (res?.total > limit + skip) {
return getEntries(limit, skip + limit)
}
})
const exec = () => {
if (!SPACE_ID || !ACCESS_TOKEN) {
console.error(
'Please provide process.env.SPACE_ID and process.env.ACCESS_TOKEN'
)
return
}
getEntries()
.then(() => {
fs.writeFileSync(
path.resolve(__dirname, 'app', '_assets', FILE_NAME),
JSON.stringify(entries)
)
console.log(`Contentful data updated in ${FILE_NAME}`)
console.log(`Total entry count: ${entries.items.length}`)
})
.catch((err) => {
console.error(err)
})
}
exec()