Skip to content

Commit

Permalink
feat: move gettings deps into plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed Dec 27, 2019
1 parent 6a427e0 commit 23f0baa
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 172 deletions.
4 changes: 1 addition & 3 deletions src/lib/monitor/count-total-deps-in-tree.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {
DepTree,
} from '../types';
import { DepTree } from '../types';

export function countTotalDependenciesInTree(depTree: DepTree): number {
let count = 0;
Expand Down
4 changes: 1 addition & 3 deletions src/lib/monitor/drop-empty-deps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {
DepTree,
} from '../types';
import { DepTree } from '../types';

export function dropEmptyDeps(depTree: DepTree) {
if (depTree.dependencies) {
Expand Down
4 changes: 1 addition & 3 deletions src/lib/monitor/filter-out-missing-deps.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {
DepTree,
} from '../types';
import { DepTree } from '../types';

interface FilteredDepTree {
filteredDepTree: DepTree;
Expand Down
6 changes: 1 addition & 5 deletions src/lib/monitor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,7 @@ import * as os from 'os';
import * as _ from 'lodash';
import { isCI } from '../is-ci';
import * as analytics from '../analytics';
import {
DepTree,
MonitorMeta,
MonitorResult,
} from '../types';
import { DepTree, MonitorMeta, MonitorResult } from '../types';
import * as projectMetadata from '../project-metadata';
import * as path from 'path';
import {
Expand Down
4 changes: 1 addition & 3 deletions src/lib/monitor/prune-dep-tree.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import * as depGraphLib from '@snyk/dep-graph';
import {
DepTree,
} from '../types';
import { DepTree } from '../types';

export async function pruneTree(
tree: DepTree,
Expand Down
79 changes: 79 additions & 0 deletions src/lib/plugins/get-deps-from-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { legacyPlugin as pluginApi } from '@snyk/cli-interface';
import detect = require('../../lib/detect');
import { TestOptions } from '../types';
import { Options } from '../types';
import { NoSupportedManifestsFoundError } from '../errors';
import { find } from '../find-files';
import { AUTO_DETECTABLE_FILES } from '../detect';
import { getSinglePluginResult } from '../plugins/get-single-plugin-result';
import { getMultiPluginResult } from '../plugins/get-multi-plugin-result';

// tslint:disable-next-line:no-var-requires
const debug = require('debug')('snyk');

// Force getDepsFromPlugin to return scannedProjects for processing
export async function getDepsFromPlugin(
root: string,
options: Options & TestOptions,
): Promise<pluginApi.MultiProjectResult> {
let inspectRes: pluginApi.InspectResult;

if (options.allProjects) {
// auto-detect only one-level deep for now
const targetFiles = await find(root, [], AUTO_DETECTABLE_FILES, 1);
debug(
`auto detect manifest files, found ${targetFiles.length}`,
targetFiles,
);
if (targetFiles.length === 0) {
throw NoSupportedManifestsFoundError([root]);
}
inspectRes = await getMultiPluginResult(root, options, targetFiles);
return inspectRes;
} else {
// TODO: is this needed for the auto detect handling above?
// don't override options.file if scanning multiple files at once
if (!options.scanAllUnmanaged) {
options.file = options.file || detect.detectPackageFile(root);
}
if (!options.docker && !(options.file || options.packageManager)) {
throw NoSupportedManifestsFoundError([...root]);
}
inspectRes = await getSinglePluginResult(root, options);
}
if (!pluginApi.isMultiResult(inspectRes)) {
if (!inspectRes.package) {
// something went wrong if both are not present...
throw Error(
`error getting dependencies from ${options.packageManager} ` +
"plugin: neither 'package' nor 'scannedProjects' were found",
);
}
if (!inspectRes.package.targetFile && inspectRes.plugin) {
inspectRes.package.targetFile = inspectRes.plugin.targetFile;
}
// We are using "options" to store some information returned from plugin that we need to use later,
// but don't want to send to Registry in the Payload.
// TODO(kyegupov): decouple inspect and payload so that we don't need this hack
if (
inspectRes.plugin.meta &&
inspectRes.plugin.meta.allSubProjectNames &&
inspectRes.plugin.meta.allSubProjectNames.length > 1
) {
options.advertiseSubprojectsCount =
inspectRes.plugin.meta.allSubProjectNames.length;
}
return {
plugin: inspectRes.plugin,
scannedProjects: [{ depTree: inspectRes.package }],
};
} else {
// We are using "options" to store some information returned from plugin that we need to use later,
// but don't want to send to Registry in the Payload.
// TODO(kyegupov): decouple inspect and payload so that we don't need this hack
(options as any).projectNames = inspectRes.scannedProjects.map(
(scannedProject) => scannedProject.depTree.name,
);
return inspectRes;
}
}
73 changes: 73 additions & 0 deletions src/lib/plugins/get-multi-plugin-result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import * as _ from 'lodash';
import * as cliInterface from '@snyk/cli-interface';

import pathUtil = require('path');
import { TestOptions, Options } from '../types';
import { detectPackageManagerFromFile } from '../../lib/detect';
import { SupportedPackageManagers } from '../package-managers';
import { getSinglePluginResult } from './get-single-plugin-result';

export interface ScannedProjectCustom
extends cliInterface.legacyCommon.ScannedProject {
packageManager: SupportedPackageManagers;
}

export async function getMultiPluginResult(
root: string,
options: Options & TestOptions,
targetFiles: string[],
): Promise<cliInterface.legacyPlugin.MultiProjectResult> {
const allResults: ScannedProjectCustom[] = [];

for (const targetFile of targetFiles) {
const optionsClone = _.cloneDeep(options);
optionsClone.file = pathUtil.basename(targetFile);
optionsClone.packageManager = detectPackageManagerFromFile(
optionsClone.file,
);
try {
const inspectRes = await getSinglePluginResult(root, optionsClone);
let resultWithScannedProjects: cliInterface.legacyPlugin.MultiProjectResult;

if (!cliInterface.legacyPlugin.isMultiResult(inspectRes)) {
resultWithScannedProjects = {
plugin: inspectRes.plugin,
scannedProjects: [
{
depTree: inspectRes.package,
targetFile: inspectRes.plugin.targetFile,
meta: inspectRes.meta,
},
],
};
} else {
resultWithScannedProjects = inspectRes;
}

// annotate the package manager, project name & targetFile to be used
// for test & monitor
// TODO: refactor how we display meta to not have to do this
(options as any).projectNames = resultWithScannedProjects.scannedProjects.map(
(scannedProject) => scannedProject.depTree.name,
);
const customScannedProject: ScannedProjectCustom[] = resultWithScannedProjects.scannedProjects.map(
(a) => {
(a as ScannedProjectCustom).targetFile = optionsClone.file;
(a as ScannedProjectCustom).packageManager =
optionsClone.packageManager;
return a as ScannedProjectCustom;
},
);
allResults.push(...customScannedProject);
} catch (err) {
console.log(err);
}
}

return {
plugin: {
name: 'custom-auto-detect',
},
scannedProjects: allResults,
};
}
18 changes: 18 additions & 0 deletions src/lib/plugins/get-single-plugin-result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import plugins = require('.');
import { ModuleInfo } from '../module-info';
import { legacyPlugin as pluginApi } from '@snyk/cli-interface';
import { TestOptions, Options } from '../types';

export async function getSinglePluginResult(
root: string,
options: Options & TestOptions,
): Promise<pluginApi.InspectResult> {
const plugin = plugins.loadPlugin(options.packageManager, options);
const moduleInfo = ModuleInfo(plugin, options.policy);
const inspectRes: pluginApi.InspectResult = await moduleInfo.inspect(
root,
options.file,
{ ...options },
);
return inspectRes;
}

0 comments on commit 23f0baa

Please sign in to comment.