-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpath.ts
More file actions
48 lines (41 loc) · 1.37 KB
/
path.ts
File metadata and controls
48 lines (41 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import * as path from "path";
import * as os from "os";
import * as vscode from 'vscode';
export function looksLikePath(arg: string): boolean {
if (
arg.includes('/')
|| arg.includes('\\')
|| arg.startsWith('.')
|| /\.[^/\\]+$/.test(arg) // filename with extension
) {
return true;
}
return false;
}
export function findWorkspaceRoot(): string {
const folders = vscode.workspace.workspaceFolders;
const workspaceRoot = folders && folders.length > 0
? folders[0].uri.fsPath
: process.cwd();
return workspaceRoot;
}
export function resolvePath(argPath: string): string {
const workspaceRoot = findWorkspaceRoot();
// Expand ${workspaceFolder}
if (argPath.includes("${workspaceFolder}")) {
argPath = argPath.replace("${workspaceFolder}", workspaceRoot);
}
// Expand tilde (~) to home directory
if (argPath.startsWith("~")) {
argPath = path.join(os.homedir(), argPath.slice(1));
}
// Expand ./ or ../ relative paths (relative to workspace root if available)
if (argPath.startsWith("./") || argPath.startsWith("../")) {
argPath = path.resolve(workspaceRoot, argPath);
}
// If still not absolute, treat it as relative to workspace root
if (!path.isAbsolute(argPath)) {
argPath = path.join(workspaceRoot, argPath);
}
return argPath;
}