2021-04-11 01:09:09 +03:00
|
|
|
// Copyright 2019-2021 Tauri Programme within The Commons Conservancy
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2021-03-07 05:19:12 +03:00
|
|
|
import { invokeTauriCommand } from './helpers/tauri'
|
2021-03-31 08:19:03 +03:00
|
|
|
import { transformCallback } from './tauri'
|
2021-02-12 08:42:40 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* spawns a process
|
|
|
|
*
|
2021-03-31 08:19:03 +03:00
|
|
|
* @param program the name of the program to execute e.g. 'mkdir' or 'node'
|
|
|
|
* @param sidecar whether the program is a sidecar or a system program
|
2021-02-12 08:42:40 +03:00
|
|
|
* @param [args] command args
|
|
|
|
* @return promise resolving to the stdout text
|
|
|
|
*/
|
2021-03-13 03:02:36 +03:00
|
|
|
async function execute(
|
2021-03-31 08:19:03 +03:00
|
|
|
program: string,
|
|
|
|
sidecar: boolean,
|
|
|
|
onEvent: (event: CommandEvent) => void,
|
2021-02-12 08:42:40 +03:00
|
|
|
args?: string | string[]
|
2021-03-31 08:19:03 +03:00
|
|
|
): Promise<number> {
|
2021-02-12 08:42:40 +03:00
|
|
|
if (typeof args === 'object') {
|
|
|
|
Object.freeze(args)
|
|
|
|
}
|
|
|
|
|
2021-03-31 08:19:03 +03:00
|
|
|
return invokeTauriCommand<number>({
|
2021-02-16 07:23:15 +03:00
|
|
|
__tauriModule: 'Shell',
|
2021-02-12 08:42:40 +03:00
|
|
|
message: {
|
|
|
|
cmd: 'execute',
|
2021-03-31 08:19:03 +03:00
|
|
|
program,
|
|
|
|
sidecar,
|
|
|
|
onEventFn: transformCallback(onEvent),
|
2021-02-12 08:42:40 +03:00
|
|
|
args: typeof args === 'string' ? [args] : args
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-31 08:19:03 +03:00
|
|
|
interface ChildProcess {
|
|
|
|
code: number | null
|
|
|
|
signal: number | null
|
|
|
|
stdout: string
|
|
|
|
stderr: string
|
|
|
|
}
|
|
|
|
|
|
|
|
class EventEmitter<E> {
|
2021-04-05 21:37:06 +03:00
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
|
|
eventListeners: { [key: string]: Array<(arg: any) => void> } = Object.create(
|
|
|
|
null
|
|
|
|
)
|
2021-03-31 08:19:03 +03:00
|
|
|
|
|
|
|
private addEventListener(event: string, handler: (arg: any) => void): void {
|
|
|
|
if (event in this.eventListeners) {
|
|
|
|
// eslint-disable-next-line security/detect-object-injection
|
|
|
|
this.eventListeners[event].push(handler)
|
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line security/detect-object-injection
|
|
|
|
this.eventListeners[event] = [handler]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_emit(event: E, payload: any): void {
|
|
|
|
if (event in this.eventListeners) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
|
|
const listeners = this.eventListeners[event as any]
|
|
|
|
for (const listener of listeners) {
|
|
|
|
listener(payload)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
on(event: E, handler: (arg: any) => void): EventEmitter<E> {
|
|
|
|
this.addEventListener(event as any, handler)
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Child {
|
|
|
|
pid: number
|
|
|
|
|
|
|
|
constructor(pid: number) {
|
|
|
|
this.pid = pid
|
|
|
|
}
|
|
|
|
|
|
|
|
async write(data: string | number[]): Promise<void> {
|
|
|
|
return invokeTauriCommand({
|
|
|
|
__tauriModule: 'Shell',
|
|
|
|
message: {
|
|
|
|
cmd: 'stdinWrite',
|
|
|
|
pid: this.pid,
|
|
|
|
buffer: data
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async kill(): Promise<void> {
|
|
|
|
return invokeTauriCommand({
|
|
|
|
__tauriModule: 'Shell',
|
|
|
|
message: {
|
|
|
|
cmd: 'killChild',
|
|
|
|
pid: this.pid
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Command extends EventEmitter<'close' | 'error'> {
|
|
|
|
program: string
|
|
|
|
args: string[]
|
|
|
|
sidecar = false
|
|
|
|
stdout = new EventEmitter<'data'>()
|
|
|
|
stderr = new EventEmitter<'data'>()
|
|
|
|
pid: number | null = null
|
|
|
|
|
|
|
|
constructor(program: string, args: string | string[] = []) {
|
|
|
|
super()
|
|
|
|
this.program = program
|
|
|
|
this.args = typeof args === 'string' ? [args] : args
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a command to execute the given sidecar binary
|
|
|
|
*
|
|
|
|
* @param {string} program Binary name
|
|
|
|
*
|
|
|
|
* @return {Command}
|
|
|
|
*/
|
|
|
|
static sidecar(program: string, args: string | string[] = []): Command {
|
|
|
|
const instance = new Command(program, args)
|
|
|
|
instance.sidecar = true
|
|
|
|
return instance
|
|
|
|
}
|
|
|
|
|
|
|
|
async spawn(): Promise<Child> {
|
|
|
|
return execute(
|
|
|
|
this.program,
|
|
|
|
this.sidecar,
|
|
|
|
(event) => {
|
|
|
|
switch (event.event) {
|
|
|
|
case 'Error':
|
|
|
|
this._emit('error', event.payload)
|
|
|
|
break
|
|
|
|
case 'Terminated':
|
|
|
|
this._emit('close', event.payload)
|
|
|
|
break
|
|
|
|
case 'Stdout':
|
|
|
|
this.stdout._emit('data', event.payload)
|
|
|
|
break
|
|
|
|
case 'Stderr':
|
|
|
|
this.stderr._emit('data', event.payload)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
},
|
|
|
|
this.args
|
|
|
|
).then((pid) => new Child(pid))
|
|
|
|
}
|
|
|
|
|
|
|
|
async execute(): Promise<ChildProcess> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.on('error', reject)
|
|
|
|
const stdout: string[] = []
|
|
|
|
const stderr: string[] = []
|
|
|
|
this.stdout.on('data', (line) => {
|
|
|
|
stdout.push(line)
|
|
|
|
})
|
|
|
|
this.stderr.on('data', (line) => {
|
|
|
|
stderr.push(line)
|
|
|
|
})
|
|
|
|
this.on('close', (payload: TerminatedPayload) => {
|
|
|
|
resolve({
|
|
|
|
code: payload.code,
|
|
|
|
signal: payload.signal,
|
|
|
|
stdout: stdout.join('\n'),
|
|
|
|
stderr: stderr.join('\n')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
this.spawn().catch(reject)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
interface Event<T, V> {
|
|
|
|
event: T
|
|
|
|
payload: V
|
|
|
|
}
|
|
|
|
|
|
|
|
interface TerminatedPayload {
|
|
|
|
code: number | null
|
|
|
|
signal: number | null
|
|
|
|
}
|
|
|
|
|
|
|
|
type CommandEvent =
|
|
|
|
| Event<'Stdout', string>
|
|
|
|
| Event<'Stderr', string>
|
|
|
|
| Event<'Terminated', TerminatedPayload>
|
|
|
|
| Event<'Error', string>
|
|
|
|
|
2021-02-12 08:42:40 +03:00
|
|
|
/**
|
2021-03-10 07:20:54 +03:00
|
|
|
* opens a path or URL with the system's default app,
|
|
|
|
* or the one specified with `openWith`
|
2021-02-12 08:42:40 +03:00
|
|
|
*
|
2021-03-10 07:20:54 +03:00
|
|
|
* @param path the path or URL to open
|
|
|
|
* @param openWith the app to open the file or URL with
|
2021-02-12 08:42:40 +03:00
|
|
|
*/
|
2021-03-13 03:02:36 +03:00
|
|
|
async function open(path: string, openWith?: string): Promise<void> {
|
2021-03-07 05:19:12 +03:00
|
|
|
return invokeTauriCommand({
|
2021-02-16 07:23:15 +03:00
|
|
|
__tauriModule: 'Shell',
|
2021-02-12 08:42:40 +03:00
|
|
|
message: {
|
|
|
|
cmd: 'open',
|
2021-03-10 07:20:54 +03:00
|
|
|
path,
|
|
|
|
with: openWith
|
2021-02-12 08:42:40 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-31 08:19:03 +03:00
|
|
|
export { Command, Child, open }
|