Skip to content

Commit

Permalink
Basic implementation of play, pause, toggle
Browse files Browse the repository at this point in the history
  • Loading branch information
LoLei committed Mar 14, 2022
1 parent b577ed8 commit 598d4a7
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
37 changes: 37 additions & 0 deletions lib/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import util from 'util';

const exec = util.promisify(require('child_process').exec);

export async function execCommand(commandToExecute: string): Promise<ExecResult> {
if (!Object.values(COMMANDS).includes(commandToExecute)) {
throw new Error(`Command ${commandToExecute} is not a valid command.`);
}
const { stdout, stderr } = await exec(commandToExecute);
return {
commandOutput: {
stdout,
stderr,
},
metaInfo: `Executed command ${commandToExecute}`,
};
}

export const COMMANDS = {
LS: 'ls', // For debugging
PLAYERCTL_PLAY: 'playerctl play',
PLAYERCTL_PAUSE: 'playerctl pause',
PLAYERCTL_TOGGLE: 'playerctl play-pause',
// Future: next, position, toggle...
PACTL_VOL_INC: 'pactl set-sink-volume 2 +5%',
PACTL_VOL_DEV: 'pactl set-sink-volume 2 -5%',
};

type CommandOutput = {
stdout?: string;
stderr?: string;
};

export type ExecResult = {
commandOutput: CommandOutput;
metaInfo: string;
};
Empty file added lib/pactl.ts
Empty file.
16 changes: 16 additions & 0 deletions pages/api/control.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { COMMANDS, execCommand } from '../../lib/command';
import type { ExecResult } from '../../lib/command';

export default async function handler(
req: NextApiRequest,
res: NextApiResponse<ExecResult | string>,
) {
try {
const execResult = await execCommand(COMMANDS.PLAYERCTL_TOGGLE);
res.status(200).json(execResult);
} catch (e: unknown) {
console.error(e);
res.status(500).send((e as Error).toString());
}
}
10 changes: 0 additions & 10 deletions pages/api/hello.ts

This file was deleted.

0 comments on commit 598d4a7

Please sign in to comment.