From 4ea7ec163e5ad49c702f978b36d803e233aebc4e Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Fri, 6 Apr 2018 14:16:54 -0700 Subject: [PATCH] Add explicit typings to node-pty Fixes #154 --- package.json | 2 +- src/unixTerminal.ts | 3 +- tsconfig.json | 3 +- typings/node-pty.d.ts | 81 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 typings/node-pty.d.ts diff --git a/package.json b/package.json index bbaa3f2cb..bd4dd8228 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,7 @@ "version": "0.7.3", "license": "MIT", "main": "./lib/index.js", - "types": "./lib/index.d.ts", + "types": "./typings/node-pty.d.ts", "repository": { "type": "git", "url": "git://github.com/Tyriar/node-pty.git" diff --git a/src/unixTerminal.ts b/src/unixTerminal.ts index 0d7ef55c5..d0364626d 100644 --- a/src/unixTerminal.ts +++ b/src/unixTerminal.ts @@ -231,8 +231,7 @@ export class UnixTerminal extends Terminal { this._socket.destroy(); } - public kill(signal?: string): void { - signal = signal || 'SIGHUP'; + public kill(signal: string = 'SIGHUP'): void { if (signal in os.constants.signals) { try { // pty.kill will not be available on systems which don't support diff --git a/tsconfig.json b/tsconfig.json index 121974db0..87c27a17d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,8 +4,7 @@ "target": "es5", "rootDir": "src", "outDir": "lib", - "sourceMap": true, - "declaration": true + "sourceMap": true }, "exclude": [ "node_modules", diff --git a/typings/node-pty.d.ts b/typings/node-pty.d.ts new file mode 100644 index 000000000..c5fb3309a --- /dev/null +++ b/typings/node-pty.d.ts @@ -0,0 +1,81 @@ +/** + * Copyright (c) 2017, Daniel Imms (MIT License). + */ + +declare module 'node-pty' { + /** + * Forks a process as a pseudoterminal. + * @param file The file to launch. + * @param args The file's arguments as argv (string[]) or in a pre-escaped CommandLine format + * (string). Note that the CommandLine option is only available on Windows and is expected to be + * escaped properly. + * @param options The options of the terminal. + * @see CommandLineToArgvW https://msdn.microsoft.com/en-us/library/windows/desktop/bb776391(v=vs.85).aspx + * @see Parsing C++ Comamnd-Line Arguments https://msdn.microsoft.com/en-us/library/17w5ykft.aspx + * @see GetCommandLine https://msdn.microsoft.com/en-us/library/windows/desktop/ms683156.aspx + */ + export function spawn(file: string, args: string[] | string, options: IPtyForkOptions): IPty; + + export interface IPtyForkOptions { + name?: string; + cols?: number; + rows?: number; + cwd?: string; + env?: { [key: string]: string }; + uid?: number; + gid?: number; + encoding?: string; + } + + /** + * An interface representing a pseudoterminal, on Windows this is emulated via the winpty library. + */ + export interface IPty { + /** + * The process ID of the outer process. + */ + pid: number; + + /** + * The title of the active process. + */ + process: string; + + /** + * Adds a listener to the data event, fired when data is returned from the pty. + * @param event The name of the event. + * @param listener The callback function. + */ + on(event: 'data', listener: (data: string) => void): void; + + /** + * Adds a listener to the exit event, fired when the pty exits. + * @param event The name of the event. + * @param listener The callback function, exitCode is the exit code of the process and signal is + * the signal that triggered the exit. signal is not supported on Windows. + */ + on(event: 'exit', listener: (exitCode: number, signal?: number) => void): void; + + /** + * Resizes the dimensions of the pty. + * @param columns THe number of columns to use. + * @param rows The number of rows to use. + */ + resize(columns: number, rows: number): void; + + /** + * Writes data to the pty. + * @param data The data to write. + */ + write(data: string): void; + + /** + * Kills the pty. + * @param signal The signal to use, defaults to SIGHUP. If the TIOCSIG/TIOCSIGNAL ioctl is not + * supported then the process will be killed instead. This parameter is not supported on + * Windows. + * @throws Will throw when signal is used on Windows. + */ + kill(signal?: string): void; + } +}