oh-my-posh/src/segment_node.go

79 lines
2 KiB
Go
Raw Normal View History

2020-09-17 07:43:45 -07:00
package main
import "fmt"
2020-10-01 11:57:02 -07: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 Property = "yarn_icon"
// NPMIcon illustrates NPM is used
NPMIcon Property = "npm_icon"
// FetchPackageManager shows if NPM or Yarn is used
FetchPackageManager Property = "fetch_package_manager"
)
2020-10-01 11:57:02 -07:00
func (n *node) string() string {
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-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
}
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
}
func (n *node) loadContext() {
if !n.language.props.getOneOfBool(FetchPackageManager, DisplayPackageManager, false) {
return
}
if n.language.env.hasFiles("yarn.lock") {
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") {
n.PackageManagerIcon = n.language.props.getString(NPMIcon, " \uE71E")
}
}
func (n *node) matchesVersionFile() bool {
fileVersion := n.language.env.getFileContent(".nvmrc")
if len(fileVersion) == 0 {
return true
}
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,
)
return matchString(regex, fileVersion)
}