forked from madetech/handbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
63 lines (51 loc) · 1.53 KB
/
build.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
const glob = require('glob')
const fs = require('fs')
const path = require('path')
const markdownLinkCheck = require('markdown-link-check')
const chalk = require('chalk')
function handleError (err) {
if (err) {
console.error(chalk.red('%s'), err)
process.exit(1)
}
}
function checkFile (fileName) {
return new Promise((resolve, reject) => {
fs.readFile(fileName, 'utf8', (err, md) => {
handleError(err)
const baseUrl = `file://${path.dirname(path.resolve(fileName))}`
markdownLinkCheck(md, { baseUrl }, (err, results) => {
handleError(err)
let hasErrored = false
results.forEach(function (result) {
if (result.status !== 'alive') {
if (!hasErrored) {
console.log('%s', fileName)
hasErrored = true
}
console.log(chalk.grey(' [' + chalk.red('%s') + '] %s'), result.status, result.link)
}
})
if (hasErrored) {
reject(fileName)
} else {
resolve(fileName)
}
})
})
})
}
glob('**/*.md', (err, fileNames) => {
handleError(err)
const checkableFileNames = fileNames.filter((fileName) => !fileName.includes('node_module'))
const allChecks = Promise.allSettled(
checkableFileNames.map((fileName) => checkFile(fileName))
)
allChecks.then((results) => {
if (results.some(({ status }) => status === 'rejected')) {
handleError('Broken links found')
} else {
console.log(chalk.green('%s'), 'No broken links found')
}
})
})