Skip to content

Commit

Permalink
feat: Support more files that contain versions including package.json
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Rename from-file to version-from-file
  • Loading branch information
danez committed Nov 24, 2020
1 parent 45608fe commit f398522
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 36 deletions.
16 changes: 10 additions & 6 deletions README.md
Expand Up @@ -45,14 +45,10 @@ nsd nodejs /path/ --version 12.15.0

#### With specific version from file

```bash
nsd nodejs /path/ --from-file ./node_version
```

#### No specific version, will try reading from CWD/.nvmrc
`package.json` (exact version in engines field) and `.nvmrc` are currently supported.

```bash
nsd nodejs /path/
nsd nodejs /path/ --version-from-file ./.nvmrc
```

### Yarn (Currently only v1 supported)
Expand All @@ -71,6 +67,14 @@ This will download the single javascript file and move it into the download dire
nsd yarn /path/ --version 1.22.5 --single-file yarn
```

#### With specific version from file

`package.json` (exact version in engines field) is currently supported.

```bash
nsd nodejs /path/ --version-from-file ./package.json
```

## How to build

> Requires go version 1.15 or newer
Expand Down
29 changes: 4 additions & 25 deletions nsd/cmd/nodejs.go
Expand Up @@ -3,11 +3,8 @@ package cmd
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"

"github.com/mholt/archiver/v3"
Checksum "github.com/researchgate/nodejs-simple-downloader/nsd/checksum"
Expand Down Expand Up @@ -97,38 +94,20 @@ var (
)

func prepareNodejsFlags() (err error) {
if nodejsVersion != "" && nodejsVersionfromFile != "" {
return errors.New("cannot figure out which version to install. Please only specify one of --version or --from-file")
}
versionSpecified := true
if nodejsVersion == "" && nodejsVersionfromFile == "" {
versionSpecified = false
nodejsVersionfromFile = ".nvmrc"
if (nodejsVersion != "" && nodejsVersionfromFile != "") || (nodejsVersion == "" && nodejsVersionfromFile == "") {
return errors.New("cannot figure out which version to install. Please specify one of --version or --from-file")
}

if nodejsVersionfromFile != "" {
nodejsVersionfromFile, err = filepath.Abs(nodejsVersionfromFile)
if err != nil {
return
}

content, err := ioutil.ReadFile(nodejsVersionfromFile)
if err != nil {
if !versionSpecified {
return errors.New("No version specified and could not find any version file in the current directory")
}
return err
}

nodejsVersion = strings.Trim(string(content), " \n\r")
nodejsVersion, err = NodeJs.VersionFromFile(nodejsVersionfromFile)
}

return
}

func init() {
nodejsCommand.Flags().StringVarP(&nodejsVersion, "version", "v", "", "Which version to install")
nodejsCommand.Flags().StringVarP(&nodejsVersionfromFile, "from-file", "r", "", "Reads the version to be installed from a file. Either specify the filename or if empty it will try to read from .nvmrc file.")
nodejsCommand.Flags().StringVarP(&nodejsVersionfromFile, "version-from-file", "f", "", "Reads the version to be installed from a file. Supported are currently package.json, .nvmrc, .node-version and .n-node-version.")
nodejsCommand.MarkFlagFilename("from-file")
rootCmd.AddCommand(nodejsCommand)
}
17 changes: 12 additions & 5 deletions nsd/cmd/yarn.go
Expand Up @@ -9,14 +9,16 @@ import (
"github.com/mholt/archiver/v3"
Checksum "github.com/researchgate/nodejs-simple-downloader/nsd/checksum"
Download "github.com/researchgate/nodejs-simple-downloader/nsd/download"
Yarn "github.com/researchgate/nodejs-simple-downloader/nsd/yarn"

"github.com/spf13/cobra"
)

var (
yarnVersion string
singleFile string
yarnCommand = &cobra.Command{
yarnVersion string
yarnVersionfromFile string
singleFile string
yarnCommand = &cobra.Command{
Use: "yarn [path]",
Short: "Download yarn to specific folder",
Long: "",
Expand Down Expand Up @@ -98,8 +100,12 @@ var (
)

func prepareYarnFlags() (err error) {
if yarnVersion == "" {
return errors.New("No version specified")
if (yarnVersion != "" && yarnVersionfromFile != "") || (yarnVersion == "" && yarnVersionfromFile == "") {
return errors.New("cannot figure out which version to install. Please specify one of --version or --from-file")
}

if yarnVersionfromFile != "" {
yarnVersion, err = Yarn.VersionFromFile(yarnVersionfromFile)
}

return
Expand All @@ -108,5 +114,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")
yarnCommand.Flags().StringVarP(&yarnVersionfromFile, "version-from-file", "f", "", "Reads the version to be installed from a file. Supported is only package.json")
rootCmd.AddCommand(yarnCommand)
}
23 changes: 23 additions & 0 deletions nsd/nodejs/packagejson.go
@@ -0,0 +1,23 @@
package nodejs

import (
"encoding/json"
"io/ioutil"
)

// PackageJSONEngines holds the data from package.json
type PackageJSONEngines struct {
Engines map[string]string `json:"engines"`
}

// GetPackageJSON returns a struct with data from package.json
func GetPackageJSON(filePath string) (packagejson *PackageJSONEngines, err error) {
content, err := ioutil.ReadFile(filePath)
if err != nil {
return
}

err = json.Unmarshal(content, &packagejson)

return
}
46 changes: 46 additions & 0 deletions nsd/nodejs/version.go
@@ -0,0 +1,46 @@
package nodejs

import (
"fmt"
"io/ioutil"
"path/filepath"
"strings"
)

func versionFromPackageJSONEngines(filePath string) (version string, err error) {
packagejson, err := GetPackageJSON(filePath)
if err != nil {
return
}

return packagejson.Engines["node"], nil
}

func versionFromManagerFiles(versionFilePath string) (version string, err error) {
content, err := ioutil.ReadFile(versionFilePath)
if err != nil {
return
}

version = strings.Trim(string(content), " \n\r")

return
}

// VersionFromFile tries to read the Node.js version from the supplied file if supported
func VersionFromFile(versionFilePath string) (version string, err error) {
switch filepath.Base(versionFilePath) {
case "package.json":
return versionFromPackageJSONEngines(versionFilePath)
case ".nvmrc": // nvm
fallthrough
case ".node-version": // nodenv
fallthrough
case ".n-node-version": // n
return versionFromManagerFiles(versionFilePath)
default:
err = fmt.Errorf("Unsupported file. Cannot read Node.js version from %s", versionFilePath)
}

return
}
30 changes: 30 additions & 0 deletions nsd/yarn/version.go
@@ -0,0 +1,30 @@
package nodejs

import (
"fmt"
"path/filepath"

NodeJs "github.com/researchgate/nodejs-simple-downloader/nsd/nodejs"
)

func versionFromPackageJSONEngines(filePath string) (version string, err error) {
packagejson, err := NodeJs.GetPackageJSON(filePath)
if err != nil {
return
}

return packagejson.Engines["yarn"], nil
}

// VersionFromFile tries to read the yarn version from the supplied file if supported
func VersionFromFile(versionFilePath string) (version string, err error) {

switch filepath.Base(versionFilePath) {
case "package.json":
return versionFromPackageJSONEngines(versionFilePath)
default:
err = fmt.Errorf("Unsupported file. Cannot read yarn version from %s", versionFilePath)
}

return
}

0 comments on commit f398522

Please sign in to comment.