From 36852057d7ec6e138e028d15a0c793a57f9f939c Mon Sep 17 00:00:00 2001 From: Sven Sauleau Date: Tue, 5 May 2020 13:50:41 +0100 Subject: [PATCH] checker: show files --- cmd/autoupdate/npm.go | 20 +------------------- cmd/checker/main.go | 18 +++++++++++++++++- npm/tar.go | 21 +++++++++++++++++++++ 3 files changed, 39 insertions(+), 20 deletions(-) diff --git a/cmd/autoupdate/npm.go b/cmd/autoupdate/npm.go index 92499fa1..59c1f31e 100644 --- a/cmd/autoupdate/npm.go +++ b/cmd/autoupdate/npm.go @@ -3,8 +3,6 @@ package main import ( "context" "fmt" - "io/ioutil" - "net/http" "os" "path" "sort" @@ -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 { @@ -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 -} diff --git a/cmd/checker/main.go b/cmd/checker/main.go index d53b9ed0..70143f62 100644 --- a/cmd/checker/main.go +++ b/cmd/checker/main.go @@ -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) { diff --git a/npm/tar.go b/npm/tar.go index 90c0c9cc..1bf27847 100644 --- a/npm/tar.go +++ b/npm/tar.go @@ -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 { @@ -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 +}