-
Notifications
You must be signed in to change notification settings - Fork 567
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: snyk protect upgrade notification for test command
- Loading branch information
Showing
13 changed files
with
550 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { EOL } from 'os'; | ||
import * as theme from './theme'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as createDebug from 'debug'; | ||
|
||
const debug = createDebug('snyk-protect-update-notification'); | ||
|
||
export function getProtectUpgradeWarningForPaths( | ||
packageJsonPaths: string[], | ||
): string { | ||
try { | ||
if (packageJsonPaths?.length > 0) { | ||
let message = theme.color.status.warn( | ||
`${theme.icon.WARNING} WARNING: It looks like you have the \`snyk\` dependency in the \`package.json\` file(s) at the following path(s):` + | ||
EOL, | ||
); | ||
|
||
packageJsonPaths.forEach((p) => { | ||
message += theme.color.status.warn(` - ${p}` + EOL); | ||
}); | ||
|
||
const githubReadmeUrlShort = 'https://snyk.co/ud1cR'; // https://github.com/snyk/snyk/tree/master/packages/snyk-protect#migrating-from-snyk-protect-to-snykprotect | ||
|
||
message += theme.color.status.warn( | ||
`For more information and migration instructions, see ${githubReadmeUrlShort}` + | ||
EOL, | ||
); | ||
|
||
return message; | ||
} else { | ||
return ''; | ||
} | ||
} catch (e) { | ||
debug('Error in getProtectUpgradeWarningForPaths()', e); | ||
return ''; | ||
} | ||
} | ||
|
||
export function packageJsonFileExistsInDirectory( | ||
directoryPath: string, | ||
): boolean { | ||
try { | ||
const packageJsonPath = path.resolve(directoryPath, 'package.json'); | ||
const fileExists = fs.existsSync(packageJsonPath); | ||
return fileExists; | ||
} catch (e) { | ||
debug('Error in packageJsonFileExistsInDirectory()', e); | ||
return false; | ||
} | ||
} | ||
|
||
export function checkPackageJsonForSnykDependency( | ||
packageJsonPath: string, | ||
): boolean { | ||
try { | ||
const fileExists = fs.existsSync(packageJsonPath); | ||
if (fileExists) { | ||
const packageJson = fs.readFileSync(packageJsonPath, 'utf8'); | ||
const packageJsonObject = JSON.parse(packageJson); | ||
const snykDependency = packageJsonObject.dependencies['snyk']; | ||
if (snykDependency) { | ||
return true; | ||
} | ||
} | ||
} catch (e) { | ||
debug('Error in checkPackageJsonForSnykDependency()', e); | ||
} | ||
return false; | ||
} | ||
|
||
export function getPackageJsonPathsContainingSnykDependency( | ||
fileOption: string | undefined, | ||
paths: string[], | ||
): string[] { | ||
const packageJsonPathsWithSnykDepForProtect: string[] = []; | ||
|
||
try { | ||
if (fileOption) { | ||
if ( | ||
fileOption.endsWith('package.json') || | ||
fileOption.endsWith('package-lock.json') | ||
) { | ||
const directoryWithPackageJson = path.dirname(fileOption); | ||
if (packageJsonFileExistsInDirectory(directoryWithPackageJson)) { | ||
const packageJsonPath = path.resolve( | ||
directoryWithPackageJson, | ||
'package.json', | ||
); | ||
const packageJsonContainsSnykDep = checkPackageJsonForSnykDependency( | ||
packageJsonPath, | ||
); | ||
if (packageJsonContainsSnykDep) { | ||
packageJsonPathsWithSnykDepForProtect.push(packageJsonPath); | ||
} | ||
} | ||
} | ||
} else { | ||
paths.forEach((testPath) => { | ||
if (packageJsonFileExistsInDirectory(testPath)) { | ||
const packageJsonPath = path.resolve(testPath, 'package.json'); | ||
const packageJsonContainsSnykDep = checkPackageJsonForSnykDependency( | ||
packageJsonPath, | ||
); | ||
if (packageJsonContainsSnykDep) { | ||
packageJsonPathsWithSnykDepForProtect.push(packageJsonPath); | ||
} | ||
} | ||
}); | ||
} | ||
} catch (e) { | ||
debug('Error in getPackageJsonPathsContainingSnykDependency()', e); | ||
} | ||
|
||
return packageJsonPathsWithSnykDepForProtect; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
test/fixtures/protect-update-notification/no-package-json/placeholder.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
placeholder file so that the `no-package-json` folder which contains this file exists for testing |
33 changes: 33 additions & 0 deletions
33
.../fixtures/protect-update-notification/with-package-json-with-snyk-dep-2/package-lock.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
test/fixtures/protect-update-notification/with-package-json-with-snyk-dep-2/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"name": "test", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"dependencies": { | ||
"snyk": "^1.773.0" | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
test/fixtures/protect-update-notification/with-package-json-with-snyk-dep/package-lock.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
12 changes: 12 additions & 0 deletions
12
test/fixtures/protect-update-notification/with-package-json-with-snyk-dep/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "test", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"dependencies": { | ||
"snyk": "^1.773.0" | ||
} | ||
} | ||
|
12 changes: 12 additions & 0 deletions
12
...fixtures/protect-update-notification/with-package-json-without-snyk-dep/package-lock.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
10 changes: 10 additions & 0 deletions
10
test/fixtures/protect-update-notification/with-package-json-without-snyk-dep/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"name": "test", | ||
"version": "1.0.0", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"dependencies": { | ||
} | ||
} |
Oops, something went wrong.