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

Add support for NPM lock file v2 #242

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions pkg/modules/npm/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,16 @@ func (m *npm) ListModulesWithDeps(path string, globalSettingFile string) ([]mode
if err != nil {
return []models.Module{}, err
}

lockFileVersion := int32(pkResults["lockfileVersion"].(float64))
deps, ok := pkResults["packages"].(map[string]interface{})
if !ok {
deps = pkResults["dependencies"].(map[string]interface{})
}

return m.buildDependencies(path, deps)
return m.buildDependencies(path, deps, lockFileVersion)
}

func (m *npm) buildDependencies(path string, deps map[string]interface{}) ([]models.Module, error) {
func (m *npm) buildDependencies(path string, deps map[string]interface{}, lockFileVersion int32) ([]models.Module, error) {
modules := make([]models.Module, 0)
de, err := m.GetRootModule(path)
if err != nil {
Expand All @@ -206,7 +206,7 @@ func (m *npm) buildDependencies(path string, deps map[string]interface{}) ([]mod
}
modules = append(modules, *de)

allDeps := appendNestedDependencies(deps)
allDeps := appendNestedDependencies(deps, lockFileVersion)
for key, dd := range allDeps {
depName := strings.TrimPrefix(key, "@")
for nkey := range dd {
Expand All @@ -216,10 +216,11 @@ func (m *npm) buildDependencies(path string, deps map[string]interface{}) ([]mod
mod.Version = strings.Split(mod.Version, " ")[0]
mod.Name = depName

r := ""
if d["resolved"] != nil {
r = d["resolved"].(string)
mod.PackageDownloadLocation = r
r, ok := d["resolved"].(string)
if ok {
mod.PackageDownloadLocation = r
}
}
if mod.PackageDownloadLocation == "" {
r := "https://www.npmjs.com/package/%s/v/%s"
Expand Down Expand Up @@ -340,7 +341,7 @@ func getPackageHomepage(path string) string {
return ""
}

func appendNestedDependencies(deps map[string]interface{}) map[string]map[string]interface{} {
func appendNestedDependencies(deps map[string]interface{}, lockFileVersion int32) map[string]map[string]interface{} {
allDeps := make(map[string]map[string]interface{})
for k, v := range deps {
mainDeps := make(map[string]interface{})
Expand All @@ -356,20 +357,24 @@ func appendNestedDependencies(deps map[string]interface{}) map[string]map[string
}

if d, ok := v.(map[string]interface{})["dependencies"]; ok {
appendDependencies(d, allDeps)
appendDependencies(d, allDeps, lockFileVersion)
}

}
return allDeps
}

func appendDependencies(d interface{}, allDeps map[string]map[string]interface{}) {
func appendDependencies(d interface{}, allDeps map[string]map[string]interface{}, lockFileVersion int32) {
for dk, dv := range d.(map[string]interface{}) {
m := allDeps[dk]
if m == nil {
m = make(map[string]interface{})
}
m[dv.(map[string]interface{})["version"].(string)] = dv.(map[string]interface{})
if lockFileVersion == 1 {
m[dv.(map[string]interface{})["version"].(string)] = dv.(map[string]interface{})
} else {
m[dv.(string)] = dv
}
allDeps[dk] = m
}
}
Expand Down