-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.js
executable file
·80 lines (67 loc) · 2.39 KB
/
run.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const localWorkspace = new RegExp("^file://(.+)$");
const remoteSSHWorkspace = new RegExp("^vscode-remote://ssh-remote+(.+?(?=/))(.+)$");
const remoteWSLWorkspace = new RegExp("^vscode-remote://wsl+(.+?(?=/))(.+)$");
const codespacesWorkspace = new RegExp("^vscode-remote://vsonline+(.+?(?=/))(.+)$");
const devContainerWorkspace = new RegExp("^vscode-remote://dev-container+(.+?(?=/))(.+)$");
function run(argv) {
const { entries } = JSON.parse(argv[0]);
const query = argv[1] || "";
const items = convertEntriesToWorkspaces(entries).map((w) => convertWorkspaceToItem(w, query));
return JSON.stringify({ items: items });
}
function convertWorkspaceToItem(w, query) {
let title = w.folderName.replace(".code-workspace", ` (${w.env})`);
let subtitle = w.isWorkspaceFile ? "Workspace" : "Project Folder";
if (w.env !== "Local") {
title += `${w.extraInfo ? ` - ${w.extraInfo}` : ""} (${w.env})`;
subtitle += ` in ${w.env}`;
}
subtitle += `: ${w.relativePath}`;
return {
title: title,
subtitle: subtitle,
arg: w.isWorkspaceFile || w.env !== "Local" ? [w.path, "--file-uri"] : [w.relativePath, "--folder-uri"],
autocomplete: title,
};
}
function convertEntriesToWorkspaces(entries) {
return entries
.filter((entry) => entry.folderUri)
.map((entry) => {
let isWorkspaceFile = false;
let uri = entry.folderUri;
if (entry.workspace && entry.workspace.configPath) {
isWorkspaceFile = true;
uri = entry.workspace.configPath;
}
const { type, machineName, path } = getWorkspaceEnvironment(uri);
let folderName = path.split("/");
return {
path: uri,
isWorkspaceFile: isWorkspaceFile,
relativePath: path,
folderName: folderName[folderName.length - 1],
extraInfo: machineName,
env: type,
};
});
}
function getWorkspaceEnvironment(uri) {
let match;
switch (true) {
case localWorkspace.test(uri):
match = localWorkspace.exec(uri);
return { type: "Local", machineName: undefined, path: match[1] };
case remoteSSHWorkspace.test(uri):
match = remoteSSHWorkspace.exec(uri);
return { type: "SSH", machineName: match[1], path: match[2] };
case codespacesWorkspace.test(uri):
match = codespacesWorkspace.exec(uri);
return { type: "Codespace", undefined, path: match[2] };
case devContainerWorkspace.test(uri):
match = devContainerWorkspace.exec(uri);
return { type: "Dev Container", undefined, path: match[2] };
default:
return;
}
}