UNPKG

907 BJavaScriptView Raw
1'use strict'
2module.exports = processFiles
3
4const fs = require('fs')
5const glob = require('glob')
6
7function processFiles (opts) {
8 opts = opts || {}
9 const afterEachRender = opts.afterEachRender
10 const processor = opts.processor
11 const pattern = opts.pattern
12 const ignore = ['**/node_modules/**']
13 if (opts.ignorePattern) {
14 ignore.push(opts.ignorePattern)
15 }
16
17 return new Promise((resolve, reject) => {
18 glob(pattern, { ignore }, (err, files) => {
19 if (err) {
20 return reject(err)
21 }
22
23 Promise
24 .all(files.map(processFile))
25 .then(resolve)
26 .catch(reject)
27 })
28
29 function processFile (filePath) {
30 const currentMD = fs.readFileSync(filePath, 'utf8')
31 return processor
32 .process(currentMD, { filePath })
33 .then(newMD => afterEachRender({
34 newMD,
35 currentMD,
36 filePath,
37 }))
38 }
39 })
40}