-
Notifications
You must be signed in to change notification settings - Fork 58
/
checkorder.js
52 lines (44 loc) · 1.24 KB
/
checkorder.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
const fs = require('fs')
const args = process.argv
const files = args.slice(2, args.length)
const reLink = /\[(.*)\]\(.*\)/g
const reLinkName = /\[(.*)\]\(.*\)/
const compareArr = (arr1, arr2) => {
let arrErrors = []
let itemError = null;
arr1.forEach((a1, n) => {
const a2 = arr2[n]
if (a1 !== a2 && !itemError) {
itemError = a2
arrErrors.push(`${n + 1}. ${itemError}`)
}
if (itemError && itemError === a1) {
itemError = null
}
})
return arrErrors
}
const isFileOrdered = (file) => {
const content = fs.readFileSync(file, 'utf-8')
const data = content
.split('\n')
.filter(line => !!line.match(reLink))
.map(line => line.match(reLinkName)[1])
const sortedData = []
.concat(data)
.sort((a, b) => a.localeCompare(b))
return compareArr(data, sortedData)
}
const checkIfAllFilesAreOrdered = files =>
files.every(file => {
const result = isFileOrdered(file)
console.log(`\n> Checking links in ${file} ${(!result.length? '✔' : '𝙭')}`)
if (result.length) {
console.error(`Issues :-(`)
console.error(` ${result.join('\n ')}\n`)
process.exit(1)
}
return result
})
checkIfAllFilesAreOrdered(files)
module.exports = checkIfAllFilesAreOrdered;