Skip to content

Commit fd95b77

Browse files
authored
feat: preserve sha1 integrity and auto detect lockfile (#10)
* bug: added `--preserve-integrity` flag to prevent removal of integrity hash when `sha1` is used due to private repositories such as Azure Artifacts not supporting anything other than `sha1`. feat: removed need for `--lockfile` flag if a `yarn.lock` or `package-lock.json` exists. Defaults to `yarn.lock` if not found. You can still specify a lockfile if you wish. chore: updated eslint-plugin-import to ^2.31.0 for eslint 9 support * Tweaked the lockFileName detector code as per feedback * Changed isYarn check to be more explicit with filename
1 parent 5d53aa9 commit fd95b77

File tree

5 files changed

+268
-158
lines changed

5 files changed

+268
-158
lines changed

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,16 @@ It is recommended that you use this tool alongside the official Snyk CLI, not re
5252
### Options
5353

5454
```console
55-
snyker --retries 3 --lockfile package-lock.json
55+
snyker --retries 3 --lockfile package-lock.json --preserve-integrity
5656
```
5757

58-
| Flag | Description | Default |
59-
| --------------------- | ---------------------------------------------------------------------- | ----------- |
60-
| `--lockfile <string>` | Specify the lockfile to use (e.g. `yarn.lock` or `package-lock.json`). | `yarn.lock` |
61-
| `--retries <int>` | Will set the number of times to retry logical steps of Snyker. | `2` |
58+
| Flag | Description | Default |
59+
| ---------------------- | -------------------------------------------------------------------------| ----------- |
60+
| `--lockfile <string>` | Specify the lockfile to use (e.g. `yarn.lock` or `package-lock.json`). | Attempts to find a `yarn.lock` or `package-lock.json` then defaults to `yarn.lock` |
61+
| `--retries <int>` | Will set the number of times to retry logical steps of Snyker. | `2` |
62+
| `--preserve-integrity` | Will not attempt to update integrity hash when `sha1` is used. \* | `false` |
63+
64+
> \* It is highly recommended to use `sha512` for the integrity hash algorithm which is default for `npm`. However, when using private repositories such as Azure Artifacts, they do not support anything other than `sha1`. In turn, if the integrity is removed, the subsequent `npm install` command does not re-instate these. This flag is a workaround for this issue.
6265
6366
## Alternatives
6467

docs/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# ChangeLog
22

3+
## [5.1.0] - 27-11-2024
4+
5+
- feat: removed need for `--lockfile` flag if a `yarn.lock` or `package-lock.json` exists. Defaults to `yarn.lock` if not found. You can still specify a lockfile if you wish.
6+
- bug: added `--preserve-integrity` flag to prevent removal of integrity hash when `sha1` is used due to private repositories such as Azure Artifacts not supporting anything other than `sha1`.
7+
- chore: updated eslint-plugin-import to ^2.31.0 for eslint 9 support
8+
39
## [5.0.0] - 31-08-2024
410

511
- feat: upgrade dependencies to latest versions

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@asos/snyker",
3-
"version": "5.0.0",
3+
"version": "5.1.0",
44
"description": "An opinionated, heavy-handed wrapper around Snyk.",
55
"author": {
66
"name": "Craig Morten",
@@ -59,7 +59,7 @@
5959
"cross-env": "^7.0.3",
6060
"eslint": "^9.10.0",
6161
"eslint-config-prettier": "^9.1.0",
62-
"eslint-plugin-import": "^2.30.0",
62+
"eslint-plugin-import": "^2.31.0",
6363
"eslint-plugin-prettier": "^5.2.1",
6464
"prettier": "^3.3.3",
6565
"rimraf": "^6.0.1",

src/index.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const LARGE_BUFFER = 1024 * 1024 * 1024 * 20;
1010
const DEFAULT_RETRIES = 2;
1111

1212
let MAX_RETRIES;
13+
let PRESERVE_INTEGRITY = false;
1314

1415
const catchAndRetry = async (fn) => {
1516
for (let retries = 0; retries < MAX_RETRIES; retries++) {
@@ -123,7 +124,9 @@ const updateYarnLock = async ({ lockFileName, depsToForceUpdate }) => {
123124
*/
124125
const shaPatch = ({ integrity, ...rest }) => ({
125126
...rest,
126-
...(!integrity || integrity.startsWith("sha1-") ? {} : { integrity }),
127+
...(!integrity || (integrity.startsWith("sha1-") && !PRESERVE_INTEGRITY)
128+
? {}
129+
: { integrity }),
127130
});
128131

129132
/**
@@ -295,9 +298,19 @@ const snyker = async () => {
295298
console.log("[SNYKER: STARTING]");
296299

297300
MAX_RETRIES = argv.retries || DEFAULT_RETRIES;
298-
299-
const lockFileName = argv.lockfile || "yarn.lock";
300-
const isYarn = lockFileName.includes("yarn");
301+
PRESERVE_INTEGRITY = argv["preserve-integrity"] || false;
302+
303+
// We need to determine whether we're using Yarn or NPM
304+
// Prioritise "lockfile" flag, then check for yarn.lock, then package-lock.json
305+
// If none of these files exist, default to yarn.lock
306+
const lockFileName =
307+
argv.lockfile ||
308+
["yarn.lock", "package-lock.json"].find((file) =>
309+
fs.existsSync(path.join(process.cwd(), file)),
310+
) ||
311+
"yarn.lock";
312+
313+
const isYarn = lockFileName === "yarn.lock";
301314

302315
console.log(
303316
`[SNYKER: STEP 1]: Ensuring lockfile '${lockFileName}' is up to date.\n`,

0 commit comments

Comments
 (0)