Skip to content
Open
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: 5 additions & 1 deletion npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func (n *NPMLookup) ReadPackagesFromFile(filename string) error {
// Returns a slice of strings with any npm packages not in the public npm package repository
func (n *NPMLookup) PackagesNotInPublic() []string {
notavail := []string{}
avail := readMap("/tmp/confused-npm-avail")
for _, pkg := range n.Packages {
if n.localReference(pkg.Version) || n.urlReference(pkg.Version) || n.gitReference(pkg.Version) {
continue
Expand All @@ -113,10 +114,13 @@ func (n *NPMLookup) PackagesNotInPublic() []string {
continue
}
}
if !n.isAvailableInPublic(pkg.Name, 0) {
if !avail[pkg.Name] && !n.isAvailableInPublic(pkg.Name, 0) {
notavail = append(notavail, pkg.Name)
} else {
avail[pkg.Name] = true
}
}
writeMap(avail, "/tmp/confused-npm-avail")
return notavail
}

Expand Down
35 changes: 34 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package main

import "strings"
import (
"bufio"
"fmt"
"os"
"strings"
)

func inSlice(what rune, where []rune) bool {
for _, r := range where {
Expand All @@ -14,3 +19,31 @@ func inSlice(what rune, where []rune) bool {
func countLeadingSpaces(line string) int {
return len(line) - len(strings.TrimLeft(line, " "))
}

// reads line-delimited contents of a file into a map of strings
func readMap(path string) map[string]bool {
file, err := os.Open(path)
if err != nil {
return map[string]bool{}
}
defer file.Close()

avail := map[string]bool{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
avail[scanner.Text()] = true
}
return avail
}

// writes a map of strings to a line-delimited file
func writeMap(lines map[string]bool, path string) {
file, _ := os.Create(path)
defer file.Close()

writer := bufio.NewWriter(file)
for key, _ := range lines {
fmt.Fprintln(writer, key)
}
writer.Flush()
}