mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
fix: npm command parser to extract package names
This commit is contained in:
@@ -29,6 +29,7 @@ pnpm add <package-name>
|
|||||||
- [Binaries](#binaries)
|
- [Binaries](#binaries)
|
||||||
- [Build from Source](#build-from-source)
|
- [Build from Source](#build-from-source)
|
||||||
- [Usage](#usage)
|
- [Usage](#usage)
|
||||||
|
- [Debugging](#debugging)
|
||||||
- [PMG in Action](#pmg-in-action)
|
- [PMG in Action](#pmg-in-action)
|
||||||
- [Malicious Package Detection](#malicious-package-detection)
|
- [Malicious Package Detection](#malicious-package-detection)
|
||||||
- [Bulk Package Analysis](#bulk-package-analysis)
|
- [Bulk Package Analysis](#bulk-package-analysis)
|
||||||
@@ -49,10 +50,10 @@ PMG supports the following package ecosystems:
|
|||||||
| --------- | --------- | --------------------------- |
|
| --------- | --------- | --------------------------- |
|
||||||
| NPM | ✅ Active | `pmg npm install <package>` |
|
| NPM | ✅ Active | `pmg npm install <package>` |
|
||||||
| PNPM | ✅ Active | `pmg pnpm add <package>` |
|
| PNPM | ✅ Active | `pmg pnpm add <package>` |
|
||||||
| PyPI | 🚧 Planned | Coming soon |
|
| PyPI | 🚧 Planned | |
|
||||||
| Go | 🚧 Planned | Coming soon |
|
| 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
|
## Installation
|
||||||
|
|
||||||
@@ -84,6 +85,26 @@ alias npm="pmg npm"
|
|||||||
alias pnpm="pmg pnpm"
|
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
|
### PMG in Action
|
||||||
|
|
||||||
#### Malicious Package Detection
|
#### Malicious Package Detection
|
||||||
|
|||||||
+13
-16
@@ -59,23 +59,24 @@ func (npm *npmPackageManager) ParseCommand(args []string) (*ParsedCommand, error
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if this is an install command
|
|
||||||
if !npm.isInstallCommand(args[0]) {
|
|
||||||
return &ParsedCommand{
|
|
||||||
Command: command,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract packages from args
|
// Extract packages from args
|
||||||
var packages []string
|
var packages []string
|
||||||
for i := 1; i < len(args); i++ {
|
for idx, arg := range args {
|
||||||
arg := args[i]
|
if slices.Contains(npm.Config.InstallCommands, arg) {
|
||||||
if !strings.HasPrefix(arg, "-") {
|
// All subsequent args are packages except for flags
|
||||||
packages = append(packages, arg)
|
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 {
|
if len(packages) == 0 {
|
||||||
return &ParsedCommand{
|
return &ParsedCommand{
|
||||||
Command: command,
|
Command: command,
|
||||||
@@ -112,10 +113,6 @@ func (npm *npmPackageManager) ParseCommand(args []string) (*ParsedCommand, error
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (npm *npmPackageManager) isInstallCommand(cmd string) bool {
|
|
||||||
return slices.Contains(npm.Config.InstallCommands, cmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
func npmParsePackageInfo(input string) (packageName, version string, err error) {
|
func npmParsePackageInfo(input string) (packageName, version string, err error) {
|
||||||
if input == "" {
|
if input == "" {
|
||||||
return "", "", fmt.Errorf("package info cannot be empty")
|
return "", "", fmt.Errorf("package info cannot be empty")
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ func TestNpmParseCommand(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "install a development package with short flag",
|
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: func(t *testing.T, parsedCommand *ParsedCommand, err error) {
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, 1, len(parsedCommand.InstallTargets))
|
assert.Equal(t, 1, len(parsedCommand.InstallTargets))
|
||||||
@@ -78,6 +78,25 @@ func TestNpmParseCommand(t *testing.T) {
|
|||||||
assert.Equal(t, 0, len(parsedCommand.InstallTargets))
|
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 {
|
for _, tc := range cases {
|
||||||
|
|||||||
Reference in New Issue
Block a user