forked from desktop/desktop
-
Notifications
You must be signed in to change notification settings - Fork 529
/
Copy pathlinux.ts
105 lines (96 loc) · 2.64 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
import { join } from 'path'
import { pathExists as pathExistsInternal } from 'fs-extra'
import {
ChildProcess,
spawn,
SpawnOptionsWithoutStdio,
SpawnOptions,
} from 'child_process'
export function isFlatpakBuild() {
return __LINUX__ && process.env.FLATPAK_HOST === '1'
}
/**
* Convert an executable path to be relative to the flatpak host
*
* @param path a path to an executable relative to the root of the filesystem
*
*/
export function convertToFlatpakPath(path: string) {
if (!__LINUX__) {
return path
}
if (path.startsWith('/opt/') || path.startsWith('/var/lib/flatpak')) {
return path
}
return join('/var/run/host', path)
}
export function formatWorkingDirectoryForFlatpak(path: string): string {
return path.replace(/(\s)/, ' ')
}
export function formatPathForFlatpak(path: string): string {
if (path.startsWith('/var/lib/flatpak/app')) {
return path.replace('/var/lib/flatpak/app/', '')
}
return path
}
/**
* Checks the file path on disk exists before attempting to launch a specific shell
*
* @param path
*
* @returns `true` if the path can be resolved, or `false` otherwise
*/
export async function pathExists(path: string): Promise<boolean> {
if (isFlatpakBuild()) {
path = convertToFlatpakPath(path)
}
try {
return await pathExistsInternal(path)
} catch {
return false
}
}
/**
* Spawn a particular shell in a way that works for Flatpak-based usage
*
* @param path path to shell, relative to the root of the filesystem
* @param args arguments to provide to the shell
* @param options additional options to provide to spawn function
*
* @returns a child process to observe and monitor
*/
export function spawnShell(
path: string,
args: string[],
options?: SpawnOptionsWithoutStdio
): ChildProcess {
if (isFlatpakBuild()) {
return spawn('flatpak-spawn', ['--host', path, ...args], options)
}
return spawn(path, args, options)
}
/**
* Spawn a given editor in a way that works for Flatpak-based usage
*
* @param path path to editor, relative to the root of the filesystem
* @param workingDirectory working directory to open initially in editor
* @param options additional options to provide to spawn function
*/
export function spawnEditor(
path: string,
workingDirectory: string,
options: SpawnOptions
): ChildProcess {
if (isFlatpakBuild()) {
const actualpath = formatPathForFlatpak(path)
const EscapedworkingDirectory =
formatWorkingDirectoryForFlatpak(workingDirectory)
return spawn(
'flatpak-spawn',
['--host', actualpath, EscapedworkingDirectory],
options
)
} else {
return spawn(path, [workingDirectory], options)
}
}