Skip to content

Commit

Permalink
allow editing scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Apr 12, 2020
1 parent 10eb466 commit a490a09
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions pkg/commands/npm_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,19 @@ func (m *NpmManager) EditDepConstraint(dep *Dependency, packageJsonPath string,

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

func (m *NpmManager) EditScript(scriptName string, packageJsonPath string, newName string, newCommand string) error {
config, err := ioutil.ReadFile(packageJsonPath)
if err != nil {
return err
}

updatedConfig := jsonparser.Delete(config, "scripts", scriptName)

updatedConfig, err = jsonparser.Set(updatedConfig, []byte(fmt.Sprintf("\"%s\"", newCommand)), "scripts", newName)
if err != nil {
return err
}

return ioutil.WriteFile(packageJsonPath, updatedConfig, 0644)
}
6 changes: 6 additions & 0 deletions pkg/gui/keybindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,12 @@ func (gui *Gui) GetInitialKeybindings() []*Binding {
Handler: gui.wrappedScriptHandler(gui.handleRemoveScript),
Description: "remove script from package.json",
},
{
ViewName: "scripts",
Key: gui.getKey("universal.edit"),
Handler: gui.wrappedScriptHandler(gui.handleEditScript),
Description: "edit script",
},
{
ViewName: "deps",
Key: gui.getKey("universal.install"),
Expand Down
10 changes: 10 additions & 0 deletions pkg/gui/scripts_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,13 @@ func (gui *Gui) wrappedScriptHandler(f func(*commands.Script) error) func(*gocui
return f(pkg)
})
}

func (gui *Gui) handleEditScript(script *commands.Script) error {
return gui.createPromptPanel(gui.getScriptsView(), "Script name:", script.Name, func(newName string) error {
return gui.createPromptPanel(gui.getScriptsView(), "Script command:", script.Command, func(newCommand string) error {
return gui.surfaceError(
gui.NpmManager.EditScript(script.Name, gui.currentPackage().ConfigPath(), newName, newCommand),
)
})
})
}

0 comments on commit a490a09

Please # to comment.