-
Notifications
You must be signed in to change notification settings - Fork 432
Expand file tree
/
Copy patheditor.ts
More file actions
363 lines (322 loc) · 8.68 KB
/
editor.ts
File metadata and controls
363 lines (322 loc) · 8.68 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*
* cmd.ts
*
* Copyright (C) 2020-2022 Posit Software, PBC
*/
import { CreateResult } from "./cmd-types.ts";
import { which } from "../../core/path.ts";
import {
isPositronTerminal,
isRStudioTerminal,
isVSCodeTerminal,
} from "../../core/platform.ts";
import { basename, dirname, join } from "../../deno_ral/path.ts";
import { existsSync } from "../../deno_ral/fs.ts";
import { isMac, isWindows } from "../../deno_ral/platform.ts";
import {
enforcer,
makeStringEnumTypeFunctions,
objectPredicate,
stringTypePredicate,
} from "../../typing/dynamic.ts";
import { call } from "../../deno_ral/process.ts";
export interface Editor {
// A short, command line friendly id
id: string;
// A display name
name: string;
// Whether this is being run from within the editor
// (e.g. from the vscode or rstudio terminal)
inEditor: boolean;
// Function that can be called to open the matched
// artifact in the editor
open: () => Promise<void>;
}
export const kEditorInfos: EditorInfo[] = [
positronEditorInfo(),
vscodeEditorInfo(),
rstudioEditorInfo(),
];
export async function scanForEditors(
editorInfos: EditorInfo[],
createResult: CreateResult,
) {
const editors: Editor[] = [];
for (const editorInfo of editorInfos) {
const editorPath = await findEditorPath(editorInfo.actions);
if (editorPath) {
editors.push({
id: editorInfo.id,
name: editorInfo.name,
open: editorInfo.open(editorPath, createResult),
inEditor: editorInfo.inEditor,
});
}
}
return editors;
}
interface EditorInfo {
// The identifier for this editor
id: string;
// The name of this editor
name: string;
// Actions that are used to scan for this editor
actions: ScanAction[];
// Whether this is being run from within the editor
// (e.g. from the vscode or rstudio terminal)
inEditor: boolean;
// Uses a path and artifact path to provide a function
// that can be used to open this editor to the given artifact
open: (path: string, createResult: CreateResult) => () => Promise<void>;
}
const scanActionActions = ["path", "which", "env"] as const;
type ScanActionAction = typeof scanActionActions[number];
type ScanAction = {
action: ScanActionAction;
arg: string;
filter?: (path: string) => string;
};
function vscodeEditorInfo(): EditorInfo {
const editorInfo: EditorInfo = {
id: "vscode",
name: "vscode",
open: (path: string, createResult: CreateResult) => {
const artifactPath = createResult.path;
const cwd = Deno.statSync(artifactPath).isDirectory
? artifactPath
: dirname(artifactPath);
return async () => {
await call(path, { args: [artifactPath], cwd });
};
},
inEditor: isVSCodeTerminal(),
actions: [],
};
if (isWindows) {
editorInfo.actions.push({
action: "which",
arg: "code.exe",
});
const pathActions: ScanAction[] = windowsAppPaths(
"Microsoft VS Code",
"code.exe",
).map((path) => ({
action: "path",
arg: path,
}));
editorInfo.actions.push(...pathActions);
} else if (isMac) {
editorInfo.actions.push({
action: "which",
arg: "code",
});
const pathActions: ScanAction[] = macosAppPaths(
"Visual Studio Code.app/Contents/Resources/app/bin/code",
).map((path) => ({
action: "path",
arg: path,
}));
editorInfo.actions.push(...pathActions);
} else {
editorInfo.actions.push({
action: "which",
arg: "code",
});
editorInfo.actions.push({
action: "path",
arg: "/snap/bin/code",
});
}
return editorInfo;
}
function positronEditorInfo(): EditorInfo {
const editorInfo: EditorInfo = {
id: "positron",
name: "positron",
open: (path: string, createResult: CreateResult) => {
const artifactPath = createResult.path;
const cwd = Deno.statSync(artifactPath).isDirectory
? artifactPath
: dirname(artifactPath);
return async () => {
await call(path, { args: [artifactPath], cwd });
};
},
inEditor: isPositronTerminal(),
actions: [],
};
if (isWindows) {
editorInfo.actions.push({
action: "which",
arg: "Positron.exe",
});
const pathActions: ScanAction[] = windowsAppPaths(
"Positron",
"Positron.exe",
).map(
(path) => ({
action: "path",
arg: path,
}),
);
editorInfo.actions.push(...pathActions);
} else if (isMac) {
editorInfo.actions.push({
action: "which",
arg: "positron",
});
const pathActions: ScanAction[] = macosAppPaths(
"Positron.app/Contents/Resources/app/bin/code",
).map((path) => {
return {
action: "path",
arg: path,
};
});
editorInfo.actions.push(...pathActions);
} else {
editorInfo.actions.push({
action: "which",
arg: "positron",
});
}
return editorInfo;
}
function rstudioEditorInfo(): EditorInfo {
const editorInfo: EditorInfo = {
id: "rstudio",
name: "RStudio",
open: (path: string, createResult: CreateResult) => {
return async () => {
const artifactPath = createResult.path;
// The directory that the artifact is in
const cwd = Deno.statSync(artifactPath).isDirectory
? artifactPath
: dirname(artifactPath);
// Write an rproj file for RStudio and open that
const artifactName = basename(artifactPath);
const rProjPath = join(cwd, `${artifactName}.Rproj`);
Deno.writeTextFileSync(rProjPath, kRProjContents);
const callCmd = path.endsWith(".app") && isMac
? ["open", "-na", path, "--args", rProjPath]
: [path, rProjPath];
const callPath = callCmd[0];
const args = callCmd.slice(1);
await call(callPath, { args, cwd });
};
},
inEditor: isRStudioTerminal(),
actions: [],
};
const rstudioExe = "rstudio.exe";
if (isWindows) {
editorInfo.actions.push({
action: "env",
arg: "RS_RPOSTBACK_PATH",
filter: (path: string) => {
return join(dirname(path), rstudioExe);
},
});
const paths: ScanAction[] = windowsAppPaths(
join("RStudio", "bin"),
rstudioExe,
).map((path) => ({
action: "path",
arg: path,
}));
editorInfo.actions.push(...paths);
} else if (isMac) {
const paths: ScanAction[] = macosAppPaths("RStudio.app").map((path) => ({
action: "path",
arg: path,
}));
editorInfo.actions.push(...paths);
} else {
editorInfo.actions.push({
action: "env",
arg: "RS_RPOSTBACK_PATH",
filter: (path: string) => {
return join(dirname(path), rstudioExe);
},
});
editorInfo.actions.push({
action: "path",
arg: "/usr/lib/rstudio/bin/rstudio",
});
editorInfo.actions.push({
action: "which",
arg: "rstudio",
});
}
return editorInfo;
}
// Write an rproj file to the cwd and open that
const kRProjContents = `Version: 1.0
RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default
EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8
RnwWeave: Knitr
LaTeX: pdfLaTeX`;
async function findEditorPath(
actions: ScanAction[],
): Promise<string | undefined> {
for (const action of actions) {
const filter = action.filter || ((path) => {
return path;
});
switch (action.action) {
case "which": {
const path = await which(action.arg);
if (path) {
return filter(path);
}
break;
}
case "path":
if (existsSync(action.arg)) {
return filter(action.arg);
}
break;
case "env": {
const envValue = Deno.env.get(action.arg);
if (envValue) {
return filter(envValue);
}
}
}
}
// Couldn't find it, give up
return undefined;
}
function windowsAppPaths(folderName: string, command: string) {
const paths: string[] = [];
// Scan local app folder
const localAppData = Deno.env.get("LOCALAPPDATA");
if (localAppData) {
paths.push(join(localAppData, "Programs", folderName, command));
}
// Scan program files folder
const programFiles = Deno.env.get("PROGRAMFILES");
if (programFiles) {
paths.push(join(programFiles, folderName, command));
}
// Scan program files x86
const programFilesx86 = Deno.env.get("PROGRAMFILES(X86)");
if (programFilesx86) {
paths.push(join(programFilesx86, folderName, command));
}
return paths;
}
function macosAppPaths(appName: string) {
const paths: string[] = [];
paths.push(join("/Applications", appName));
const home = Deno.env.get("HOME");
if (home) {
paths.push(join(home, "Applications", appName));
}
return paths;
}