[Security Solution] modify circular deps checker to output images of circular deps graphs (#75579) (#75660)

This commit is contained in:
Robert Austin 2020-08-21 12:50:15 -04:00 committed by GitHub
parent 9ba8942c2c
commit 622bf2df9d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,17 +4,17 @@
* you may not use this file except in compliance with the Elastic License.
*/
import { resolve } from 'path';
/* eslint-disable-next-line import/no-extraneous-dependencies */
import madge from 'madge';
/* eslint-disable-next-line import/no-extraneous-dependencies */
import { run, createFailError } from '@kbn/dev-utils';
import * as os from 'os';
import * as path from 'path';
run(
async ({ log }) => {
async ({ log, flags }) => {
const result = await madge(
[resolve(__dirname, '../../public'), resolve(__dirname, '../../common')],
[path.resolve(__dirname, '../../public'), path.resolve(__dirname, '../../common')],
{
fileExtensions: ['ts', 'js', 'tsx'],
excludeRegExp: [
@ -34,6 +34,13 @@ run(
const circularFound = result.circular();
if (circularFound.length !== 0) {
if (flags.svg) {
await outputSVGs(circularFound);
} else {
console.log(
'Run this program with the --svg flag to save an SVG showing the dependency graph.'
);
}
throw createFailError(
`SIEM circular dependencies of imports has been found:\n - ${circularFound.join('\n - ')}`
);
@ -42,6 +49,34 @@ run(
}
},
{
description: 'Check the SIEM plugin for circular deps',
description:
'Check the Security Solution plugin for circular deps. If any are found, this will throw an Error.',
flags: {
help: ' --svg, Output SVGs of circular dependency graphs',
boolean: ['svg'],
default: {
svg: false,
},
},
}
);
async function outputSVGs(circularFound) {
let count = 0;
for (const found of circularFound) {
// Calculate the path using the os tmpdir and an increasing 'count'
const expectedImagePath = path.join(os.tmpdir(), `security_solution-circular-dep-${count}.svg`);
console.log(`Attempting to save SVG for circular dependency: ${found}`);
count++;
// Graph just the files in the found circular dependency.
const specificGraph = await madge(found, {
fileExtensions: ['ts', 'js', 'tsx'],
});
// Output an SVG in the tmp directory
const imagePath = await specificGraph.image(expectedImagePath);
console.log(`Saved SVG: ${imagePath}`);
}
}