diff --git a/src/excmds.ts b/src/excmds.ts index e5c81f8d..1c2ab501 100644 --- a/src/excmds.ts +++ b/src/excmds.ts @@ -92,6 +92,7 @@ import * as TTS from "@src/lib/text_to_speech" import * as excmd_parser from "@src/parsers/exmode" import * as escape from "@src/lib/escape" import * as R from "ramda" +import * as semverCompare from "semver-compare" /** * This is used to drive some excmd handling in `composite`. @@ -832,16 +833,29 @@ export async function source_quiet(...args: string[]) { */ //#background export async function updatenative(interactive = true) { - const tag = TRI_VERSION.includes("pre") ? "master" : TRI_VERSION - if (await Native.nativegate("0", interactive)) { - if ((await browser.runtime.getPlatformInfo()).os === "mac") { - if (interactive) logger.error("Updating the native messenger on OSX is broken. Please use `:nativeinstall` instead.") - return - } - await Native.run((await config.get("nativeinstallcmd")).replace("%TAG", tag)) - - if (interactive) native() + if (!(await Native.nativegate("0", interactive))) { + return + } else if ((await browser.runtime.getPlatformInfo()).os === "mac") { + if (interactive) logger.error("Updating the native messenger on OSX is broken. Please use `:nativeinstall` instead.") + return } + + const tag = TRI_VERSION.includes("pre") ? "master" : TRI_VERSION + const update_command = (await config.get("nativeinstallcmd")).replace("%TAG", tag) + const native_version = await Native.getNativeMessengerVersion() + + if (semverCompare(native_version, "0.2.0") < 0) { + await Native.run(update_command) + } else if (semverCompare(native_version, "0.3.1") < 0) { + if (interactive) { + throw new Error("Updating is broken on this version of the native messenger. Please use `:nativeinstall` instead.") + } + return + } else { + await Native.runAsync(update_command) + } + + if (interactive) native() } /** diff --git a/src/lib/config.ts b/src/lib/config.ts index ef88d600..fee604d7 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -1151,7 +1151,11 @@ const platform_defaults = { "": "buffer #", } as unknown, - nativeinstallcmd: `powershell -NoProfile -Command "(New-Object System.Net.WebClient).DownloadFile('https://raw.githubusercontent.com/tridactyl/native_messenger/master/installers/windows.ps1', """$env:temp/tridactyl_installnative.ps1""")" & powershell -NoProfile -File %temp%\\tridactyl_installnative.ps1 -Tag %TAG & del %temp%\\tridactyl_installnative.ps1`, + nativeinstallcmd: `powershell -NoProfile -Command "\ +[Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12;\ +(New-Object System.Net.WebClient).DownloadFile('https://raw.githubusercontent.com/tridactyl/native_messenger/master/installers/windows.ps1', '%TEMP%/tridactyl_installnative.ps1');\ +& '%TEMP%/tridactyl_installnative.ps1' -Tag %TAG;\ +Remove-Item '%TEMP%/tridactyl_installnative.ps1'"`, }, linux: { nmaps: { diff --git a/src/lib/native.ts b/src/lib/native.ts index 5b96cbe8..08056d81 100644 --- a/src/lib/native.ts +++ b/src/lib/native.ts @@ -13,6 +13,7 @@ const NATIVE_NAME = "tridactyl" type MessageCommand = | "version" | "run" + | "run_async" | "read" | "write" | "writerc" @@ -383,6 +384,16 @@ export async function run(command: string, content = "") { return msg } +export async function runAsync(command: string) { + const required_version = "0.3.1" + if (!await nativegate(required_version, false)) { + throw new Error( + `runAsync needs native messenger version >= ${required_version}.`, + ) + } + logger.info(await sendNativeMsg("run_async", { command })) +} + /** Evaluates a string in the native messenger. This has to be python code. If * you want to run shell strings, use run() instead. *