Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache npmjs package lookups in a local file #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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()
}