Skip to content

Commit

Permalink
Merge pull request #14 from kyoshidajp/support_npm
Browse files Browse the repository at this point in the history
Support npm
  • Loading branch information
kyoshidajp authored Oct 27, 2023
2 parents c75aede + 9ece663 commit d3abcfa
Show file tree
Hide file tree
Showing 6 changed files with 353 additions and 3 deletions.
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

0 comments on commit d3abcfa

Please sign in to comment.