Skip to content
This repository has been archived by the owner on Nov 7, 2024. It is now read-only.

Commit

Permalink
refactor: open in browser in vscode so it works for remote
Browse files Browse the repository at this point in the history
  • Loading branch information
Enter-tainer committed Aug 15, 2023
1 parent d840b84 commit 080c14e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
31 changes: 26 additions & 5 deletions addons/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,14 @@ function syncEditorChanges(addonΠserver: WebSocket) {
}));
}

function runServer(command: string, args: string[], outputChannel: vscode.OutputChannel): Promise<[string, string, ChildProcessWithoutNullStreams]> {
interface LaunchCliResult {
serverProcess: ChildProcessWithoutNullStreams,
controlPlanePort: string,
dataPlanePort: string,
staticFilePort?: string,
}

function runServer(command: string, args: string[], outputChannel: vscode.OutputChannel, openInBrowser: boolean): Promise<LaunchCliResult> {
const serverProcess = spawn(command, args, {
env: {
...process.env,
Expand Down Expand Up @@ -181,19 +188,30 @@ function runServer(command: string, args: string[], outputChannel: vscode.Output
return new Promise((resolve, reject) => {
let dataPlanePort: string | undefined = undefined;
let controlPlanePort: string | undefined = undefined;
let staticFilePort: string | undefined = undefined;
serverProcess.stderr.on('data', (data: Buffer) => {
if (data.toString().includes("listening on")) {
console.log(data.toString());
let ctrlPort = data.toString().match(/Control plane server listening on: 127\.0\.0\.1:(\d+)/)?.[1];
let dataPort = data.toString().match(/Data plane server listening on: 127\.0\.0\.1:(\d+)/)?.[1];
let staticPort = data.toString().match(/Static file server listening on: 127\.0\.0\.1:(\d+)/)?.[1];
if (ctrlPort !== undefined) {
controlPlanePort = ctrlPort;
}
if (dataPort !== undefined) {
dataPlanePort = dataPort;
}
if (staticPort !== undefined) {
staticFilePort = staticPort;
}
if (dataPlanePort !== undefined && controlPlanePort !== undefined) {
resolve([dataPlanePort, controlPlanePort, serverProcess]);
if (openInBrowser) {
if (staticFilePort !== undefined) {
resolve({ dataPlanePort, controlPlanePort, staticFilePort, serverProcess });
}
} else {
resolve({ dataPlanePort, controlPlanePort, serverProcess });
}
}
}
});
Expand Down Expand Up @@ -353,18 +371,21 @@ const launchPreview = async (task: LaunchInBrowserTask | LaunchInWebViewTask) =>
console.log(`Watching ${filePath} for changes`);
const projectRoot = getProjectRoot(filePath);
const rootArgs = ["--root", projectRoot];
const staticFileArgs = openInBrowser ? ["--open-in-browser", "--open-in-browser-host", "127.0.0.1:0"] : [];
const staticFileArgs = openInBrowser ? ["--server-static-file", "--static-file-host", "127.0.0.1:0"] : [];
const partialRenderingArgs = vscode.workspace.getConfiguration().get<boolean>('typst-preview.partialRendering') ? ["--partial-rendering"] : [];
const [dataPlanePort, controlPlanePort, serverProcess] = await runServer(serverPath, [
const { dataPlanePort, controlPlanePort, staticFilePort, serverProcess } = await runServer(serverPath, [
"--data-plane-host", "127.0.0.1:0",
"--control-plane-host", "127.0.0.1:0",
...rootArgs,
...staticFileArgs,
...partialRenderingArgs,
...codeGetCliFontArgs(),
filePath,
], outputChannel);
], outputChannel, openInBrowser);
console.log(`Launched server, data plane port:${dataPlanePort}, control plane port:${controlPlanePort}`);
if (openInBrowser) {
vscode.env.openExternal(vscode.Uri.parse(`http://127.0.0.1:${staticFilePort}`));
}
// window.typstWebsocket.send("current");
return {
serverProcess, dataPlanePort, controlPlanePort
Expand Down
8 changes: 6 additions & 2 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,19 @@ pub struct CliArguments {

/// Host to open the preview in the browser.
#[clap(
long = "open-in-browser-host",
long = "static-file-host",
value_name = "HOST",
default_value = "127.0.0.1:23627"
)]
pub open_in_browser_host: String,
pub static_file_host: String,

/// Open the preview in the browser after compilation.
#[clap(long = "open-in-browser")]
pub open_in_browser: bool,

/// Serve html for preview in the browser.
#[clap(long = "server-static-file")]
pub server_static_file: bool,

/// Only render visible part of the document. This can improve performance but still being experimental.
#[clap(long = "partial-rendering")]
Expand Down
12 changes: 7 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,8 @@ async fn main() {
}
}
});
let static_file_addr = arguments.open_in_browser_host;
if arguments.open_in_browser {
let static_file_addr = arguments.static_file_host;
if arguments.server_static_file || arguments.open_in_browser {
let data_plane_port = data_plane_port_rx.await.unwrap();
let make_service = make_service_fn(|_| {
let data_plane_port = data_plane_port;
Expand Down Expand Up @@ -510,9 +510,11 @@ async fn main() {
}
});
let server = hyper::Server::bind(&static_file_addr.parse().unwrap()).serve(make_service);
if let Err(e) = open::that_detached(format!("http://{}", server.local_addr())) {
error!("failed to open browser: {}", e);
};
if arguments.open_in_browser {
if let Err(e) = open::that_detached(format!("http://{}", server.local_addr())) {
error!("failed to open browser: {}", e);
};
}
info!("Static file server listening on: {}", server.local_addr());
if let Err(e) = server.await {
error!("Static file server error: {}", e);
Expand Down

0 comments on commit 080c14e

Please sign in to comment.