Skip to content

Commit 7555dc6

Browse files
fix: introduce lint command (#26)
1 parent f5b9772 commit 7555dc6

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

src/commands/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import './serve'
44
import './favicon'
55
import './config'
66
import './update'
7+
import './lint'
78
import { argv } from 'yargs'
89

910
argv

src/commands/lint.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { command } from 'yargs'
2+
import { logInfo, log } from '../utilities/log'
3+
import { Linter, Configuration, ILinterOptions } from 'tslint'
4+
import * as fs from 'fs'
5+
import chalk from 'chalk'
6+
import { resolve } from 'path'
7+
8+
const fileName = resolve('src/server/server.ts')
9+
const configurationFilename = resolve('tslint.json')
10+
const options: ILinterOptions = {
11+
fix: false,
12+
formatter: 'json'
13+
}
14+
15+
const fileContents = fs.readFileSync(fileName, 'utf8')
16+
const linter = new Linter(options)
17+
const configuration = Configuration.findConfiguration(
18+
configurationFilename,
19+
fileName
20+
).results
21+
linter.lint(fileName, fileContents, configuration)
22+
23+
command(
24+
'lint',
25+
'check your code quality',
26+
args => {
27+
return args
28+
},
29+
args => {
30+
lint()
31+
}
32+
)
33+
34+
function warnings(count: number) {
35+
const str = count.toString()
36+
return count > 0
37+
? chalk.underline(chalk.bgYellow(` ${str} `))
38+
: chalk.underline(` ${str} `)
39+
}
40+
41+
function errors(count: number) {
42+
const str = count.toString()
43+
return count > 0
44+
? chalk.underline(chalk.bgRed(` ${str} `))
45+
: chalk.underline(` ${str} `)
46+
}
47+
48+
function lint() {
49+
logInfo('Linter\n')
50+
const result = linter.getResult()
51+
log(` Errors: `, errors(result.errorCount))
52+
log(`Warnings: `, warnings(result.warningCount), '\n')
53+
54+
result.failures.map(obj => obj.toJson()).forEach(json => {
55+
log(
56+
`${json.ruleSeverity}(${json.ruleName}): ${json.name} (${
57+
json.startPosition.line
58+
}, ${json.startPosition.character}): ${json.failure}`
59+
)
60+
})
61+
62+
log('\n')
63+
}

src/utilities/log.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export function logErrorWithPrefix(msg: any) {
1414
log(chalk.bgRedBright(`Error: ${msg}`))
1515
}
1616

17-
export function logInfo(msg: any) {
17+
export function logInfo(msg: string) {
1818
log(chalk.blue(msg))
1919
}
2020

0 commit comments

Comments
 (0)