forked from desktop/desktop
-
Notifications
You must be signed in to change notification settings - Fork 529
/
Copy pathlinux.ts
127 lines (107 loc) · 3.34 KB
/
linux.ts
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
import { pathExists } from 'fs-extra'
import { IFoundEditor } from './found-editor'
import { assertNever } from '../fatal-error'
export enum ExternalEditor {
Atom = 'Atom',
VisualStudioCode = 'Visual Studio Code',
VisualStudioCodeInsiders = 'Visual Studio Code (Insiders)',
SublimeText = 'Sublime Text',
Typora = 'Typora',
SlickEdit = 'SlickEdit',
}
export function parse(label: string): ExternalEditor | null {
if (label === ExternalEditor.Atom) {
return ExternalEditor.Atom
}
if (label === ExternalEditor.VisualStudioCode) {
return ExternalEditor.VisualStudioCode
}
if (label === ExternalEditor.VisualStudioCodeInsiders) {
return ExternalEditor.VisualStudioCode
}
if (label === ExternalEditor.SublimeText) {
return ExternalEditor.SublimeText
}
if (label === ExternalEditor.Typora) {
return ExternalEditor.Typora
}
if (label === ExternalEditor.SlickEdit) {
return ExternalEditor.SlickEdit
}
return null
}
async function getPathIfAvailable(path: string): Promise<string | null> {
return (await pathExists(path)) ? path : null
}
async function getEditorPath(editor: ExternalEditor): Promise<string | null> {
switch (editor) {
case ExternalEditor.Atom:
return getPathIfAvailable('/usr/bin/atom')
case ExternalEditor.VisualStudioCode:
return getPathIfAvailable('/usr/bin/code')
case ExternalEditor.VisualStudioCodeInsiders:
return getPathIfAvailable('/usr/bin/code-insiders')
case ExternalEditor.SublimeText:
return getPathIfAvailable('/usr/bin/subl')
case ExternalEditor.Typora:
return getPathIfAvailable('/usr/bin/typora')
case ExternalEditor.SlickEdit:
const possiblePaths = [
'/opt/slickedit-pro2018/bin/vs',
'/opt/slickedit-pro2017/bin/vs',
'/opt/slickedit-pro2016/bin/vs',
'/opt/slickedit-pro2015/bin/vs',
]
for (const possiblePath of possiblePaths) {
const slickeditPath = await getPathIfAvailable(possiblePath)
if (slickeditPath) {
return slickeditPath
}
}
return null
default:
return assertNever(editor, `Unknown editor: ${editor}`)
}
}
export async function getAvailableEditors(): Promise<
ReadonlyArray<IFoundEditor<ExternalEditor>>
> {
const results: Array<IFoundEditor<ExternalEditor>> = []
const [
atomPath,
codePath,
codeInsidersPath,
sublimePath,
typoraPath,
slickeditPath,
] = await Promise.all([
getEditorPath(ExternalEditor.Atom),
getEditorPath(ExternalEditor.VisualStudioCode),
getEditorPath(ExternalEditor.VisualStudioCodeInsiders),
getEditorPath(ExternalEditor.SublimeText),
getEditorPath(ExternalEditor.Typora),
getEditorPath(ExternalEditor.SlickEdit),
])
if (atomPath) {
results.push({ editor: ExternalEditor.Atom, path: atomPath })
}
if (codePath) {
results.push({ editor: ExternalEditor.VisualStudioCode, path: codePath })
}
if (codeInsidersPath) {
results.push({
editor: ExternalEditor.VisualStudioCode,
path: codeInsidersPath,
})
}
if (sublimePath) {
results.push({ editor: ExternalEditor.SublimeText, path: sublimePath })
}
if (typoraPath) {
results.push({ editor: ExternalEditor.Typora, path: typoraPath })
}
if (slickeditPath) {
results.push({ editor: ExternalEditor.SlickEdit, path: slickeditPath })
}
return results
}