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

Update coffee extension #12549

Merged
merged 1 commit into from
May 24, 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
4 changes: 4 additions & 0 deletions extensions/coffee/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Coffee Changelog

## [Update] – 2024-05-24

- Fixed an issue that caused the extension to crash when the `Caffeinate Status` command was disabled.

## [Update] – 2024-05-19

- Replaced the form in the `Caffeinate For` command with arguments in the root search.
Expand Down
65 changes: 38 additions & 27 deletions extensions/coffee/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
import { getPreferenceValues, launchCommand, LaunchType, showHUD } from "@raycast/api";
import { exec, execSync } from "node:child_process";

function generateArgs(additionalArgs?: string) {
const preferences = getPreferenceValues<Preferences>();
const args = [];

if (preferences.preventDisplay) args.push("d");
if (preferences.preventDisk) args.push("m");
if (preferences.preventSystem) args.push("i");
if (additionalArgs) args.push(` ${additionalArgs}`);

return args.length > 0 ? `-${args.join("")}` : "";
}
type Preferences = {
preventDisplay: boolean;
preventDisk: boolean;
preventSystem: boolean;
icon: string;
};

type Updates = {
menubar: boolean;
status: boolean;
};

async function update(updates: Updates, caffeinated: boolean) {
if (updates.menubar) {
try {
await launchCommand({ name: "index", type: LaunchType.Background, context: { caffeinated } });
} catch (error) {
// catch error if menubar is not enabled
}
}
if (updates.status) {
await launchCommand({ name: "status", type: LaunchType.Background, context: { caffeinated } });
export async function startCaffeinate(updates: Updates, hudMessage?: string, additionalArgs?: string) {
await stopCaffeinate({ menubar: false, status: false });
exec(`/usr/bin/caffeinate ${generateArgs(additionalArgs)} || true`);
await update(updates, true);
if (hudMessage) {
await showHUD(hudMessage);
}
}

Expand All @@ -39,11 +30,31 @@ export async function stopCaffeinate(updates: Updates, hudMessage?: string) {
}
}

export async function startCaffeinate(updates: Updates, hudMessage?: string, additionalArgs?: string) {
await stopCaffeinate({ menubar: false, status: false });
exec(`/usr/bin/caffeinate ${generateArgs(additionalArgs)} || true`);
await update(updates, true);
if (hudMessage) {
await showHUD(hudMessage);
async function update(updates: Updates, caffeinated: boolean) {
if (updates.menubar) {
await tryLaunchCommand("index", { caffeinated });
}
if (updates.status) {
await tryLaunchCommand("status", { caffeinated });
}
}

async function tryLaunchCommand(commandName: string, context: { caffeinated: boolean }) {
try {
await launchCommand({ name: commandName, type: LaunchType.Background, context });
} catch (error) {
// Handle error if command is not enabled
}
}

function generateArgs(additionalArgs?: string) {
const preferences = getPreferenceValues<Preferences>();
const args = [];

if (preferences.preventDisplay) args.push("d");
if (preferences.preventDisk) args.push("m");
if (preferences.preventSystem) args.push("i");
if (additionalArgs) args.push(` ${additionalArgs}`);

return args.length > 0 ? `-${args.join("")}` : "";
}