snyk-mvn-plugin
Version:
Snyk CLI Maven plugin
94 lines • 3.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseDigraphs = parseDigraphs;
const maven_graph_builder_1 = require("./maven-graph-builder");
const dependency_1 = require("./dependency");
const DEFAULT_PARSE_OPTIONS = { mavenVerboseIncludeAllVersions: false };
const newLine = /[\r\n]+/g;
function parseDigraphs(digraphs, options = DEFAULT_PARSE_OPTIONS) {
const graphs = [];
for (const digraph of digraphs) {
const lines = digraph.split(newLine);
const rootId = findQuotedContents(options, lines[0]);
if (!rootId) {
throw new Error(`Unexpected digraph could not find root node. Could not parse "${lines[0]}".`);
}
const builder = new maven_graph_builder_1.MavenGraphBuilder(rootId);
for (let i = 1; i < lines.length - 1; i++) {
const line = parseLine(options, lines[i]);
if (!line) {
throw new Error(`Unexpected digraph could not connect nodes. Could not parse "${lines[i]}".`);
}
builder.connect(line.from, line.to);
}
graphs.push(builder.graph);
}
return graphs;
}
function parseLine(options, line) {
if (!line)
return null;
const [left, right] = line.split('->');
if (!left || !right)
return null;
const from = findQuotedContents(options, left);
const to = findQuotedContents(options, right);
if (!from || !to)
return null;
return { from, to };
}
function findQuotedContents(options, value) {
if (!value)
return null;
const start = value.indexOf('"') + 1;
const end = value.lastIndexOf('"');
if (options.mavenVerboseIncludeAllVersions === false &&
isVerboseVersionOmittedForConflict(value)) {
const [ommitedDep, conflictString] = value
.substring(start + (value[start] == '(' ? 1 : 0), end)
.split(/ [-(]/);
const resolvedVersion = conflictString
.split('omitted for conflict with ')[1]
.split(')')[0];
const parsedDep = (0, dependency_1.parseDependency)(ommitedDep);
const resolvedDep = {
...parsedDep,
version: resolvedVersion,
};
return (0, dependency_1.buildDependencyString)(resolvedDep);
}
if (isVerbose(value)) {
const [left] = value
.substring(start + (value[start] == '(' ? 1 : 0), end)
.split(/ [-(]/);
return left;
}
return value.substring(start, end);
}
function isVerbose(value) {
// clear string of leading and trailing non letters
// when using -Dverbose ensure omitted reasons are parsed correctly
// https://github.com/apache/maven/blob/ab6ec5bd74af20ab429509eb56fc8e3dff4c7fc7/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultNode.java#L113
const dverboseReasons = [
'version managed from',
'omitted for duplicate',
'omitted for conflict with',
'scope not updated to compile',
'scope not updated to runtime',
'scope not updated to test',
];
for (const dverboseReason of dverboseReasons) {
if (value.includes(dverboseReason))
return true;
}
return false;
}
function isVerboseVersionOmittedForConflict(value) {
const dverboseReasons = ['omitted for conflict with'];
for (const dverboseReason of dverboseReasons) {
if (value.includes(dverboseReason))
return true;
}
return false;
}
//# sourceMappingURL=digraph.js.map