-
Notifications
You must be signed in to change notification settings - Fork 2
/
404-links.js
93 lines (80 loc) · 3.38 KB
/
404-links.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const $404 = require('./src')
const pullRequest = require('./src/pull-request')
const path = require('path')
const fs = require('fs')
const YAML = require('yaml')
const chalk = require('chalk')
const versionCheck = require('./src/version-check')
let options = {
configFile: path.resolve(process.cwd(), '.404-links.yml'),
log: console.log,
folder: process.cwd(),
delay: {},
pullRequestReview: true,
ignore: {
urls: [],
files: []
}
}
options.log(`\n`)
options.log(`😁 I'm so thrilled to be here an support you on this task.`)
options.log(`Let's take a look at the different links from your markdown files real quick!`)
if (true === fs.existsSync(options.configFile)) {
const yaml = YAML.parse(fs.readFileSync(options.configFile).toString('utf-8'))
options = Object.assign(options, yaml)
} else {
options.log(`> No config file has been found on the folder ${path.dirname(options.configFile)}`)
}
options.log('=====================================================')
options.log(`> Folder to parse: ${options.folder}`)
options.ignore.urls && options.log(`> Url to ignore: ${options.ignore.urls.length}`)
options.ignore.files && options.log(`> File to ignore: ${options.ignore.files.length}`)
options.log('=====================================================')
const stream = new $404(options)
stream
.on('data', (chunk) => {
const { status, passed, url} = JSON.parse(chunk.toString())
options.log(`> ${passed ? '✅':'❌'} - [${status}] - ${url}`)
})
.on('end', async function() {
options.log('=====================================================')
let summaryContent
const errors = this.errors
if (this.errors.length) {
options.log(`> ${this.errors.length} Errors:`)
errors.forEach(err => {
options.log(` * ${chalk.red(err.status)} - ${chalk.underline(err.url)} in the file ${chalk.yellow(err.file + ':' + err.line)}`)
})
if (options.pullRequestReview) {
await pullRequest(this.errors)
}
const {
GITHUB_SHA,
GITHUB_REPOSITORY
} = process.env
if (GITHUB_REPOSITORY && GITHUB_SHA) {
summaryContent = [
`🐛 Oups, **${this.errors.length}** links are broken in you documentation:`,
'',
'| # | Status | URL | File | Line |',
'| - | ----- | --- | ---- | ---- |',
this.errors.map((item, index) => {
return `| ${index + 1} | **${item.status}** | ${item.url} | [${item.file}](https://github.com/${GITHUB_REPOSITORY}/blob/${GITHUB_SHA}/${item.file}?plain=1#L${item.line}) | ${item.line}|`
}),
''
]
.flat()
.join('\n')
}
} else {
summaryContent = `🤘 All the ${this.result.length} links from your documentation are reachable. \n It's sounds like someone is maintaining an outstanding documentation 🤗`
options.log('> All the links are reachable 🥳')
}
if (summaryContent && process.env.GITHUB_STEP_SUMMARY) {
fs.appendFileSync(process.env.GITHUB_STEP_SUMMARY, '\n' + summaryContent)
options.log(`\n Access to the summary page: ${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}`)
}
options.log(`\nIf you find any issue do not hesitate to open an issue on ${chalk.green('https://github.com/atalent-labs/404-links')}`)
await versionCheck()
process.exit(errors.length ? 1 : 0)
})