diff --git a/readline-sync/index.d.ts b/readline-sync/index.d.ts new file mode 100644 index 0000000000..78d7e313f9 --- /dev/null +++ b/readline-sync/index.d.ts @@ -0,0 +1,87 @@ +// 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 | OptionType[]; + limitMessage?: string; + defaultInput?: string; + trueValue?: OptionType | OptionType[]; + falseValue?: OptionType | OptionType[]; + 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 + +/** + * @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; + +/** + * @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 new file mode 100644 index 0000000000..02c8d8e952 --- /dev/null +++ b/readline-sync/readline-sync-tests.ts @@ -0,0 +1,109 @@ + + +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" }