This repository has been archived by the owner on Nov 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
65 lines (56 loc) · 1.45 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'use strict'
const processor = require('./lib/processor')
const debug = require('debug')('metalsmith-contentful-files')
/**
* Plugin function
*
* @param {Object|undefined} options
*
* @return {Function} Function to be used in metalsmith process
*/
function plugin (options) {
options = options || {}
/**
* Function to process all read files by metalsmith
*
* @param {Object} files file map
* @param {Object} metalsmith metalsmith
* @param {Function} done success callback
*/
return function (files, metalsmith, done) {
options.metadata = metalsmith.metadata()
debug('Files before processing:')
debug(files)
return new Promise(resolve => {
resolve(Object.keys(files))
})
.then(fileNames => {
return Promise.all(
fileNames.map(fileName => {
files[fileName]._fileName = fileName
return processor.processFile(files[fileName], options)
})
)
})
.then((fileMaps) => {
fileMaps.forEach(map => {
Object.assign(files, map)
})
debug('Files after processing:')
debug(files)
done()
})
.catch((error) => {
// friendly error formatting to give
// more information in error case by api
// -> see error.details
done(
new Error(`
${error.message}
${error.details ? JSON.stringify(error.details, null, 2) : ''}
`)
)
})
}
}
module.exports = plugin