From 2909ac3accf4982cb7fdceadcb7e16c7bae76ca4 Mon Sep 17 00:00:00 2001 From: Zev Spitz Date: Sat, 12 May 2018 00:22:21 +0300 Subject: [PATCH] Split activex-interop from windows-script-host; replace activex-iwshruntimelibrary (#25695) * Interop definitions - https://github.com/Microsoft/TypeScript/issues/21440 * WSH definitions * Merge IWshRuntimeLibrary; -Object methods use ActiveXObjectNameMap * WshNamed, WshUnnamed and default properties * Merge activex-iwshruntimelibrary into windows-script-host * WScript.Echo optional parameter * Deleted package.json --- .../activex-interop/activex-interop-tests.ts | 49 +++ types/activex-interop/index.d.ts | 111 +++++++ types/activex-interop/tsconfig.json | 21 ++ .../tslint.json | 0 types/activex-iwshruntimelibrary/package.json | 6 - .../index.d.ts | 306 ++++++++++++++++-- .../tsconfig.json | 7 +- types/windows-script-host/tslint.json | 6 + .../windows-script-host-tests.ts} | 31 ++ 9 files changed, 493 insertions(+), 44 deletions(-) create mode 100644 types/activex-interop/activex-interop-tests.ts create mode 100644 types/activex-interop/index.d.ts create mode 100644 types/activex-interop/tsconfig.json rename types/{activex-iwshruntimelibrary => activex-interop}/tslint.json (100%) delete mode 100644 types/activex-iwshruntimelibrary/package.json rename types/{activex-iwshruntimelibrary => windows-script-host}/index.d.ts (61%) rename types/{activex-iwshruntimelibrary => windows-script-host}/tsconfig.json (82%) create mode 100644 types/windows-script-host/tslint.json rename types/{activex-iwshruntimelibrary/activex-iwshruntimelibrary-tests.ts => windows-script-host/windows-script-host-tests.ts} (67%) diff --git a/types/activex-interop/activex-interop-tests.ts b/types/activex-interop/activex-interop-tests.ts new file mode 100644 index 0000000000..e2ece7401e --- /dev/null +++ b/types/activex-interop/activex-interop-tests.ts @@ -0,0 +1,49 @@ +// copied from the definitions in activex-scripting +interface Dictionary { + /** Add a new key and item to the dictionary. */ + Add(Key: TKey, Item: TItem): void; + + /** Get the number of items in the dictionary. */ + readonly Count: number; + + /** Determine if a given key is in the dictionary. */ + Exists(Key: TKey): boolean; + HashVal(Key: TKey): any; + + /** Set or get the item for a given key */ + Item(Key: TKey): TItem; + + /** Get an array containing all items in the dictionary. */ + Items(): SafeArray; + + /** Change a key to a different key. */ + Key(Key: TKey): TKey; + + /** Get an array containing all keys in the dictionary. */ + Keys(): SafeArray; + + /** Remove a given key from the dictionary. */ + Remove(Key: TKey): void; + + /** Remove all information from the dictionary. */ + RemoveAll(): void; + + /** Set or get the item for a given key */ + (Key: TKey): TItem; +} + +interface ActiveXObjectNameMap { + 'Scripting.Dictionary': Dictionary; +} + +const dict: Dictionary = new ActiveXObject('Scripting.Dictionary'); +dict.Add('one', 1); +dict.Add('two', 2); +dict.Add('three', 3); + +const keyEnumerator = new Enumerator(dict.Keys()); +keyEnumerator.moveFirst(); +while (!keyEnumerator.atEnd()) { + const item = dict(keyEnumerator.item()); + const power = Math.pow(item, 2); +} diff --git a/types/activex-interop/index.d.ts b/types/activex-interop/index.d.ts new file mode 100644 index 0000000000..caa74fff38 --- /dev/null +++ b/types/activex-interop/index.d.ts @@ -0,0 +1,111 @@ +// Type definitions for Javascript Automation interop 0.0 +// Project: https://msdn.microsoft.com/en-us/library/ff521046(v=vs.85).aspx +// Definitions by: Zev Spitz +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +// tslint:disable-next-line no-empty-interface +interface ActiveXObjectNameMap { } + +interface ActiveXObject { + new (progid: K): ActiveXObjectNameMap[K]; + new(s: string): any; +} +declare var ActiveXObject: ActiveXObject; + +/** + * Represents an Automation SAFEARRAY + */ +declare class SafeArray { + private constructor(); + private SafeArray_typekey: SafeArray; +} + +/** + * Allows enumerating over a COM collection, which may not have indexed item access. + */ +interface Enumerator { + /** + * Returns true if the current item is the last one in the collection, or the collection is empty, + * or the current item is undefined. + */ + atEnd(): boolean; + + /** + * Returns the current item in the collection + */ + item(): T; + + /** + * Resets the current item in the collection to the first item. If there are no items in the collection, + * the current item is set to undefined. + */ + moveFirst(): void; + + /** + * Moves the current item to the next item in the collection. If the enumerator is at the end of + * the collection or the collection is empty, the current item is set to undefined. + */ + moveNext(): void; +} + +interface EnumeratorConstructor { + new (collection: SafeArray | { Item(index: any): T }): Enumerator; + new (collection: any): Enumerator; +} + +declare var Enumerator: EnumeratorConstructor; + +/** + * Enables reading from a COM safe array, which might have an alternate lower bound, or multiple dimensions. + */ +interface VBArray { + /** + * Returns the number of dimensions (1-based). + */ + dimensions(): number; + + /** + * Takes an index for each dimension in the array, and returns the item at the corresponding location. + */ + getItem(dimension1Index: number, ...dimensionNIndexes: number[]): T; + + /** + * Returns the smallest available index for a given dimension. + * @param dimension 1-based dimension (defaults to 1) + */ + lbound(dimension?: number): number; + + /** + * Returns the largest available index for a given dimension. + * @param dimension 1-based dimension (defaults to 1) + */ + ubound(dimension?: number): number; + + /** + * Returns a Javascript array with all the elements in the VBArray. If there are multiple dimensions, + * each successive dimension is appended to the end of the array. + * Example: [[1,2,3],[4,5,6]] becomes [1,2,3,4,5,6] + */ + toArray(): T[]; +} + +interface VBArrayConstructor { + new (safeArray: SafeArray): VBArray; +} + +declare var VBArray: VBArrayConstructor; + +/** Automation date (VT_DATE) */ +declare class VarDate { + private constructor(); + private VarDate_typekey: VarDate; +} + +interface DateConstructor { + new(vd: VarDate): Date; +} + +interface Date { + getVarDate: () => VarDate; +} diff --git a/types/activex-interop/tsconfig.json b/types/activex-interop/tsconfig.json new file mode 100644 index 0000000000..49c7698384 --- /dev/null +++ b/types/activex-interop/tsconfig.json @@ -0,0 +1,21 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es5" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ "../"], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "activex-interop-tests.ts" + ] +} \ No newline at end of file diff --git a/types/activex-iwshruntimelibrary/tslint.json b/types/activex-interop/tslint.json similarity index 100% rename from types/activex-iwshruntimelibrary/tslint.json rename to types/activex-interop/tslint.json diff --git a/types/activex-iwshruntimelibrary/package.json b/types/activex-iwshruntimelibrary/package.json deleted file mode 100644 index d9b1031263..0000000000 --- a/types/activex-iwshruntimelibrary/package.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "private": true, - "dependencies": { - "activex-helpers": "*" - } -} \ No newline at end of file diff --git a/types/activex-iwshruntimelibrary/index.d.ts b/types/windows-script-host/index.d.ts similarity index 61% rename from types/activex-iwshruntimelibrary/index.d.ts rename to types/windows-script-host/index.d.ts index d5b052eaae..50f0c52216 100644 --- a/types/activex-iwshruntimelibrary/index.d.ts +++ b/types/windows-script-host/index.d.ts @@ -1,14 +1,15 @@ -// Type definitions for Windows Script Host Object Model - IWshRuntimeLibrary 1.0 -// Project: https://msdn.microsoft.com/en-us/library/9bbdkx3k(v=vs.84).aspx +// Type definitions for Windows Script Host 5.8 +// Project: https://msdn.microsoft.com/en-us/library/9bbdkx3k.aspx // Definitions by: Zev Spitz // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.6 +// TypeScript Version: 2.3 + +/// declare namespace IWshRuntimeLibrary { type WindowStyle = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10; type ShortcutWindowStyle = 1 | 3 | 7; - // tslint:disable-next-line no-const-enum const enum ButtonType { OK, OKCancel, @@ -19,7 +20,6 @@ declare namespace IWshRuntimeLibrary { CancelTryagainContinue } - // tslint:disable-next-line no-const-enum const enum EventType { AuditFailure = 5, AuditSuccess = 4, @@ -29,7 +29,6 @@ declare namespace IWshRuntimeLibrary { Warning = 2 } - // tslint:disable-next-line no-const-enum const enum IconType { Stop = 16, QuestionMark = 32, @@ -37,7 +36,6 @@ declare namespace IWshRuntimeLibrary { InformationMark = 64, } - // tslint:disable-next-line no-const-enum const enum PopupType { SecondButtonDefault = 256, ThirdButtonDefault = 512, @@ -46,7 +44,6 @@ declare namespace IWshRuntimeLibrary { RTL = 1048576, } - // tslint:disable-next-line no-const-enum const enum PopupSelection { NoButton = -1, OK = 1, @@ -60,14 +57,12 @@ declare namespace IWshRuntimeLibrary { Continue = 11, } - // tslint:disable-next-line no-const-enum const enum WshExecStatus { WshFailed = 2, WshFinished = 1, WshRunning = 0, } - // tslint:disable-next-line no-const-enum const enum WshWindowStyle { WshHide = 0, WshMaximizedFocus = 3, @@ -77,24 +72,108 @@ declare namespace IWshRuntimeLibrary { WshNormalNoFocus = 4, } - class TextStream { - private 'IWshRuntimeLibrary.TextStream_typekey': TextStream; - private constructor(); - readonly AtEndOfLine: boolean; - readonly AtEndOfStream: boolean; - Close(): void; - readonly Column: number; - readonly Line: number; - Read(Characters: number): string; - ReadAll(): string; - ReadLine(): string; - Skip(Characters: number): void; - SkipLine(): void; - Write(Text: string): void; - WriteBlankLines(Lines: number): void; + class TextStreamBase { + /** + * The column number of the current character position in an input stream. + */ + Column: number; - /** @param string [Text=''] */ - WriteLine(Text?: string): void; + /** + * The current line number in an input stream. + */ + Line: number; + + /** + * Closes a text stream. + * It is not necessary to close standard streams; they close automatically when the process ends. If + * you close a standard stream, be aware that any other pointers to that standard stream become invalid. + */ + Close(): void; + } + + class TextStreamWriter extends TextStreamBase { + private 'IWshRuntimeLibrary.TextStreamWriter_typekey': TextStreamWriter; + private constructor(); + + /** + * Sends a string to an output stream. + */ + Write(s: string): void; + + /** + * Sends a specified number of blank lines (newline characters) to an output stream. + */ + WriteBlankLines(intLines: number): void; + + /** + * Sends a string followed by a newline character to an output stream. + */ + WriteLine(s: string): void; + } + + class TextStreamReader extends TextStreamBase { + private 'IWshRuntimeLibrary.TextStreamReader_typekey': TextStreamReader; + private constructor(); + + /** + * Returns a specified number of characters from an input stream, starting at the current pointer position. + * Does not return until the ENTER key is pressed. + * Can only be used on a stream in reading mode; causes an error in writing or appending mode. + */ + Read(characters: number): string; + + /** + * Returns all characters from an input stream. + * Can only be used on a stream in reading mode; causes an error in writing or appending mode. + */ + ReadAll(): string; + + /** + * Returns an entire line from an input stream. + * Although this method extracts the newline character, it does not add it to the returned string. + * Can only be used on a stream in reading mode; causes an error in writing or appending mode. + */ + ReadLine(): string; + + /** + * Skips a specified number of characters when reading from an input text stream. + * Can only be used on a stream in reading mode; causes an error in writing or appending mode. + * @param characters Positive number of characters to skip forward. (Backward skipping is not supported.) + */ + Skip(characters: number): void; + + /** + * Skips the next line when reading from an input text stream. + * Can only be used on a stream in reading mode, not writing or appending mode. + */ + SkipLine(): void; + + /** + * Indicates whether the stream pointer position is at the end of a line. + */ + AtEndOfLine: boolean; + + /** + * Indicates whether the stream pointer position is at the end of a stream. + */ + AtEndOfStream: boolean; + } + + /** Provides access to the entire collection of command-line parameters, in the order in which they were originally entered. */ + interface WshArguments { + Count(): number; + Item(index: number): string; + Length: number; + Named: WshNamed; + + /** + * When you run the **ShowUsage** method, a help screen (referred to as the usage) appears and displays details about the script's command line options. + * This information comes from the runtime section of the `*.WSF` file. Everything written between the `` and `` tags is pieced together + * to produce what is called a "usage statement." The usage statement tells the user how to use the script. + */ + ShowUsage(): void; + Unnamed: WshUnnamed; + (index: number): string; } /** Generic Collection Object */ @@ -109,7 +188,7 @@ declare namespace IWshRuntimeLibrary { interface WshEnvironment { Count(): number; Item(Name: string): string; - readonly length: number; + readonly Length: number; Remove(Name: string): void; (Name: string): string; } @@ -121,12 +200,25 @@ declare namespace IWshRuntimeLibrary { readonly ExitCode: number; readonly ProcessID: number; readonly Status: WshExecStatus; - readonly StdErr: TextStream; - readonly StdIn: TextStream; - readonly StdOut: TextStream; + readonly StdErr: TextStreamWriter; + readonly StdIn: TextStreamReader; + readonly StdOut: TextStreamWriter; Terminate(): void; } + /** + * Provides access to the named command-line arguments + * + * Note that enumerating over this object returns the **names** of the arguments, not the values + */ + interface WshNamed { + Count(): number; + Exists(Key: string): boolean; + Item(name: string): string; + Length: number; + (name: string): string; + } + /** Network Object */ class WshNetwork { private 'IWshRuntimeLibrary.WshNetwork_typekey': WshNetwork; @@ -326,6 +418,14 @@ declare namespace IWshRuntimeLibrary { WorkingDirectory: string; } + /** Provides access to the unnamed command-line arguments */ + interface WshUnnamed { + Count(): number; + Item(index: number): string; + Length: number; + (index: number): string; + } + /** URLShortcut Object */ class WshURLShortcut { private 'IWshRuntimeLibrary.WshURLShortcut_typekey': WshURLShortcut; @@ -337,12 +437,150 @@ declare namespace IWshRuntimeLibrary { } } -interface ActiveXObject { - set(obj: IWshRuntimeLibrary.WshEnvironment, propertyName: 'Item', parameterTypes: [string], newValue: string): void; - new (progid: K): ActiveXObjectNameMap[K]; +declare var WScript: { + /** + * Outputs text to either a message box (under WScript.exe) or the command console window followed by + * a newline (under CScript.exe). + */ + Echo(s?: any): void; + + /** + * Exposes the write-only error output stream for the current script. + * Can be accessed only while using CScript.exe. + */ + StdErr: IWshRuntimeLibrary.TextStreamWriter; + + /** + * Exposes the write-only output stream for the current script. + * Can be accessed only while using CScript.exe. + */ + StdOut: IWshRuntimeLibrary.TextStreamWriter; + Arguments: IWshRuntimeLibrary.WshArguments; + + /** + * The full path of the currently running script. + */ + ScriptFullName: string; + + /** + * Forces the script to stop immediately, with an optional exit code. + */ + Quit(exitCode?: number): number; + + /** + * The Windows Script Host build version number. + */ + BuildVersion: number; + + /** + * Fully qualified path of the host executable. + */ + FullName: string; + + /** + * Gets/sets the script mode - interactive(true) or batch(false). + */ + Interactive: boolean; + + /** + * The name of the host executable (WScript.exe or CScript.exe). + */ + Name: string; + + /** + * Path of the directory containing the host executable. + */ + Path: string; + + /** + * The filename of the currently running script. + */ + ScriptName: string; + + /** + * Exposes the read-only input stream for the current script. + * Can be accessed only while using CScript.exe. + */ + StdIn: IWshRuntimeLibrary.TextStreamReader; + + /** + * Windows Script Host version + */ + Version: string; + + /** + * Connects a COM object's event sources to functions named with a given prefix, in the form prefix_event. + */ + ConnectObject(objEventSource: any, strPrefix: string): void; + + /** + * Creates a COM object. + * @param strProgiID + * @param strPrefix Function names in the form prefix_event will be bound to this object's COM events. + */ + CreateObject(strProgID: K, strPrefix?: string): ActiveXObjectNameMap[K]; + + /** + * Disconnects a COM object from its event sources. + */ + DisconnectObject(obj: any): void; + + /** + * Retrieves an existing object with the specified ProgID from memory, or creates a new one from a file. + * @param strPathname Fully qualified path to the file containing the object persisted to disk. + * For objects in memory, pass a zero-length string. + * @param strProgID + * @param strPrefix Function names in the form prefix_event will be bound to this object's COM events. + */ + GetObject(strPathname: string, strProgID: K, strPrefix?: string): ActiveXObjectNameMap[K]; + GetObject(strPathname: string, strProgID?: string, strPrefix?: string): any; + + /** + * Suspends script execution for a specified length of time, then continues execution. + * @param intTime Interval (in milliseconds) to suspend script execution. + */ + Sleep(intTime: number): void; +}; + +/** + * WSH is an alias for WScript under Windows Script Host + */ +declare var WSH: typeof WScript; + +declare namespace WSHControllerLibrary { + class WSHController { + private 'WSHControllerLibrary.WSHController_typekey': WSHController; + private constructor(); + CreateScript(Command: string, Server?: any): any; + } +} + +declare namespace ScriptSigner { + class Signer { + private 'ScriptSigner.Signer_typekey': Signer; + private constructor(); + + /** @param Store [Store='my'] */ + Sign(FileExtension: string, Text: string, Certificate: string, Store?: string): string; + + /** @param Store [Store='my'] */ + SignFile(FileName: string, Certificate: string, Store?: string): void; + + /** @param ShowUI [ShowUI=false] */ + Verify(FileExtension: string, Text: string, ShowUI?: boolean): boolean; + + /** @param ShowUI [ShowUI=false] */ + VerifyFile(FileName: string, ShowUI?: boolean): boolean; + } } interface ActiveXObjectNameMap { + 'WSHController': WSHControllerLibrary.WSHController; + 'Scripting.Signer': ScriptSigner.Signer; 'WScript.Network': IWshRuntimeLibrary.WshNetwork; 'WScript.Shell': IWshRuntimeLibrary.WshShell; } + +interface ActiveXObject { + set(obj: IWshRuntimeLibrary.WshEnvironment, propertyName: 'Item', parameterTypes: [string], newValue: string): void; +} diff --git a/types/activex-iwshruntimelibrary/tsconfig.json b/types/windows-script-host/tsconfig.json similarity index 82% rename from types/activex-iwshruntimelibrary/tsconfig.json rename to types/windows-script-host/tsconfig.json index 1fa5abc39b..d53dad8a20 100644 --- a/types/activex-iwshruntimelibrary/tsconfig.json +++ b/types/windows-script-host/tsconfig.json @@ -2,8 +2,7 @@ "compilerOptions": { "module": "commonjs", "lib": [ - "es5", - "scripthost" + "es5" ], "noImplicitAny": true, "noImplicitThis": true, @@ -19,6 +18,6 @@ }, "files": [ "index.d.ts", - "activex-iwshruntimelibrary-tests.ts" + "windows-script-host-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/windows-script-host/tslint.json b/types/windows-script-host/tslint.json new file mode 100644 index 0000000000..ed91c31f9c --- /dev/null +++ b/types/windows-script-host/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "no-const-enum": false + } +} \ No newline at end of file diff --git a/types/activex-iwshruntimelibrary/activex-iwshruntimelibrary-tests.ts b/types/windows-script-host/windows-script-host-tests.ts similarity index 67% rename from types/activex-iwshruntimelibrary/activex-iwshruntimelibrary-tests.ts rename to types/windows-script-host/windows-script-host-tests.ts index 14a90f9a8a..c6b3449482 100644 --- a/types/activex-iwshruntimelibrary/activex-iwshruntimelibrary-tests.ts +++ b/types/windows-script-host/windows-script-host-tests.ts @@ -1,3 +1,34 @@ +const collectionToArray = (col: { Item(key: any): T }): T[] => { + const results: T[] = []; + const enumerator = new Enumerator(col); + enumerator.moveFirst(); + while (!enumerator.atEnd()) { + results.push(enumerator.item()); + enumerator.moveNext(); + } + return results; +}; + +// Show all of the arguments. +WScript.Echo(`${WScript.Arguments.Length} arguments`); + +for (const arg of collectionToArray(WScript.Arguments)) { + WScript.Echo(` ${arg}`); +} + +// Show the unnamed arguments. +WScript.Echo(`${WScript.Arguments.Unnamed.Length} unnamed arguments`); + +for (const unnamed of collectionToArray(WScript.Arguments.Unnamed)) { + WScript.Echo(` ${unnamed}`); +} + +// Show the named arguments. +WScript.Echo(`${WScript.Arguments.Named.Length} named arguments`); +for (const key of collectionToArray(WScript.Arguments.Named)) { + WScript.Echo(` ${key}=${WScript.Arguments.Named(key)}`); +} + const wshn = new ActiveXObject('WScript.Network'); // https://msdn.microsoft.com/en-us/library/s6wt333f(v=vs.84).aspx