feat: Add package manager guard as the orchestrator

This commit is contained in:
abhisek
2025-05-14 11:31:35 +05:30
parent 01f2fff82f
commit 96842cfcf2
7 changed files with 138 additions and 81 deletions
+3 -37
View File
@@ -2,51 +2,17 @@ package npm
import (
_ "embed"
"fmt"
"os"
"github.com/safedep/pmg/pkg/common/utils"
"github.com/safedep/pmg/pkg/registry"
"github.com/safedep/pmg/pkg/wrapper"
"github.com/spf13/cobra"
)
func NewNpmCommand() *cobra.Command {
cmd := &cobra.Command{
return &cobra.Command{
Use: "npm [action] [package]",
Short: "Scan packages from npm registry",
Short: "Guard npm package manager",
DisableFlagParsing: true,
RunE: func(cmd *cobra.Command, args []string) error {
execPath, err := utils.GetExecutablePath(string(registry.RegistryNPM))
if err != nil {
fmt.Fprintf(os.Stderr, "npm not found: %v\n", err)
return err
}
if len(args) >= 2 && utils.IsInstallCommand(string(registry.RegistryNPM), args[0]) {
// Parse arguments to separate flags and packages
flags, packages := utils.ParseNpmInstallArgs(args[1:])
// If no packages specified, just pass through to npm
if len(packages) == 0 {
return utils.ExecCmd(execPath, args, []string{})
}
// Create single wrapper instance for all packages
pmw := wrapper.NewPackageManagerWrapper(registry.RegistryNPM, flags, packages, args[0])
if err := pmw.Wrap(); err != nil {
return err
}
return nil
}
if err := utils.ExecCmd(execPath, args, []string{}); err != nil {
os.Exit(1)
}
os.Exit(0)
return nil
return executeNpmFlow(args)
},
}
return cmd
}