|
| 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 | +} |
0 commit comments