From f39852295d75ea6b246a4d66b223fdf3ca8714d4 Mon Sep 17 00:00:00 2001 From: Daniel Tschinder Date: Tue, 24 Nov 2020 16:25:34 +0100 Subject: [PATCH] feat: Support more files that contain versions including package.json BREAKING CHANGE: Rename from-file to version-from-file --- README.md | 16 +++++++++----- nsd/cmd/nodejs.go | 29 ++++-------------------- nsd/cmd/yarn.go | 17 ++++++++++----- nsd/nodejs/packagejson.go | 23 ++++++++++++++++++++ nsd/nodejs/version.go | 46 +++++++++++++++++++++++++++++++++++++++ nsd/yarn/version.go | 30 +++++++++++++++++++++++++ 6 files changed, 125 insertions(+), 36 deletions(-) create mode 100644 nsd/nodejs/packagejson.go create mode 100644 nsd/nodejs/version.go create mode 100644 nsd/yarn/version.go diff --git a/README.md b/README.md index 45982d8..870a61c 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 diff --git a/nsd/cmd/nodejs.go b/nsd/cmd/nodejs.go index 5331a5e..5d1f6cc 100644 --- a/nsd/cmd/nodejs.go +++ b/nsd/cmd/nodejs.go @@ -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" @@ -97,30 +94,12 @@ 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 @@ -128,7 +107,7 @@ func prepareNodejsFlags() (err error) { 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) } diff --git a/nsd/cmd/yarn.go b/nsd/cmd/yarn.go index 714a327..d77e265 100644 --- a/nsd/cmd/yarn.go +++ b/nsd/cmd/yarn.go @@ -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: "", @@ -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 @@ -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) } diff --git a/nsd/nodejs/packagejson.go b/nsd/nodejs/packagejson.go new file mode 100644 index 0000000..06776d9 --- /dev/null +++ b/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 +} diff --git a/nsd/nodejs/version.go b/nsd/nodejs/version.go new file mode 100644 index 0000000..cd1ce09 --- /dev/null +++ b/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 +} diff --git a/nsd/yarn/version.go b/nsd/yarn/version.go new file mode 100644 index 0000000..a83dc88 --- /dev/null +++ b/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 +}