From 974d4cb8fce7c8ca87ec1b1a170944b9bed2eef0 Mon Sep 17 00:00:00 2001 From: Asher Date: Thu, 16 Apr 2020 11:50:53 -0500 Subject: [PATCH] Allow specifying a workspace on the command line Fixes #1535. --- doc/FAQ.md | 4 ++-- src/node/app/vscode.ts | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/doc/FAQ.md b/doc/FAQ.md index 0aff1886a..e44f698ce 100644 --- a/doc/FAQ.md +++ b/doc/FAQ.md @@ -139,8 +139,8 @@ code-server tries the following in order: 1. The `workspace` query parameter. 2. The `folder` query parameter. -3. The directory passed on the command line. -4. The last opened workspace or folder. +3. The workspace or directory passed on the command line. +4. The last opened workspace or directory. ## Enterprise diff --git a/src/node/app/vscode.ts b/src/node/app/vscode.ts index ece990dcc..9ecdcd803 100644 --- a/src/node/app/vscode.ts +++ b/src/node/app/vscode.ts @@ -1,6 +1,7 @@ import { field, logger } from "@coder/logger" import * as cp from "child_process" import * as crypto from "crypto" +import * as fs from "fs-extra" import * as http from "http" import * as net from "net" import * as path from "path" @@ -209,6 +210,15 @@ export class VscodeHttpProvider extends HttpProvider { private async getFirstPath( startPaths: Array<{ url?: string | string[]; workspace?: boolean } | undefined>, ): Promise { + const isFile = async (path: string): Promise => { + try { + const stat = await fs.stat(path) + return stat.isFile() + } catch (error) { + logger.warn(error.message) + return false + } + } for (let i = 0; i < startPaths.length; ++i) { const startPath = startPaths[i] const url = @@ -216,7 +226,10 @@ export class VscodeHttpProvider extends HttpProvider { if (startPath && url) { return { url, - workspace: !!startPath.workspace, + // The only time `workspace` is undefined is for the command-line + // argument, in which case it's a path (not a URL) so we can stat it + // without having to parse it. + workspace: typeof startPath.workspace !== "undefined" ? startPath.workspace : await isFile(url), } } }