forked from onivim/oni2
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OSX: Add to PATH Command. (onivim#1121)
* Start an add to path script. * Remove debug calls. * Update to use run script on macOS. * Temporarily jam into KeyDisplayer. This is very temporary, and just so I can test it out. * Fix path to resource. * Move action around. * Run format. * Tidy up effect. * Add remove action. * Fix mac link location. Should be more general now. * Don't run if the link location doesn't exist. * Format Co-authored-by: Bryan Phelps <[email protected]>
- Loading branch information
Showing
4 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 (_) {} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters