From 1776ecb31a881ac95d0a3c2766c3d81c276f6117 Mon Sep 17 00:00:00 2001 From: Tristan Jones Date: Mon, 2 Jan 2017 19:14:19 -0700 Subject: [PATCH 1/3] Added declarations for 'readline-sync'. --- readline-sync/index.d.ts | 67 ++++++++++++++++ readline-sync/readline-sync-tests.ts | 110 +++++++++++++++++++++++++++ readline-sync/tsconfig.json | 20 +++++ readline-sync/tslint.json | 1 + 4 files changed, 198 insertions(+) create mode 100644 readline-sync/index.d.ts create mode 100644 readline-sync/readline-sync-tests.ts create mode 100644 readline-sync/tsconfig.json create mode 100644 readline-sync/tslint.json diff --git a/readline-sync/index.d.ts b/readline-sync/index.d.ts new file mode 100644 index 0000000000..ce7d15d6a4 --- /dev/null +++ b/readline-sync/index.d.ts @@ -0,0 +1,67 @@ +// Type definitions for readline-sync 1.4 +// Project: https://github.com/anseki/readline-sync +// Definitions by: Tristan Jones +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +type optionType = string | number | RegExp | ((input:string) => boolean); + +interface BasicOptions { + prompt?: any; + hideEchoBack?: boolean; + mask?: string; + limit?: optionType | Array; + limitMessage?: string; + defaultInput?: string; + trueValue?: optionType | Array; + falseValue?: optionType | Array; + caseSensitive?: boolean; + keepWhitespace?: boolean; + encoding?: string; + bufferSize?: number; + print?: ((display:string, encoding:string) => void); + history?: boolean; + cd?: boolean; + charlist?: string; + min?: any; + max?: any; + confirmMessage?: any; + unmatchMessage?: any; + exists?: any; + isFile?: boolean; + isDirectory?: boolean; + validate?: ((path:string) => boolean | string); + create?: boolean; + guide?: boolean; +} + +// Basic Functions +export function question(query?: any, options?: BasicOptions): string; +export function prompt(options?: BasicOptions): string; +export function keyIn(query?: any, options?: BasicOptions): string; +export function setDefaultOptions(options?: BasicOptions): BasicOptions; + +// Utility Functions +export function questionEMail(query?: any, options?: BasicOptions): string; +export function questionNewPassword(query?: any, options?: BasicOptions): string; +export function questionInt(query?: any, options?: BasicOptions): number; +export function questionFloat(query?: any, options?: BasicOptions): number; +export function questionPath(query?: any, options?: BasicOptions): string; + +export function promptCL(commandHandler?: { [id:string]: ((...args:string[]) => void) } | ((command:string, ...args:string[]) => void), options?: BasicOptions): string[]; +export function promptLoop(inputHandler: ((value:string) => boolean), options?: BasicOptions): void; +export function promptCLLoop(commandHandler?: { [id:string]: ((...args:string[]) => boolean | void) } | ((command:string, ...args:string[]) => boolean | void), options?: BasicOptions): void; +export function promptSimShell(options?: BasicOptions): string; + +export function keyInYN(query?: any, options?: BasicOptions): boolean | string; +export function keyInYNStrict(query?: any, options?: BasicOptions): boolean; +export function keyInPause(query?: any, options?: BasicOptions): void; +export function keyInSelect(items: string[], query?: any, options?: BasicOptions): number; + +export function getRawInput(): string; + +// Deprecated +export function setBufferSize(value: number): void; +export function setEncoding(value: string): void; +export function setMask(value: string): void; +export function setPrint(value: ((display:string, encoding:string) => void)): void; +export function setPrompt(value: any): void; \ No newline at end of file diff --git a/readline-sync/readline-sync-tests.ts b/readline-sync/readline-sync-tests.ts new file mode 100644 index 0000000000..a201659e7a --- /dev/null +++ b/readline-sync/readline-sync-tests.ts @@ -0,0 +1,110 @@ + + +import readlineSync = require('readline-sync'); + +let result:string = readlineSync.question('Which program starts do you want? ', { + defaultInput: 'firefox' +}); + +let result2:string = readlineSync.prompt({prompt: '$$'}); + +let result3:string = readlineSync.keyIn('Press a key', { limit: '$<1-5>' }); + +let result4:{} = readlineSync.setDefaultOptions({ + prompt: '$$', + hideEchoBack: true, + mask: '*', + limit: [ /^a|b$/, /[a-zA-Z]+/], + limitMessage: 'Limit reached', + defaultInput: 'English', + trueValue: 2, + falseValue: (value:string) => { return true; }, + caseSensitive: true, + keepWhitespace: true, + encoding: 'utf-8', + bufferSize: 12, + print: (display:string, encoding:string) => { console.log(display)}, + history: false, + cd: true, + charlist: 'abc', + min: 0, + max: 10, + confirmMessage: 'Yes', + unmatchMessage: new Date(), + exists: false, + isFile: true, + isDirectory: false, + validate: (path:string) => { return (path === '/usr/local/bin'); }, + create: true, + guide: false, +}); + +let result5:string = readlineSync.questionEMail('Enter email'); + +let result6:string = readlineSync.questionNewPassword('PASSWORD: ', {charlist: '$#$@%'}); + +let result7:number = readlineSync.questionInt('Enter an integer', { limitMessage: 'Enter a valid integer' }); + +let result8:number = readlineSync.questionFloat('Enter a float', { limitMessage: 'Enter a valid float' }); + +let result9:string = readlineSync.questionPath('Save to: ', { + isDirectory: true, + exists: null, + create: true +}); + +readlineSync.promptCL((command:string, arg1:string, arg2:string) => { + if (command === 'add') { + console.log(arg1 + ' is added.'); + } else if (command === 'copy') { + console.log(arg1 + ' is copied to ' + arg2 + '.'); + } +}); + +readlineSync.promptCL({ + add: (element:string) => { // It's called by also "ADD", "Add", "aDd", etc.. + console.log(element + ' is added.'); + }, + copy: (from:string, to:string) => { + console.log(from + ' is copied to ' + to + '.'); + } +}); + +readlineSync.promptLoop((input:string) => { + console.log('-- You said "' + input + '"'); + return input === 'bye'; +}); + +readlineSync.promptCLLoop({ + add: (element:string) => { + console.log(element + ' is added.'); + }, + copy: (from:string, to:string) => { + console.log(from + ' is copied to ' + to + '.'); + }, + bye: () => { return true; } +}); + +let result10:string = readlineSync.promptSimShell(); + +let result11:(boolean | string) = readlineSync.keyInYN('Do you want to install this?'); + +let result12:boolean = readlineSync.keyInYNStrict('Do you want to install this?', { guide: true }); + +readlineSync.keyInPause({ guide:true }); + +let frameworks = ['Express', 'hapi', 'flatiron', 'MEAN.JS', 'locomotive']; +let result13:number = readlineSync.keyInSelect(frameworks, 'Which framework?'); + +let result14:string = readlineSync.getRawInput(); + +readlineSync.setPrint((display:string, encoding:string) => { console.log(display) }); + +readlineSync.setPrompt(new Date()); + +readlineSync.setEncoding('utf-16'); + +readlineSync.setMask('%'); + +readlineSync.setBufferSize(128); + diff --git a/readline-sync/tsconfig.json b/readline-sync/tsconfig.json new file mode 100644 index 0000000000..144427ec3c --- /dev/null +++ b/readline-sync/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "readline-sync-tests.ts" + ] +} diff --git a/readline-sync/tslint.json b/readline-sync/tslint.json new file mode 100644 index 0000000000..377cc837d4 --- /dev/null +++ b/readline-sync/tslint.json @@ -0,0 +1 @@ +{ "extends": "../tslint.json" } From 1982f9cfaae81a6b2fb6bc6654e42d8a78e13556 Mon Sep 17 00:00:00 2001 From: Tristan Jones Date: Mon, 2 Jan 2017 19:22:17 -0700 Subject: [PATCH 2/3] Added fixes to pass linting --- readline-sync/index.d.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/readline-sync/index.d.ts b/readline-sync/index.d.ts index ce7d15d6a4..0307af5eda 100644 --- a/readline-sync/index.d.ts +++ b/readline-sync/index.d.ts @@ -3,22 +3,22 @@ // Definitions by: Tristan Jones // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -type optionType = string | number | RegExp | ((input:string) => boolean); +type optionType = string | number | RegExp | ((input: string) => boolean); interface BasicOptions { prompt?: any; hideEchoBack?: boolean; mask?: string; - limit?: optionType | Array; + limit?: optionType | optionType[]; limitMessage?: string; defaultInput?: string; - trueValue?: optionType | Array; - falseValue?: optionType | Array; + trueValue?: optionType | optionType[]; + falseValue?: optionType | optionType[]; caseSensitive?: boolean; keepWhitespace?: boolean; encoding?: string; bufferSize?: number; - print?: ((display:string, encoding:string) => void); + print?: ((display: string, encoding: string) => void); history?: boolean; cd?: boolean; charlist?: string; @@ -29,7 +29,7 @@ interface BasicOptions { exists?: any; isFile?: boolean; isDirectory?: boolean; - validate?: ((path:string) => boolean | string); + validate?: ((path: string) => boolean | string); create?: boolean; guide?: boolean; } @@ -47,9 +47,9 @@ export function questionInt(query?: any, options?: BasicOptions): number; export function questionFloat(query?: any, options?: BasicOptions): number; export function questionPath(query?: any, options?: BasicOptions): string; -export function promptCL(commandHandler?: { [id:string]: ((...args:string[]) => void) } | ((command:string, ...args:string[]) => void), options?: BasicOptions): string[]; -export function promptLoop(inputHandler: ((value:string) => boolean), options?: BasicOptions): void; -export function promptCLLoop(commandHandler?: { [id:string]: ((...args:string[]) => boolean | void) } | ((command:string, ...args:string[]) => boolean | void), options?: BasicOptions): void; +export function promptCL(commandHandler?: { [id: string]: ((...args: string[]) => void) } | ((command: string, ...args: string[]) => void), options?: BasicOptions): string[]; +export function promptLoop(inputHandler: ((value: string) => boolean), options?: BasicOptions): void; +export function promptCLLoop(commandHandler?: { [id: string]: ((...args: string[]) => boolean | void) } | ((command: string, ...args: string[]) => boolean | void), options?: BasicOptions): void; export function promptSimShell(options?: BasicOptions): string; export function keyInYN(query?: any, options?: BasicOptions): boolean | string; @@ -63,5 +63,5 @@ export function getRawInput(): string; export function setBufferSize(value: number): void; export function setEncoding(value: string): void; export function setMask(value: string): void; -export function setPrint(value: ((display:string, encoding:string) => void)): void; +export function setPrint(value: ((display: string, encoding: string) => void)): void; export function setPrompt(value: any): void; \ No newline at end of file From daf4d787ea7347674e177665fe7f10fa499e8591 Mon Sep 17 00:00:00 2001 From: Tristan Jones Date: Mon, 2 Jan 2017 22:01:28 -0700 Subject: [PATCH 3/3] Fixed capitalization of OptionTypes and extra parens around functions --- readline-sync/index.d.ts | 40 +++++++++++++++++++++------- readline-sync/readline-sync-tests.ts | 1 - 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/readline-sync/index.d.ts b/readline-sync/index.d.ts index 0307af5eda..78d7e313f9 100644 --- a/readline-sync/index.d.ts +++ b/readline-sync/index.d.ts @@ -3,22 +3,22 @@ // Definitions by: Tristan Jones // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -type optionType = string | number | RegExp | ((input: string) => boolean); +type OptionType = string | number | RegExp | ((input: string) => boolean); interface BasicOptions { prompt?: any; hideEchoBack?: boolean; mask?: string; - limit?: optionType | optionType[]; + limit?: OptionType | OptionType[]; limitMessage?: string; defaultInput?: string; - trueValue?: optionType | optionType[]; - falseValue?: optionType | optionType[]; + trueValue?: OptionType | OptionType[]; + falseValue?: OptionType | OptionType[]; caseSensitive?: boolean; keepWhitespace?: boolean; encoding?: string; bufferSize?: number; - print?: ((display: string, encoding: string) => void); + print?: (display: string, encoding: string) => void; history?: boolean; cd?: boolean; charlist?: string; @@ -29,7 +29,7 @@ interface BasicOptions { exists?: any; isFile?: boolean; isDirectory?: boolean; - validate?: ((path: string) => boolean | string); + validate?: (path: string) => boolean | string; create?: boolean; guide?: boolean; } @@ -47,9 +47,9 @@ export function questionInt(query?: any, options?: BasicOptions): number; export function questionFloat(query?: any, options?: BasicOptions): number; export function questionPath(query?: any, options?: BasicOptions): string; -export function promptCL(commandHandler?: { [id: string]: ((...args: string[]) => void) } | ((command: string, ...args: string[]) => void), options?: BasicOptions): string[]; -export function promptLoop(inputHandler: ((value: string) => boolean), options?: BasicOptions): void; -export function promptCLLoop(commandHandler?: { [id: string]: ((...args: string[]) => boolean | void) } | ((command: string, ...args: string[]) => boolean | void), options?: BasicOptions): void; +export function promptCL(commandHandler?: { [id: string]: (...args: string[]) => void } | ((command: string, ...args: string[]) => void), options?: BasicOptions): string[]; +export function promptLoop(inputHandler: (value: string) => boolean, options?: BasicOptions): void; +export function promptCLLoop(commandHandler?: { [id: string]: (...args: string[]) => boolean | void } | ((command: string, ...args: string[]) => boolean | void), options?: BasicOptions): void; export function promptSimShell(options?: BasicOptions): string; export function keyInYN(query?: any, options?: BasicOptions): boolean | string; @@ -60,8 +60,28 @@ export function keyInSelect(items: string[], query?: any, options?: BasicOptions export function getRawInput(): string; // Deprecated + +/** + * @deprecated Use the bufferSize option instead: readlineSync.setDefaultOptions({bufferSize: value}); + */ export function setBufferSize(value: number): void; + +/** + * @deprecated Use the encoding option instead: readlineSync.setDefaultOptions({encoding: value}); + */ export function setEncoding(value: string): void; + +/** + * @deprecated Use the mask option instead: readlineSync.setDefaultOptions({mask: value}); + */ export function setMask(value: string): void; -export function setPrint(value: ((display: string, encoding: string) => void)): void; + +/** + * @deprecated Use the print option instead: readlineSync.setDefaultOptions({print: value}); + */ +export function setPrint(value: (display: string, encoding: string) => void): void; + +/** + * @deprecated Use the prompt option instead: readlineSync.setDefaultOptions({prompt: value}); + */ export function setPrompt(value: any): void; \ No newline at end of file diff --git a/readline-sync/readline-sync-tests.ts b/readline-sync/readline-sync-tests.ts index a201659e7a..02c8d8e952 100644 --- a/readline-sync/readline-sync-tests.ts +++ b/readline-sync/readline-sync-tests.ts @@ -107,4 +107,3 @@ readlineSync.setEncoding('utf-16'); readlineSync.setMask('%'); readlineSync.setBufferSize(128); -