Skip to content

Commit

Permalink
Handle kill signals
Browse files Browse the repository at this point in the history
  • Loading branch information
LitoMore committed Jun 9, 2024
1 parent ac83ccf commit 0ec9d5d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
38 changes: 23 additions & 15 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {execa} from 'execa';
import {ExecaError, execa} from 'execa';
import {killRunningSay} from './utils.js';

export type SayOptions = {
Expand All @@ -15,20 +15,28 @@ export type SayOptions = {
export async function say(text: string, options: SayOptions = {}) {
await killRunningSay();
const {voice, rate, audioDevice, quality, inputFile, outputFile, networkSend, channels} = options;
await execa(
'say',
[
text,
voice && ['--voice', voice],
rate && ['--rate', rate],
audioDevice && ['--audio-device', audioDevice],
quality && ['--quality', quality],
inputFile && ['--input-file', inputFile],
outputFile && ['--output-file', outputFile],
networkSend && ['--network-send', networkSend],
channels && ['--channels', channels],
].flat().filter(Boolean) as string[],
);
try {
await execa(
'say',
[
text,
voice && ['--voice', voice],
rate && ['--rate', rate],
audioDevice && ['--audio-device', audioDevice],
quality && ['--quality', quality],
inputFile && ['--input-file', inputFile],
outputFile && ['--output-file', outputFile],
networkSend && ['--network-send', networkSend],
channels && ['--channels', channels],
].flat().filter(Boolean) as string[],
);
} catch (error) {
if (error instanceof ExecaError && error.signal === 'SIGKILL') {
return;
}

throw error;
}
}

export {
Expand Down
2 changes: 1 addition & 1 deletion source/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ export const checkIfSayIsRunning = async () => {
export const killRunningSay = async () => {
const sayProcess = await checkIfSayIsRunning();
if (sayProcess) {
await fkill(sayProcess.pid, {force: true});
await fkill(sayProcess.pid, {force: true, silent: true});
}
};

0 comments on commit 0ec9d5d

Please sign in to comment.