axe-sarif-converter
Version:
Convert axe-core accessibility scan results to the SARIF format
101 lines (100 loc) • 3.93 kB
JavaScript
;
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
var fs = require("fs");
var yargs = require("yargs");
var _1 = require(".");
var argv = yargs
.scriptName('axe-sarif-converter')
.version() // inferred from package.json
.usage('$0: Converts JSON files containing axe-core Result object(s) into SARIF files')
.example('$0 -i axe-results.json -o axe-results.sarif', '')
.option('input-files', {
alias: 'i',
describe: 'Input JSON file(s) containing axe-core Result object(s). Does not support globs. Each input file may consist of either a single root-level axe-core Results object or a root-level array of axe-core Results objects.',
demandOption: true,
type: 'string',
})
.array('input-files')
.option('output-file', {
alias: 'o',
describe: 'Output SARIF file. Multiple input files (or input files containing multiple Result objects) will be combined into one output file with a SARIF Run per axe-core Result.',
demandOption: true,
type: 'string',
})
.option('verbose', {
alias: 'v',
describe: 'Enables verbose console output.',
default: false,
type: 'boolean',
})
.option('pretty', {
alias: 'p',
describe: 'Includes line breaks and indentation in the output.',
default: false,
type: 'boolean',
})
.option('force', {
alias: 'f',
describe: 'Overwrites the output file if it already exists.',
default: false,
type: 'boolean',
})
.parseSync();
var verboseLog = argv.verbose ? console.log : function () { };
function exitWithErrorMessage(message) {
console.error(message);
process.exit(1);
}
function flatten(nestedArray) {
return nestedArray.reduce(function (accumulator, next) { return accumulator.concat(next); }, []);
}
var sarifLogs = flatten(argv['input-files'].map(function (inputFilePath, index) {
verboseLog("Reading input file ".concat(index + 1, "/").concat(argv['input-files'].length, " ").concat(inputFilePath));
// tslint:disable-next-line: non-literal-fs-path
var rawInputFileContents = fs.readFileSync(inputFilePath);
var inputFileJson = JSON.parse(rawInputFileContents.toString());
if (Array.isArray(inputFileJson)) {
// Treating as array of axe results, like @axe-core/cli produces
return inputFileJson.map(_1.convertAxeToSarif);
}
else {
// Treating as a single axe results object, like
// JSON.stringify(await axe.run(...)) would produce
return [(0, _1.convertAxeToSarif)(inputFileJson)];
}
}));
verboseLog("Aggregating converted input file(s) into one SARIF log");
var combinedLog = __assign(__assign({}, sarifLogs[0]), { runs: flatten(sarifLogs.map(function (log) { return log.runs; })) });
verboseLog("Formatting SARIF data into file contents");
var jsonSpacing = argv.pretty ? 2 : undefined;
var outputFileContent = JSON.stringify(combinedLog, null, jsonSpacing);
verboseLog("Writing output file ".concat(argv['output-file']));
try {
// tslint:disable-next-line: non-literal-fs-path
fs.writeFileSync(argv['output-file'], outputFileContent, {
flag: argv.force ? 'w' : 'wx',
});
}
catch (e) {
if (typeof e === 'object' && e != null && e.code === 'EEXIST') {
exitWithErrorMessage("Error: EEXIST: Output file ".concat(argv['output-file'], " already exists. Did you mean to use --force?"));
}
else {
throw e;
}
}
verboseLog("Done");