2020-09-17 07:43:45 -07:00
|
|
|
package main
|
|
|
|
|
2021-04-17 04:47:28 -07:00
|
|
|
import "fmt"
|
|
|
|
|
2020-10-01 11:57:02 -07:00
|
|
|
type node struct {
|
2021-12-03 11:19:57 -08:00
|
|
|
language
|
2021-12-04 04:06:05 -08:00
|
|
|
|
|
|
|
PackageManagerIcon string
|
2020-09-17 07:43:45 -07:00
|
|
|
}
|
|
|
|
|
2021-04-17 04:47:28 -07:00
|
|
|
const (
|
|
|
|
// YarnIcon illustrates Yarn is used
|
|
|
|
YarnIcon Property = "yarn_icon"
|
|
|
|
// NPMIcon illustrates NPM is used
|
|
|
|
NPMIcon Property = "npm_icon"
|
2021-12-04 04:06:05 -08:00
|
|
|
// FetchPackageManager shows if NPM or Yarn is used
|
|
|
|
FetchPackageManager Property = "fetch_package_manager"
|
2021-04-17 04:47:28 -07:00
|
|
|
)
|
|
|
|
|
2020-10-01 11:57:02 -07:00
|
|
|
func (n *node) string() string {
|
2021-12-04 04:06:05 -08:00
|
|
|
segmentTemplate := n.language.props.getString(SegmentTemplate, "")
|
|
|
|
if len(segmentTemplate) == 0 {
|
|
|
|
version := n.language.string()
|
|
|
|
return fmt.Sprintf("%s%s", version, n.PackageManagerIcon)
|
|
|
|
}
|
|
|
|
return n.language.renderTemplate(segmentTemplate, n)
|
2020-09-17 07:43:45 -07:00
|
|
|
}
|
|
|
|
|
2022-01-01 11:09:52 -08:00
|
|
|
func (n *node) init(props Properties, env Environment) {
|
2021-12-03 11:19:57 -08:00
|
|
|
n.language = language{
|
2021-02-03 10:11:32 -08:00
|
|
|
env: env,
|
|
|
|
props: props,
|
2021-09-04 11:43:43 -07:00
|
|
|
extensions: []string{"*.js", "*.ts", "package.json", ".nvmrc", "pnpm-workspace.yaml", ".pnpmfile.cjs", ".npmrc"},
|
2021-02-03 10:11:32 -08:00
|
|
|
commands: []*cmd{
|
|
|
|
{
|
|
|
|
executable: "node",
|
|
|
|
args: []string{"--version"},
|
|
|
|
regex: `(?:v(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+))))`,
|
|
|
|
},
|
2021-01-10 03:14:43 -08:00
|
|
|
},
|
2021-02-03 10:11:32 -08:00
|
|
|
versionURLTemplate: "[%[1]s](https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V%[2]s.md#%[1]s)",
|
2021-02-06 22:06:24 -08:00
|
|
|
matchesVersionFile: n.matchesVersionFile,
|
2021-04-17 04:47:28 -07:00
|
|
|
loadContext: n.loadContext,
|
2020-11-14 11:04:04 -08:00
|
|
|
}
|
2020-09-17 07:43:45 -07:00
|
|
|
}
|
|
|
|
|
2020-10-01 11:57:02 -07:00
|
|
|
func (n *node) enabled() bool {
|
2020-11-14 11:04:04 -08:00
|
|
|
return n.language.enabled()
|
2020-09-17 07:43:45 -07:00
|
|
|
}
|
2021-02-06 22:06:24 -08:00
|
|
|
|
2021-04-17 04:47:28 -07:00
|
|
|
func (n *node) loadContext() {
|
2021-12-04 04:06:05 -08:00
|
|
|
if !n.language.props.getOneOfBool(FetchPackageManager, DisplayPackageManager, false) {
|
2021-04-17 04:47:28 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if n.language.env.hasFiles("yarn.lock") {
|
2021-12-04 04:06:05 -08:00
|
|
|
n.PackageManagerIcon = n.language.props.getString(YarnIcon, " \uF61A")
|
2021-04-17 22:16:38 -07:00
|
|
|
return
|
2021-04-17 04:47:28 -07:00
|
|
|
}
|
|
|
|
if n.language.env.hasFiles("package-lock.json") || n.language.env.hasFiles("package.json") {
|
2021-12-04 04:06:05 -08:00
|
|
|
n.PackageManagerIcon = n.language.props.getString(NPMIcon, " \uE71E")
|
2021-04-17 04:47:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 22:06:24 -08:00
|
|
|
func (n *node) matchesVersionFile() bool {
|
2021-02-15 11:51:48 -08:00
|
|
|
fileVersion := n.language.env.getFileContent(".nvmrc")
|
|
|
|
if len(fileVersion) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
2021-11-15 02:35:45 -08:00
|
|
|
|
|
|
|
regex := fmt.Sprintf(
|
|
|
|
`(?im)^v?%s(\.?%s)?(\.?%s)?$`,
|
2021-12-03 11:19:57 -08:00
|
|
|
n.language.version.Major,
|
|
|
|
n.language.version.Minor,
|
|
|
|
n.language.version.Patch,
|
2021-11-15 02:35:45 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
return matchString(regex, fileVersion)
|
2021-02-06 22:06:24 -08:00
|
|
|
}
|