forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTslintRunner.ts
More file actions
83 lines (75 loc) · 2.76 KB
/
TslintRunner.ts
File metadata and controls
83 lines (75 loc) · 2.76 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as path from 'path';
import { ITerminalProvider } from '@rushstack/node-core-library';
import * as TSLint from 'tslint';
import { CmdRunner } from './CmdRunner';
import { ToolPaths } from './ToolPaths';
import { ILintRunnerConfig } from './ILintRunnerConfig';
import {
RushStackCompilerBase,
WriteFileIssueFunction
} from './RushStackCompilerBase';
/**
* @public
*/
export interface ITslintRunnerConfig extends ILintRunnerConfig { }
/**
* @beta
*/
export class TslintRunner extends RushStackCompilerBase<ITslintRunnerConfig> {
private _cmdRunner: CmdRunner;
public constructor(taskOptions: ITslintRunnerConfig, rootPath: string, terminalProvider: ITerminalProvider) {
super(taskOptions, rootPath, terminalProvider);
this._cmdRunner = new CmdRunner(
this._standardBuildFolders,
this._terminal,
{
packagePath: ToolPaths.tslintPackagePath,
packageJson: ToolPaths.tslintPackageJson,
packageBinPath: path.join('bin', 'tslint')
}
);
}
public invoke(): Promise<void> {
const args: string[] = [
'--format', 'json',
'--project', this._standardBuildFolders.projectFolderPath
];
return this._cmdRunner.runCmd({
args: args,
onData: (data: Buffer) => {
const dataStr: string = data.toString().trim();
const tslintErrorLogFn: WriteFileIssueFunction = this._taskOptions.displayAsError
? this._taskOptions.fileError
: this._taskOptions.fileWarning;
// TSLint errors are logged to stdout
try {
const errors: TSLint.IRuleFailureJson[] = JSON.parse(dataStr);
for (const error of errors) {
const pathFromRoot: string = path.relative(this._standardBuildFolders.projectFolderPath, error.name);
tslintErrorLogFn(
pathFromRoot,
error.startPosition.line + 1,
error.startPosition.character + 1,
error.ruleName,
error.failure
);
}
} catch (e) {
// If we fail to parse the JSON, it's likely TSLint encountered an error parsing the config file,
// or it experienced an inner error. In this case, log the output as an error regardless of the
// displayAsError value
this._terminal.writeErrorLine(dataStr);
}
},
onClose: (code: number, hasErrors: boolean, resolve: () => void, reject: (error: Error) => void) => {
if (this._taskOptions.displayAsError && (code !== 0 || hasErrors)) {
reject(new Error(`exited with code ${code}`));
} else {
resolve();
}
}
});
}
}