UNPKG

@atomist/sdm

Version:

Atomist Software Delivery Machine SDK

124 lines 4.87 kB
"use strict"; /* * Copyright © 2020 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. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.TslintAutoInspectRegistration = exports.TslintInspection = exports.RunTslintOnProject = exports.mapTslintResultsToReviewComments = exports.tsLintReviewCategory = void 0; const LocalProject_1 = require("@atomist/automation-client/lib/project/local/LocalProject"); const logger_1 = require("@atomist/automation-client/lib/util/logger"); const appRoot = require("app-root-path"); const path = require("path"); const child_process_1 = require("../../../api-helper/misc/child_process"); const tsPushTests_1 = require("../pushtest/tsPushTests"); 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) { logger_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. */ const RunTslintOnProject = async (p) => { const review = { repoId: p.id, comments: [] }; const tslintJson = "tslint.json"; const tslintConfigFile = await 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 (!LocalProject_1.isLocalProject(p)) { logger_1.logger.error(`Project ${p.name} is not a local project`); return review; } const cwd = p.baseDir; logger_1.logger.debug(`Running ${tslintExe} using ${tslintConfig} on ${p.name} in ${cwd}`); const tslintArgs = ["--config", tslintConfig, "--format", "json", "--project", cwd, "--force"]; try { const tslintResult = await child_process_1.execPromise(tslintExe, tslintArgs, { cwd }); if (tslintResult.stderr) { logger_1.logger.debug(`TSLint standard error from ${p.name}: ${tslintResult.stderr}`); } const comments = mapTslintResultsToReviewComments(tslintResult.stdout, p.baseDir); review.comments.push(...comments); } catch (e) { logger_1.logger.error(`Failed to run TSLint: ${e.message}`); } return review; }; exports.RunTslintOnProject = RunTslintOnProject; /** * 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", }; /** * Provide an auto inspect registration that runs TSLint and returns a * ProjectReview. */ exports.TslintAutoInspectRegistration = { name: "TSLintAutoInspection", inspection: exports.RunTslintOnProject, pushTest: tsPushTests_1.IsTypeScript, }; //# sourceMappingURL=tslint.js.map