|
|
@ -75,27 +75,27 @@
|
|
|
|
* @module
|
|
|
|
* @module
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
import { invoke, transformCallback } from '@tauri-apps/api/tauri'
|
|
|
|
import { invoke, transformCallback } from "@tauri-apps/api/tauri";
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @since 1.0.0
|
|
|
|
* @since 1.0.0
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
interface SpawnOptions {
|
|
|
|
interface SpawnOptions {
|
|
|
|
/** Current working directory. */
|
|
|
|
/** Current working directory. */
|
|
|
|
cwd?: string
|
|
|
|
cwd?: string;
|
|
|
|
/** Environment variables. set to `null` to clear the process env. */
|
|
|
|
/** Environment variables. set to `null` to clear the process env. */
|
|
|
|
env?: Record<string, string>
|
|
|
|
env?: Record<string, string>;
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Character encoding for stdout/stderr
|
|
|
|
* Character encoding for stdout/stderr
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @since 1.1.0
|
|
|
|
* @since 1.1.0
|
|
|
|
* */
|
|
|
|
* */
|
|
|
|
encoding?: string
|
|
|
|
encoding?: string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @ignore */
|
|
|
|
/** @ignore */
|
|
|
|
interface InternalSpawnOptions extends SpawnOptions {
|
|
|
|
interface InternalSpawnOptions extends SpawnOptions {
|
|
|
|
sidecar?: boolean
|
|
|
|
sidecar?: boolean;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -103,13 +103,13 @@ interface InternalSpawnOptions extends SpawnOptions {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
interface ChildProcess<O extends IOPayload> {
|
|
|
|
interface ChildProcess<O extends IOPayload> {
|
|
|
|
/** Exit code of the process. `null` if the process was terminated by a signal on Unix. */
|
|
|
|
/** Exit code of the process. `null` if the process was terminated by a signal on Unix. */
|
|
|
|
code: number | null
|
|
|
|
code: number | null;
|
|
|
|
/** If the process was terminated by a signal, represents that signal. */
|
|
|
|
/** If the process was terminated by a signal, represents that signal. */
|
|
|
|
signal: number | null
|
|
|
|
signal: number | null;
|
|
|
|
/** The data that the process wrote to `stdout`. */
|
|
|
|
/** The data that the process wrote to `stdout`. */
|
|
|
|
stdout: O
|
|
|
|
stdout: O;
|
|
|
|
/** The data that the process wrote to `stderr`. */
|
|
|
|
/** The data that the process wrote to `stderr`. */
|
|
|
|
stderr: O
|
|
|
|
stderr: O;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -128,16 +128,16 @@ async function execute<O extends IOPayload>(
|
|
|
|
args: string | string[] = [],
|
|
|
|
args: string | string[] = [],
|
|
|
|
options?: InternalSpawnOptions
|
|
|
|
options?: InternalSpawnOptions
|
|
|
|
): Promise<number> {
|
|
|
|
): Promise<number> {
|
|
|
|
if (typeof args === 'object') {
|
|
|
|
if (typeof args === "object") {
|
|
|
|
Object.freeze(args)
|
|
|
|
Object.freeze(args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return invoke<number>('plugin:shell|execute', {
|
|
|
|
return invoke<number>("plugin:shell|execute", {
|
|
|
|
program,
|
|
|
|
program,
|
|
|
|
args,
|
|
|
|
args,
|
|
|
|
options,
|
|
|
|
options,
|
|
|
|
onEventFn: transformCallback(onEvent)
|
|
|
|
onEventFn: transformCallback(onEvent),
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -147,7 +147,7 @@ class EventEmitter<E extends Record<string, any>> {
|
|
|
|
/** @ignore */
|
|
|
|
/** @ignore */
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
|
|
private eventListeners: Record<keyof E, Array<(arg: any) => void>> =
|
|
|
|
private eventListeners: Record<keyof E, Array<(arg: any) => void>> =
|
|
|
|
Object.create(null)
|
|
|
|
Object.create(null);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Alias for `emitter.on(eventName, listener)`.
|
|
|
|
* Alias for `emitter.on(eventName, listener)`.
|
|
|
@ -158,7 +158,7 @@ class EventEmitter<E extends Record<string, any>> {
|
|
|
|
eventName: N,
|
|
|
|
eventName: N,
|
|
|
|
listener: (arg: E[typeof eventName]) => void
|
|
|
|
listener: (arg: E[typeof eventName]) => void
|
|
|
|
): this {
|
|
|
|
): this {
|
|
|
|
return this.on(eventName, listener)
|
|
|
|
return this.on(eventName, listener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -170,7 +170,7 @@ class EventEmitter<E extends Record<string, any>> {
|
|
|
|
eventName: N,
|
|
|
|
eventName: N,
|
|
|
|
listener: (arg: E[typeof eventName]) => void
|
|
|
|
listener: (arg: E[typeof eventName]) => void
|
|
|
|
): this {
|
|
|
|
): this {
|
|
|
|
return this.off(eventName, listener)
|
|
|
|
return this.off(eventName, listener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -189,12 +189,12 @@ class EventEmitter<E extends Record<string, any>> {
|
|
|
|
): this {
|
|
|
|
): this {
|
|
|
|
if (eventName in this.eventListeners) {
|
|
|
|
if (eventName in this.eventListeners) {
|
|
|
|
// eslint-disable-next-line security/detect-object-injection
|
|
|
|
// eslint-disable-next-line security/detect-object-injection
|
|
|
|
this.eventListeners[eventName].push(listener)
|
|
|
|
this.eventListeners[eventName].push(listener);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line security/detect-object-injection
|
|
|
|
// eslint-disable-next-line security/detect-object-injection
|
|
|
|
this.eventListeners[eventName] = [listener]
|
|
|
|
this.eventListeners[eventName] = [listener];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -210,11 +210,11 @@ class EventEmitter<E extends Record<string, any>> {
|
|
|
|
listener: (arg: E[typeof eventName]) => void
|
|
|
|
listener: (arg: E[typeof eventName]) => void
|
|
|
|
): this {
|
|
|
|
): this {
|
|
|
|
const wrapper = (arg: E[typeof eventName]): void => {
|
|
|
|
const wrapper = (arg: E[typeof eventName]): void => {
|
|
|
|
this.removeListener(eventName, wrapper)
|
|
|
|
this.removeListener(eventName, wrapper);
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
|
|
listener(arg)
|
|
|
|
listener(arg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return this.addListener(eventName, wrapper)
|
|
|
|
return this.addListener(eventName, wrapper);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -231,9 +231,9 @@ class EventEmitter<E extends Record<string, any>> {
|
|
|
|
// eslint-disable-next-line security/detect-object-injection
|
|
|
|
// eslint-disable-next-line security/detect-object-injection
|
|
|
|
this.eventListeners[eventName] = this.eventListeners[eventName].filter(
|
|
|
|
this.eventListeners[eventName] = this.eventListeners[eventName].filter(
|
|
|
|
(l) => l !== listener
|
|
|
|
(l) => l !== listener
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -246,12 +246,12 @@ class EventEmitter<E extends Record<string, any>> {
|
|
|
|
removeAllListeners<N extends keyof E>(event?: N): this {
|
|
|
|
removeAllListeners<N extends keyof E>(event?: N): this {
|
|
|
|
if (event) {
|
|
|
|
if (event) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete,security/detect-object-injection
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete,security/detect-object-injection
|
|
|
|
delete this.eventListeners[event]
|
|
|
|
delete this.eventListeners[event];
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
|
|
this.eventListeners = Object.create(null)
|
|
|
|
this.eventListeners = Object.create(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -264,12 +264,12 @@ class EventEmitter<E extends Record<string, any>> {
|
|
|
|
emit<N extends keyof E>(eventName: N, arg: E[typeof eventName]): boolean {
|
|
|
|
emit<N extends keyof E>(eventName: N, arg: E[typeof eventName]): boolean {
|
|
|
|
if (eventName in this.eventListeners) {
|
|
|
|
if (eventName in this.eventListeners) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,security/detect-object-injection
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment,security/detect-object-injection
|
|
|
|
const listeners = this.eventListeners[eventName]
|
|
|
|
const listeners = this.eventListeners[eventName];
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
|
|
for (const listener of listeners) listener(arg)
|
|
|
|
for (const listener of listeners) listener(arg);
|
|
|
|
return true
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -280,8 +280,8 @@ class EventEmitter<E extends Record<string, any>> {
|
|
|
|
listenerCount<N extends keyof E>(eventName: N): number {
|
|
|
|
listenerCount<N extends keyof E>(eventName: N): number {
|
|
|
|
if (eventName in this.eventListeners)
|
|
|
|
if (eventName in this.eventListeners)
|
|
|
|
// eslint-disable-next-line security/detect-object-injection
|
|
|
|
// eslint-disable-next-line security/detect-object-injection
|
|
|
|
return this.eventListeners[eventName].length
|
|
|
|
return this.eventListeners[eventName].length;
|
|
|
|
return 0
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -300,12 +300,12 @@ class EventEmitter<E extends Record<string, any>> {
|
|
|
|
): this {
|
|
|
|
): this {
|
|
|
|
if (eventName in this.eventListeners) {
|
|
|
|
if (eventName in this.eventListeners) {
|
|
|
|
// eslint-disable-next-line security/detect-object-injection
|
|
|
|
// eslint-disable-next-line security/detect-object-injection
|
|
|
|
this.eventListeners[eventName].unshift(listener)
|
|
|
|
this.eventListeners[eventName].unshift(listener);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// eslint-disable-next-line security/detect-object-injection
|
|
|
|
// eslint-disable-next-line security/detect-object-injection
|
|
|
|
this.eventListeners[eventName] = [listener]
|
|
|
|
this.eventListeners[eventName] = [listener];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -321,11 +321,11 @@ class EventEmitter<E extends Record<string, any>> {
|
|
|
|
listener: (arg: E[typeof eventName]) => void
|
|
|
|
listener: (arg: E[typeof eventName]) => void
|
|
|
|
): this {
|
|
|
|
): this {
|
|
|
|
const wrapper = (arg: any): void => {
|
|
|
|
const wrapper = (arg: any): void => {
|
|
|
|
this.removeListener(eventName, wrapper)
|
|
|
|
this.removeListener(eventName, wrapper);
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
|
|
listener(arg)
|
|
|
|
listener(arg);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
return this.prependListener(eventName, wrapper)
|
|
|
|
return this.prependListener(eventName, wrapper);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -334,10 +334,10 @@ class EventEmitter<E extends Record<string, any>> {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
class Child {
|
|
|
|
class Child {
|
|
|
|
/** The child process `pid`. */
|
|
|
|
/** The child process `pid`. */
|
|
|
|
pid: number
|
|
|
|
pid: number;
|
|
|
|
|
|
|
|
|
|
|
|
constructor(pid: number) {
|
|
|
|
constructor(pid: number) {
|
|
|
|
this.pid = pid
|
|
|
|
this.pid = pid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -356,11 +356,11 @@ class Child {
|
|
|
|
* @returns A promise indicating the success or failure of the operation.
|
|
|
|
* @returns A promise indicating the success or failure of the operation.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async write(data: IOPayload): Promise<void> {
|
|
|
|
async write(data: IOPayload): Promise<void> {
|
|
|
|
return invoke('plugin:shell|stdin_write', {
|
|
|
|
return invoke("plugin:shell|stdin_write", {
|
|
|
|
pid: this.pid,
|
|
|
|
pid: this.pid,
|
|
|
|
// correctly serialize Uint8Arrays
|
|
|
|
// correctly serialize Uint8Arrays
|
|
|
|
buffer: typeof data === 'string' ? data : Array.from(data)
|
|
|
|
buffer: typeof data === "string" ? data : Array.from(data),
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -369,20 +369,20 @@ class Child {
|
|
|
|
* @returns A promise indicating the success or failure of the operation.
|
|
|
|
* @returns A promise indicating the success or failure of the operation.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async kill(): Promise<void> {
|
|
|
|
async kill(): Promise<void> {
|
|
|
|
return invoke('plugin:shell|kill', {
|
|
|
|
return invoke("plugin:shell|kill", {
|
|
|
|
cmd: 'killChild',
|
|
|
|
cmd: "killChild",
|
|
|
|
pid: this.pid
|
|
|
|
pid: this.pid,
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
interface CommandEvents {
|
|
|
|
interface CommandEvents {
|
|
|
|
close: TerminatedPayload
|
|
|
|
close: TerminatedPayload;
|
|
|
|
error: string
|
|
|
|
error: string;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
interface OutputEvents<O extends IOPayload> {
|
|
|
|
interface OutputEvents<O extends IOPayload> {
|
|
|
|
data: O
|
|
|
|
data: O;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -408,15 +408,15 @@ interface OutputEvents<O extends IOPayload> {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
class Command<O extends IOPayload> extends EventEmitter<CommandEvents> {
|
|
|
|
class Command<O extends IOPayload> extends EventEmitter<CommandEvents> {
|
|
|
|
/** @ignore Program to execute. */
|
|
|
|
/** @ignore Program to execute. */
|
|
|
|
private readonly program: string
|
|
|
|
private readonly program: string;
|
|
|
|
/** @ignore Program arguments */
|
|
|
|
/** @ignore Program arguments */
|
|
|
|
private readonly args: string[]
|
|
|
|
private readonly args: string[];
|
|
|
|
/** @ignore Spawn options. */
|
|
|
|
/** @ignore Spawn options. */
|
|
|
|
private readonly options: InternalSpawnOptions
|
|
|
|
private readonly options: InternalSpawnOptions;
|
|
|
|
/** Event emitter for the `stdout`. Emits the `data` event. */
|
|
|
|
/** Event emitter for the `stdout`. Emits the `data` event. */
|
|
|
|
readonly stdout = new EventEmitter<OutputEvents<O>>()
|
|
|
|
readonly stdout = new EventEmitter<OutputEvents<O>>();
|
|
|
|
/** Event emitter for the `stderr`. Emits the `data` event. */
|
|
|
|
/** Event emitter for the `stderr`. Emits the `data` event. */
|
|
|
|
readonly stderr = new EventEmitter<OutputEvents<O>>()
|
|
|
|
readonly stderr = new EventEmitter<OutputEvents<O>>();
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @ignore
|
|
|
|
* @ignore
|
|
|
@ -432,23 +432,23 @@ class Command<O extends IOPayload> extends EventEmitter<CommandEvents> {
|
|
|
|
args: string | string[] = [],
|
|
|
|
args: string | string[] = [],
|
|
|
|
options?: SpawnOptions
|
|
|
|
options?: SpawnOptions
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
super()
|
|
|
|
super();
|
|
|
|
this.program = program
|
|
|
|
this.program = program;
|
|
|
|
this.args = typeof args === 'string' ? [args] : args
|
|
|
|
this.args = typeof args === "string" ? [args] : args;
|
|
|
|
this.options = options ?? {}
|
|
|
|
this.options = options ?? {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static create(program: string, args?: string | string[]): Command<string>
|
|
|
|
static create(program: string, args?: string | string[]): Command<string>;
|
|
|
|
static create(
|
|
|
|
static create(
|
|
|
|
program: string,
|
|
|
|
program: string,
|
|
|
|
args?: string | string[],
|
|
|
|
args?: string | string[],
|
|
|
|
options?: SpawnOptions & { encoding: 'raw' }
|
|
|
|
options?: SpawnOptions & { encoding: "raw" }
|
|
|
|
): Command<Uint8Array>
|
|
|
|
): Command<Uint8Array>;
|
|
|
|
static create(
|
|
|
|
static create(
|
|
|
|
program: string,
|
|
|
|
program: string,
|
|
|
|
args?: string | string[],
|
|
|
|
args?: string | string[],
|
|
|
|
options?: SpawnOptions
|
|
|
|
options?: SpawnOptions
|
|
|
|
): Command<string>
|
|
|
|
): Command<string>;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Creates a command to execute the given program.
|
|
|
|
* Creates a command to execute the given program.
|
|
|
@ -467,20 +467,20 @@ class Command<O extends IOPayload> extends EventEmitter<CommandEvents> {
|
|
|
|
args: string | string[] = [],
|
|
|
|
args: string | string[] = [],
|
|
|
|
options?: SpawnOptions
|
|
|
|
options?: SpawnOptions
|
|
|
|
): Command<O> {
|
|
|
|
): Command<O> {
|
|
|
|
return new Command(program, args, options)
|
|
|
|
return new Command(program, args, options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static sidecar(program: string, args?: string | string[]): Command<string>
|
|
|
|
static sidecar(program: string, args?: string | string[]): Command<string>;
|
|
|
|
static sidecar(
|
|
|
|
static sidecar(
|
|
|
|
program: string,
|
|
|
|
program: string,
|
|
|
|
args?: string | string[],
|
|
|
|
args?: string | string[],
|
|
|
|
options?: SpawnOptions & { encoding: 'raw' }
|
|
|
|
options?: SpawnOptions & { encoding: "raw" }
|
|
|
|
): Command<Uint8Array>
|
|
|
|
): Command<Uint8Array>;
|
|
|
|
static sidecar(
|
|
|
|
static sidecar(
|
|
|
|
program: string,
|
|
|
|
program: string,
|
|
|
|
args?: string | string[],
|
|
|
|
args?: string | string[],
|
|
|
|
options?: SpawnOptions
|
|
|
|
options?: SpawnOptions
|
|
|
|
): Command<string>
|
|
|
|
): Command<string>;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Creates a command to execute the given sidecar program.
|
|
|
|
* Creates a command to execute the given sidecar program.
|
|
|
@ -499,9 +499,9 @@ class Command<O extends IOPayload> extends EventEmitter<CommandEvents> {
|
|
|
|
args: string | string[] = [],
|
|
|
|
args: string | string[] = [],
|
|
|
|
options?: SpawnOptions
|
|
|
|
options?: SpawnOptions
|
|
|
|
): Command<O> {
|
|
|
|
): Command<O> {
|
|
|
|
const instance = new Command<O>(program, args, options)
|
|
|
|
const instance = new Command<O>(program, args, options);
|
|
|
|
instance.options.sidecar = true
|
|
|
|
instance.options.sidecar = true;
|
|
|
|
return instance
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -513,24 +513,24 @@ class Command<O extends IOPayload> extends EventEmitter<CommandEvents> {
|
|
|
|
return execute<O>(
|
|
|
|
return execute<O>(
|
|
|
|
(event) => {
|
|
|
|
(event) => {
|
|
|
|
switch (event.event) {
|
|
|
|
switch (event.event) {
|
|
|
|
case 'Error':
|
|
|
|
case "Error":
|
|
|
|
this.emit('error', event.payload)
|
|
|
|
this.emit("error", event.payload);
|
|
|
|
break
|
|
|
|
break;
|
|
|
|
case 'Terminated':
|
|
|
|
case "Terminated":
|
|
|
|
this.emit('close', event.payload)
|
|
|
|
this.emit("close", event.payload);
|
|
|
|
break
|
|
|
|
break;
|
|
|
|
case 'Stdout':
|
|
|
|
case "Stdout":
|
|
|
|
this.stdout.emit('data', event.payload)
|
|
|
|
this.stdout.emit("data", event.payload);
|
|
|
|
break
|
|
|
|
break;
|
|
|
|
case 'Stderr':
|
|
|
|
case "Stderr":
|
|
|
|
this.stderr.emit('data', event.payload)
|
|
|
|
this.stderr.emit("data", event.payload);
|
|
|
|
break
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
this.program,
|
|
|
|
this.program,
|
|
|
|
this.args,
|
|
|
|
this.args,
|
|
|
|
this.options
|
|
|
|
this.options
|
|
|
|
).then((pid) => new Child(pid))
|
|
|
|
).then((pid) => new Child(pid));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -549,38 +549,38 @@ class Command<O extends IOPayload> extends EventEmitter<CommandEvents> {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async execute(): Promise<ChildProcess<O>> {
|
|
|
|
async execute(): Promise<ChildProcess<O>> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.on('error', reject)
|
|
|
|
this.on("error", reject);
|
|
|
|
|
|
|
|
|
|
|
|
const stdout: O[] = []
|
|
|
|
const stdout: O[] = [];
|
|
|
|
const stderr: O[] = []
|
|
|
|
const stderr: O[] = [];
|
|
|
|
this.stdout.on('data', (line: O) => {
|
|
|
|
this.stdout.on("data", (line: O) => {
|
|
|
|
stdout.push(line)
|
|
|
|
stdout.push(line);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
this.stderr.on('data', (line: O) => {
|
|
|
|
this.stderr.on("data", (line: O) => {
|
|
|
|
stderr.push(line)
|
|
|
|
stderr.push(line);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
this.on('close', (payload: TerminatedPayload) => {
|
|
|
|
this.on("close", (payload: TerminatedPayload) => {
|
|
|
|
resolve({
|
|
|
|
resolve({
|
|
|
|
code: payload.code,
|
|
|
|
code: payload.code,
|
|
|
|
signal: payload.signal,
|
|
|
|
signal: payload.signal,
|
|
|
|
stdout: this.collectOutput(stdout) as O,
|
|
|
|
stdout: this.collectOutput(stdout) as O,
|
|
|
|
stderr: this.collectOutput(stderr) as O
|
|
|
|
stderr: this.collectOutput(stderr) as O,
|
|
|
|
})
|
|
|
|
});
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
this.spawn().catch(reject)
|
|
|
|
this.spawn().catch(reject);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** @ignore */
|
|
|
|
/** @ignore */
|
|
|
|
private collectOutput(events: O[]): string | Uint8Array {
|
|
|
|
private collectOutput(events: O[]): string | Uint8Array {
|
|
|
|
if (this.options.encoding === 'raw') {
|
|
|
|
if (this.options.encoding === "raw") {
|
|
|
|
return events.reduce<Uint8Array>((p, c) => {
|
|
|
|
return events.reduce<Uint8Array>((p, c) => {
|
|
|
|
return new Uint8Array([...p, ...(c as Uint8Array), 10])
|
|
|
|
return new Uint8Array([...p, ...(c as Uint8Array), 10]);
|
|
|
|
}, new Uint8Array())
|
|
|
|
}, new Uint8Array());
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
return events.join('\n')
|
|
|
|
return events.join("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -589,8 +589,8 @@ class Command<O extends IOPayload> extends EventEmitter<CommandEvents> {
|
|
|
|
* Describes the event message received from the command.
|
|
|
|
* Describes the event message received from the command.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
interface Event<T, V> {
|
|
|
|
interface Event<T, V> {
|
|
|
|
event: T
|
|
|
|
event: T;
|
|
|
|
payload: V
|
|
|
|
payload: V;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -598,20 +598,20 @@ interface Event<T, V> {
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
interface TerminatedPayload {
|
|
|
|
interface TerminatedPayload {
|
|
|
|
/** Exit code of the process. `null` if the process was terminated by a signal on Unix. */
|
|
|
|
/** Exit code of the process. `null` if the process was terminated by a signal on Unix. */
|
|
|
|
code: number | null
|
|
|
|
code: number | null;
|
|
|
|
/** If the process was terminated by a signal, represents that signal. */
|
|
|
|
/** If the process was terminated by a signal, represents that signal. */
|
|
|
|
signal: number | null
|
|
|
|
signal: number | null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Event payload type */
|
|
|
|
/** Event payload type */
|
|
|
|
type IOPayload = string | Uint8Array
|
|
|
|
type IOPayload = string | Uint8Array;
|
|
|
|
|
|
|
|
|
|
|
|
/** Events emitted by the child process. */
|
|
|
|
/** Events emitted by the child process. */
|
|
|
|
type CommandEvent<O extends IOPayload> =
|
|
|
|
type CommandEvent<O extends IOPayload> =
|
|
|
|
| Event<'Stdout', O>
|
|
|
|
| Event<"Stdout", O>
|
|
|
|
| Event<'Stderr', O>
|
|
|
|
| Event<"Stderr", O>
|
|
|
|
| Event<'Terminated', TerminatedPayload>
|
|
|
|
| Event<"Terminated", TerminatedPayload>
|
|
|
|
| Event<'Error', string>
|
|
|
|
| Event<"Error", string>;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Opens a path or URL with the system's default app,
|
|
|
|
* Opens a path or URL with the system's default app,
|
|
|
@ -640,18 +640,18 @@ type CommandEvent<O extends IOPayload> =
|
|
|
|
* @since 1.0.0
|
|
|
|
* @since 1.0.0
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
async function open(path: string, openWith?: string): Promise<void> {
|
|
|
|
async function open(path: string, openWith?: string): Promise<void> {
|
|
|
|
return invoke('plugin:shell|open', {
|
|
|
|
return invoke("plugin:shell|open", {
|
|
|
|
path,
|
|
|
|
path,
|
|
|
|
with: openWith
|
|
|
|
with: openWith,
|
|
|
|
})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export { Command, Child, EventEmitter, open }
|
|
|
|
export { Command, Child, EventEmitter, open };
|
|
|
|
export type {
|
|
|
|
export type {
|
|
|
|
IOPayload,
|
|
|
|
IOPayload,
|
|
|
|
CommandEvents,
|
|
|
|
CommandEvents,
|
|
|
|
TerminatedPayload,
|
|
|
|
TerminatedPayload,
|
|
|
|
OutputEvents,
|
|
|
|
OutputEvents,
|
|
|
|
ChildProcess,
|
|
|
|
ChildProcess,
|
|
|
|
SpawnOptions
|
|
|
|
SpawnOptions,
|
|
|
|
}
|
|
|
|
};
|
|
|
|