-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
executable file
·120 lines (105 loc) · 2.74 KB
/
cli.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
118
119
120
#!/usr/bin/env node
const fs = require("fs/promises")
const glob = require("glob")
const meow = require("meow")
const { checkSync } = require("recheck")
const cli = meow(
`
Usage
$ recheck [arguments] "<dir glob>"
Flags
-n include node_modules (default: false)
Examples
$ recheck "**/*.js"
$ recheck -n "**/*.js"
`,
{
flags: {
nodeModules: {
type: "boolean",
alias: "n",
},
},
}
)
const [globPattern] = cli.input
const flags = cli.flags
if (!globPattern && process.stdin.isTTY) {
console.error("Path is required")
process.exit(1)
}
const getFiles = async (globPattern) => {
const ignoreNodeModules = [
"node_modules",
"**/node_modules/**",
"**/node_modules",
"./node_modules",
"./node_modules/**",
"node_modules/**",
]
const files = glob.sync(globPattern, {
nosort: true,
nodir: true,
nonull: true,
ignore: !flags.nodeModules && ignoreNodeModules,
})
return files
}
const loopFiles = async (files) => {
if (!Array.isArray(files)) {
throw Error("No Files Found")
}
Promise.all(files.map((file) => parseRegexes(file))).then((data) => {
data
.filter((item) => Object.getOwnPropertyNames(item).length !== 0)
.forEach((entry) => {
const target = Object.entries(entry).flat()
console.log()
console.log(`File: ${target[0]}`)
console.log(` Unsafe Regex`)
target[1].forEach((line) => {
console.log(` - (L${line.lineNr}) ${line.line}`)
console.log(` - Summary: ${line.summary}`)
})
})
})
}
const parseRegexes = async (file) => {
const fileContents = await fs.readFile(file, "utf8")
const fileLines = fileContents.split("\n")
const re = new RegExp(
/\/((?![*+?])(?:[^\r\n\[/\\]|\\.|\[(?:[^\r\n\]\\]|\\.)*\])+)\/((?:g(?:im?|mi?)?|i(?:gm?|mg?)?|m(?:gi?|ig?)?)?)/
)
return fileLines.reduce((obj, line, index) => {
if (re.test(line)) {
const foundRegex = line.match(re)
const checkResult = checkSync(foundRegex[0].slice(1, -1), "", {
timeout: 1000,
checker: "hybrid",
})
if (checkResult.status !== "safe" && checkResult.status !== "unknown") {
if (!obj[file]) {
obj[file] = []
}
obj[file].push({
lineNr: index,
line: line.trim(),
summary: checkResult.complexity?.summary ?? "",
})
}
}
return obj
}, {})
}
;(async () => {
console.log(`
recheck results:
> Go to https://makenowjust-labo.github.io/recheck/ for more details
> or if you want to double-check the matched regexes.
---
`)
const files = await getFiles(globPattern)
console.log(`Checking ${files.length} files...`)
console.log()
await loopFiles(files)
})()