-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move gettings deps into plugins
- Loading branch information
Showing
9 changed files
with
178 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.