これは、なにをしたくて書いたもの?
個人的にはあまりVisual Studio Codeは使っていないのですが、Webブラウザーからアクセスする方法を覚えたのでメモして
おこうということで。
Visual Studio Code for the Web
Visual Studio Code for the Webについてはこちら。
Visual Studio Code for the Web
Webブラウザー上で動くVisual Studio Codeです。
実際に使うとなると、こちらですね。
これをローカルのVisual Studio Codeでやりたいというのが今回の話です。
方法としてはtunnelとserve-webの2つがあります。
tunnelというのはRemote - Tunnels拡張機能を使って、Visual Studio Code for the Webの接続先を別のホストにすることが
できる機能です。
Visual Studio Code for the Web / Use your own compute instance with Remote Tunnels
Remote - Tunnels - Visual Studio Marketplace
code tunnelコマンドを使うと、vscode.dev経由でこのコマンドを実行したホストに接続して作業できるようです。
$ code tunnel --help Create a tunnel that's accessible on vscode.dev from anywhere. Run `code tunnel --help` for more usage info Usage: code-tunnel tunnel [OPTIONS] [COMMAND] Commands: prune Delete all servers which are currently not running kill Stops any running tunnel on the system restart Restarts any running tunnel on the system status Gets whether there is a tunnel running on the current machine rename Rename the name of this machine associated with port forwarding service unregister Remove this machine's association with the port forwarding service user service (Preview) Manages the tunnel when installed as a system service, help Print this message or the help of the given subcommand(s) Options: --install-extension <INSTALL_EXTENSION> Requests that extensions be preloaded and installed on connecting servers --server-data-dir <SERVER_DATA_DIR> Specifies the directory that server data is kept in --extensions-dir <EXTENSIONS_DIR> Set the root path for extensions --reconnection-grace-time <RECONNECTION_GRACE_TIME> Reconnection grace time in seconds. Defaults to 10800 (3 hours) -h, --help Print help ADVANCED OPTIONS: --random-name Randomly name machine for port forwarding service --no-sleep Prevents the machine going to sleep while this command runs --name <NAME> Sets the machine name for port forwarding service --accept-server-license-terms If set, the user accepts the server license terms and the server will be started without a user prompt GLOBAL OPTIONS: --cli-data-dir <CLI_DATA_DIR> Directory where CLI metadata should be stored [env: VSCODE_CLI_DATA_DIR=] --verbose Print verbose output (implies --wait) --log <level> Log level to use [possible values: trace, debug, info, warn, error, critical, off]
今回見るのはこちらではなくcode serve-webです。
環境
今回の環境はこちら。
$ code --version 1.109.5 072586267e68ece9a47aa43f8c108e0dcbf44622 x64
serve-web
serve-webサブコマンドを使うと、このコマンドを実行して起動したサーバーに直接接続できます。
ドキュメントはないようなので、ヘルプの内容がすべてです。
$ code serve-web --help Runs a local web version of Visual Studio Code Usage: code-tunnel serve-web [OPTIONS] Options: --host <HOST> Host to listen on, defaults to 'localhost' --socket-path <SOCKET_PATH> --port <PORT> Port to listen on. If 0 is passed a random free port is picked [default: 8000] --connection-token <CONNECTION_TOKEN> A secret that must be included with all requests --connection-token-file <CONNECTION_TOKEN_FILE> A file containing a secret that must be included with all requests --without-connection-token Run without a connection token. Only use this if the connection is secured by other means --accept-server-license-terms If set, the user accepts the server license terms and the server will be started without a user prompt --server-base-path <SERVER_BASE_PATH> Specifies the path under which the web UI and the code server is provided --server-data-dir <SERVER_DATA_DIR> Specifies the directory that server data is kept in --commit-id <COMMIT_ID> Use a specific commit SHA for the client -h, --help Print help GLOBAL OPTIONS: --cli-data-dir <CLI_DATA_DIR> Directory where CLI metadata should be stored [env: VSCODE_CLI_DATA_DIR=] --verbose Print verbose output (implies --wait) --log <level> Log level to use [possible values: trace, debug, info, warn, error, critical, off]
ちなみに、Visual Studio Codeのヘルプを見ても、サブコマンドに現れています。
$ code --help 〜省略〜 Subcommands chat Pass in a prompt to run in a chat session in the current working directory. serve-web Run a server that displays the editor UI in browsers. tunnel Make the current machine accessible from vscode.dev or other machines through a secure tunnel.
起動してみましょう。
$ code serve-web
すると、こんな表示が出てきます。
* * Visual Studio Code Server * * By using the software, you agree to * the Visual Studio Code Server License Terms (https://aka.ms/vscode-server-license) and * the Microsoft Privacy Statement (https://privacy.microsoft.com/en-US/privacystatement). * Web UI available at http://127.0.0.1:8000?tkn=8cc03a42-a485-4d32-84ce-046dc3c7324a
表示されているトークン付きのURLにアクセスしてみます。
初回はVisual Studio Code Serverのダウンロードが行われるようで、時間がかかります。

しばらく待っていると、ブラウザー上にVisual Studio Codeが表示されます。

あとはWebブラウザーで動くVisual Studio Codeになります。
もう少しオプションを見てみましょう。
Options:
--host <HOST>
Host to listen on, defaults to 'localhost'
--socket-path <SOCKET_PATH>
--port <PORT>
Port to listen on. If 0 is passed a random free port is picked [default: 8000]
--connection-token <CONNECTION_TOKEN>
A secret that must be included with all requests
--connection-token-file <CONNECTION_TOKEN_FILE>
A file containing a secret that must be included with all requests
```
Run without a connection token. Only use this if the connection is secured by other means
--accept-server-license-terms
If set, the user accepts the server license terms and the server will be started without a user prompt
--server-base-path <SERVER_BASE_PATH>
Specifies the path under which the web UI and the code server is provided
--server-data-dir <SERVER_DATA_DIR>
Specifies the directory that server data is kept in
--commit-id <COMMIT_ID>
Use a specific commit SHA for the client
リモートから接続したい場合は、--hostオプションを指定します。
$ code serve-web --host 0.0.0.0
リッスンポートはデフォルトで8000ですが、変更する場合は--portオプションですね。
デフォルトでは接続にトークンが必要ですが、これを固定するには--connection-tokenオプションを使います。
$ code serve-web --connection-token [token]
トークン自体を不要にしたい場合は--without-connection-tokenオプションを指定します。
$ code serve-web --without-connection-token
このあたりはセキュリティーをどんどん緩くしていく設定になるので、使う時には前提となる環境を十分に確認しましょう。
操作するファイルはVisual Studio Codeが動作している環境になるので、リモートからファイル変更などができることを
意味するので。
なんならターミナルも動かせますし。