Skip to content

Commit

Permalink
allow modifying constraint of dep
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Apr 12, 2020
1 parent e392a88 commit 10eb466
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 54 deletions.
36 changes: 29 additions & 7 deletions pkg/commands/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (
)

type Dependency struct {
Name string
Version string
LinkPath string
Present bool
PackageConfig *PackageConfig
Path string
Kind string
Name string
Constraint string
LinkPath string
Present bool
PackageConfig *PackageConfig
Path string
Kind string
ParentPackagePath string
}

func (d *Dependency) Linked() bool {
Expand All @@ -26,3 +27,24 @@ func (d *Dependency) ConfigPath() string {
func (d *Dependency) ID() string {
return fmt.Sprintf("dep:%s|kind:%s", d.Path, d.Kind)
}

func KindKeyMap() map[string]string {
return map[string]string{
"prod": "dependencies",
"dev": "devDependencies",
"optional": "optionalDependencies",
"peer": "peerDependencies",
}
}

func KindFlagMap() map[string]string {
return map[string]string{
"prod": "--save-prod",
"dev": "--save-dev",
"optional": "--save-optional",
}
}

func (d *Dependency) kindKey() string {
return KindKeyMap()[d.Kind]
}
16 changes: 16 additions & 0 deletions pkg/commands/npm_manager.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -126,6 +127,7 @@ func (m *NpmManager) GetDeps(currentPkg *Package) ([]*Dependency, error) {
for _, dep := range deps {
depPath := filepath.Join(currentPkg.Path, "node_modules", dep.Name)
dep.Path = depPath
dep.ParentPackagePath = currentPkg.Path
fileInfo, err := os.Lstat(depPath)
if err != nil {
// must not be present in node modules
Expand Down Expand Up @@ -174,3 +176,17 @@ func (m *NpmManager) RemoveScript(scriptName string, packageJsonPath string) err

return ioutil.WriteFile(packageJsonPath, updatedConfig, 0644)
}

func (m *NpmManager) EditDepConstraint(dep *Dependency, packageJsonPath string, constraint string) error {
config, err := ioutil.ReadFile(packageJsonPath)
if err != nil {
return err
}

updatedConfig, err := jsonparser.Set(config, []byte(fmt.Sprintf("\"%s\"", constraint)), dep.kindKey(), dep.Name)
if err != nil {
return err
}

return ioutil.WriteFile(packageJsonPath, updatedConfig, 0644)
}
6 changes: 3 additions & 3 deletions pkg/commands/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ func (p *Package) SortedDependencies() []*Dependency {
depsForKind := make([]*Dependency, 0, len(mapping.depMap))
for name, constraint := range mapping.depMap {
depsForKind = append(depsForKind, &Dependency{
Name: name,
Version: constraint,
Kind: mapping.kind,
Name: name,
Constraint: constraint,
Kind: mapping.kind,
})
}
sort.Slice(depsForKind, func(i, j int) bool { return strings.Compare(depsForKind[i].Name, depsForKind[j].Name) < 0 })
Expand Down
71 changes: 29 additions & 42 deletions pkg/gui/dependencies_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gui

import (
"fmt"
"path/filepath"

"github.com/fatih/color"
"github.com/go-errors/errors"
Expand Down Expand Up @@ -133,29 +134,18 @@ func (gui *Gui) wrappedDependencyHandler(f func(*commands.Dependency) error) fun
}

func (gui *Gui) handleChangeDepType(dep *commands.Dependency) error {
installProd := fmt.Sprintf("npm install --save-prod %s", dep.Name)
installDev := fmt.Sprintf("npm install --save-dev %s", dep.Name)
installOptional := fmt.Sprintf("npm install --save-optional %s", dep.Name)

menuItems := []*menuItem{
{
displayStrings: []string{"dependencies", utils.ColoredString(installProd, color.FgYellow)},
onPress: func() error {
return gui.newMainCommand(installProd, dep.ID())
},
},
{
displayStrings: []string{"devDependencies", utils.ColoredString(installDev, color.FgYellow)},
onPress: func() error {
return gui.newMainCommand(installDev, dep.ID())
},
},
{
displayStrings: []string{"optionalDependencies", utils.ColoredString(installOptional, color.FgYellow)},
kindKeyMap := commands.KindKeyMap()
kindFlagMap := commands.KindFlagMap()
menuItems := make([]*menuItem, 0, len(commands.KindKeyMap()))
for kind := range kindFlagMap {
kind := kind
cmdStr := fmt.Sprintf("npm install %s %s", kindFlagMap[kind], dep.Name)
menuItems = append(menuItems, &menuItem{
displayStrings: []string{kindKeyMap[kind], utils.ColoredString(cmdStr, color.FgYellow)},
onPress: func() error {
return gui.newMainCommand(installOptional, dep.ID())
return gui.newMainCommand(cmdStr, dep.ID())
},
},
})
}

return gui.createMenu("Change dependency type", menuItems, createMenuOptions{showCancel: true})
Expand All @@ -171,30 +161,27 @@ func (gui *Gui) handleAddDependency(dep *commands.Dependency) error {
})
}

installProd := "npm install --save-prod"
installDev := "npm install --save-dev"
installOptional := "npm install --save-optional"

menuItems := []*menuItem{
{
displayStrings: []string{"dependencies", utils.ColoredString(installProd, color.FgYellow)},
onPress: func() error {
return prompt(installProd)
},
},
{
displayStrings: []string{"devDependencies", utils.ColoredString(installDev, color.FgYellow)},
onPress: func() error {
return prompt(installDev)
},
},
{
displayStrings: []string{"optionalDependencies", utils.ColoredString(installOptional, color.FgYellow)},
kindKeyMap := commands.KindKeyMap()
kindFlagMap := commands.KindFlagMap()
menuItems := make([]*menuItem, 0, len(commands.KindKeyMap()))
for kind := range kindFlagMap {
kind := kind
cmdStr := fmt.Sprintf("npm install %s", kindFlagMap[kind])
menuItems = append(menuItems, &menuItem{
displayStrings: []string{kindKeyMap[kind], utils.ColoredString(cmdStr, color.FgYellow)},
onPress: func() error {
return prompt(installOptional)
return prompt(cmdStr)
},
},
})
}

return gui.createMenu("Install dependency to:", menuItems, createMenuOptions{showCancel: true})
}

func (gui *Gui) handleEditDepConstraint(dep *commands.Dependency) error {
return gui.createPromptPanel(gui.getDepsView(), "Edit constraint", dep.Constraint, func(input string) error {

packageConfigPath := filepath.Join(dep.ParentPackagePath, "package.json")
return gui.NpmManager.EditDepConstraint(dep, packageConfigPath, input)
})
}
6 changes: 6 additions & 0 deletions pkg/gui/keybindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,12 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
Handler: gui.wrappedDependencyHandler(gui.handleAddDependency),
Description: fmt.Sprintf("%s new dependency", utils.ColoredString("`npm install`", color.FgYellow)),
},
{
ViewName: "deps",
Key: gui.getKey("universal.edit"),
Handler: gui.wrappedDependencyHandler(gui.handleEditDepConstraint),
Description: "edit dependency constraint",
},
}

for _, viewName := range []string{"status", "packages", "deps", "scripts", "menu"} {
Expand Down
4 changes: 2 additions & 2 deletions pkg/gui/presentation/dependencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func getDepDisplayStrings(d *commands.Dependency, commandView *commands.CommandV
if d.Linked() {
localVersionCol = utils.ColoredString("linked: "+d.LinkPath, color.FgCyan)
} else if d.PackageConfig != nil {
status, ok := semverStatus(d.PackageConfig.Version, d.Version)
status, ok := semverStatus(d.PackageConfig.Version, d.Constraint)
if ok {
localVersionCol = utils.ColoredString(d.PackageConfig.Version, color.FgGreen)
} else {
Expand All @@ -44,7 +44,7 @@ func getDepDisplayStrings(d *commands.Dependency, commandView *commands.CommandV
"optional": theme.DefaultTextColor,
}

return []string{commandView.Status(), d.Name, utils.ColoredString(d.Kind, kindColorMap[d.Kind]), utils.ColoredString(d.Version, color.FgMagenta), localVersionCol}
return []string{commandView.Status(), d.Name, utils.ColoredString(d.Kind, kindColorMap[d.Kind]), utils.ColoredString(d.Constraint, color.FgMagenta), localVersionCol}
}

func statusMap() map[int]string {
Expand Down

0 comments on commit 10eb466

Please # to comment.