1 | 'use strict'
|
2 | module.exports = processFiles
|
3 |
|
4 | const fs = require('fs')
|
5 | const glob = require('glob')
|
6 |
|
7 | function processFiles (opts) {
|
8 | opts = opts || {}
|
9 | const afterEachRender = opts.afterEachRender
|
10 | const process = opts.process
|
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 process({ content: currentMD, filePath })
|
32 | .then(newMD => afterEachRender({
|
33 | newMD,
|
34 | currentMD,
|
35 | filePath,
|
36 | }))
|
37 | }
|
38 | })
|
39 | }
|