@atomist/sdm-pack-node
Version:
Extension pack for an Atomist SDM to work with Node.js projects
124 lines • 4.92 kB
JavaScript
;
/*
* Copyright © 2019 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const automation_client_1 = require("@atomist/automation-client");
const sdm_1 = require("@atomist/sdm");
const appRoot = require("app-root-path");
const path = require("path");
exports.tsLintReviewCategory = "Tslint";
/**
* Return a review comment for a TSLint violation.
*/
function tslintReviewComment(detail, rule, severity = "info", sourceLocation) {
return {
category: exports.tsLintReviewCategory,
detail,
severity,
sourceLocation,
subcategory: rule,
};
}
/**
* Convert the JSON output of TSLint to proper ReviewComments. If any
* part of the process fails, an empty array is returned.
*
* @param tslintOutput string output from running `tslint` that will be parsed and converted.
* @return TSLint errors and warnings as ReviewComments
*/
function mapTslintResultsToReviewComments(tslintOutput, dir) {
let results;
try {
results = JSON.parse(tslintOutput);
}
catch (e) {
automation_client_1.logger.error(`Failed to parse TSLint output '${tslintOutput}': ${e.message}`);
return [];
}
const comments = results.map(r => {
const location = {
path: r.name.replace(dir + path.sep, ""),
offset: r.startPosition.position,
columnFrom1: r.startPosition.character + 1,
lineFrom1: r.startPosition.line + 1,
};
const severity = (r.ruleSeverity === "ERROR") ? "error" : "warn";
return tslintReviewComment(r.failure, r.ruleName, severity, location);
});
return comments;
}
exports.mapTslintResultsToReviewComments = mapTslintResultsToReviewComments;
/**
* Run TSLint on a project with a tslint.json file, using the standard
* version of TSLint and its configuration, i.e., the ones in this
* project. At most 20 TSLint violations are returned, since they are
* used to create a GitHub issue and if the body of that POST is too
* large, it is rejected.
*/
exports.RunTslintOnProject = (p) => __awaiter(this, void 0, void 0, function* () {
const review = { repoId: p.id, comments: [] };
const tslintJson = "tslint.json";
const tslintConfigFile = yield p.getFile(tslintJson);
if (!tslintConfigFile) {
return review;
}
const baseDir = appRoot.path;
const tslintExe = path.join(baseDir, "node_modules", ".bin", "tslint");
const tslintConfig = path.join(baseDir, tslintJson);
if (!automation_client_1.isLocalProject(p)) {
automation_client_1.logger.error(`Project ${p.name} is not a local project`);
return review;
}
const cwd = p.baseDir;
automation_client_1.logger.debug(`Running ${tslintExe} using ${tslintConfig} on ${p.name} in ${cwd}`);
const tslintArgs = [
"--config", tslintConfig,
"--format", "json",
"--project", cwd,
"--force",
];
try {
const tslintResult = yield sdm_1.execPromise(tslintExe, tslintArgs, { cwd });
if (tslintResult.stderr) {
automation_client_1.logger.debug(`TSLint standard error from ${p.name}: ${tslintResult.stderr}`);
}
const comments = mapTslintResultsToReviewComments(tslintResult.stdout, p.baseDir);
review.comments.push(...comments);
}
catch (e) {
automation_client_1.logger.error(`Failed to run TSLint: ${e.message}`);
}
return review;
});
/**
* Provide a code inspection that runs TSLint and returns a
* ProjectReview.
*/
exports.TslintInspection = {
name: "RunTSLint",
description: "Run TSLint on project",
inspection: exports.RunTslintOnProject,
intent: "ts lint",
};
//# sourceMappingURL=tslint.js.map