Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@deltachat/stdio-rpc-server): fix version check when deltachat-rpc-server is found in path #5579

Merged
merged 4 commits into from
May 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions deltachat-rpc-server/npm-package/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,6 +39,32 @@ function findRPCServerInNodeModules() {
}
}

/**
* @returns {Promise<string>}
*/
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 }
Expand All @@ -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(
Expand Down
Loading