From 35b7fd0b7f32e052c01975dece6efc03510432e3 Mon Sep 17 00:00:00 2001 From: "Rodrigo F. Fernandes" Date: Thu, 2 Mar 2017 23:25:07 -0300 Subject: [PATCH] Added typings for 'node-powershell' --- node-powershell/index.d.ts | 92 ++++++++++++++++++++++++ node-powershell/node-powershell-tests.ts | 34 +++++++++ node-powershell/tsconfig.json | 22 ++++++ 3 files changed, 148 insertions(+) create mode 100644 node-powershell/index.d.ts create mode 100644 node-powershell/node-powershell-tests.ts create mode 100644 node-powershell/tsconfig.json diff --git a/node-powershell/index.d.ts b/node-powershell/index.d.ts new file mode 100644 index 0000000000..bdddd1a448 --- /dev/null +++ b/node-powershell/index.d.ts @@ -0,0 +1,92 @@ +// Type definitions for node-powershell v3.1.1 +// Project: https://github.com/rannn505/node-powershell +// Definitions by: Rodrigo F. Fernandes +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +declare module 'node-powershell' { + module NodePowershell { + export interface ShellOptions { + /** + * Determines whether to log verbose to the console. + */ + debugMsg?: boolean; + + /** + * Sets the input encoding for the current shell. + */ + inputEncoding?: string; + + /** + * Sets the output encoding for the current shell. + */ + outputEncoding?: string; + + /** + * Sets the default execution policy for the current shell session + */ + executionPolicy?: string; + + /** + * Determines whether to load the Windows PS profile + */ + noProfile?: boolean; + } + + export interface ShellStream { + stdin: NodeJS.WritableStream; + stdout: NodeJS.ReadableStream; + err: NodeJS.ReadableStream; + } + } + + class NodePowershell { + /** + * An array containing the command history ever added to the shell instance. + */ + public history: string[]; + + /** + * An object containing the sdtio (in,out,err) [stream.Readable] of the PowerShell Instance. + */ + public streams: NodePowershell.ShellStream; + + /** + * Creates a new Shell instance. + */ + constructor(options: NodePowershell.ShellOptions); + + /** + * Adds a command to the end of the pipeline of the shell object. + * @param {string} required - PowerShell command - PowerShell script + * @param {array} optional - {name: ‘’, value: ‘’} - {name: value} - ‘switch’ + * @returns Every command currently in the pipeline. + */ + addCommand(command: string, params?: string[] | { [key: string]: string }[]): Promise; + + /** + * Runs the commands of the shell object pipeline. + * @returns Promise with the command output. + */ + invoke(): Promise; + + /** + * Releases all resources used by the shell object and closes the PowerShell child_process. + * @returns Promise with the error message. + */ + dispose(): Promise; + + /** + * Listens to events emitted from the shell. + * @param eventName required - Possible events: + * 'output' - Emits when shell has an output. + * 'err' - Emits when shell has an error. + * 'end' - Emits when shell ends. + * @param callback required - Callback function to be called when the event emits. + */ + on(eventName: string, callback: (output: string) => void): void; + } + + export = NodePowershell; +} diff --git a/node-powershell/node-powershell-tests.ts b/node-powershell/node-powershell-tests.ts new file mode 100644 index 0000000000..e1b28e4b92 --- /dev/null +++ b/node-powershell/node-powershell-tests.ts @@ -0,0 +1,34 @@ +import * as nodePowershell from 'node-powershell'; + +var options: nodePowershell.ShellOptions = { + debugMsg: true +}; + +// Initialization +var ps = new nodePowershell(options); + +// Methods +ps.addCommand('Write-Host node-powershell', [ + { name: 'foregroundcolor', value: 'red' }, + { name: 'nonewline', value: null } //switch +]).then((cmdsArr: string[]) => { }).catch((err: any) => { }); + +ps.addCommand('Write-Host node-powershell', [ + { foregroundcolor: 'red' }, + { nonewline: null } //switch +]); + +ps.invoke().then((output: string) => { }).catch((err: any) => { }); + +ps.dispose().then((code: string) => { }).catch((err: any) => { }); + +// Properties +console.log(ps.history); + +ps.streams.stdin.write('data'); +ps.streams.stdout.on('data', (data: any) => { }); + +// Events +ps.on('output', (data: string) => { }); +ps.on('err', (err: string) => { }); +ps.on('end', (code: string) => { }); diff --git a/node-powershell/tsconfig.json b/node-powershell/tsconfig.json new file mode 100644 index 0000000000..c8ca0fed82 --- /dev/null +++ b/node-powershell/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": false, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "node-powershell-tests.ts" + ] +}