Skip to content

Commit

Permalink
refactor: Add package manager lib to keep track of support
Browse files Browse the repository at this point in the history
  • Loading branch information
lili2311 committed May 21, 2019
1 parent 721c38c commit 2cc3f64
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 45 deletions.
19 changes: 5 additions & 14 deletions src/cli/commands/protect/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,7 @@ var snyk = require('../../../lib/');
var protect = require('../../../lib/protect');
var analytics = require('../../../lib/analytics');
var detect = require('../../../lib/detect');
var unsupportedPackageManagers = {
rubygems: 'RubyGems',
maven: 'Maven',
pip: 'Python',
sbt: 'SBT',
gradle: 'Gradle',
golangdep: 'Golang/Dep',
govendor: 'Govendor',
nuget: 'NuGet',
composer: 'Composer',
};
var pm = require('../../../lib/package-managers');


function protectFunc(options = {}) {
Expand All @@ -31,10 +21,11 @@ function protectFunc(options = {}) {

try {
var packageManager = detect.detectPackageManager(process.cwd(), options);
var unsupported = unsupportedPackageManagers[packageManager];
if (unsupported) {
var supportsProtect = pm.PROTECT_SUPPORTED_PACKAGE_MANAGERS
.includes(packageManager);
if (!supportsProtect) {
throw new Error(
'Snyk protect for ' + unsupported +
'Snyk protect for ' + pm.SUPPORTED_PACKAGE_MANAGER_NAME[packageManager] +
' projects is not currently supported');
}
} catch (error) {
Expand Down
8 changes: 4 additions & 4 deletions src/cli/commands/protect/wizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ const detect = require('../../../lib/detect');
const plugins = require('../../../lib/plugins');
const moduleInfo = require('../../../lib/module-info');
const {MissingTargetFileError} = require('../../../lib/errors/missing-targetfile-error');

const wizardSupportedPackageManagers = ['npm', 'yarn'];
const pm = require('../../../lib/package-managers');

function wizard(options = {}) {
options.org = options.org || config.org || null;
Expand All @@ -47,10 +46,11 @@ function wizard(options = {}) {
async function processPackageManager(options) {
const packageManager = detect.detectPackageManager(cwd, options);

const supportsWizard = wizardSupportedPackageManagers.includes(packageManager);
const supportsWizard = pm.WIZARD_SUPPORTED_PACKAGE_MANAGERS
.includes(packageManager);
if (!supportsWizard) {
return Promise.reject(
`Snyk wizard for ${packageManager} projects is not currently supported`);
`Snyk wizard for ${pm.SUPPORTED_PACKAGE_MANAGER_NAME[packageManager]} projects is not currently supported`);
}

return fs.exists(path.join('.', 'node_modules'))
Expand Down
9 changes: 6 additions & 3 deletions src/lib/detect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import * as pathLib from 'path';
import * as debugLib from 'debug';
import * as _ from 'lodash';
import { NoSupportedManifestsFoundError } from './errors';
import { SupportedPackageManagers } from './package-managers';

const debug = debugLib('snyk');
const debug = debugLib('snyk-detect');

const DETECTABLE_FILES = [
const DETECTABLE_FILES: string[] = [
'yarn.lock',
'package-lock.json',
'package.json',
Expand All @@ -28,7 +29,9 @@ const DETECTABLE_FILES = [
];

// when file is specified with --file, we look it up here
const DETECTABLE_PACKAGE_MANAGERS = {
const DETECTABLE_PACKAGE_MANAGERS: {
[name: string]: SupportedPackageManagers;
} = {
'Gemfile': 'rubygems',
'Gemfile.lock': 'rubygems',
'.gemspec': 'rubygems',
Expand Down
25 changes: 25 additions & 0 deletions src/lib/package-managers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
export type SupportedPackageManagers = 'rubygems' | 'npm' | 'yarn' |
'maven' | 'pip' | 'sbt' | 'gradle' | 'golangdep' | 'govendor' |
'nuget' | 'paket' | 'composer';

export const SUPPORTED_PACKAGE_MANAGER_NAME: {
readonly [packageManager in SupportedPackageManagers]: string;
} = {
rubygems: 'RubyGems',
npm: 'npm',
yarn: 'Yarn',
maven: 'Maven',
pip: 'pip',
sbt: 'SBT',
gradle: 'Gradle',
golangdep: 'dep (Go)',
govendor: 'govendor',
nuget: 'NuGet',
paket: 'Paket',
composer: 'Composer',
};

export const WIZARD_SUPPORTED_PACKAGE_MANAGERS: SupportedPackageManagers[]
= ['yarn', 'npm'];
export const PROTECT_SUPPORTED_PACKAGE_MANAGERS: SupportedPackageManagers[]
= ['yarn', 'npm'];
4 changes: 3 additions & 1 deletion src/lib/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import * as nugetPlugin from 'snyk-nuget-plugin';
import * as phpPlugin from 'snyk-php-plugin';
import * as nodejsPlugin from './nodejs-plugin';
import * as types from './types';
import {SupportedPackageManagers} from '../package-managers';

export function loadPlugin(packageManager: string, options: types.Options = {}): types.Plugin {
export function loadPlugin(packageManager: SupportedPackageManagers,
options: types.Options = {}): types.Plugin {
if (options.docker) {
return dockerPlugin;
}
Expand Down
16 changes: 2 additions & 14 deletions src/lib/snyk-test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module.exports = test;
var detect = require('../detect');
var runTest = require('./run-test');
var chalk = require('chalk');
var pm = require('../package-managers');

function test(root, options, callback) {
if (typeof options === 'function') {
Expand Down Expand Up @@ -47,20 +48,7 @@ function executeTest(root, options) {

function run(root, options) {
var packageManager = options.packageManager;
if (!options.docker && [
'npm',
'yarn',
'rubygems',
'maven',
'gradle',
'sbt',
'pip',
'golangdep',
'govendor',
'nuget',
'paket',
'composer',
].indexOf(packageManager) === -1) {
if (!(options.docker || pm.SUPPORTED_PACKAGE_MANAGER_NAME[packageManager])) {
throw new Error('Unsupported package manager: ' + packageManager);
}
return runTest(packageManager, root, options);
Expand Down
3 changes: 2 additions & 1 deletion src/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as depGraphLib from '@snyk/dep-graph';
import { SupportedPackageManagers } from './package-managers';

// TODO(kyegupov): use a shared repository snyk-cli-interface

Expand Down Expand Up @@ -66,7 +67,7 @@ export interface TestOptions {
'project-name'?: string;
'show-vulnerable-paths'?: string;
showVulnPaths?: boolean;
packageManager: string;
packageManager: SupportedPackageManagers;
advertiseSubprojectsCount?: number;
subProjectNames?: string[];
severityThreshold?: string;
Expand Down
16 changes: 8 additions & 8 deletions test/acceptance/cli.acceptance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2663,18 +2663,18 @@ test('`wizard` for unsupported package managers', async (t) => {
const cases = [
{ file: 'ruby-app/Gemfile.lock', type: 'RubyGems' },
{ file: 'maven-app/pom.xml', type: 'Maven' },
{ file: 'pip-app/requirements.txt', type: 'Python' },
{ file: 'pip-app/requirements.txt', type: 'pip' },
{ file: 'sbt-app/build.sbt', type: 'SBT' },
{ file: 'gradle-app/build.gradle', type: 'Gradle' },
{ file: 'gradle-kotlin-dsl-app/build.gradle.kts', type: 'Gradle' },
{ file: 'golang-app/Gopkg.lock', type: 'Golang/Dep' },
{ file: 'golang-app/vendor/vendor.json', type: 'Govendor' },
{ file: 'golang-app/Gopkg.lock', type: 'dep (Go)' },
{ file: 'golang-app/vendor/vendor.json', type: 'govendor' },
{ file: 'composer-app/composer.lock', type: 'Composer' },
];
const results = await Promise.all(cases.map(testUnsupported));
results.map((result, i) => {
const type = cases[i].type;
t.match(result, 'Snyk wizard for ' + type +
t.equal(result, 'Snyk wizard for ' + type +
' projects is not currently supported', type);
});
});
Expand All @@ -2692,18 +2692,18 @@ test('`protect` for unsupported package managers', async (t) => {
const cases = [
{ file: 'ruby-app/Gemfile.lock', type: 'RubyGems' },
{ file: 'maven-app/pom.xml', type: 'Maven' },
{ file: 'pip-app/requirements.txt', type: 'Python' },
{ file: 'pip-app/requirements.txt', type: 'pip' },
{ file: 'sbt-app/build.sbt', type: 'SBT' },
{ file: 'gradle-app/build.gradle', type: 'Gradle' },
{ file: 'gradle-kotlin-dsl-app/build.gradle.kts', type: 'Gradle' },
{ file: 'golang-app/Gopkg.lock', type: 'Golang/Dep' },
{ file: 'golang-app/vendor/vendor.json', type: 'Govendor' },
{ file: 'golang-app/Gopkg.lock', type: 'dep (Go)' },
{ file: 'golang-app/vendor/vendor.json', type: 'govendor' },
{ file: 'composer-app/composer.lock', type: 'Composer' },
];
const results = await Promise.all(cases.map(testUnsupported));
results.map((result, i) => {
const type = cases[i].type;
t.match(result.message, 'Snyk protect for ' + type +
t.equal(result.message, 'Snyk protect for ' + type +
' projects is not currently supported', type);
});
});
Expand Down

0 comments on commit 2cc3f64

Please sign in to comment.