-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
executable file
·110 lines (91 loc) · 2.77 KB
/
index.mjs
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
#!/usr/bin/env node
import { collectBlocks, executeBlocks } from '@doctes/core'
import { parseArgs } from 'node:util'
import { readdirSync, statSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
// mostly inspired by this article: https://coderrocketfuel.com/article/recursively-list-all-the-files-in-a-directory-using-node-js
async function getAllFiles (dirPath, arrayOfFiles) {
const files = readdirSync(dirPath)
arrayOfFiles = arrayOfFiles || []
files.forEach(function (file) {
if (statSync(`${dirPath}/${file}`).isDirectory()) {
arrayOfFiles = getAllFiles(`${dirPath}/${file}`, arrayOfFiles)
} else {
arrayOfFiles.push(fileURLToPath(new URL(`${dirPath}/${file}`, import.meta.url)))
}
})
return arrayOfFiles
}
const options = {
json: {
type: 'boolean'
},
dir: {
type: 'string'
},
file: {
type: 'string'
},
silent: {
type: 'boolean'
}
}
if (!process.argv.includes('--dir') && !process.argv.includes('--file')) {
process.argv.push('--dir=./')
}
const { values } = parseArgs({ options })
async function execute () {
if (values.file && values.dir) {
console.log('Please choose either a file or a directory, not both.')
process.exitCode(1)
}
if (values.file && !values.dir) {
const blocks = await collectBlocks(values.file)
const executedBlocks = await executeBlocks(blocks)
logCheck(executedBlocks, values.file, values)
}
if (values.dir && !values.file) {
const normalizedDir = fileURLToPath(new URL(values.dir, import.meta.url))
const files = await getAllFiles(normalizedDir)
for (const file of files) {
const blocks = await collectBlocks(file)
const executedBlocks = await executeBlocks(blocks)
logCheck(executedBlocks, file, values)
}
}
}
async function logCheck (executedBlocks, filename, values) {
const results = {
pass: [],
fail: []
}
for (const result of executedBlocks) {
if (result.exitCode === 0) {
results.pass.push({ line: result.line, filename })
} else if (result.exitCode === 1) {
results.fail.push({ line: result.line, filename })
}
}
if (values.json && values.silent) {
throw new Error('You cannot use the --json and --silent flags together.')
}
if (values.json === true) { // output JSON if
console.log(JSON.stringify(results, null, 2))
}
// TODO: rework styling to be nicer
if (values.silent !== true) {
console.log(`\n${results.pass.length} passed, ${results.fail.length} failed\n`)
if (results.fail.length !== 0) {
console.log('Failed checks:')
for (const fail of results.fail) {
console.log(`- ${fail.filename}:${fail.line}`)
}
}
}
if (results.fail.length !== 0) {
process.exitCode = 1
} else {
process.exitCode = 0
}
}
await execute()