Running shell commands with ko/shell


I am trying to run a shell command on a file save. I’m using this script:

var filePath = require("ko/views").current().filePath;

However, all I ever get is “ko-shell: child process ended with code -1”. I’ve tried it with “echo” in case the shell couldn’t find “golint” but I get the same result. So, obviously I’m doing this wrong, but I don’t know what.

Eventually, when I get this working, I’d like to try something a bit more complex like this:

var filePath = require("ko/views").current().filePath;
var shell = require("ko/shell");
shell._showOutputInHud("golint",[filePath]), "Go Lint");

Is this the right approach?


Here’s what i used:

var shell = require('ko/shell');
var commandname = "blah blah shell command";
var process = shell.exec(commandname, {});
process.stdout.on('data', function(data){
  // do something

The command in my case was just building up some Git stuff, and then in my event handler I parse it an update the UI.


In addition to @neurobashing’s response, this is how you would have it show in the HUD:

require('ko/shell').exec("command foo", {runIn: "hud"});
1 Like

Ok, I’m back. :smile:

Thanks @neurobashing. How do you update the UI in your “do something”?

With the suggestion from @nathanr, this is what I have now:

var view = require('ko/views').current();
if (view.language === 'Go') {
    var filePath = view.filePath;
    require('ko/shell').exec('golint', [filePath], {runIn: 'hud'});

However, I still don’t see anything. I think I’m confused by what is meant by the “hud”. I would expect to see something appear in maybe the Command Output. Perhaps I’m misunderstanding what the “hud” is.

You are inventing a new argument with [filePath], my example only has 2 arguments.

Ah. You’re right. I had intended to change that to “run”.

I’ll try this again.

So it appears that runIn: 'hud' only works with exec. If I use exec and I concatenate my filePath to the command, I get the hud popup. Yay!

var view = require('ko/views').current();
if (view.language === 'Go') {
    var filePath = view.filePath;
    require('ko/shell').exec("golint '" + filePath + "'", {runIn: 'hud'});

What types of options would you pass to the “run” method? Would it make sense to allow it to use runIn: 'hud' too?

I did something w/ XUL as output, not the hud.

The exec method is for running a command using the Komodo interface, the run method is simply for running a process in a very opinionated manner.

These method names are probably a bit too generic, but as this is an SDK and backwards compatibility is important we likely will not be renaming them.

Sounds fair.

Thanks for the help.