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 3685205
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 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
}
18 changes: 17 additions & 1 deletion cmd/checker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,23 @@ 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]

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

fmt.Printf("%s", 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
}

0 comments on commit 3685205

Please sign in to comment.