diff --git a/deltachat-rpc-server/npm-package/index.js b/deltachat-rpc-server/npm-package/index.js index 1c427a1aa5..5e44817faf 100644 --- a/deltachat-rpc-server/npm-package/index.js +++ b/deltachat-rpc-server/npm-package/index.js @@ -22,10 +22,6 @@ import { import package_json from "./package.json" with { type: "json" }; import { createRequire } from "node:module"; -// exports -// - [ ] a raw starter that has a stdin/out handle thingie like desktop uses -// - [X] a function that already wraps the stdio handle from above into the deltachat jsonrpc bindings - function findRPCServerInNodeModules() { const arch = os.arch(); const operating_system = process.platform; @@ -43,6 +39,32 @@ function findRPCServerInNodeModules() { } } +/** + * @returns {Promise} + */ +async function getLocationInPath() { + const exec = promisify(execFile); + + if (os.platform() === "win32") { + const { stdout: executable } = await exec("where", [PATH_EXECUTABLE_NAME], { + shell: true, + }); + return executable; + } + + try { + const { stdout: executable } = await exec( + "command", + ["-v", PATH_EXECUTABLE_NAME], + { shell: true } + ); + return executable; + } catch (error) { + if (error.code == 127) return ""; + else throw error + } +} + /** @type {import("./index").FnTypes.getRPCServerPath} */ export async function getRPCServerPath( options = { skipSearchInPath: false, disableEnvPath: false } @@ -65,12 +87,7 @@ export async function getRPCServerPath( // 2. check if it can be found in PATH if (!process.env[SKIP_SEARCH_IN_PATH] && !skipSearchInPath) { - const exec = promisify(execFile); - - const { stdout: executable } = - os.platform() !== "win32" - ? await exec("command", ["-v", PATH_EXECUTABLE_NAME], { shell: true }) - : await exec("where", [PATH_EXECUTABLE_NAME], { shell: true }); + const executable = await getLocationInPath(); // by just trying to execute it and then use "command -v deltachat-rpc-server" (unix) or "where deltachat-rpc-server" (windows) to get the path to the executable if (executable.length > 1) {