forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisable-rules.js
executable file
·74 lines (64 loc) · 2.02 KB
/
disable-rules.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
#!/usr/bin/env node
// Disables markdownlint rules in markdown files with same-line comments. This is
// useful when introducing a new rule that causes many failures. The comments
// can be fixed and removed at while updating the file later.
//
// Usage:
//
// src/content-linter/scripts/disable-rules.js no-generic-link-text
import fs from 'fs'
import { spawn } from 'child_process'
const rule = process.argv[2]
if (!rule) {
console.error('Please specify a rule to disable.')
process.exit(1)
}
let verbose = false
if (process.argv[3] === '--verbose' || process.argv[3] === '-v') {
verbose = true
}
// Cleanup from previous run
if (fs.existsSync('markdown-violations.json')) {
fs.unlinkSync('markdown-violations.json')
}
console.log(`Disabling "${rule}" rule in markdown files...`)
const childProcess = spawn('npm', [
'run',
'lint-content',
'--',
'-p',
'content',
'data',
'-o',
'markdown-violations.json',
'-r',
rule,
])
childProcess.stdout.on('data', (data) => {
if (verbose) console.log(data.toString())
})
childProcess.stderr.on('data', function (data) {
if (verbose) console.log(data.toString())
})
let matchingRulesFound = 0
childProcess.on('close', (code) => {
if (code === 0) {
console.log(`No violations for rule, "${rule}" found.`)
process.exit(0)
}
const markdownViolations = JSON.parse(fs.readFileSync('markdown-violations.json', 'utf8'))
console.log(`${Object.values(markdownViolations).flat().length} violations found.`)
Object.entries(markdownViolations).forEach(([fileName, results]) => {
console.log(fileName)
console.log(results)
const fileLines = fs.readFileSync(fileName, 'utf8').split('\n')
results.forEach((result) => {
matchingRulesFound++
const lineIndex = result.lineNumber - 1
const offendingLine = fileLines[lineIndex]
fileLines[lineIndex] = offendingLine.concat(` <!-- markdownlint-disable-line ${rule} -->`)
})
fs.writeFileSync(fileName, fileLines.join('\n'), 'utf8')
})
console.log(`${matchingRulesFound} violations ignored.`)
})