Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,13 @@
"scope": "resource"
},
"livePreview.serverRoot": {
"type": "string",
"default": "",
"type": [
"string",
"array"
],
"items": {
"type": "string"
},
"description": "%settings.serverRoot%",
"scope": "resource"
},
Expand Down
2 changes: 1 addition & 1 deletion package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
25 changes: 22 additions & 3 deletions src/utils/pathUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
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 '';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils/settingsUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface ILivePreviewConfigItem {
debugOnExternalPreview: boolean;
hostIP: string;
customExternalBrowser: CustomExternalBrowser;
serverRoot: string;
serverRoot: string | string[];
previewDebounceDelay: number;
httpHeaders: any;
}
Expand Down