Skip to content

Commit

Permalink
feat: filter missing deps before graph conversion in monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
orsagie authored and dkontorovskyy committed Jul 30, 2019
1 parent e047043 commit 53ceb5f
Show file tree
Hide file tree
Showing 5 changed files with 5,913 additions and 5 deletions.
27 changes: 26 additions & 1 deletion src/lib/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,29 @@ async function pruneTree(tree: DepTree, packageManagerName: string): Promise<Dep
return tree;
}

function filterOutMissingDeps(depTree) {
const filteredDeps = {};
const missingDeps: string[] = [];

for (const depKey of Object.keys(depTree.dependencies)) {
const dep = depTree.dependencies[depKey];
if (dep.missingLockFileEntry) {
missingDeps.push(`${dep.name}@${dep.version}`);
} else {
filteredDeps[depKey] = dep;
}
}
const filteredDepTree = {
...depTree,
dependencies: filteredDeps,
};

return {
filteredDepTree,
missingDeps,
};
}

export async function monitor(
root: string,
meta: MonitorMeta,
Expand Down Expand Up @@ -205,8 +228,9 @@ export async function monitorGraph(
const pluginMeta = info.plugin;
const policyPath = meta['policy-path'] || root;
const policyLocations = [policyPath].concat(pluckPolicies(pkg)).filter(Boolean);
const {filteredDepTree, missingDeps} = filterOutMissingDeps(info.package);

const depGraph: depGraphLib.DepGraph = await depGraphLib.legacy.depTreeToGraph(info.package, packageManager);
const depGraph: depGraphLib.DepGraph = await depGraphLib.legacy.depTreeToGraph(filteredDepTree, packageManager);

// docker doesn't have a policy as it can be run from anywhere
if (!meta.isDocker || !policyLocations.length) {
Expand Down Expand Up @@ -251,6 +275,7 @@ export async function monitorGraph(
dockerfileLayers: pkg.docker ? pkg.docker.dockerfileLayers : undefined,
projectName: meta['project-name'],
prePruneDepCount, // undefined unless 'prune' is used
missingDeps,
},
policy: policy ? policy.toString() : undefined,
depGraphJSON: prunedGraph, // depGraph will be auto serialized to JSON on send
Expand Down
9 changes: 5 additions & 4 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,13 @@ export interface MonitorOptions {
}

export interface MonitorMeta {
'method': 'cli';
'packageManager': SupportedPackageManagers;
method: 'cli';
missingDeps?: string[];
packageManager: SupportedPackageManagers;
'policy-path': string;
'project-name': string;
'isDocker': boolean;
'prune': boolean;
isDocker: boolean;
prune: boolean;
'experimental-dep-graph'?: boolean;
}

Expand Down
11 changes: 11 additions & 0 deletions test/acceptance/cli.acceptance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2362,6 +2362,17 @@ test('`monitor npm-package`', async (t) => {
t.notOk(req.body.meta.prePruneDepCount, "doesn't send meta.prePruneDepCount");
});

test('`monitor npm-out-of-sync`', async (t) => {
chdirWorkspaces();
await cli.monitor('npm-out-of-sync', { 'experimental-dep-graph': true, 'strict': false });
const req = server.popRequest();
t.match(req.url, '/monitor/npm/graph', 'puts at correct url');
t.ok(req.body.depGraphJSON, 'sends depGraphJSON');
t.notOk(req.body.depGraphJSON.getPkgs().find(
(pkg) => pkg.name === 'body-parser'), 'filetered out missingLockFileEntry');
t.equals(req.body.meta.missingDeps, ['body-parser@1.18.2'], 'missingDeps passed');
});

test('`monitor npm-package-pruneable --prune-repeated-subdependencies`', async (t) => {
chdirWorkspaces();

Expand Down
Loading

0 comments on commit 53ceb5f

Please sign in to comment.