Skip to content

Commit

Permalink
feat: simplify multi scan check with a function
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed Aug 25, 2020
1 parent 812dc4d commit d02a212
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 8 deletions.
3 changes: 2 additions & 1 deletion src/cli/commands/monitor/index.ts
Expand Up @@ -43,6 +43,7 @@ import {
execShell,
} from '../../../lib/monitor/dev-count-analysis';
import { FailedToRunTestError, MonitorError } from '../../../lib/errors';
import { isMultiProjectScan } from '../../../lib/is-multi-project-scan';

const SEPARATOR = '\n-------------------------------------------------------\n';
const debug = Debug('snyk');
Expand Down Expand Up @@ -120,7 +121,7 @@ async function monitor(...args0: MethodArgs): Promise<any> {
validateMonitorPath(path, options.docker);
let analysisType = 'all';
let packageManager;
if (options.allProjects || options.yarnWorkspaces) {
if (isMultiProjectScan(options)) {
analysisType = 'all';
} else if (options.docker) {
analysisType = 'docker';
Expand Down
4 changes: 2 additions & 2 deletions src/cli/commands/test/index.ts
Expand Up @@ -50,6 +50,7 @@ import * as utils from './utils';
import { getIacDisplayedOutput } from './iac-output';
import { getEcosystem, testEcosystem } from '../../../lib/ecosystems';
import { TestLimitReachedError } from '../../../lib/errors';
import { isMultiProjectScan } from '../../../lib/is-multi-project-scan';

const debug = Debug('snyk-test');
const SEPARATOR = '\n-------------------------------------------------------\n';
Expand Down Expand Up @@ -449,8 +450,7 @@ function displayResult(
}
const advertiseAllProjectsCount =
projectType !== 'gradle' &&
!options.allProjects &&
!options.yarnWorkspaces &&
!isMultiProjectScan(options) &&
foundProjectCount;
if (advertiseAllProjectsCount) {
multiProjAdvice = chalk.bold.white(
Expand Down
7 changes: 7 additions & 0 deletions src/lib/is-multi-project-scan.ts
@@ -0,0 +1,7 @@
import { Options, TestOptions, MonitorOptions, PolicyOptions } from './types';

export function isMultiProjectScan(
options: Partial<Options & TestOptions & MonitorOptions & PolicyOptions>,
): boolean {
return !!(options.allProjects || options.yarnWorkspaces);
}
3 changes: 2 additions & 1 deletion src/lib/options-validator.ts
Expand Up @@ -2,14 +2,15 @@ import { MonitorOptions, Options, TestOptions } from './types';
import * as config from './config';
import { SupportedPackageManagers } from './package-managers';
import * as reachableVulns from './reachable-vulns';
import { isMultiProjectScan } from './is-multi-project-scan';

export async function validateOptions(
options: (Options & TestOptions) | (Options & MonitorOptions),
packageManager?: SupportedPackageManagers,
): Promise<void> {
if (options.reachableVulns) {
// Throwing error only in case when both packageManager and allProjects not defined
if (!packageManager && !options.allProjects && !options.yarnWorkspaces) {
if (!packageManager && !isMultiProjectScan(options)) {
throw new Error('Could not determine package manager');
}
const org = options.org || config.org;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/reachable-vulns.ts
Expand Up @@ -12,6 +12,7 @@ import {
UnsupportedFeatureFlagError,
} from './errors';
import { MonitorOptions, Options, TestOptions } from './types';
import { isMultiProjectScan } from './is-multi-project-scan';

const featureFlag = 'reachableVulns';

Expand All @@ -36,8 +37,7 @@ export async function validatePayload(
): Promise<boolean> {
if (
packageManager &&
!options.allProjects &&
!options.yarnWorkspaces &&
!isMultiProjectScan(options) &&
!REACHABLE_VULNS_SUPPORTED_PACKAGE_MANAGERS.includes(packageManager)
) {
throw new FeatureNotSupportedByPackageManagerError(
Expand Down
4 changes: 2 additions & 2 deletions src/lib/snyk-test/index.js
Expand Up @@ -9,6 +9,7 @@ const {
UnsupportedPackageManagerError,
NotSupportedIacFileError,
} = require('../errors');
const { isMultiProjectScan } = require('../is-multi-project-scan');

async function test(root, options, callback) {
if (typeof options === 'function') {
Expand Down Expand Up @@ -69,8 +70,7 @@ function validateProjectType(options, projectType) {
if (
!(
options.docker ||
options.allProjects ||
options.yarnWorkspaces ||
isMultiProjectScan(options) ||
pm.SUPPORTED_PACKAGE_MANAGER_NAME[projectType]
)
) {
Expand Down

0 comments on commit d02a212

Please sign in to comment.