Skip to content

Commit

Permalink
move ID functions into model
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Apr 12, 2020
1 parent 1a47dee commit 0257edd
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 31 deletions.
9 changes: 8 additions & 1 deletion pkg/commands/dependency.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package commands

import "path/filepath"
import (
"fmt"
"path/filepath"
)

type Dependency struct {
Name string
Expand All @@ -19,3 +22,7 @@ func (d *Dependency) Linked() bool {
func (d *Dependency) ConfigPath() string {
return filepath.Join(d.Path, "package.json")
}

func (d *Dependency) ID() string {
return fmt.Sprintf("dep:%s|kind:%s", d.Path, d.Kind)
}
6 changes: 5 additions & 1 deletion pkg/commands/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func (p *Package) SortedDependencies() []*Dependency {
func (p *Package) SortedScripts() []*Script {
scripts := make([]*Script, 0, len(p.Config.Scripts))
for name, command := range p.Config.Scripts {
scripts = append(scripts, &Script{Name: name, Command: command})
scripts = append(scripts, &Script{Name: name, Command: command, ParentPackagePath: p.Path})
}
sort.Slice(scripts, func(i, j int) bool { return strings.Compare(scripts[i].Name, scripts[j].Name) < 0 })
return scripts
Expand All @@ -138,3 +138,7 @@ func (p *Package) SortedScripts() []*Script {
func (p *Package) ConfigPath() string {
return filepath.Join(p.Path, "package.json")
}

func (p *Package) ID() string {
return fmt.Sprintf("package:%s", p.Path)
}
11 changes: 9 additions & 2 deletions pkg/commands/script.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
package commands

import "fmt"

type Script struct {
Name string
Command string
Name string
Command string
ParentPackagePath string
}

func (s *Script) ID() string {
return fmt.Sprintf("package:%s|script:%s", s.ParentPackagePath, s.Name)
}
16 changes: 6 additions & 10 deletions pkg/gui/dependencies_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (gui *Gui) handleDepSelect(g *gocui.Gui, v *gocui.View) error {
} else {
gui.renderString("secondary", "dependency not present in node_modules")
}
gui.activateContextView(gui.depContextKey(dep))
gui.activateContextView(dep.ID())
return nil
}

Expand All @@ -57,7 +57,7 @@ func (gui *Gui) handleDepInstall() error {
}

cmdStr := fmt.Sprintf("npm install %s", dep.Name)
return gui.newMainCommand(cmdStr, gui.depContextKey(dep))
return gui.newMainCommand(cmdStr, dep.ID())
}

func (gui *Gui) handleDepUpdate() error {
Expand All @@ -67,7 +67,7 @@ func (gui *Gui) handleDepUpdate() error {
}

cmdStr := fmt.Sprintf("npm update %s", dep.Name)
return gui.newMainCommand(cmdStr, gui.depContextKey(dep))
return gui.newMainCommand(cmdStr, dep.ID())
}

func (gui *Gui) handleOpenDepPackageConfig() error {
Expand Down Expand Up @@ -99,7 +99,7 @@ func (gui *Gui) handleDepUninstall() error {
{
displayStrings: []string{"uninstall", utils.ColoredString(uninstallStr, color.FgYellow)},
onPress: func() error {
return gui.newMainCommand(uninstallStr, gui.depContextKey(selectedDep))
return gui.newMainCommand(uninstallStr, selectedDep.ID())
},
},
}
Expand All @@ -117,21 +117,17 @@ func (gui *Gui) handleDepUninstall() error {
{
displayStrings: []string{"uninstall and save", utils.ColoredString(uninstallAndSaveCmdStr, color.FgYellow)},
onPress: func() error {
return gui.newMainCommand(uninstallAndSaveCmdStr, gui.depContextKey(selectedDep))
return gui.newMainCommand(uninstallAndSaveCmdStr, selectedDep.ID())
},
},
{
displayStrings: []string{"just uninstall", utils.ColoredString(uninstallCmdStr, color.FgYellow)},
onPress: func() error {
return gui.newMainCommand(uninstallCmdStr, gui.depContextKey(selectedDep))
return gui.newMainCommand(uninstallCmdStr, selectedDep.ID())
},
},
}
}

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

func (gui *Gui) depContextKey(dep *commands.Dependency) string {
return fmt.Sprintf("package:%s|dep:%s|kind:%s", gui.currentPackage().Path, dep.Name, dep.Kind)
}
16 changes: 6 additions & 10 deletions pkg/gui/packages_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (gui *Gui) handlePackageSelect(g *gocui.Gui, v *gocui.View) error {
summary := presentation.PackageSummary(pkg.Config)
summary = fmt.Sprintf("%s\nPath: %s", summary, utils.ColoredString(pkg.Path, color.FgCyan))
gui.renderString("secondary", summary)
gui.activateContextView(gui.packageContextKey(pkg))
gui.activateContextView(pkg.ID())
return nil
}

Expand Down Expand Up @@ -140,11 +140,7 @@ func (gui *Gui) handleLinkPackage() error {
}
}

return gui.newMainCommand(cmdStr, gui.packageContextKey(selectedPkg))
}

func (gui *Gui) packageContextKey(pkg *commands.Package) string {
return fmt.Sprintf("package:%s", pkg.Path)
return gui.newMainCommand(cmdStr, selectedPkg.ID())
}

func (gui *Gui) handleGlobalLinkPackage() error {
Expand All @@ -164,7 +160,7 @@ func (gui *Gui) handleGlobalLinkPackage() error {
cmdStr = "npm link"
}

return gui.newMainCommand(cmdStr, gui.packageContextKey(selectedPkg))
return gui.newMainCommand(cmdStr, selectedPkg.ID())
}

func (gui *Gui) handleInstall() error {
Expand All @@ -180,7 +176,7 @@ func (gui *Gui) handleInstall() error {
cmdStr = "npm install --prefix " + selectedPkg.Path
}

return gui.newMainCommand(cmdStr, gui.packageContextKey(selectedPkg))
return gui.newMainCommand(cmdStr, selectedPkg.ID())
}

func (gui *Gui) handleBuild() error {
Expand All @@ -196,7 +192,7 @@ func (gui *Gui) handleBuild() error {
cmdStr = "npm run build --prefix " + selectedPkg.Path
}

return gui.newMainCommand(cmdStr, gui.packageContextKey(selectedPkg))
return gui.newMainCommand(cmdStr, selectedPkg.ID())
}

func (gui *Gui) handleOpenPackageConfig() error {
Expand Down Expand Up @@ -254,5 +250,5 @@ func (gui *Gui) handlePackPackage() error {
cmdStr = fmt.Sprintf("npm pack %s", selectedPkg.Path)
}

return gui.newMainCommand(cmdStr, gui.packageContextKey(selectedPkg))
return gui.newMainCommand(cmdStr, selectedPkg.ID())
}
10 changes: 3 additions & 7 deletions pkg/gui/scripts_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,18 @@ func (gui *Gui) handleScriptSelect(g *gocui.Gui, v *gocui.View) error {
return nil
}
gui.renderString("secondary", presentation.ScriptSummary(script))
gui.activateContextView(gui.scriptContextKey(script))
gui.activateContextView(script.ID())
return nil
}

func (gui *Gui) handleRunScript() error {
script := gui.getSelectedScript()

return gui.createPromptPanel(gui.getScriptsView(), "run script", fmt.Sprintf("npm run %s ", script.Name), func(input string) error {
return gui.newMainCommand(input, gui.scriptContextKey(script))
return gui.createPromptPanel(gui.getScriptsView(), "run script", fmt.Sprintf("npm run %s", script.Name), func(input string) error {
return gui.newMainCommand(input, script.ID())
})
}

func (gui *Gui) scriptContextKey(script *commands.Script) string {
return fmt.Sprintf("package:%s|script:%s", gui.currentPackage().Path, script.Name)
}

func (gui *Gui) handleRemoveScript() error {
script := gui.getSelectedScript()

Expand Down

0 comments on commit 0257edd

Please sign in to comment.