fix: npm command parser to extract package names

This commit is contained in:
abhisek
2025-05-14 15:52:49 +05:30
parent ce193f8dcb
commit 86a0a8f5a4
3 changed files with 57 additions and 20 deletions
+24 -3
View File
@@ -29,6 +29,7 @@ pnpm add <package-name>
- [Binaries](#binaries)
- [Build from Source](#build-from-source)
- [Usage](#usage)
- [Debugging](#debugging)
- [PMG in Action](#pmg-in-action)
- [Malicious Package Detection](#malicious-package-detection)
- [Bulk Package Analysis](#bulk-package-analysis)
@@ -49,10 +50,10 @@ PMG supports the following package ecosystems:
| --------- | --------- | --------------------------- |
| NPM | ✅ Active | `pmg npm install <package>` |
| PNPM | ✅ Active | `pmg pnpm add <package>` |
| PyPI | 🚧 Planned | Coming soon |
| Go | 🚧 Planned | Coming soon |
| PyPI | 🚧 Planned | |
| Go | 🚧 Planned | |
Want us to support your favorite package manager? [Open an issue](https://github.com/safedep/pmg/issues) and let us know!
> Want us to support your favorite package manager? [Open an issue](https://github.com/safedep/pmg/issues) and let us know!
## Installation
@@ -84,6 +85,26 @@ alias npm="pmg npm"
alias pnpm="pmg pnpm"
```
Continue using your favorite package manager as usual:
```bash
npm install <package-name>
```
```bash
pnpm add <package-name>
```
### Debugging
Use the `--debug` flag to enable debug mode:
```bash
pmg --debug npm install <package-name>
```
### PMG in Action
#### Malicious Package Detection
+13 -16
View File
@@ -59,23 +59,24 @@ func (npm *npmPackageManager) ParseCommand(args []string) (*ParsedCommand, error
}, nil
}
// Check if this is an install command
if !npm.isInstallCommand(args[0]) {
return &ParsedCommand{
Command: command,
}, nil
}
// Extract packages from args
var packages []string
for i := 1; i < len(args); i++ {
arg := args[i]
if !strings.HasPrefix(arg, "-") {
packages = append(packages, arg)
for idx, arg := range args {
if slices.Contains(npm.Config.InstallCommands, arg) {
// All subsequent args are packages except for flags
for i := idx + 1; i < len(args); i++ {
if strings.HasPrefix(args[i], "-") {
continue
}
packages = append(packages, args[i])
}
break
}
}
// No packages specified
// No packages found
if len(packages) == 0 {
return &ParsedCommand{
Command: command,
@@ -112,10 +113,6 @@ func (npm *npmPackageManager) ParseCommand(args []string) (*ParsedCommand, error
}, nil
}
func (npm *npmPackageManager) isInstallCommand(cmd string) bool {
return slices.Contains(npm.Config.InstallCommands, cmd)
}
func npmParsePackageInfo(input string) (packageName, version string, err error) {
if input == "" {
return "", "", fmt.Errorf("package info cannot be empty")
+20 -1
View File
@@ -44,7 +44,7 @@ func TestNpmParseCommand(t *testing.T) {
},
{
name: "install a development package with short flag",
command: "npm i -D @types/node",
command: "npm i @types/node -D",
assert: func(t *testing.T, parsedCommand *ParsedCommand, err error) {
assert.NoError(t, err)
assert.Equal(t, 1, len(parsedCommand.InstallTargets))
@@ -78,6 +78,25 @@ func TestNpmParseCommand(t *testing.T) {
assert.Equal(t, 0, len(parsedCommand.InstallTargets))
},
},
{
name: "skip intermediate flags",
command: "npm --x -y install @types/node",
assert: func(t *testing.T, parsedCommand *ParsedCommand, err error) {
assert.NoError(t, err)
assert.Equal(t, 1, len(parsedCommand.InstallTargets))
assert.Equal(t, "@types/node", parsedCommand.InstallTargets[0].PackageVersion.Package.Name)
},
},
{
name: "multiple development packages",
command: "npm i @types/node @types/react -D",
assert: func(t *testing.T, parsedCommand *ParsedCommand, err error) {
assert.NoError(t, err)
assert.Equal(t, 2, len(parsedCommand.InstallTargets))
assert.Equal(t, "@types/node", parsedCommand.InstallTargets[0].PackageVersion.Package.Name)
assert.Equal(t, "@types/react", parsedCommand.InstallTargets[1].PackageVersion.Package.Name)
},
},
}
for _, tc := range cases {