From 53be4e756f72d7a5a492222a6e3670ea73a04a24 Mon Sep 17 00:00:00 2001 From: Yoav Sabag Date: Wed, 21 Feb 2024 11:19:13 +0200 Subject: [PATCH] fix: yarn argument value identified as a package (#166) Fixes #156 In case of Number, handle it as Argument and skip this word. After the fix: ![Screen Shot 2024-02-18 at 21 42 13](https://github.com/os-scar/overlay/assets/6542413/02c26ac7-62d4-4ba0-886b-3a3b1ec0df6b) --------- Co-authored-by: Baruch Odem (Rothkoff) --- src/content/registry/npm.js | 24 ++++++++++++++----- src/content/registry/npm.test.js | 1 + .../real-examples/real-examples-results.yaml | 10 ++++++++ tests/real-examples/real-examples.yaml | 4 ++++ 4 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/content/registry/npm.js b/src/content/registry/npm.js index f53a15f..2c41170 100644 --- a/src/content/registry/npm.js +++ b/src/content/registry/npm.js @@ -20,12 +20,24 @@ const parsePackageString = (str) => { }; }; -const parseNpmCommand = createParseCommand( - 'npm', - (line) => line.match(npmInstall), - (word) => word.length + 1, - parsePackageString -); +const npmOptionWithArgToIgnore = ['--network-timeout', '--network-concurrency']; + +//npm +const handleArgument = (argument, restCommandWords) => { + let index = 0; + index += argument.length + 1; // +1 for the space removed by split + + if (!npmOptionWithArgToIgnore.includes(argument)) { + return index; + } + + if (argument.includes('=')) return index; + + index += restCommandWords.shift().length + 1; + return index; +}; + +const parseNpmCommand = createParseCommand('npm', (line) => line.match(npmInstall), handleArgument, parsePackageString); // npx const parseOnlyFirstPackage = (str, argsAndPackagesWords) => { diff --git a/src/content/registry/npm.test.js b/src/content/registry/npm.test.js index 97c58ff..5fc202a 100644 --- a/src/content/registry/npm.test.js +++ b/src/content/registry/npm.test.js @@ -20,6 +20,7 @@ describe('npm', () => { 'npm install -g', '`npm install node-sass`', // this is not a valid command because of the ` 'npm create-react-app my-app', + 'yarn add --network-timeout 100000', ])('should return empty array if no packages found', (command) => { expect(parseCommand(command)).toStrictEqual([]); }); diff --git a/tests/real-examples/real-examples-results.yaml b/tests/real-examples/real-examples-results.yaml index 526373d..2fbed30 100644 --- a/tests/real-examples/real-examples-results.yaml +++ b/tests/real-examples/real-examples-results.yaml @@ -424,6 +424,16 @@ https://stackoverflow.com/questions/3608384: type: npm name: jade +https://stackoverflow.com/questions/51508364: + comment: ignore 'yarn add --network-timeout 100000' + post: answer + registry: npm + results: + - range: |- + npx create-react-app app --use-npm + + type: npm + name: create-react-app https://stackoverflow.com/questions/12008719: comment: npm install --save post: answer diff --git a/tests/real-examples/real-examples.yaml b/tests/real-examples/real-examples.yaml index 296cbd7..7bc4ba5 100644 --- a/tests/real-examples/real-examples.yaml +++ b/tests/real-examples/real-examples.yaml @@ -27,6 +27,10 @@ links: comment: ignore 'npm install -d' & find '/usr/local/bin/npm install jade' post: answer registry: npm + https://stackoverflow.com/questions/51508364: + comment: ignore 'yarn add --network-timeout 100000' + post: answer + registry: npm https://stackoverflow.com/questions/12008719: comment: npm install --save post: answer