Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support npm-init and npx commands #89

Merged
merged 14 commits into from
Jun 11, 2023
10 changes: 7 additions & 3 deletions src/content/registry/npm.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
import { createParseCommand } from './shared';

const npmInstall = /(npm|yarn)( -g)?( global)? (install|i|add|update) /;
const npmInstall = /((npm|yarn)( -g)?( global)? (install|i|add|update))\s/;
hagarfisher marked this conversation as resolved.
Show resolved Hide resolved
const npmInit = /(npx|npm init)\b/;
const packageName = /(?<package_name>[a-z0-9_@][a-z0-9_./-]*)/;
const packageVersion = /@(?<package_version>[~^]?\d+(\.(\d|X|x)+){0,2}(-[a-z0-9_-]+)?)/;
const packageLabel = /@[a-z0-9_-]+/;
const fullPackage = new RegExp(String.raw`^${packageName.source}(${packageVersion.source}|${packageLabel.source})?$`);

const parsePackageString = (str) => {
const parsePackageString = (str, baseCommand, packagesLength) => {
if ((baseCommand === 'npx' || baseCommand === 'npm init') && packagesLength > 0) {
hagarfisher marked this conversation as resolved.
Show resolved Hide resolved
return null;
}
const match = str.match(fullPackage);
if (!match) return null;
return {
Expand All @@ -18,7 +22,7 @@ const parsePackageString = (str) => {

export const parseCommand = createParseCommand(
'npm',
(line) => line.match(npmInstall),
(line) => line.match(npmInstall) || line.match(npmInit),
(word) => word.length + 1,
parsePackageString
);
Expand Down
4 changes: 2 additions & 2 deletions src/content/registry/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const finishAllWords = (argsAndPackagesWords) => {
* @param {string} registryName `type` in result element
* @param {(line: string) => RegExpMatchArray} getBaseCommandMatch get the base command match for a line (examples: `pip install`, `yarn add`)
* @param {(word: string, argsAndPackagesWords: string[]) => number} handleArgument add a length to the counter to move it to the next argument
* @param {(word: string) => { packageName: string, packageVersion?: string, packagePart: string } | null } parsePackageWord parse a word to its package name and its whole part (includes the version)
* @param {(word: string, baseCommand?: string, packagesLength?: number) => { packageName: string, packageVersion?: string, packagePart: string } | null } parsePackageWord parse a word to its package name and its whole part (includes the version)
*/
export const createParseCommand =
(registryName, getBaseCommandMatch, handleArgument, parsePackageWord) =>
Expand Down Expand Up @@ -50,7 +50,7 @@ export const createParseCommand =
continue;
}

const packageMatch = parsePackageWord(word);
const packageMatch = parsePackageWord(word, baseCommandMatch[0], packages.length);
if (!packageMatch) {
counterIndex += word.length + 1;
continue;
Expand Down