-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
regenerate-fixtures.js
49 lines (38 loc) · 1.07 KB
/
regenerate-fixtures.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
import fs from 'fs'
import path from 'path'
import {bail} from 'bail'
import {rehype} from '../packages/rehype/index.js'
const join = path.join
const root = join(process.cwd(), 'test', 'fixtures')
fs.readdir(join(root), (error, files) => {
let index = -1
bail(error)
while (++index < files.length) {
const base = join(root, files[index])
let config
if (files[index].charAt(0) === '.') {
continue
}
try {
config = JSON.parse(fs.readFileSync(join(base, 'config.json')))
} catch {
config = {}
}
fs.readFile(join(base, 'index.html'), 'utf8', (error, doc) => {
const processor = rehype().use({settings: config})
const tree = processor.parse(doc)
const result = processor.stringify(tree)
bail(error)
fs.writeFile(
join(base, 'index.json'),
JSON.stringify(tree, 0, 2) + '\n',
bail
)
if (result === doc) {
fs.unlink(join(base, 'result.html'), Function.prototype)
} else {
fs.writeFile(join(base, 'result.html'), result, bail)
}
})
}
})