Skip to content

Commit

Permalink
feat: add --help to snyk protect
Browse files Browse the repository at this point in the history
feat: add --help to snyk protect

feat: add help flag to protect

feat: some fixes

feat: add help flag tests
  • Loading branch information
Avishag Israeli committed Jul 27, 2021
1 parent af4015f commit 7580e9f
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/snyk-protect/help.txt
@@ -0,0 +1,18 @@
$ snyk-protect --help

NAME
@snyk/protect - Patch vulnerable code in your project's dependencies.

DESCRIPTION
You don't typically need to add the @snyk/protect dependency manually.
For more information on how to setup and use @snyk/protect, visit:
https://github.com/snyk/snyk/tree/master/packages/snyk-protect

SYNOPSIS
snyk-protect [<options>]

OPTIONS
--help
Display this help text.
--version
Display the current version.
23 changes: 23 additions & 0 deletions packages/snyk-protect/src/lib/index.ts
Expand Up @@ -13,6 +13,16 @@ import { getAllPatches } from './fetch-patches';
import { sendAnalytics } from './analytics';

async function protect(projectFolderPath: string) {
// Handle runs with flags
if (process.argv.includes('--help')) {
console.log(getHelp());
return;
}
if (process.argv.includes('--version')) {
console.log(getVersion());
return;
}

const snykFilePath = path.resolve(projectFolderPath, '.snyk');

if (!fs.existsSync(snykFilePath)) {
Expand Down Expand Up @@ -92,4 +102,17 @@ async function protect(projectFolderPath: string) {
});
}

export function getHelp(): string {
const filePath = path.resolve(__dirname, '../../help.txt');
const helpText = fs.readFileSync(filePath, 'utf8');
return helpText;
}

export function getVersion() {
const filePath = path.resolve(__dirname, '../../package.json');
const rawData = fs.readFileSync(filePath, 'utf8');
const packageJSON = JSON.parse(rawData);
return packageJSON.version;
}

export default protect;
36 changes: 36 additions & 0 deletions packages/snyk-protect/test/acceptance/protect.spec.ts
@@ -1,6 +1,8 @@
import protect from '../../src/lib';
import { getVersion, getHelp } from '../../src/lib';
import { createProject } from '../util/createProject';
import { getPatchedLodash } from '../util/getPatchedLodash';
import { runCommand } from '../util/runCommand';
import * as http from '../../src/lib/http';
import * as analytics from '../../src/lib/analytics';
import * as path from 'path';
Expand Down Expand Up @@ -238,4 +240,38 @@ describe('@snyk/protect', () => {
});
});
});

describe('outputs correct help documentation', () => {
it('when called with --help', async () => {
const project = await createProject('help-flag');

const { code, stdout } = await runCommand(
'node',
[path.resolve(__dirname, '../../dist/index.js'), '--help'],
{
cwd: project.path(),
},
);

expect(stdout).toMatch(getHelp());
expect(code).toEqual(0);
});
});

describe('outputs correct version', () => {
it('when called with --version', async () => {
const project = await createProject('version-flag');

const { code, stdout } = await runCommand(
'node',
[path.resolve(__dirname, '../../dist/index.js'), '--version'],
{
cwd: project.path(),
},
);

expect(stdout).toMatch(getVersion());
expect(code).toEqual(0);
});
});
});
1 change: 1 addition & 0 deletions packages/snyk-protect/test/fixtures/help-flag/README.md
@@ -0,0 +1 @@
Test fixture for help flag
12 changes: 12 additions & 0 deletions packages/snyk-protect/test/fixtures/help-flag/package.json
@@ -0,0 +1,12 @@
{
"name": "help-flag",
"version": "1.0.1",
"description": "A test fixture",
"repository": {
"type": "git",
"url": "https://github.com/Snyk/snyk-todo-list-demo-app/"
},
"dependencies": {
},
"license": "Apache-2.0"
}
1 change: 1 addition & 0 deletions packages/snyk-protect/test/fixtures/version-flag/README.md
@@ -0,0 +1 @@
Test fixture for version flag
12 changes: 12 additions & 0 deletions packages/snyk-protect/test/fixtures/version-flag/package.json
@@ -0,0 +1,12 @@
{
"name": "version-flag",
"version": "1.0.1",
"description": "A test fixture",
"repository": {
"type": "git",
"url": "https://github.com/Snyk/snyk-todo-list-demo-app/"
},
"dependencies": {
},
"license": "Apache-2.0"
}

0 comments on commit 7580e9f

Please sign in to comment.