0
0
Fork 0
mirror of https://github.com/DavidAnson/markdownlint-cli2-action.git synced 2024-12-22 21:03:53 +01:00
markdownlint-cli2-action/markdownlint-cli2-action.js

95 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-01-13 07:10:33 +01:00
// @ts-check
"use strict";
const core = require("@actions/core");
const {"main": markdownlintCli2} = require("markdownlint-cli2");
2021-01-13 07:10:33 +01:00
const logMessage = core.info;
const outputFormatter = (options) => {
const {results} = options;
for (const lintError of results) {
const {
errorContext,
errorDetail,
errorRange,
fileName,
lineNumber,
ruleDescription,
ruleInformation,
ruleNames
} = lintError;
const line = `:${lineNumber}`;
const column = errorRange ? `:${errorRange[0]}` : "";
const name = ruleNames.join("/");
const detail = errorDetail ? ` [${errorDetail}]` : "";
const context = errorContext ? ` [Context: "${errorContext}"]` : "";
const information = ruleInformation ? ` ${ruleInformation}` : "";
const message =
// eslint-disable-next-line max-len
`${fileName}${line}${column} ${name} ${ruleDescription}${detail}${context}${information}`;
const annotation = {
"title": ruleDescription,
"file": fileName,
"startLine": lineNumber,
"endLine": lineNumber
};
if (errorRange) {
const [
errorColumn,
errorLength
] = errorRange;
annotation.startColumn = errorColumn;
annotation.endColumn = errorColumn + errorLength - 1;
}
core.error(message, annotation);
}
};
const separator = core.getInput("separator") || "\n";
const argv =
core.getInput("globs").
split(separator).
filter(String);
const config = core.getInput("config");
if (config) {
argv.push("--config", config);
}
const fix = Boolean(core.getInput("fix"));
if (fix) {
argv.push("--fix");
}
let invoke = true;
const command = core.getInput("command");
switch (command) {
case "":
// Default behavior
break;
case "config":
argv.unshift("--config");
break;
case "fix":
argv.unshift("--fix");
break;
default:
core.setFailed(`Unsupported command: ${command}`);
invoke = false;
break;
}
if (invoke) {
const parameters = {
argv,
logMessage,
"optionsOverride": {
"outputFormatters": [[outputFormatter]]
}
};
markdownlintCli2(parameters).then(
(code) => code && core.setFailed(`Failed with exit code: ${code}`),
(error) => core.setFailed(`Failed due to error: ${error}`)
);
}