-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Basic implementation of play, pause, toggle
- Loading branch information
Showing
4 changed files
with
53 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.