feat(node): show NPM/Yarn context

resolves #653
This commit is contained in:
Jan De Dobbeleer 2021-04-17 13:47:28 +02:00 committed by Jan De Dobbeleer
parent 13e8102434
commit 52f260caf6
5 changed files with 91 additions and 3 deletions

View file

@ -36,3 +36,6 @@ returned node version
- color_background: `boolean` - color the background or foreground for `version_mismatch_color` - defaults to `false`
- version_mismatch_color: `string` [color][colors] - the color to use for `enable_version_mismatch` - defaults to
segment's background or foreground color
- display_package_manager: `boolean` - show whether the current project uses Yarn or NPM - defaults to `false`
- yarn_icon: `string` - the icon/text to display when using Yarn - defaults to ` \uF61A`
- npm_icon: `string` - the icon/text to display when using NPM - defaults to ` \uE71E`

View file

@ -1,11 +1,24 @@
package main
import "fmt"
type node struct {
language *language
language *language
packageManagerIcon string
}
const (
// YarnIcon illustrates Yarn is used
YarnIcon Property = "yarn_icon"
// NPMIcon illustrates NPM is used
NPMIcon Property = "npm_icon"
// DisplayPackageManager shows if NPM or Yarn is used
DisplayPackageManager Property = "display_package_manager"
)
func (n *node) string() string {
return n.language.string()
version := n.language.string()
return fmt.Sprintf("%s%s", version, n.packageManagerIcon)
}
func (n *node) init(props *properties, env environmentInfo) {
@ -22,6 +35,7 @@ func (n *node) init(props *properties, env environmentInfo) {
},
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,
}
}
@ -29,6 +43,18 @@ func (n *node) enabled() bool {
return n.language.enabled()
}
func (n *node) loadContext() {
if !n.language.props.getBool(DisplayPackageManager, false) {
return
}
if n.language.env.hasFiles("yarn.lock") {
n.packageManagerIcon = n.language.props.getString(YarnIcon, " \uF61A")
}
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 {

View file

@ -34,3 +34,41 @@ func TestNodeMatchesVersionFile(t *testing.T) {
assert.Equal(t, tc.Expected, node.matchesVersionFile(), tc.Case)
}
}
func TestNodeInContext(t *testing.T) {
cases := []struct {
Case string
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: "npm", hasNPM: true, ExpectedString: "npm", PkgMgrEnabled: true},
{Case: "default", hasDefault: true, ExpectedString: "npm", PkgMgrEnabled: true},
{Case: "disabled", HasYarn: true, ExpectedString: "", PkgMgrEnabled: false},
}
for _, tc := range cases {
env := new(MockedEnvironment)
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{
values: map[Property]interface{}{
YarnIcon: "yarn",
NPMIcon: "npm",
DisplayPackageManager: tc.PkgMgrEnabled,
},
},
},
}
node.loadContext()
assert.Equal(t, tc.ExpectedString, node.packageManagerIcon, tc.Case)
}
}

View file

@ -13,7 +13,7 @@ const (
)
func (p *python) string() string {
if p.venvName == "" || !p.language.props.getBool(DisplayVirtualEnv, true) {
if p.venvName == "" {
return p.language.string()
}
version := p.language.string()
@ -51,6 +51,9 @@ func (p *python) enabled() bool {
}
func (p *python) loadContext() {
if !p.language.props.getBool(DisplayVirtualEnv, true) {
return
}
venvVars := []string{
"VIRTUAL_ENV",
"CONDA_ENV_PATH",

View file

@ -845,6 +845,24 @@
},
"missing_command_text": {
"$ref": "#/definitions/missing_command_text"
},
"display_package_manager": {
"type": "boolean",
"title": "Display Package Manager",
"description": "Show whether the current project uses Yarn or NPM",
"default": false
},
"yarn_icon": {
"type": "string",
"title": "Yarn Icon",
"description": "Icon/text to use for Yarn",
"default": " \uF61A"
},
"npm_icon": {
"type": "string",
"title": "NPM Icon",
"description": "Icon/text to use for NPM",
"default": " \uE71E"
}
}
}