Skip to content

Commit

Permalink
feat(node): add PNPM segment and update Node context
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelschwobe authored and JanDeDobbeleer committed Jun 17, 2024
1 parent dc2915c commit 6554dbf
Show file tree
Hide file tree
Showing 9 changed files with 214 additions and 18 deletions.
3 changes: 3 additions & 0 deletions src/engine/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ const (
PHP SegmentType = "php"
// PLASTIC represents the plastic scm status and information
PLASTIC SegmentType = "plastic"
// pnpm version
PNPM SegmentType = "pnpm"
// Project version
PROJECT SegmentType = "project"
// PULUMI writes the pulumi user, store and stack
Expand Down Expand Up @@ -331,6 +333,7 @@ var Segments = map[SegmentType]func() SegmentWriter{
PERL: func() SegmentWriter { return &segments.Perl{} },
PHP: func() SegmentWriter { return &segments.Php{} },
PLASTIC: func() SegmentWriter { return &segments.Plastic{} },
PNPM: func() SegmentWriter { return &segments.Pnpm{} },
PROJECT: func() SegmentWriter { return &segments.Project{} },
PULUMI: func() SegmentWriter { return &segments.Pulumi{} },
PYTHON: func() SegmentWriter { return &segments.Python{} },
Expand Down
8 changes: 7 additions & 1 deletion src/segments/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ type Node struct {
}

const (
// PnpmIcon illustrates PNPM is used
PnpmIcon properties.Property = "pnpm_icon"
// 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 shows if NPM, PNPM, or Yarn is used
FetchPackageManager properties.Property = "fetch_package_manager"
)

Expand Down Expand Up @@ -54,6 +56,10 @@ func (n *Node) loadContext() {
if !n.language.props.GetBool(FetchPackageManager, false) {
return
}
if n.language.env.HasFiles("pnpm-lock.yaml") {
n.PackageManagerIcon = n.language.props.GetString(PnpmIcon, "\U000F02C1")
return
}
if n.language.env.HasFiles("yarn.lock") {
n.PackageManagerIcon = n.language.props.GetString(YarnIcon, "\U000F011B")
return
Expand Down
17 changes: 12 additions & 5 deletions src/segments/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,29 +55,36 @@ func TestNodeMatchesVersionFile(t *testing.T) {
func TestNodeInContext(t *testing.T) {
cases := []struct {
Case string
HasYarn bool
hasPNPM bool
hasYarn bool
hasNPM bool
hasDefault bool
PkgMgrEnabled bool
ExpectedString string
}{
{Case: "no package manager file", ExpectedString: "", PkgMgrEnabled: true},
{Case: "yarn", HasYarn: true, ExpectedString: "yarn", PkgMgrEnabled: true},
{Case: "pnpm", hasPNPM: true, ExpectedString: "pnpm", PkgMgrEnabled: true},
{Case: "yarn", hasYarn: true, ExpectedString: "yarn", PkgMgrEnabled: true},
{Case: "npm", hasNPM: true, ExpectedString: "npm", PkgMgrEnabled: true},
{Case: "default", hasDefault: true, ExpectedString: "npm", PkgMgrEnabled: true},
{Case: "disabled", HasYarn: true, ExpectedString: "", PkgMgrEnabled: false},
{Case: "yarn and npm", HasYarn: true, hasNPM: true, ExpectedString: "yarn", PkgMgrEnabled: true},
{Case: "disabled by pnpm", hasPNPM: true, ExpectedString: "", PkgMgrEnabled: false},
{Case: "disabled by yarn", hasYarn: true, ExpectedString: "", PkgMgrEnabled: false},
{Case: "pnpm and npm", hasPNPM: true, hasNPM: true, ExpectedString: "pnpm", PkgMgrEnabled: true},
{Case: "yarn and npm", hasYarn: true, hasNPM: true, ExpectedString: "yarn", PkgMgrEnabled: true},
{Case: "pnpm, yarn, and npm", hasPNPM: true, hasYarn: true, hasNPM: true, ExpectedString: "pnpm", PkgMgrEnabled: true},
}

for _, tc := range cases {
env := new(mock.MockedEnvironment)
env.On("HasFiles", "yarn.lock").Return(tc.HasYarn)
env.On("HasFiles", "pnpm-lock.yaml").Return(tc.hasPNPM)
env.On("HasFiles", "yarn.lock").Return(tc.hasYarn)
env.On("HasFiles", "package-lock.json").Return(tc.hasNPM)
env.On("HasFiles", "package.json").Return(tc.hasDefault)
node := &Node{
language: language{
env: env,
props: properties.Map{
PnpmIcon: "pnpm",
YarnIcon: "yarn",
NPMIcon: "npm",
FetchPackageManager: tc.PkgMgrEnabled,
Expand Down
34 changes: 34 additions & 0 deletions src/segments/pnpm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package segments

import (
"github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
)

type Pnpm struct {
language
}

func (n *Pnpm) Enabled() bool {
return n.language.Enabled()
}

func (n *Pnpm) Template() string {
return " \U000F02C1 {{.Full}} "
}

func (n *Pnpm) Init(props properties.Properties, env platform.Environment) {
n.language = language{
env: env,
props: props,
extensions: []string{"package.json", "pnpm-lock.yaml"},
commands: []*cmd{
{
executable: "pnpm",
args: []string{"--version"},
regex: `(?P<version>((?P<major>[0-9]+).(?P<minor>[0-9]+).(?P<patch>[0-9]+)))`,
},
},
versionURLTemplate: "https://github.com/pnpm/pnpm/releases/tag/v{{ .Full }}",
}
}
31 changes: 31 additions & 0 deletions src/segments/pnpm_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package segments

import (
"fmt"
"testing"

"github.com/alecthomas/assert"
)

func TestPnpm(t *testing.T) {
cases := []struct {
Case string
ExpectedString string
Version string
}{
{Case: "1.0.0", ExpectedString: "\U000F02C1 1.0.0", Version: "1.0.0"},
}
for _, tc := range cases {
params := &mockedLanguageParams{
cmd: "pnpm",
versionParam: "--version",
versionOutput: tc.Version,
extension: "package.json",
}
env, props := getMockedLanguageEnv(params)
pnpm := &Pnpm{}
pnpm.Init(props, env)
assert.True(t, pnpm.Enabled(), fmt.Sprintf("Failed in case: %s", tc.Case))
assert.Equal(t, tc.ExpectedString, renderTemplate(env, pnpm.Template(), pnpm), fmt.Sprintf("Failed in case: %s", tc.Case))
}
}
50 changes: 50 additions & 0 deletions themes/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@
"python",
"php",
"plastic",
"pnpm",
"project",
"pulumi",
"quasar",
Expand Down Expand Up @@ -4589,6 +4590,55 @@
}
}
}
},
{
"if": {
"properties": {
"type": {
"const": "pnpm"
}
}
},
"then": {
"title": "PNPM Segment",
"description": "https://ohmyposh.dev/docs/segments/pnpm",
"properties": {
"properties": {
"properties": {
"home_enabled": {
"$ref": "#/definitions/home_enabled"
},
"fetch_version": {
"$ref": "#/definitions/fetch_version"
},
"display_mode": {
"$ref": "#/definitions/display_mode"
},
"missing_command_text": {
"$ref": "#/definitions/missing_command_text"
},
"version_url_template": {
"$ref": "#/definitions/version_url_template"
},
"cache_version": {
"$ref": "#/definitions/cache_version"
},
"extensions": {
"type": "array",
"title": "Extensions",
"description": "The extensions to look for when determining if a folder is an PNPM workspace",
"default": ["package.json", "pnpm-lock.yaml"],
"items": {
"type": "string"
}
},
"folders": {
"$ref": "#/definitions/folders"
}
}
}
}
}
}
]
}
Expand Down
25 changes: 13 additions & 12 deletions website/docs/segments/node.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ import Config from "@site/src/components/Config.js";
| `missing_command_text` | `string` | | text to display when the command is missing |
| `display_mode` | `string` | `context` | <ul><li>`always`: the segment is always displayed</li><li>`files`: the segment is only displayed when file `extensions` listed are present</li><li>`context`: displays the segment when the environment or files is active</li></ul> |
| `version_url_template` | `string` | | a go [text/template][go-text-template] [template][templates] that creates the URL of the version info / release notes |
| `fetch_package_manager` | `boolean` | `false` | define if the current project uses Yarn or NPM |
| `fetch_package_manager` | `boolean` | `false` | define if the current project uses PNPM, Yarn, or NPM |
| `pnpm_icon` | `string` | `\uF02C1` | the icon/text to display when using PNPM |
| `yarn_icon` | `string` | `\uF011B` | the icon/text to display when using Yarn |
| `npm_icon` | `string` | `\uE71E` | the icon/text to display when using NPM |
| `extensions` | `[]string` | `*.js, *.ts, package.json, .nvmrc, pnpm-workspace.yaml, .pnpmfile.cjs, .vue` | allows to override the default list of file extensions to validate |
Expand All @@ -51,17 +52,17 @@ import Config from "@site/src/components/Config.js";

### Properties

| Name | Type | Description |
| --------------------- | --------- | ------------------------------------------------------------------- |
| `.Full` | `string` | the full version |
| `.Major` | `string` | major number |
| `.Minor` | `string` | minor number |
| `.Patch` | `string` | patch number |
| `.URL` | `string` | URL of the version info / release notes |
| `.Error` | `string` | error encountered when fetching the version string |
| `.PackageManagerIcon` | `string` | the Yarn or NPM icon when setting `fetch_package_manager` to `true` |
| `.Mismatch` | `boolean` | true if the version in `.nvmrc` is not equal to `.Full` |
| `.Expected` | `string` | the expected version set in `.nvmrc` |
| Name | Type | Description |
| --------------------- | --------- | -------------------------------------------------------------------------- |
| `.Full` | `string` | the full version |
| `.Major` | `string` | major number |
| `.Minor` | `string` | minor number |
| `.Patch` | `string` | patch number |
| `.URL` | `string` | URL of the version info / release notes |
| `.Error` | `string` | error encountered when fetching the version string |
| `.PackageManagerIcon` | `string` | the PNPM, Yarn, or NPM icon when setting `fetch_package_manager` to `true` |
| `.Mismatch` | `boolean` | true if the version in `.nvmrc` is not equal to `.Full` |
| `.Expected` | `string` | the expected version set in `.nvmrc` |

[go-text-template]: https://golang.org/pkg/text/template/
[templates]: /docs/configuration/templates
Expand Down
63 changes: 63 additions & 0 deletions website/docs/segments/pnpm.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
id: pnpm
title: PNPM
sidebar_label: PNPM
---

## What

Display the currently active [pnpm][pnpm-docs] version.

## Sample Configuration

import Config from "@site/src/components/Config.js";

<Config
data={{
type: "pnpm",
style: "diamond",
leading_diamond: "\uE0B2",
trailing_diamond: "\uE0D6",
foreground: "#000000",
background: "#F9AD00",
template: " \uDB80\uDEC1 {{ .Full }} ",
}}
/>

## Properties

| Name | Type | Default | Description |
| ---------------------- | :--------: | :----------------------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `home_enabled` | `boolean` | `false` | display the segment in the HOME folder or not |
| `fetch_version` | `boolean` | `true` | fetch the PNPM version |
| `missing_command_text` | `string` | | text to display when the command is missing |
| `display_mode` | `string` | `context` | <ul><li>`always`: the segment is always displayed</li><li>`files`: the segment is only displayed when file `extensions` listed are present</li><li>`context`: displays the segment when the environment or files is active</li></ul> |
| `version_url_template` | `string` | | a go [text/template][go-text-template] [template][templates] that creates the URL of the version info / release notes |
| `extensions` | `[]string` | `package.json, yarn-lock.yaml` | allows to override the default list of file extensions to validate |
| `folders` | `[]string` | | allows to override the list of folder names to validate |
| `cache_version` | `boolean` | `false` | cache the executable's version or not |

## Template ([info][templates])

:::note default template

```template
\uDB80\uDEC1 {{.Full}}
```

:::

### Properties

| Name | Type | Description |
| -------- | -------- | -------------------------------------------------- |
| `.Full` | `string` | the full version |
| `.Major` | `string` | major number |
| `.Minor` | `string` | minor number |
| `.Patch` | `string` | patch number |
| `.URL` | `string` | URL of the version info / release notes |
| `.Error` | `string` | error encountered when fetching the version string |

[go-text-template]: https://golang.org/pkg/text/template/
[templates]: /docs/configuration/templates
[pnpm-docs]: https://pnpm.io
1 change: 1 addition & 0 deletions website/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ module.exports = {
"segments/perl",
"segments/php",
"segments/plastic",
"segments/pnpm",
"segments/project",
"segments/pulumi",
"segments/python",
Expand Down

0 comments on commit 6554dbf

Please sign in to comment.