I am connected to a remote server via SSH using the Remote - SSH plugin by VSCode. I would like to run a task with the path to the remote file on the local machine.
Specifically, I have a HTML file on the remote machine that I would like to preview/interact with on my local windows machine using an URL.
My task.json should look something like this:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "Open Remote HTML",
"type": "shell",
"command": "start https://url_to_remote.com/${file}"
}
]
}
Edit: Additionally, I have tasks that should continue run on the remote machine and I would like the option to run on the local or host machine on a per task basis.
Unfortunately running a task locally while using the Remote SSH extension is not possible, though it looks like there's sufficient demand and it has been added to their backlog:
https://github.com/microsoft/vscode-remote-release/issues/168
If you're determined to have this, though, a very hacky way to go about this would be to:
Set up a basic webserver on your local machine at localhostIP
Expose an endpoint that reads a URL parameter named file (http://localhostIP:3000/?file=blah.html) and opens a new tab in firefox/your browser of choice like so:
firefox --new-tab https://remotehostIP:3000/blah.html
Here's a one line web server runnable from bash through a local terminal (ctrl+shift+P, search integrated to start a local terminal while connected to remote host). This web server accomplishes steps 1 and 2 mentioned here (ctrl+c to stop web server):
echo "const http = require('http');const {exec} = require('child_process');http.createServer(function (req, res) {h='http://remotehostIP:3000/';exec(\`firefox --new-tab \${h+req.url.slice('/?file='.length)}\`);res.end();}).listen(3000);" | node
Indented, easier to read code:
const http = require('http');
const {exec} = require('child_process');
http.createServer(function (req, res) {
h="http://remotehostIP:3000/";
exec(`firefox --new-tab ${h+req.url.slice("/?file=".length)}`);
res.end();
}).listen(3000);
Lastly, create a task that curls and hits that localhostIP webserver and passes along ${relativeFile}. This task runs on the remote machine, but will trigger your localhostIP's webserver to open firefox. Here's what that might look like
"tasks": [
{
"label": "show current html file",
"type": "shell",
"command": "curl",
"args": ["http://localhostIP:3000/?file=${relativeFile}"],
"group": {
"kind": "build",
"isDefault": true
}
}
]
I think for now this is about as good as you're going to get with a best-effort solution while not investing a bunch of time. Let me know if you run into any issues!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With