2022-01-26 06:54:36 -08:00
|
|
|
package segments
|
2020-09-17 07:43:45 -07:00
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"oh-my-posh/environment"
|
2022-01-26 04:53:35 -08:00
|
|
|
"oh-my-posh/properties"
|
2022-01-26 01:23:18 -08:00
|
|
|
"oh-my-posh/regex"
|
|
|
|
)
|
2021-04-17 04:47:28 -07:00
|
|
|
|
2022-01-26 05:10:18 -08: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
|
2022-01-26 04:53:35 -08:00
|
|
|
YarnIcon properties.Property = "yarn_icon"
|
2021-04-17 04:47:28 -07:00
|
|
|
// NPMIcon illustrates NPM is used
|
2022-01-26 04:53:35 -08:00
|
|
|
NPMIcon properties.Property = "npm_icon"
|
2021-12-04 04:06:05 -08:00
|
|
|
// FetchPackageManager shows if NPM or Yarn is used
|
2022-01-26 04:53:35 -08:00
|
|
|
FetchPackageManager properties.Property = "fetch_package_manager"
|
2021-04-17 04:47:28 -07:00
|
|
|
)
|
|
|
|
|
2022-01-26 05:26:56 -08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-01-26 05:26:56 -08: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,
|
2022-02-03 02:48:21 -08:00
|
|
|
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-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
|
|
|
}
|
|
|
|
|
2022-01-26 05:26:56 -08:00
|
|
|
func (n *Node) Enabled() bool {
|
2022-02-28 04:55:33 -08:00
|
|
|
if n.language.Enabled() {
|
|
|
|
n.Mismatch = !n.matchesVersionFile()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2020-09-17 07:43:45 -07:00
|
|
|
}
|
2021-02-06 22:06:24 -08: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) {
|
2021-04-17 04:47:28 -07:00
|
|
|
return
|
|
|
|
}
|
2022-01-23 12:37:51 -08:00
|
|
|
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
|
2021-04-17 04:47:28 -07:00
|
|
|
}
|
2022-01-23 12:37:51 -08:00
|
|
|
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")
|
2021-04-17 04:47:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 05:10:18 -08:00
|
|
|
func (n *Node) matchesVersionFile() bool {
|
2022-01-23 12:37:51 -08:00
|
|
|
fileVersion := n.language.env.FileContent(".nvmrc")
|
2021-02-15 11:51:48 -08:00
|
|
|
if len(fileVersion) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
2021-11-15 02:35:45 -08:00
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
re := fmt.Sprintf(
|
2021-11-15 02:35:45 -08:00
|
|
|
`(?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
|
|
|
)
|
|
|
|
|
2022-01-26 01:23:18 -08:00
|
|
|
return regex.MatchString(re, fileVersion)
|
2021-02-06 22:06:24 -08:00
|
|
|
}
|