diff --git a/packages/snyk-protect/help.txt b/packages/snyk-protect/help.txt new file mode 100644 index 00000000000..7eecc7d04ff --- /dev/null +++ b/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 + --help + Display this help text. + --version + Display the current version. \ No newline at end of file diff --git a/packages/snyk-protect/src/lib/index.ts b/packages/snyk-protect/src/lib/index.ts index 6c0442a01c2..d25b81740bd 100644 --- a/packages/snyk-protect/src/lib/index.ts +++ b/packages/snyk-protect/src/lib/index.ts @@ -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)) { @@ -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; diff --git a/packages/snyk-protect/test/acceptance/protect.spec.ts b/packages/snyk-protect/test/acceptance/protect.spec.ts index 7f2f1f35467..c335e748ecc 100644 --- a/packages/snyk-protect/test/acceptance/protect.spec.ts +++ b/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'; @@ -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); + }); + }); }); diff --git a/packages/snyk-protect/test/fixtures/help-flag/README.md b/packages/snyk-protect/test/fixtures/help-flag/README.md new file mode 100644 index 00000000000..05e0595793f --- /dev/null +++ b/packages/snyk-protect/test/fixtures/help-flag/README.md @@ -0,0 +1 @@ +Test fixture for help flag \ No newline at end of file diff --git a/packages/snyk-protect/test/fixtures/help-flag/package.json b/packages/snyk-protect/test/fixtures/help-flag/package.json new file mode 100644 index 00000000000..f0424027e8c --- /dev/null +++ b/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" +} diff --git a/packages/snyk-protect/test/fixtures/version-flag/README.md b/packages/snyk-protect/test/fixtures/version-flag/README.md new file mode 100644 index 00000000000..623c13e9d6d --- /dev/null +++ b/packages/snyk-protect/test/fixtures/version-flag/README.md @@ -0,0 +1 @@ +Test fixture for version flag \ No newline at end of file diff --git a/packages/snyk-protect/test/fixtures/version-flag/package.json b/packages/snyk-protect/test/fixtures/version-flag/package.json new file mode 100644 index 00000000000..3b2b98159a9 --- /dev/null +++ b/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" +}