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

feat: use registry as fallback when evaluating app path #45

Merged
merged 2 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

# Change Log

## 1.1.0

### ✨ New

- Add support for non-standard installation paths on Windows.

## 1.0.1

### ♻️ Update
Expand Down
2 changes: 1 addition & 1 deletion src/commands/restart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const restart = command<RestartOptions>(async ({ uuid }, output) => {
return output.error("Restarting failed").log(`Plugin not found: ${uuid}`).exit(1);
}

const appPath = `"${getStreamDeckPath()}"`;
const appPath = `"${await getStreamDeckPath()}"`;

// When Stream Deck isn't running, start it.
if (!(await isStreamDeckRunning())) {
Expand Down
2 changes: 1 addition & 1 deletion src/commands/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const stop = command<StopOptions>(async ({ uuid }, output) => {
}

// Stop the plugin.
await run(`"${getStreamDeckPath()}"`, ["-s", uuid]);
await run(`"${await getStreamDeckPath()}"`, ["-s", uuid]);
output.success(`Stopped ${chalk.green(uuid)}`);
});

Expand Down
45 changes: 37 additions & 8 deletions src/stream-deck.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import find from "find-process";
import { Dirent, readdirSync, readlinkSync } from "node:fs";
import { Dirent, existsSync, readdirSync, readlinkSync } from "node:fs";
import os from "node:os";
import { basename, join, resolve } from "node:path";
import { Registry } from "rage-edit";

const PLUGIN_SUFFIX = ".sdPlugin";

Expand Down Expand Up @@ -58,20 +59,48 @@ export function isPluginInstalled(uuid: string): boolean {
* Gets the path to the Stream Deck application.
* @returns The path.
*/
export function getStreamDeckPath(): string {
if (os.platform() === "darwin") {
return "/Applications/Elgato Stream Deck.app/Contents/MacOS/Stream Deck";
} else {
return "C:\\Program Files\\Elgato\\StreamDeck\\StreamDeck.exe";
export const getStreamDeckPath = ((): (() => Promise<string>) => {
let appPath: string | undefined = undefined;
return async () => (appPath ??= await __getStreamDeckPath());

/**
* Gets the path to the Stream Deck application.
* @returns The path.
*/
async function __getStreamDeckPath(): Promise<string> {
if (os.platform() === "darwin") {
return "/Applications/Elgato Stream Deck.app/Contents/MacOS/Stream Deck";
} else {
// Before checking the registry, check if the default path exists.
const defaultWinPath = "C:\\Program Files\\Elgato\\StreamDeck\\StreamDeck.exe";
if (existsSync(defaultWinPath)) {
return defaultWinPath;
}

// Otherwise, attempt to get the installation directory from the registry.
const registryValue = await Registry.get(
"HKEY_CURRENT_USER\\Software\\Elgato Systems GmbH\\StreamDeck",
"InstallDir",
);

if (registryValue && typeof registryValue === "string") {
const winPath = join(registryValue, "StreamDeck.exe");
if (existsSync(winPath)) {
return winPath;
}
}

throw new Error("StreamDeck.exe could not be found");
}
}
}
})();

/**
* Determines if the Stream Deck application is currently running.
* @returns `true` when the application is running; otherwise `false`.
*/
export async function isStreamDeckRunning(): Promise<boolean> {
const appPath = getStreamDeckPath();
const appPath = await getStreamDeckPath();

if (os.platform() === "darwin") {
const processes = await find("name", "Elgato Stream Deck");
Expand Down
8 changes: 8 additions & 0 deletions src/types/rage-edit.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
declare module "rage-edit" {
export const Registry: {
/**
* Gets the specified registry key value.
* @param path Path to the registry entry.
* @param name Name of the registry entry.
* @returns Data stored in the registry entry.
*/
get(path: string, name: string): Promise<unknown>;

/**
* Sets the specified registry key.
* @param path Path to the registry entry.
Expand Down