Skip to content

Commit

Permalink
feat: Allow single file distribution download
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Nov 24, 2020
1 parent 2ec5331 commit 45608fe
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,20 @@ nsd nodejs /path/

### Yarn (Currently only v1 supported)

#### With specific version
#### Full distribution

```bash
nsd yarn /path/ --version 1.22.5
```

#### Single file distribution

This will download the single javascript file and move it into the download directory with the name supplied to `--single-file`

```bash
nsd yarn /path/ --version 1.22.5 --single-file yarn
```

## How to build

> Requires go version 1.15 or newer
Expand Down
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkU
github.com/spf13/cobra v1.1.1 h1:KfztREH0tPxJJ+geloSLaAkaPkr4ki2Er5quFV1TDo4=
github.com/spf13/cobra v1.1.1/go.mod h1:WnodtKOvamDL/PwE2M4iKs8aMDBZ5Q5klgD3qfVJQMI=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
Expand Down
32 changes: 26 additions & 6 deletions nsd/cmd/yarn.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"path"

"github.com/mholt/archiver/v3"
Checksum "github.com/researchgate/nodejs-simple-downloader/nsd/checksum"
Expand All @@ -14,6 +15,7 @@ import (

var (
yarnVersion string
singleFile string
yarnCommand = &cobra.Command{
Use: "yarn [path]",
Short: "Download yarn to specific folder",
Expand All @@ -27,14 +29,17 @@ var (
RunE: func(cmd *cobra.Command, args []string) (err error) {
downloadPath := args[0]

// https://github.com/yarnpkg/yarn/releases/download/v'.$version.'/yarn-v'.$version.'.tar.gz

err = prepareYarnFlags()
if err != nil {
return
}

yarnURL := fmt.Sprintf("https://github.com/yarnpkg/yarn/releases/download/v%s/yarn-v%s.tar.gz", yarnVersion, yarnVersion)
yarnURL := fmt.Sprintf("https://github.com/yarnpkg/yarn/releases/download/v%s/yarn-", yarnVersion)
if singleFile != "" {
yarnURL = fmt.Sprintf(yarnURL+"%s.js", yarnVersion)
} else {
yarnURL = fmt.Sprintf(yarnURL+"v%s.tar.gz", yarnVersion)
}
yarnFilePath, err := Download.File(yarnURL)
if err != nil {
return
Expand Down Expand Up @@ -65,9 +70,23 @@ var (
return
}

tar := archiver.NewTarGz()
tar.StripComponents = 1
err = tar.Unarchive(yarnFilePath, downloadPath)
if singleFile != "" {
err = os.MkdirAll(downloadPath, 0755)
if err != nil {
return
}

destinationFilePath := path.Join(downloadPath, singleFile)
err = os.Rename(yarnFilePath, destinationFilePath)
if err != nil {
return err
}
err = os.Chmod(destinationFilePath, 0755)
} else {
tar := archiver.NewTarGz()
tar.StripComponents = 1
err = tar.Unarchive(yarnFilePath, downloadPath)
}

if err != nil {
return
Expand All @@ -88,5 +107,6 @@ func prepareYarnFlags() (err error) {

func init() {
yarnCommand.Flags().StringVarP(&yarnVersion, "version", "v", "", "Which version to install")
yarnCommand.Flags().StringVarP(&singleFile, "single-file", "s", "", "Download only the single file distribution from yarn and save with the supplied name in the download path")
rootCmd.AddCommand(yarnCommand)
}

0 comments on commit 45608fe

Please sign in to comment.