forked from UmbrellaDocs/linkspector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·117 lines (107 loc) · 3.72 KB
/
index.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env node
import { program } from 'commander'
import kleur from 'kleur'
import ora from 'ora'
import { linkspector } from './linkspector.js'
import { createRequire } from 'module'
const require = createRequire(import.meta.url)
const pkg = require('./package.json')
program
.version(pkg.version)
.description('🔍 Uncover broken links in your content.')
.command('check')
.description('Check hyperlinks based on the configuration file.')
.option('-c, --config <path>', 'Specify a custom configuration file path')
.option('-j, --json', 'Output the results in JSON format')
.action(async (cmd) => {
const configFile = cmd.config || '.linkspector.yml' // Use custom config file path if provided
let currentFile = '' // Variable to store the current file name
let results = [] // Array to store the results if json is true
const spinner = cmd.json ? null : ora().start()
try {
let hasErrorLinks = false
// Initialize the results object
let results = {
source: {
name: 'linkspector',
url: 'https://github.com/UmbrellaDocs/linkspector',
},
severity: 'ERROR',
diagnostics: [],
}
for await (const { file, result } of linkspector(configFile, cmd)) {
// Update the current file name
currentFile = file
if (!cmd.json) {
spinner.text = `Checking ${currentFile}...\n`
}
for (const linkStatusObj of result) {
if (linkStatusObj.status === 'error') {
if (cmd.json) {
results.diagnostics.push({
message: `Cannot reach ${linkStatusObj.link}. Status: ${linkStatusObj.status_code}${linkStatusObj.error_message ? ` ${linkStatusObj.error_message}` : ''}`,
location: {
path: currentFile,
range: {
start: {
line: linkStatusObj.line_number,
column: linkStatusObj.position.start.column,
},
end: {
line: linkStatusObj.position.end.line,
column: linkStatusObj.position.end.column,
},
},
},
severity: linkStatusObj.status.toUpperCase(),
})
} else {
// If json is false, print the results in the console
spinner.stop()
console.log(
kleur.red(
`🚫 ${currentFile}, ${linkStatusObj.link} , ${linkStatusObj.status_code}, ${linkStatusObj.line_number}, ${linkStatusObj.error_message}`
)
)
spinner.start(`Checking ${currentFile}...\n`)
}
hasErrorLinks = true
}
}
}
if (cmd.json) {
// If there are no links with a status of "error", print a blank object
if (results.diagnostics.length === 0) {
console.log('{}')
} else {
console.log(JSON.stringify(results, null, 2))
}
}
if (!hasErrorLinks) {
if (!cmd.json) {
spinner.stop()
console.log(
kleur.green(
'✨ Success: All hyperlinks in the specified files are valid.'
)
)
}
process.exit(0)
} else {
if (!cmd.json) {
spinner.stop()
console.error(
kleur.red(
'💥 Error: Some hyperlinks in the specified files are invalid.'
)
)
}
process.exit(1)
}
} catch (error) {
console.error(kleur.red(`💥 Main error: ${error.message}`))
process.exit(1)
}
})
// Parse the command line arguments
program.parse(process.argv)