Skip to content

Commit 52f260c

Browse files
feat(node): show NPM/Yarn context
resolves #653
1 parent 13e8102 commit 52f260c

File tree

5 files changed

+91
-3
lines changed

5 files changed

+91
-3
lines changed

docs/docs/segment-node.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,6 @@ returned node version
3636
- color_background: `boolean` - color the background or foreground for `version_mismatch_color` - defaults to `false`
3737
- version_mismatch_color: `string` [color][colors] - the color to use for `enable_version_mismatch` - defaults to
3838
segment's background or foreground color
39+
- display_package_manager: `boolean` - show whether the current project uses Yarn or NPM - defaults to `false`
40+
- yarn_icon: `string` - the icon/text to display when using Yarn - defaults to ` \uF61A`
41+
- npm_icon: `string` - the icon/text to display when using NPM - defaults to ` \uE71E`

src/segment_node.go

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
package main
22

3+
import "fmt"
4+
35
type node struct {
4-
language *language
6+
language *language
7+
packageManagerIcon string
58
}
69

10+
const (
11+
// YarnIcon illustrates Yarn is used
12+
YarnIcon Property = "yarn_icon"
13+
// NPMIcon illustrates NPM is used
14+
NPMIcon Property = "npm_icon"
15+
// DisplayPackageManager shows if NPM or Yarn is used
16+
DisplayPackageManager Property = "display_package_manager"
17+
)
18+
719
func (n *node) string() string {
8-
return n.language.string()
20+
version := n.language.string()
21+
return fmt.Sprintf("%s%s", version, n.packageManagerIcon)
922
}
1023

1124
func (n *node) init(props *properties, env environmentInfo) {
@@ -22,13 +35,26 @@ func (n *node) init(props *properties, env environmentInfo) {
2235
},
2336
versionURLTemplate: "[%[1]s](https://github.com/nodejs/node/blob/master/doc/changelogs/CHANGELOG_V%[2]s.md#%[1]s)",
2437
matchesVersionFile: n.matchesVersionFile,
38+
loadContext: n.loadContext,
2539
}
2640
}
2741

2842
func (n *node) enabled() bool {
2943
return n.language.enabled()
3044
}
3145

46+
func (n *node) loadContext() {
47+
if !n.language.props.getBool(DisplayPackageManager, false) {
48+
return
49+
}
50+
if n.language.env.hasFiles("yarn.lock") {
51+
n.packageManagerIcon = n.language.props.getString(YarnIcon, " \uF61A")
52+
}
53+
if n.language.env.hasFiles("package-lock.json") || n.language.env.hasFiles("package.json") {
54+
n.packageManagerIcon = n.language.props.getString(NPMIcon, " \uE71E")
55+
}
56+
}
57+
3258
func (n *node) matchesVersionFile() bool {
3359
fileVersion := n.language.env.getFileContent(".nvmrc")
3460
if len(fileVersion) == 0 {

src/segment_node_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,41 @@ func TestNodeMatchesVersionFile(t *testing.T) {
3434
assert.Equal(t, tc.Expected, node.matchesVersionFile(), tc.Case)
3535
}
3636
}
37+
38+
func TestNodeInContext(t *testing.T) {
39+
cases := []struct {
40+
Case string
41+
HasYarn bool
42+
hasNPM bool
43+
hasDefault bool
44+
PkgMgrEnabled bool
45+
ExpectedString string
46+
}{
47+
{Case: "no package manager file", ExpectedString: "", PkgMgrEnabled: true},
48+
{Case: "yarn", HasYarn: true, ExpectedString: "yarn", PkgMgrEnabled: true},
49+
{Case: "npm", hasNPM: true, ExpectedString: "npm", PkgMgrEnabled: true},
50+
{Case: "default", hasDefault: true, ExpectedString: "npm", PkgMgrEnabled: true},
51+
{Case: "disabled", HasYarn: true, ExpectedString: "", PkgMgrEnabled: false},
52+
}
53+
54+
for _, tc := range cases {
55+
env := new(MockedEnvironment)
56+
env.On("hasFiles", "yarn.lock").Return(tc.HasYarn)
57+
env.On("hasFiles", "package-lock.json").Return(tc.hasNPM)
58+
env.On("hasFiles", "package.json").Return(tc.hasDefault)
59+
node := &node{
60+
language: &language{
61+
env: env,
62+
props: &properties{
63+
values: map[Property]interface{}{
64+
YarnIcon: "yarn",
65+
NPMIcon: "npm",
66+
DisplayPackageManager: tc.PkgMgrEnabled,
67+
},
68+
},
69+
},
70+
}
71+
node.loadContext()
72+
assert.Equal(t, tc.ExpectedString, node.packageManagerIcon, tc.Case)
73+
}
74+
}

src/segment_python.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const (
1313
)
1414

1515
func (p *python) string() string {
16-
if p.venvName == "" || !p.language.props.getBool(DisplayVirtualEnv, true) {
16+
if p.venvName == "" {
1717
return p.language.string()
1818
}
1919
version := p.language.string()
@@ -51,6 +51,9 @@ func (p *python) enabled() bool {
5151
}
5252

5353
func (p *python) loadContext() {
54+
if !p.language.props.getBool(DisplayVirtualEnv, true) {
55+
return
56+
}
5457
venvVars := []string{
5558
"VIRTUAL_ENV",
5659
"CONDA_ENV_PATH",

themes/schema.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,6 +845,24 @@
845845
},
846846
"missing_command_text": {
847847
"$ref": "#/definitions/missing_command_text"
848+
},
849+
"display_package_manager": {
850+
"type": "boolean",
851+
"title": "Display Package Manager",
852+
"description": "Show whether the current project uses Yarn or NPM",
853+
"default": false
854+
},
855+
"yarn_icon": {
856+
"type": "string",
857+
"title": "Yarn Icon",
858+
"description": "Icon/text to use for Yarn",
859+
"default": " \uF61A"
860+
},
861+
"npm_icon": {
862+
"type": "string",
863+
"title": "NPM Icon",
864+
"description": "Icon/text to use for NPM",
865+
"default": " \uE71E"
848866
}
849867
}
850868
}

0 commit comments

Comments
 (0)