Skip to content

Commit

Permalink
Merge pull request #572 from snyk/fix/drop-empty-dependencies
Browse files Browse the repository at this point in the history
fix: drop empty dependencies (payload size optimization)
  • Loading branch information
Konstantin Yegupov committed Jun 12, 2019
2 parents a87cede + dd4f623 commit 7a3f1cf
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/lib/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as os from 'os';
import * as _ from 'lodash';
import {isCI} from './is-ci';
import * as analytics from './analytics';
import { SingleDepRootResult, MonitorError } from './types';
import { SingleDepRootResult, MonitorError, DepTree } from './types';
import * as projectMetadata from './project-metadata';
import * as path from 'path';

Expand All @@ -15,7 +15,7 @@ import * as path from 'path';
interface MonitorBody {
meta: Meta;
policy: string;
package: {}; // TODO(kyegupov): DepTree
package: DepTree;
target: {};
targetFileRelativePath: string;
targetFile: string;
Expand All @@ -39,6 +39,19 @@ interface Meta {
projectName: string;
}

function dropEmptyDeps(node: DepTree) {
if (node.dependencies) {
const keys = Object.keys(node.dependencies);
if (keys.length === 0) {
delete node.dependencies;
} else {
for (const k of keys) {
dropEmptyDeps(node.dependencies[k]);
}
}
}
}

export async function monitor(root, meta, info: SingleDepRootResult, targetFile): Promise<any> {
apiTokenExists();
const pkg = info.package;
Expand All @@ -59,6 +72,8 @@ export async function monitor(root, meta, info: SingleDepRootResult, targetFile)
const target = await projectMetadata.getInfo(pkg);
const targetFileRelativePath = targetFile ? path.relative(root, targetFile) : '';

dropEmptyDeps(pkg);

// TODO(kyegupov): async/await
return new Promise((resolve, reject) => {
request({
Expand Down

0 comments on commit 7a3f1cf

Please sign in to comment.