Skip to content

Commit

Permalink
refactor: remove special npm/yarn path in snyk-test
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Yegupov committed May 9, 2019
1 parent 8a1393a commit 73e96aa
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 544 deletions.
7 changes: 3 additions & 4 deletions src/lib/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import * as dockerPlugin from 'snyk-docker-plugin';
import * as npmPlugin from './npm';
import * as rubygemsPlugin from './rubygems';
import * as mvnPlugin from 'snyk-mvn-plugin';
import * as gradlePlugin from 'snyk-gradle-plugin';
import * as sbtPlugin from 'snyk-sbt-plugin';
import * as yarnPlugin from './yarn';
import * as pythonPlugin from 'snyk-python-plugin';
import * as goPlugin from 'snyk-go-plugin';
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';

export function loadPlugin(packageManager: string, options: types.Options = {}): types.Plugin {
Expand All @@ -18,7 +17,7 @@ export function loadPlugin(packageManager: string, options: types.Options = {}):

switch (packageManager) {
case 'npm': {
return npmPlugin;
return nodejsPlugin;
}
case 'rubygems': {
return rubygemsPlugin;
Expand All @@ -33,7 +32,7 @@ export function loadPlugin(packageManager: string, options: types.Options = {}):
return sbtPlugin;
}
case 'yarn': {
return yarnPlugin;
return nodejsPlugin;
}
case 'pip': {
return pythonPlugin;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,4 @@ This directory contains a logic for parsing Node.js projects (npm and Yarn).

Historically, Node.js support was the first to be implemented in Snyk and thus it was built in.
All other languages and platforms are supported via plugins.
This code will also eventually move to a plugin.

See also src/lib/plugins/npm/index.ts
This code will also eventually move to a separate plugin repository.
19 changes: 19 additions & 0 deletions src/lib/plugins/nodejs-plugin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import * as modulesParser from './npm-modules-parser';
import * as lockParser from './npm-lock-parser';
import * as types from '../types';

export async function inspect(root: string, targetFile: string, options: types.Options = {}):
Promise<types.InspectResult> {
const isLockFileBased = (targetFile.endsWith('package-lock.json') || targetFile.endsWith('yarn.lock'));

const getLockFileDeps = isLockFileBased && !options.traverseNodeModules;
return {
plugin: {
name: 'snyk-nodejs-lockfile-parser',
runtime: process.version,
},
package: getLockFileDeps ?
await lockParser.parse(root, targetFile, options) :
await modulesParser.parse(root, targetFile, options),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import * as analytics from '../../analytics';
import * as fs from 'fs';
import * as lockFileParser from 'snyk-nodejs-lockfile-parser';
import {PkgTree} from 'snyk-nodejs-lockfile-parser';
import {Options} from '../types';

export async function parse(root, targetFile, options): Promise<PkgTree> {
export async function parse(root: string, targetFile: string, options: Options): Promise<PkgTree> {
const lockFileFullPath = path.resolve(root, targetFile);
if (!fs.existsSync(lockFileFullPath)) {
throw new Error('Lockfile ' + targetFile + ' not found at location: ' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import * as spinner from '../../spinner';
import * as analytics from '../../analytics';
import * as fs from 'then-fs';
import {PkgTree} from 'snyk-nodejs-lockfile-parser';
import {Options} from '../types';

export async function parse(root, targetFile, options): Promise<PkgTree> {
export async function parse(root: string, targetFile: string, options: Options): Promise<PkgTree> {
const nodeModulesPath = path.join(
path.dirname(path.resolve(root, targetFile)),
'node_modules',
Expand All @@ -21,18 +22,18 @@ export async function parse(root, targetFile, options): Promise<PkgTree> {
lockFile: false,
targetFile,
});
options.root = root;

const resolveModuleSpinnerLabel = 'Analyzing npm dependencies for ' +
path.dirname(path.resolve(root, targetFile));
try {
await spinner(resolveModuleSpinnerLabel);
if (targetFile.endsWith('yarn.lock')) {
options.file = options.file.replace('yarn.lock', 'package.json');
options.file = options.file && options.file.replace('yarn.lock', 'package.json');
}

// package-lock.json falls back to package.json (used in wizard code)
if (targetFile.endsWith('package-lock.json')) {
options.file = options.file.replace('package-lock.json', 'package.json');
options.file = options.file && options.file.replace('package-lock.json', 'package.json');
}
return snyk.modules(
root, Object.assign({}, options, {noFromArrays: true}));
Expand Down
71 changes: 0 additions & 71 deletions src/lib/plugins/npm/index.ts

This file was deleted.

2 changes: 2 additions & 0 deletions src/lib/plugins/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ export interface InspectResult {
}

export interface Options {
file?: string;
docker?: boolean;
traverseNodeModules?: boolean;
dev?: boolean;
strictOutOfSync?: boolean | 'true' | 'false';
multiDepRoots?: boolean;
debug?: boolean;
packageManager?: string;
}

export interface Plugin {
Expand Down
85 changes: 0 additions & 85 deletions src/lib/plugins/yarn/index.ts

This file was deleted.

6 changes: 2 additions & 4 deletions src/lib/snyk-test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module.exports = test;
var detect = require('../detect');
var runTest = require('./run-test');
var chalk = require('chalk');
var nodejs = require('./nodejs');

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

function run(root, options) {
var packageManager = options.packageManager;
if (['npm', 'yarn'].indexOf(packageManager) >= 0) {
return nodejs(packageManager, root, options).then((res) => [res]);
}
if (!options.docker && [
'npm',
'yarn',
'rubygems',
'maven',
'gradle',
Expand Down
12 changes: 0 additions & 12 deletions src/lib/snyk-test/nodejs-plugin/index.ts

This file was deleted.

Loading

0 comments on commit 73e96aa

Please sign in to comment.