oh-my-posh/src/segments/node.go

84 lines
2.1 KiB
Go
Raw Normal View History

2022-01-26 06:54:36 -08:00
package segments
2020-09-17 07:43:45 -07:00
import (
"fmt"
"oh-my-posh/environment"
"oh-my-posh/properties"
"oh-my-posh/regex"
)
2022-01-26 05:10:18 -08:00
type Node struct {
2021-12-03 11:19:57 -08:00
language
PackageManagerIcon string
2020-09-17 07:43:45 -07:00
}
const (
// YarnIcon illustrates Yarn is used
YarnIcon properties.Property = "yarn_icon"
// NPMIcon illustrates NPM is used
NPMIcon properties.Property = "npm_icon"
// FetchPackageManager shows if NPM or Yarn is used
FetchPackageManager properties.Property = "fetch_package_manager"
)
func (n *Node) Template() string {
2022-02-01 05:07:58 -08:00
return " {{ if .PackageManagerIcon }}{{ .PackageManagerIcon }} {{ end }}{{ .Full }} "
2020-09-17 07:43:45 -07:00
}
func (n *Node) Init(props properties.Properties, env environment.Environment) {
2021-12-03 11:19:57 -08:00
n.language = language{
2021-02-03 10:11:32 -08:00
env: env,
props: props,
extensions: []string{"*.js", "*.ts", "package.json", ".nvmrc", "pnpm-workspace.yaml", ".pnpmfile.cjs", ".npmrc", ".vue"},
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-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)",
matchesVersionFile: n.matchesVersionFile,
loadContext: n.loadContext,
2020-11-14 11:04:04 -08:00
}
2020-09-17 07:43:45 -07:00
}
func (n *Node) Enabled() bool {
if n.language.Enabled() {
n.Mismatch = !n.matchesVersionFile()
return true
}
return false
2020-09-17 07:43:45 -07:00
}
2022-01-26 05:10:18 -08:00
func (n *Node) loadContext() {
2022-01-26 04:09:21 -08:00
if !n.language.props.GetBool(FetchPackageManager, false) {
return
}
if n.language.env.HasFiles("yarn.lock") {
2022-01-26 04:09:21 -08:00
n.PackageManagerIcon = n.language.props.GetString(YarnIcon, " \uF61A")
2021-04-17 22:16:38 -07:00
return
}
if n.language.env.HasFiles("package-lock.json") || n.language.env.HasFiles("package.json") {
2022-01-26 04:09:21 -08:00
n.PackageManagerIcon = n.language.props.GetString(NPMIcon, " \uE71E")
}
}
2022-01-26 05:10:18 -08:00
func (n *Node) matchesVersionFile() bool {
fileVersion := n.language.env.FileContent(".nvmrc")
if len(fileVersion) == 0 {
return true
}
re := 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,
)
return regex.MatchString(re, fileVersion)
}