Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support more files that contain versions including package.json
BREAKING CHANGE: Rename from-file to version-from-file
- Loading branch information
Showing
6 changed files
with
125 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |