Toggle Node Build Window on Sublime Text 2

2019-06-20 11:39发布

问题:

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.

回答1:

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.



回答2:

Anyway, I fix it in windows like that, stupid? who care

{
    "shell": true,
    "windows": {
        "cmd": "(TASKKILL /F /IM node.exe || 1)&& node \"$file\""
    }
}


回答3:

I did it the same way but not checking for any file other than .js files

{
    "cmd": [ "killall node || node \"$file\"" ],
    "selector": "*.js",
    "shell": true
    "windows": {
        "cmd": "(TASKKILL /F /IM node.exe || 1)&& node \"$file\""
    }
}

This will kill any running node and restart the file whose tab is open