From 0ba0bd3d77bb8c12322cd18f05ab325e4276885c Mon Sep 17 00:00:00 2001 From: Simon Laux Date: Fri, 17 May 2024 11:11:11 +0200 Subject: [PATCH] fix(@deltachat/stdio-rpc-server): fix version check when deltachat-rpc-server is found in path (#5579) --- deltachat-rpc-server/npm-package/index.js | 43 +++++++++++++++++------ 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/deltachat-rpc-server/npm-package/index.js b/deltachat-rpc-server/npm-package/index.js index d933a4618..35d348f9f 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 > 0) return ""; + else throw error; + } +} + /** @type {import("./index").FnTypes.getRPCServerPath} */ export async function getRPCServerPath( options = { skipSearchInPath: false, disableEnvPath: false } @@ -65,19 +87,18 @@ 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]) - : await exec("where", [PATH_EXECUTABLE_NAME]); + 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) { // test if it is the right version try { // for some unknown reason it is in stderr and not in stdout - const { stderr } = await promisify(execFile)(executable, ["--version"]); + const { stderr } = await promisify(execFile)( + executable, + ["--version"], + { shell: true } + ); const version = stderr.slice(0, stderr.indexOf("\n")); if (package_json.version !== version) { throw new Error(