Skip to content

Commit dddd4d7

Browse files
committed
Support npm
1 parent c75aede commit dddd4d7

File tree

6 files changed

+351
-1
lines changed

6 files changed

+351
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
| Ruby | bundler | Gemfile.lock | :heavy_check_mark: |
1616
| Ruby | bundler | gemspec | (soon) |
1717
| JavaScript | yarn | yarn.lock | :heavy_check_mark: |
18-
| JavaScript | npm | package.json | (soon) |
18+
| JavaScript | npm | package-lock.json | :heavy_check_mark: |
1919
| Python | pip | requirements.txt | :heavy_check_mark: |
2020
| Go | | go.sum | (soon) |
2121

cmd/diagnose.go

+1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ var doctors = map[string]Doctor{
5757
"bundler": NewBundlerDoctor(),
5858
"yarn": NewYarnDoctor(),
5959
"pip": NewPipDoctor(),
60+
"npm": NewNPMDoctor(),
6061
}
6162

6263
var diagnoseCmd = &cobra.Command{

cmd/nodejs/npm/testdata/package-lock.json

+241
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/npm.go

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
parser_io "github.com/aquasecurity/go-dep-parser/pkg/io"
7+
"github.com/aquasecurity/go-dep-parser/pkg/nodejs/npm"
8+
"github.com/kyoshidajp/dep-doctor/cmd/github"
9+
)
10+
11+
type NPMDoctor struct {
12+
}
13+
14+
func NewNPMDoctor() *NPMDoctor {
15+
return &NPMDoctor{}
16+
}
17+
18+
func (d *NPMDoctor) Diagnose(r parser_io.ReadSeekerAt, year int) map[string]Diagnosis {
19+
diagnoses := make(map[string]Diagnosis)
20+
slicedNameWithOwners := [][]github.NameWithOwner{}
21+
nameWithOwners := d.NameWithOwners(r)
22+
sliceSize := len(nameWithOwners)
23+
24+
for i := 0; i < sliceSize; i += github.SEARCH_REPOS_PER_ONCE {
25+
end := i + github.SEARCH_REPOS_PER_ONCE
26+
if sliceSize < end {
27+
end = sliceSize
28+
}
29+
slicedNameWithOwners = append(slicedNameWithOwners, nameWithOwners[i:end])
30+
}
31+
32+
for _, nameWithOwners := range slicedNameWithOwners {
33+
repos := github.FetchFromGitHub(nameWithOwners)
34+
for _, r := range repos {
35+
diagnosis := Diagnosis{
36+
Name: r.Name,
37+
Url: r.Url,
38+
Archived: r.Archived,
39+
Diagnosed: true,
40+
IsActive: r.IsActive(year),
41+
}
42+
diagnoses[r.Name] = diagnosis
43+
}
44+
}
45+
46+
for _, nameWithOwner := range nameWithOwners {
47+
if nameWithOwner.CanSearch {
48+
continue
49+
}
50+
51+
diagnosis := Diagnosis{
52+
Name: nameWithOwner.PackageName,
53+
Diagnosed: false,
54+
}
55+
diagnoses[nameWithOwner.PackageName] = diagnosis
56+
}
57+
return diagnoses
58+
}
59+
60+
func (d *NPMDoctor) NameWithOwners(r parser_io.ReadSeekerAt) []github.NameWithOwner {
61+
var nameWithOwners []github.NameWithOwner
62+
libs, _, _ := npm.NewParser().Parse(r)
63+
64+
nodejs := Nodejs{}
65+
for _, lib := range libs {
66+
fmt.Printf("%s\n", lib.Name)
67+
68+
githubUrl, err := nodejs.fetchURLFromRegistry(lib.Name)
69+
if err != nil {
70+
nameWithOwners = append(nameWithOwners,
71+
github.NameWithOwner{
72+
PackageName: lib.Name,
73+
CanSearch: false,
74+
},
75+
)
76+
continue
77+
}
78+
79+
repo, err := github.ParseGitHubUrl(githubUrl)
80+
if err != nil {
81+
nameWithOwners = append(nameWithOwners,
82+
github.NameWithOwner{
83+
PackageName: lib.Name,
84+
CanSearch: false,
85+
},
86+
)
87+
continue
88+
}
89+
90+
nameWithOwners = append(nameWithOwners,
91+
github.NameWithOwner{
92+
Repo: repo.Repo,
93+
Owner: repo.Owner,
94+
PackageName: lib.Name,
95+
CanSearch: true,
96+
},
97+
)
98+
}
99+
100+
return nameWithOwners
101+
}

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/davecgh/go-spew v1.1.1 // indirect
99
github.com/golang/protobuf v1.5.3 // indirect
1010
github.com/google/go-querystring v1.1.0 // indirect
11+
github.com/liamg/jfather v0.0.7 // indirect
1112
github.com/mattn/go-colorable v0.1.13 // indirect
1213
github.com/mattn/go-isatty v0.0.20 // indirect
1314
github.com/pmezard/go-difflib v1.0.0 // indirect

0 commit comments

Comments
 (0)