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

Support npm #14

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@

## Support dependencies files

| Language | package manager | file (e.g.) | status |
| language | package manager | file (e.g.) | status |
| -------- | ------------- | -- | :----: |
| Ruby | bundler | Gemfile.lock | :heavy_check_mark: |
| Ruby | bundler | gemspec | (soon) |
| Ruby | bundler | rake.gemspec | (soon) |
| JavaScript | yarn | yarn.lock | :heavy_check_mark: |
| JavaScript | npm | package.json | (soon) |
| JavaScript | npm | package-lock.json | :heavy_check_mark: |
| Python | pip | requirements.txt | :heavy_check_mark: |
| Go | | go.sum | (soon) |

Expand Down
1 change: 1 addition & 0 deletions cmd/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ var doctors = map[string]Doctor{
"bundler": NewBundlerDoctor(),
"yarn": NewYarnDoctor(),
"pip": NewPipDoctor(),
"npm": NewNPMDoctor(),
}

var diagnoseCmd = &cobra.Command{
Expand Down
241 changes: 241 additions & 0 deletions cmd/nodejs/npm/testdata/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 101 additions & 0 deletions cmd/npm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package cmd

import (
"fmt"

parser_io "github.com/aquasecurity/go-dep-parser/pkg/io"
"github.com/aquasecurity/go-dep-parser/pkg/nodejs/npm"
"github.com/kyoshidajp/dep-doctor/cmd/github"
)

type NPMDoctor struct {
}

func NewNPMDoctor() *NPMDoctor {
return &NPMDoctor{}
}

func (d *NPMDoctor) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]Diagnosis {
diagnoses := make(map[string]Diagnosis)
slicedNameWithOwners := [][]github.NameWithOwner{}
nameWithOwners := d.NameWithOwners(r)
sliceSize := len(nameWithOwners)

for i := 0; i < sliceSize; i += github.SEARCH_REPOS_PER_ONCE {
end := i + github.SEARCH_REPOS_PER_ONCE
if sliceSize < end {
end = sliceSize
}
slicedNameWithOwners = append(slicedNameWithOwners, nameWithOwners[i:end])
}

for _, nameWithOwners := range slicedNameWithOwners {
repos := github.FetchFromGitHub(nameWithOwners)
for _, r := range repos {
diagnosis := Diagnosis{
Name: r.Name,
Url: r.Url,
Archived: r.Archived,
Diagnosed: true,
IsActive: r.IsActive(year),
}
diagnoses[r.Name] = diagnosis
}
}

for _, nameWithOwner := range nameWithOwners {
if nameWithOwner.CanSearch {
continue
}

diagnosis := Diagnosis{
Name: nameWithOwner.PackageName,
Diagnosed: false,
}
diagnoses[nameWithOwner.PackageName] = diagnosis
}
return diagnoses
}

func (d *NPMDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOwner {
var nameWithOwners []github.NameWithOwner
libs, _, _ := npm.NewParser().Parse(r)

nodejs := Nodejs{}
for _, lib := range libs {
fmt.Printf("%s\n", lib.Name)

githubUrl, err := nodejs.fetchURLFromRegistry(lib.Name)
if err != nil {
nameWithOwners = append(nameWithOwners,
github.NameWithOwner{
PackageName: lib.Name,
CanSearch: false,
},
)
continue
}

repo, err := github.ParseGitHubUrl(githubUrl)
if err != nil {
nameWithOwners = append(nameWithOwners,
github.NameWithOwner{
PackageName: lib.Name,
CanSearch: false,
},
)
continue
}

nameWithOwners = append(nameWithOwners,
github.NameWithOwner{
Repo: repo.Repo,
Owner: repo.Owner,
PackageName: lib.Name,
CanSearch: true,
},
)
}

return nameWithOwners
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/liamg/jfather v0.0.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
Expand Down
Loading
Loading