Skip to content

Commit

Permalink
wip client
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-berger committed Nov 25, 2024
1 parent 0f3d539 commit f5d9c87
Showing 1 changed file with 23 additions and 16 deletions.
39 changes: 23 additions & 16 deletions packages/client-api/src/desktop/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export interface ShellProcess {
onExit: (
callback: (status: Omit<ShellExitStatus, 'stdout' | 'stderr'>) => void,
) => void;
kill: () => void;
write: (data: string | Uint8Array) => void;
}

export interface ShellExitStatus {
Expand All @@ -38,6 +40,14 @@ export interface ShellExitStatus {
stderr: string;
}

/**
* Executes a shell command and waits for completion
*
* @param {string} command - Path to program executable, or program name
* (if in $PATH).
* @param {string | string[]} args - Arguments to pass to the program.
* @param {Object} options - Spawn options (optional).
*/
export async function shellExec(
program: string,
args?: string | string[],
Expand All @@ -55,8 +65,9 @@ export async function shellExec(
/**
* Starts a shell command without waiting for completion.
*
* @param {string} command - The command to execute.
* @param {string | string[]} args - Array of command arguments.
* @param {string} command - Path to program executable, or program name
* (if in $PATH).
* @param {string | string[]} args - Arguments to pass to the program.
* @param {Object} options - Spawn options (optional).
*/
export async function shellSpawn(
Expand All @@ -69,33 +80,29 @@ export async function shellSpawn(

return {
processId: process.pid,
onStdout: callback =>
command.stdout.on('data', data => callback(data.toString())),
onStderr: callback =>
command.stderr.on('data', data => callback(data.toString())),
onStdout: callback => command.stdout.on('data', callback),
onStderr: callback => command.stderr.on('data', callback),
onExit: callback =>
command.on('close', status =>
callback({
exitCode: status.code ?? 0,
}),
callback({ exitCode: status.code ?? 0 }),
),
kill: () => process.kill(),
write: data => process.write(data),
};
}

/**
* Creates a Tauri command via its shell plugin.
* Creates a shell command via Tauri's shell plugin.
*/
function createCommand(
program: string,
args?: string | string[],
options?: ShellCommandOptions,
): Command<Uint8Array | string> {
// Convert encoding option to Tauri's format.
const tauriOptions = {
return Command.create(program, args, {
...options,
// Tauri's `SpawnOptions` type is not explicit about allowing `env` to
// be `null`.
env: options?.env ?? undefined,
encoding: options?.encoding === 'raw' ? 'raw' : undefined,
};

return Command.create(program, args, tauriOptions);
});
}

0 comments on commit f5d9c87

Please sign in to comment.