Skip to content

Commit

Permalink
[Security Solution] modify circular deps checker to output images of …
Browse files Browse the repository at this point in the history
…circular deps graphs (elastic#75579)
  • Loading branch information
Robert Austin committed Aug 21, 2020
1 parent 01170a4 commit 75b6a2e
Showing 1 changed file with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -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 - ')}`
);
Expand All @@ -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}`);
}
}

0 comments on commit 75b6a2e

Please sign in to comment.