Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Aug 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`);
}
}