From f6e8fc932a464025162ce0ea9f3d8596fd80c532 Mon Sep 17 00:00:00 2001 From: lixvbnet Date: Fri, 15 Dec 2023 12:46:17 +0800 Subject: [PATCH] support multiple perferred server roots (#476) --- package.json | 9 +++++++-- package.nls.json | 2 +- src/utils/pathUtil.ts | 25 ++++++++++++++++++++++--- src/utils/settingsUtil.ts | 2 +- 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index f18001fc..52e242cb 100644 --- a/package.json +++ b/package.json @@ -245,8 +245,13 @@ "scope": "resource" }, "livePreview.serverRoot": { - "type": "string", - "default": "", + "type": [ + "string", + "array" + ], + "items": { + "type": "string" + }, "description": "%settings.serverRoot%", "scope": "resource" }, diff --git a/package.nls.json b/package.nls.json index 36337511..777f7c1a 100644 --- a/package.nls.json +++ b/package.nls.json @@ -20,7 +20,7 @@ "settings.tasks.runTaskWithExternalPreview": "Whether or not to pair external preview instances with the auto-generated server task. When disabled, the server will also not automatically close (until the window is closed).", "settings.defaultPreviewPath": "The file to automatically show upon starting the server. Leave blank to open at the index.", "settings.debugOnExternalPreview": "Whether or not to attach the JavaScript debugger on external preview launches.", - "settings.serverRoot": "The relative path from the workspace root that the files are served from. Files will be previewed as if the workspace root is at this relative path. If this directory path doesn't exist in your workspace, it will default to the workspace root. This setting only applies if you have a workspace open.", + "settings.serverRoot": "The preferred relative paths from the workspace root that the files are served from. A special '.' can be used to denote the relative path of current open file. The first directory found will be used as the server root. Files will be previewed as if the workspace root is at this relative path. If none of the directory paths exists in your workspace, it will default to the workspace root. This setting only applies if you have a workspace open.", "settings.hostIP": "The local IP host address to host your files on.", "settings.customExternalBrowser": "The browser you want to launch when previewing a file in an external browser. Only works for normal preview (non-debug) and only works on desktop.", "settings.httpHeaders": "The extra HTTP headers that should be set in the server's HTTP responses.", diff --git a/src/utils/pathUtil.ts b/src/utils/pathUtil.ts index 5f01fdd9..8700b840 100644 --- a/src/utils/pathUtil.ts +++ b/src/utils/pathUtil.ts @@ -203,11 +203,30 @@ export class PathUtil { /** * @description used to get the `serverRoot` setting properly, as it is only applied when using it would make a valid path * @param workspace - * @returns the server root from settings if it would point to an existing directory + * @returns the server root from settings if any of the paths would point to an existing directory */ public static async GetValidServerRootForWorkspace(workspace: vscode.WorkspaceFolder): Promise { - const root = SettingUtil.GetConfig(workspace).serverRoot; - return (await PathUtil.FileExistsStat(path.join(workspace.uri.fsPath, root))).exists ? root : ''; + const serverRoot = SettingUtil.GetConfig(workspace).serverRoot; + const roots: string[] = Array.isArray(serverRoot) ? serverRoot : [serverRoot]; + + for (const root of roots) { + if (root === '.') { + const activeFilePath = vscode.window.activeTextEditor?.document.uri.fsPath; + if (!activeFilePath) { + continue; + } + const relative = path.relative(workspace.uri.fsPath, path.dirname(activeFilePath)); + if (relative.startsWith('..')) { + continue; + } + return relative; + } + + if ((await PathUtil.FileExistsStat(path.join(workspace.uri.fsPath, root))).exists) { + return root; + } + } + return ''; } /** diff --git a/src/utils/settingsUtil.ts b/src/utils/settingsUtil.ts index b6b2a172..4329c6fc 100644 --- a/src/utils/settingsUtil.ts +++ b/src/utils/settingsUtil.ts @@ -21,7 +21,7 @@ export interface ILivePreviewConfigItem { debugOnExternalPreview: boolean; hostIP: string; customExternalBrowser: CustomExternalBrowser; - serverRoot: string; + serverRoot: string | string[]; previewDebounceDelay: number; httpHeaders: any; }