-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
limitlines.js
executable file
·110 lines (95 loc) · 2.94 KB
/
limitlines.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
#!/usr/bin/env node
'use strict'
const countLinesInFile = require('count-lines-in-file')
const globby = require('globby')
const path = require('path')
const chalk = require('chalk')
const program = require('commander')
const version = require('./package.json').version
let maxErrors = 0
let minLinesByFile = 1
let maxLinesByFile = 300
let scanPaths = ['.']
const list = (val) => {
return val.split(', ')
}
program
.version(version, '-v, --version')
.option(
'--maxlines <maxlines>',
`Maximum lines by file. Default: ${maxLinesByFile}`,
parseInt,
)
.option(
'--minlines <minlines>',
`Minimum lines by file. Default: ${minLinesByFile}`,
parseInt,
)
.option(
'--maxerrors <maxerrors>',
`Maximum errors accept. Default: ${maxErrors}`,
parseInt,
)
.option('--path <path>', `Globby paths to scan. Default: ${scanPaths}`, list)
.option('--ignore <ignore>', 'Globby paths to ignore', list)
.parse(process.argv)
program.maxerrors && (maxErrors = program.maxerrors)
program.maxlines && (maxLinesByFile = program.maxlines)
program.minlines && (minLinesByFile = program.minlines)
program.path && (scanPaths = program.path)
if (program.ignore) {
program.ignore.forEach((path) => {
scanPaths.push(`!${path}`)
})
}
scanPaths.push('!node_modules')
globby(scanPaths)
.then((paths) => {
let totalLines = 0
let totalErrors = 0
let currentTotalFiles = 0
console.log(chalk.inverse('[Init Limit Lines]'))
console.log(
chalk.inverse.underline(
'Docs: https://github.com/tiagoporto/limit-lines',
),
)
paths.forEach((file) => {
countLinesInFile(file, (error, numberOfLines) => {
currentTotalFiles += 1
totalLines += numberOfLines
if (error) {
console.error(error)
}
const message = `${numberOfLines} ${chalk.underline(
path.resolve(file),
)}`
if (numberOfLines > maxLinesByFile || numberOfLines < minLinesByFile) {
totalErrors += 1
console.log(chalk.reset('Lines by file:'), chalk.red(message))
}
if (currentTotalFiles === paths.length) {
console.log('')
console.log(chalk(`Total Files: ${paths.length}`))
console.log(chalk(`Total Lines: ${totalLines}`))
console.log(chalk(`Min lines by file: ${minLinesByFile}`))
console.log(chalk(`Max lines by file: ${maxLinesByFile}`))
let color = 'reset'
totalErrors > maxErrors && (color = 'red')
totalErrors > 0 && totalErrors <= maxErrors && (color = 'yellow')
console.log(
chalk[color](
`Max Errors: ${maxErrors} Founded Errors: ${totalErrors}`,
),
)
if (color !== 'red') {
console.log(chalk.green('Limit Lines Passed'))
}
if (totalErrors > maxErrors) {
process.exit(1)
}
}
})
})
})
.catch((err) => console.error(err))