diff --git a/lib/command.ts b/lib/command.ts new file mode 100644 index 0000000..cb5944c --- /dev/null +++ b/lib/command.ts @@ -0,0 +1,37 @@ +import util from 'util'; + +const exec = util.promisify(require('child_process').exec); + +export async function execCommand(commandToExecute: string): Promise { + 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; +}; diff --git a/lib/pactl.ts b/lib/pactl.ts new file mode 100644 index 0000000..e69de29 diff --git a/pages/api/control.ts b/pages/api/control.ts new file mode 100644 index 0000000..9e6060f --- /dev/null +++ b/pages/api/control.ts @@ -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, +) { + 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()); + } +} diff --git a/pages/api/hello.ts b/pages/api/hello.ts deleted file mode 100644 index eb4cc66..0000000 --- a/pages/api/hello.ts +++ /dev/null @@ -1,10 +0,0 @@ -// Next.js API route support: https://nextjs.org/docs/api-routes/introduction -import type { NextApiRequest, NextApiResponse } from 'next'; - -type Data = { - name: string; -}; - -export default function handler(req: NextApiRequest, res: NextApiResponse) { - res.status(200).json({ name: 'John Doe' }); -}