Skip to content

Commit

Permalink
npm: crawl the root dev dependencies, like npm ls does.
Browse files Browse the repository at this point in the history
Fixes #21
  • Loading branch information
aboodman committed Mar 14, 2019
1 parent 4d7358c commit c809703
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
15 changes: 11 additions & 4 deletions dep/crawl/crawl.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func Crawl(repourl, dataDir string, l *log.Logger) <-chan Result {
go func() {
// queue the initial children to explore
// we don't create a dep for the starting point
_, cdids := processRepo(repourl, l)
_, cdids := processRepo(repourl, true, l)
push(cdids)

// explore the graph concurrently until there are no more depnames queued
Expand Down Expand Up @@ -150,16 +150,23 @@ func processDepID(id dep.ID, dataDir string, l *log.Logger) (r dep.Dep, childDep
return dep.Dep{}, nil
}

c, cdns := processRepo(dir, l)
c, cdns := processRepo(dir, false, l)
return dep.Dep{
ID: id,
Conf: c,
}, cdns
}

func processRepo(path string, l *log.Logger) (*conf.Config, []dep.ID) {
func processRepo(path string, isRoot bool, l *log.Logger) (*conf.Config, []dep.ID) {
opts := npm.Options{
Dependencies: true,
}
if isRoot {
opts.DevDependencies = true
opts.PeerDependencies = true
}
var r []dep.ID
ds := npm.Dependencies(path, l)
ds := npm.Dependencies(path, opts, l)
r = append(r, ds...)
ds, err := golang.Dependencies(path)
if err != nil {
Expand Down
27 changes: 22 additions & 5 deletions dep/npm/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ type dist struct {
}

type pkg struct {
Name string `json:"name"`
Dependencies map[string]string `json:"dependencies"`
DevDependencies map[string]string `json:"devDependencies"`
Name string `json:"name"`
Dependencies map[string]string `json:"dependencies"`
DevDependencies map[string]string `json:"devDependencies"`
PeerDependencies map[string]string `json:"peerDependencies"`
}

func httpClient(l *log.Logger) *retryablehttp.Client {
Expand Down Expand Up @@ -80,7 +81,13 @@ func Dir(name, dataDir string, l *log.Logger) string {
return p
}

func Dependencies(repoPath string, l *log.Logger) []dep.ID {
type Options struct {
Dependencies bool
DevDependencies bool
PeerDependencies bool
}

func Dependencies(repoPath string, opts Options, l *log.Logger) []dep.ID {
// parse the manifest
pf, err := os.Open(path.Join(repoPath, "package.json"))
if err != nil {
Expand All @@ -99,8 +106,18 @@ func Dependencies(repoPath string, l *log.Logger) []dep.ID {
}

// return dependencies
depss := []map[string]string{}
if opts.Dependencies {
depss = append(depss, pj.Dependencies)
}
if opts.DevDependencies {
depss = append(depss, pj.DevDependencies)
}
if opts.PeerDependencies {
depss = append(depss, pj.PeerDependencies)
}
var r []dep.ID
for _, deps := range []map[string]string{pj.Dependencies} { // TODO: add devDependencies, peerDependencies
for _, deps := range depss {
for d := range deps {
r = append(r, dep.ID{
Type: dep.NPM,
Expand Down

0 comments on commit c809703

Please sign in to comment.