-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.ts
More file actions
116 lines (102 loc) · 3.42 KB
/
main.ts
File metadata and controls
116 lines (102 loc) · 3.42 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
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { writeResponse } from "./src/flow-api.ts";
import { readRequest, Result } from "./src/flow-api.ts";
import Fuse, { type FuseResultMatch } from "npm:fuse.js";
import { getProfiles, getWorkspaces, type WorkspaceDetail } from "./src/edge-workspaces.ts";
import { discoverEdgeInstances, type EdgeInstance } from "./src/edge-instances.ts";
type Methods = 'open_workspace';
const {method, parameters} = readRequest<Methods>();
interface Workspace {
edgeInstance: EdgeInstance;
profile: string;
workspace: WorkspaceDetail;
}
if (method === "query") {
const fuzzyName = parameters[0];
// Read all profiles across all edge instances and profiles.
const workspaces: Workspace[] = [];
const edgeInstances = await discoverEdgeInstances();
for(const edgeInstance of edgeInstances) {
const edgeProfiles = getProfiles(edgeInstance);
for(const profile of edgeProfiles) {
const edgeWorkspaces = getWorkspaces(edgeInstance, profile);
for(const workspace of edgeWorkspaces) {
workspaces.push({
edgeInstance,
profile,
workspace
});
}
}
}
// Fuzzy matching, or list all profiles if no query
const fuzzyMatcher = new Fuse(workspaces, {keys: ["workspace.name", "profile"], includeScore: true, includeMatches: true});
const matches = fuzzyName ? fuzzyMatcher.search(fuzzyName) : workspaces.map(w => ({item: w, score: 0, matches: null}));
const scoreRange = 100;
// Send back to Flow Launcher
const results: Result<Methods>[] = matches.map(m => {
const w = m.item;
const title = `${w.workspace.name}`;
const subtitle = `Edge Workspace: Profile "${w.profile}"; ${w.workspace.count} tabs`;
const result: Result<Methods> = {
Title: title,
TitleHighlightData: m.matches ? getHighlight(m.matches) : undefined,
TitleToolTip: title,
Subtitle: subtitle,
SubTitleToolTip: subtitle,
Score: Math.floor((1 - m.score!) * scoreRange),
JsonRPCAction: {
method: "open_workspace",
parameters: [
JSON.stringify(w)
]
},
IcoPath: "Images\\app.png",
RecordKey: w.workspace.id
};
return result;
});
writeResponse({
result: results
});
}
// When user chooses a workspace
if (method === "open_workspace") {
const workspace = JSON.parse(parameters[0]) as Workspace;
const command = new Deno.Command(workspace.edgeInstance.ExecutablePath, {
args: [
`--profile-directory=${workspace.profile}`,
`--launch-workspace=${workspace.workspace.id}`
],
stdin: 'null',
stderr: 'null',
stdout: 'null'
});
const childProcess = command.spawn();
await childProcess.status;
}
// Value from Fuse looks like this:
// [
// { indices: [ [ 2, 3 ] ], value: "Default", key: "profile" },
// {
// indices: [ [ 0, 0 ], [ 3, 5 ] ],
// value: "A: Family",
// key: "workspace.name"
// }
// ],
// Desired output:
// [0, 3, 4, 5]
//
// Each indices tuple is a range of characters. The output should include an int for each character.
// Only lok at "key": "workspace.name", ignore "profile"
function getHighlight(indices: readonly FuseResultMatch[], offset = 0): number[] {
const highlightedCharacters: number[] = [];
indices
.filter(i => i.key === "workspace.name")
.flatMap(i => i.indices)
.forEach(i => {
for(let j = i[0]; j <= i[1]; j++) {
highlightedCharacters.push(j + offset);
}
});
return highlightedCharacters;
}