Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonQDixon committed Jul 15, 2021
1 parent 7bcd790 commit 2379596
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
2 changes: 1 addition & 1 deletion coverage/lcov-report/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ <h1>All files</h1>
<div class='footer quiet pad2 space-top1 center small'>
Code coverage generated by
<a href="https://istanbul.js.org/" target="_blank">istanbul</a>
at Wed Jul 14 2021 10:43:40 GMT-0400 (Eastern Daylight Time)
at Thu Jul 15 2021 09:54:22 GMT-0400 (Eastern Daylight Time)
</div>
</div>
<script src="prettify.js"></script>
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@byte-this/js-cli",
"version": "1.0.21",
"version": "1.0.22",
"description": "Functionality to streamline the process of scripting for the console with javascript",
"main": "public-api.js",
"types": "public-api.d.ts",
Expand Down Expand Up @@ -38,4 +38,4 @@
"ts-node": "^9.1.1",
"typescript": "^4.2.4"
}
}
}
73 changes: 46 additions & 27 deletions src/input-requestor/base-input-requestor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { iCliOutputter } from "../models/cli-outputter";
import { iCliUserInputRequestor } from "../models/cli-user-input-requestor";

export abstract class BaseUserInputRequestor implements iCliUserInputRequestor {
constructor(protected cliOutputter: iCliOutputter) {}
constructor(protected cliOutputter: iCliOutputter) { }

async awaitInput(param: iCliCommandParam): Promise<any> {
if (param.doRepeat) {
Expand Down Expand Up @@ -57,35 +57,54 @@ export abstract class BaseUserInputRequestor implements iCliUserInputRequestor {
numAttempts: number
): any {
let parsed;
switch (param.type || "string") {
case "number":
parsed = this.parseNumber(input);
break;
case "boolean":
parsed = this.parseBoolean(input);
break;
default:
parsed = input.trim();
break;
}
if (param.isValid) {
const validation = param.isValid(parsed, numAttempts);
if (validation.isValid) {
return parsed;

const isValidDecorator = (err: any, callback?: (parsed: any, numAttempts: number) => { isValid: boolean; message?: string; tryAgain?: boolean; }): ((parsed: any, numAttempts: number) => { isValid: boolean; message?: string; tryAgain?: boolean; }) => {
if (err) {
return (parsed: any, numAttempts: number) => ({
isValid: false,
message: 'Error processing user input: ' + err.toString(),
tryAgain: true
});
} else if (callback) {
return callback;
} else {
const message =
validation.message || `Input is invalid, please try again.`;
this.cliOutputter.pushWarning(message);
if (
typeof validation.tryAgain === "undefined" ||
!validation.tryAgain
) {
throw new Error(`Input failed`);
}
return this.awaitSingleInput(param, numAttempts + 1);
return (parsed: any, numAttempts: number) => ({
isValid: true
});
}
} else {
}

let error = null;
try {
switch (param.type || "string") {
case "number":
parsed = this.parseNumber(input);
break;
case "boolean":
parsed = this.parseBoolean(input);
break;
default:
parsed = input.trim();
break;
}
} catch (err) {
error = err;
}
const decorated = isValidDecorator(error, param.isValid);
const validation = decorated(parsed, numAttempts);
if (validation.isValid) {
return parsed;
} else {
const message =
validation.message || `Input is invalid, please try again.`;
this.cliOutputter.pushWarning(message);
if (
typeof validation.tryAgain === "undefined" ||
!validation.tryAgain
) {
throw new Error(`Input failed`);
}
return this.awaitSingleInput(param, numAttempts + 1);
}
}

Expand Down

0 comments on commit 2379596

Please sign in to comment.