diff --git a/src/commands/index.ts b/src/commands/index.ts index fcba5e2..f78f7e4 100644 --- a/src/commands/index.ts +++ b/src/commands/index.ts @@ -4,6 +4,7 @@ import './serve' import './favicon' import './config' import './update' +import './lint' import { argv } from 'yargs' argv diff --git a/src/commands/lint.ts b/src/commands/lint.ts new file mode 100644 index 0000000..57ff3a3 --- /dev/null +++ b/src/commands/lint.ts @@ -0,0 +1,63 @@ +import { command } from 'yargs' +import { logInfo, log } from '../utilities/log' +import { Linter, Configuration, ILinterOptions } from 'tslint' +import * as fs from 'fs' +import chalk from 'chalk' +import { resolve } from 'path' + +const fileName = resolve('src/server/server.ts') +const configurationFilename = resolve('tslint.json') +const options: ILinterOptions = { + fix: false, + formatter: 'json' +} + +const fileContents = fs.readFileSync(fileName, 'utf8') +const linter = new Linter(options) +const configuration = Configuration.findConfiguration( + configurationFilename, + fileName +).results +linter.lint(fileName, fileContents, configuration) + +command( + 'lint', + 'check your code quality', + args => { + return args + }, + args => { + lint() + } +) + +function warnings(count: number) { + const str = count.toString() + return count > 0 + ? chalk.underline(chalk.bgYellow(` ${str} `)) + : chalk.underline(` ${str} `) +} + +function errors(count: number) { + const str = count.toString() + return count > 0 + ? chalk.underline(chalk.bgRed(` ${str} `)) + : chalk.underline(` ${str} `) +} + +function lint() { + logInfo('Linter\n') + const result = linter.getResult() + log(` Errors: `, errors(result.errorCount)) + log(`Warnings: `, warnings(result.warningCount), '\n') + + result.failures.map(obj => obj.toJson()).forEach(json => { + log( + `${json.ruleSeverity}(${json.ruleName}): ${json.name} (${ + json.startPosition.line + }, ${json.startPosition.character}): ${json.failure}` + ) + }) + + log('\n') +} diff --git a/src/utilities/log.ts b/src/utilities/log.ts index 2c31697..5b1b573 100644 --- a/src/utilities/log.ts +++ b/src/utilities/log.ts @@ -14,7 +14,7 @@ export function logErrorWithPrefix(msg: any) { log(chalk.bgRedBright(`Error: ${msg}`)) } -export function logInfo(msg: any) { +export function logInfo(msg: string) { log(chalk.blue(msg)) }