Skip to content

Commit

Permalink
checker: show files
Browse files Browse the repository at this point in the history
  • Loading branch information
xtuc committed May 5, 2020
1 parent 3ba9f62 commit 14c2c20
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 21 deletions.
20 changes: 1 addition & 19 deletions cmd/autoupdate/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package main
import (
"context"
"fmt"
"io/ioutil"
"net/http"
"os"
"path"
"sort"
Expand Down Expand Up @@ -92,7 +90,7 @@ func doUpdateNpm(ctx context.Context, pckg *packages.Package, versions []npm.Npm

util.Check(os.MkdirAll(pckgpath, os.ModePerm))

tarballDir := downloadTar(ctx, version.Tarball)
tarballDir := npm.DownloadTar(ctx, version.Tarball)
filesToCopy := pckg.NpmFilesFrom(tarballDir)

if len(filesToCopy) > 0 {
Expand Down Expand Up @@ -149,19 +147,3 @@ func npmVersionDiff(a []npm.NpmVersion, b []string) []npm.NpmVersion {

return diff
}

// Extract the tarball url in a temporary location
func downloadTar(ctx context.Context, url string) string {
dest, err := ioutil.TempDir("", "npmtarball")
util.Check(err)

util.Debugf(ctx, "download %s in %s", url, dest)

resp, err := http.Get(url)
util.Check(err)

defer resp.Body.Close()

util.Check(npm.Untar(dest, resp.Body))
return dest
}
24 changes: 23 additions & 1 deletion cmd/checker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,29 @@ func main() {

func showFiles(path string) {
ctx := util.ContextWithName(path)
err(ctx, "not implemented yet")
pckg, readerr := packages.ReadPackageJSON(ctx, path)
if readerr != nil {
err(ctx, readerr.Error())
return
}

npmVersions := npm.GetVersions(pckg.Autoupdate.Target)
if len(npmVersions) == 0 {
err(ctx, "no version found on npm")
return
}
lastNpmVersion := npmVersions[len(npmVersions)-1]
util.Printf(ctx, "%s:\n", lastNpmVersion)

tarballDir := npm.DownloadTar(ctx, lastNpmVersion.Tarball)
filesToCopy := pckg.NpmFilesFrom(tarballDir)

if len(filesToCopy) == 0 {
err(ctx, "no files to copy")
return
}

util.Printf(ctx, "%s\n", filesToCopy)
}

func lintPackage(path string) {
Expand Down
21 changes: 21 additions & 0 deletions npm/tar.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ package npm
import (
"archive/tar"
"compress/gzip"
"context"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"

"github.com/cdnjs/tools/util"
)

func removePackageDir(path string) string {
Expand Down Expand Up @@ -93,3 +98,19 @@ func Untar(dst string, r io.Reader) error {
}
}
}

// Extract the tarball url in a temporary location
func DownloadTar(ctx context.Context, url string) string {
dest, err := ioutil.TempDir("", "npmtarball")
util.Check(err)

util.Debugf(ctx, "download %s in %s", url, dest)

resp, err := http.Get(url)
util.Check(err)

defer resp.Body.Close()

util.Check(Untar(dest, resp.Body))
return dest
}
2 changes: 1 addition & 1 deletion util/fileglob.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func ListFilesGlob(base string, pattern string) []string {
return []string{}
}

fmt.Println("match", pattern, "in", base)
// fmt.Println("match", pattern, "in", base)

cmd := exec.Command(path.Join(GetBotBasePath(), "glob", "index.js"), pattern)
var out bytes.Buffer
Expand Down

0 comments on commit 14c2c20

Please sign in to comment.