-
Notifications
You must be signed in to change notification settings - Fork 1
/
version.go
46 lines (38 loc) · 1.04 KB
/
version.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
}