Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle Node Build Window on Sublime Text 2

I would like to be able to use one shortcut for running and killing node process including showing and hiding build window as needed.

I press ctrl + B:

  1. Show Build Window
  2. Run Node process

I press ctrl + B again:

  1. Kill Node process
  2. Close Build Window.

I also need kill signal to be sent so that I can read it in node application and perform couple of actions prior to the exiting.

Currently the first part of what I need is working but to close node app I need to ctrl + shift + B and then hit esc to close build window.

Is this possible and if it is, how?

EDIT 1 (18.09.14)

Made a sublime text plugin that does exactly what I described above. Currently tested on ST3 (Windows and Linux). Only issue currently is that on windows platform, your node script wont get proper kill signal (SIGINT or something similar) for pre-exit proecedures. I use sublime texts own console for io procedures - very convenient. Might release plugin soon if people feel interested in it.

like image 485
Tauri28 Avatar asked Nov 02 '22 11:11

Tauri28


1 Answers

One approach would be to, in your build system, first kill the running node process & if that's not successful pass the current file to node. So your node.sublime-build might look like this:

{
    "cmd": [ "killall node || node \"$file\"" ],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.js",
    "shell": true
}

Sublime only allows one command which is passed multiple arguments, so something like "cmd": [ "killall node ||", "node", "$file"] wasn't working, though I think it worked previously in Sublime 2. Thus the need to wrap $file in escaped quotes, because otherwise the command will fail if the path to $file has a space in it. Also, || is pivotal here, because killall exits with a status of 1 (error) if there were no processes to kill, so the first time it will run node but the second time it won't.

Note that, since we're working in the shell here, this is platform-specific. I don't know how to do the equivalent of || on Windows, but you'd just need something like this:

{
    "cmd": [ "killall node || node \"$file\"" ],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.js",
    "shell": true
    "windows": {
        "cmd": [ "insert whatever works here..." ]
    }
}

One big limitation is that this kills all running node processes; that'd be annoying if you want to keep something running in the background. If someone knows how to target a specific node process in a single command, please add to my answer.

Secondly, you could wrap the cmd in a bash script, which would make it easier to do multiple things like kill a particular process. Then the cmd array would be [ "bash", "name-of-script.sh" ]. Using lsof it would be rather easy to get the particular node process that's running your current file. You could also use bash -c as the command and then paste a whole script into the second element of the cmd array.

Hope that helps. I'm missing one piece of your requirement: ESC will still be needed to close the build window. Not sure what else can be done there without writing a Sublime plug-in.

like image 195
phette23 Avatar answered Nov 15 '22 03:11

phette23