Skip to content

Commit

Permalink
fix: check if user specify package file name as part of path
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill89 committed Oct 8, 2018
1 parent 23fd0df commit 1138eb2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
13 changes: 13 additions & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {copy} from './copy';
import spinner = require('../lib/spinner');
import errors = require('../lib/error');
import ansiEscapes = require('ansi-escapes');
import {isPathToPackageFile} from '../lib/detect';

async function runCommand(args) {
const result = await args.method(...args.options._);
Expand Down Expand Up @@ -95,6 +96,17 @@ function checkRuntime() {
}
}

// Check if user specify package file name as part of path
// and throw error if so.
function checkPaths(args) {
for (const path of args.options._) {
if (typeof path === 'string' && isPathToPackageFile(path)) {
throw new Error(`Path "${path}" contains package file name, ` +
'please use --file=<string> instead.');
}
}
}

async function main() {
checkRuntime();

Expand All @@ -108,6 +120,7 @@ async function main() {
let failed = false;

try {
checkPaths(args);
res = await runCommand(args);
} catch (error) {
failed = true;
Expand Down
18 changes: 14 additions & 4 deletions src/lib/detect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as fs from 'then-fs';
import * as path from 'path';
import * as pathLib from 'path';
import * as debugLib from 'debug';
import chalk from 'chalk';
import * as _ from 'lodash';

const debug = debugLib('snyk');

Expand Down Expand Up @@ -47,6 +48,15 @@ const DETECTABLE_PACKAGE_MANAGERS = {
};
/*tslint:enable object-literal-sort-keys*/

export function isPathToPackageFile(path) {
for (const fileName of DETECTABLE_FILES) {
if (_.endsWith(path, fileName)) {
return true;
}
}
return false;
}

export function detectPackageManager(root, options) {
// If user specified a package manager let's use it.
if (options.packageManager) {
Expand Down Expand Up @@ -95,7 +105,7 @@ export function detectPackageManager(root, options) {
// User supplied a "local" file, but that file doesn't exist
function localFileSuppliedButNotFound(root, file) {
return file && fs.existsSync(root) &&
!fs.existsSync(path.resolve(root, file));
!fs.existsSync(pathLib.resolve(root, file));
}

function isLocalFolder(root) {
Expand All @@ -108,7 +118,7 @@ function isLocalFolder(root) {

export function detectPackageFile(root) {
for (const file of DETECTABLE_FILES) {
if (fs.existsSync(path.resolve(root, file))) {
if (fs.existsSync(pathLib.resolve(root, file))) {
debug('found package file ' + file + ' in ' + root);
return file;
}
Expand All @@ -118,7 +128,7 @@ export function detectPackageFile(root) {
}

function detectPackageManagerFromFile(file) {
let key = path.basename(file);
let key = pathLib.basename(file);

if (/\.gemspec$/.test(key)) {
key = '.gemspec';
Expand Down

0 comments on commit 1138eb2

Please sign in to comment.