forked from onivim/oni2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd-to-path.js
76 lines (64 loc) · 1.93 KB
/
add-to-path.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
// add-to-path.js
// This is a Node-side script to add Oni2 to their path.
// This is done in node, to get sudo-prompt.
const fs = require("fs")
const os = require("os")
const path = require("path")
const sudo = require("sudo-prompt")
const isWindows = () => os.platform() === "win32"
const isMac = () => os.platform() === "darwin"
const isLinux = () => os.platform() === "linux"
const getLinkPath = () => (isMac() || isLinux() ? "/usr/local/bin/oni2" : "")
const isAddedToPath = () => {
try {
fs.lstatSync(getLinkPath())
} catch (_) {
return false
}
return true
}
const removeFromPath = () => {
if (isAddedToPath() && !isWindows()) {
fs.unlinkSync(getLinkPath())
}
}
const addToPath = async () => {
if (!isAddedToPath() && !isWindows()) {
const appDirectory = path.join(path.dirname(process.mainModule.filename), "..")
let imgPath = path.join(appDirectory, "Onivim2.icns")
// TODO: Check this is valid for all use cases.
if (!fs.existsSync(imgPath)) {
imgPath = path.join(appDirectory, "assets", "images", "Onivim2.icns")
}
const options = { name: "Oni2", icns: imgPath }
let linkDest = ""
if (isMac()) {
linkDest = path.join(appDirectory, "run.sh")
} else {
linkDest = "" // TODO.
}
if (!fs.existsSync(linkDest)) {
return
}
await runSudoCommand(`ln -fs ${linkDest} ${getLinkPath()}`, options)
}
}
const runSudoCommand = async (command, options) => {
return new Promise((resolve) => {
sudo.exec(command, options, (error, stdout, stderr) => {
resolve({ error, stdout, stderr })
})
})
}
const toggleAddToPath = async () => {
if (isAddedToPath()) {
removeFromPath()
} else {
await addToPath()
}
}
;(async () => {
try {
await Promise.resolve(toggleAddToPath())
} catch (_) {}
})()