From 9a45a75e2b7ec98fb7094442bc2e7f6e179922c8 Mon Sep 17 00:00:00 2001 From: Drew Hintz Date: Wed, 15 Feb 2023 22:13:32 -0600 Subject: [PATCH] cache npmjs package lookups in a local file --- npm.go | 6 +++++- util.go | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/npm.go b/npm.go index 6243204..582e4d6 100644 --- a/npm.go +++ b/npm.go @@ -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 @@ -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 } diff --git a/util.go b/util.go index fa45963..b71c90a 100644 --- a/util.go +++ b/util.go @@ -1,6 +1,11 @@ package main -import "strings" +import ( + "bufio" + "fmt" + "os" + "strings" +) func inSlice(what rune, where []rune) bool { for _, r := range where { @@ -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() +}