forked from hdoro/metalsmith-sanity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (44 loc) · 1.08 KB
/
index.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
const importFromSanity = require('./importFromSanity')
const debug = require('debug')('metalsmith:sanity')
/*
Options:
- clientConfig:
* dataset = 'production'
* projectId
* useCdn = true
* token
- useCache = false
- cacheFilePath = ".cache/sanity-cached.json"
- filesKey = "sanity.json"
*/
module.exports = options => {
if (
!options ||
typeof options !== 'object' ||
(!options.clientConfig || !options.clientConfig.projectId)
) {
throw debug(
'Please provide a valid clientConfig.projectId string for the Sanity project'
)
}
// getting the default options
const {
useCache = false,
cacheFilePath = './.cache/sanity-cached.json',
filesKey = 'sanity.json',
clientConfig
} = options
return function(files, _metalsmith, done) {
debug('Fetching data from Sanity')
importFromSanity({
useCache,
cacheFilePath,
clientConfig,
debug
}).then(sanityData => {
files[filesKey] = sanityData
debug(`Sanity data injeted into files[${filesKey}]`)
setImmediate(done)
})
}
}