consultant
Advanced tools
Comparing version 0.1.20 to 0.1.23
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var InputFactory_1 = require("./InputFactory"); | ||
@@ -10,4 +11,3 @@ var Consultant = (function () { | ||
exports.Consultant = Consultant; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = Consultant; | ||
//# sourceMappingURL=Consultant.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Consultant_1 = require("./Consultant"); | ||
var consultant = new Consultant_1.Consultant(); | ||
var rules = { | ||
makefile: { | ||
type: String, | ||
aliases: ['f'], | ||
label: 'Makefile', | ||
description: 'Load tasks from FILE', | ||
parameter: 'FILE', | ||
'default': ['makefile.js'], | ||
uiHidden: false, | ||
min: 0, | ||
max: undefined, | ||
validate: function (value) { | ||
return value.length >= 3 || 'minimum 3 chars required'; | ||
} | ||
} | ||
}; | ||
var x = consultant.input.fromCommandLine(rules); | ||
console.log(x.validate()); | ||
module.exports = consultant; | ||
//# sourceMappingURL=index.js.map |
@@ -1,2 +0,3 @@ | ||
import { Rule, RuleCollection } from './Rule'; | ||
import { RuleCollection } from './Rule'; | ||
import { ValidationResult } from './Validator'; | ||
export declare class Input { | ||
@@ -6,16 +7,5 @@ rules: RuleCollection; | ||
constructor(rules: RuleCollection, argv: Object); | ||
checkType(rule: Rule, item: any): void; | ||
checkValues(rule: Rule, item: any): void; | ||
checkOccurences(rule: Rule, item: any): void; | ||
checkCustomValidation(rule: Rule, item: any): void; | ||
findValues(rule: Rule, argv: any, key: any): never[]; | ||
validate(): { | ||
values: {}; | ||
validationErrors: {}; | ||
}; | ||
consume(): { | ||
consumed: null; | ||
result: null; | ||
}; | ||
validate(): ValidationResult; | ||
consume(): any; | ||
} | ||
export default Input; |
100
lib/Input.js
"use strict"; | ||
var pathNotation_1 = require("./utils/pathNotation"); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var Validator_1 = require("./Validator"); | ||
var Input = (function () { | ||
@@ -8,97 +9,5 @@ function Input(rules, argv) { | ||
} | ||
Input.prototype.checkType = function (rule, item) { | ||
for (var _i = 0, _a = item.value; _i < _a.length; _i++) { | ||
var value = _a[_i]; | ||
if (value.constructor !== rule.type) { | ||
item.validationErrors.push({ error: "invalid type - " + value.constructor }); | ||
return; | ||
} | ||
} | ||
}; | ||
Input.prototype.checkValues = function (rule, item) { | ||
if (rule.values !== undefined) { | ||
for (var _i = 0, _a = item.value; _i < _a.length; _i++) { | ||
var value = _a[_i]; | ||
if (rule.values.indexOf(value) === -1) { | ||
item.validationErrors.push({ error: "invalid value - " + value }); | ||
return; | ||
} | ||
} | ||
} | ||
}; | ||
Input.prototype.checkOccurences = function (rule, item) { | ||
var length = item.value.length; | ||
if ((rule.min !== undefined && length < rule.min) || | ||
(rule.max !== undefined && length > rule.max)) { | ||
item.validationErrors.push({ error: "out of range - " + length }); | ||
} | ||
}; | ||
Input.prototype.checkCustomValidation = function (rule, item) { | ||
if (rule.validate === undefined) { | ||
return; | ||
} | ||
item.value.forEach(function (currentValue) { | ||
if (rule.validate(currentValue) !== true) { | ||
item.validationErrors.push({ error: "validation failed - " + currentValue }); | ||
} | ||
}); | ||
}; | ||
Input.prototype.findValues = function (rule, argv, key) { | ||
var foundValues = []; | ||
var value = pathNotation_1.pathNotation(argv, key); | ||
if (value !== undefined) { | ||
if (Array.isArray(value)) { | ||
foundValues = foundValues.concat(value); | ||
} | ||
else { | ||
foundValues.push(value); | ||
} | ||
} | ||
if (rule.aliases !== undefined) { | ||
for (var _i = 0, _a = rule.aliases; _i < _a.length; _i++) { | ||
var alias = _a[_i]; | ||
var value2 = pathNotation_1.pathNotation(argv, alias); | ||
if (value2 === undefined) { | ||
continue; | ||
} | ||
if (Array.isArray(value2)) { | ||
foundValues = foundValues.concat(value2); | ||
} | ||
else { | ||
foundValues.push(value2); | ||
} | ||
} | ||
} | ||
return foundValues; | ||
}; | ||
Input.prototype.validate = function () { | ||
var result = { | ||
values: {}, | ||
validationErrors: {} | ||
}; | ||
for (var _i = 0, _a = Object.keys(this.rules); _i < _a.length; _i++) { | ||
var ruleKey = _a[_i]; | ||
var rule = this.rules[ruleKey]; | ||
var item = { | ||
validationErrors: [], | ||
value: this.findValues(rule, this.argv, ruleKey) | ||
}; | ||
this.checkType(rule, item); | ||
this.checkValues(rule, item); | ||
this.checkOccurences(rule, item); | ||
this.checkCustomValidation(rule, item); | ||
if (item.value.length === 0) { | ||
item.value = rule.default; | ||
} | ||
else if (rule.max <= 1) { | ||
item.value = item.value[0]; | ||
} | ||
if (item.value !== undefined) { | ||
result.values[ruleKey] = item.value; | ||
} | ||
if (item.validationErrors.length > 0) { | ||
result.validationErrors[ruleKey] = item.validationErrors; | ||
} | ||
} | ||
return result; | ||
var validator = new Validator_1.Validator(); | ||
return validator.validate(this.rules, this.argv); | ||
}; | ||
@@ -114,4 +23,3 @@ Input.prototype.consume = function () { | ||
exports.Input = Input; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = Input; | ||
//# sourceMappingURL=Input.js.map |
@@ -1,2 +0,1 @@ | ||
/// <reference types="es6-shim" /> | ||
import { RuleCollection } from './Rule'; | ||
@@ -3,0 +2,0 @@ import { Input } from './Input'; |
@@ -7,3 +7,3 @@ "use strict"; | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments)).next()); | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
@@ -38,2 +38,3 @@ }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var yargsParser = require("yargs-parser"); | ||
@@ -89,4 +90,3 @@ var colors = require("colors/safe"); | ||
exports.InputFactory = InputFactory; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = InputFactory; | ||
//# sourceMappingURL=InputFactory.js.map |
@@ -1,2 +0,1 @@ | ||
/// <reference types="es6-shim" /> | ||
import { Rule } from './Rule'; | ||
@@ -16,10 +15,10 @@ import { RuleCollection } from './Rule'; | ||
questionInfo: QuestionInfo[]; | ||
constructor(cancelValue: any); | ||
importRuleForStringWithMultipleChoices(key: string, rule: Rule): void; | ||
importRuleForString(key: string, rule: Rule): void; | ||
importRuleForBoolean(key: string, rule: Rule): void; | ||
importRule(key: string, rule: Rule): void; | ||
importRules(rules: RuleCollection): void; | ||
constructor(cancelValue: any, questionInfo?: QuestionInfo[]); | ||
importRuleForStringWithMultipleChoices(key: string, rule: Rule): Inquiry; | ||
importRuleForString(key: string, rule: Rule): Inquiry; | ||
importRuleForBoolean(key: string, rule: Rule): Inquiry; | ||
importRule(key: string, rule: Rule): Inquiry; | ||
importRules(rules: RuleCollection): Inquiry; | ||
prompt(): Promise<InquiryResult>; | ||
} | ||
export default Inquiry; |
@@ -7,3 +7,3 @@ "use strict"; | ||
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments)).next()); | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
@@ -38,7 +38,10 @@ }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var inquirer = require("inquirer"); | ||
var immunity = require("immunity"); | ||
var Inquiry = (function () { | ||
function Inquiry(cancelValue) { | ||
function Inquiry(cancelValue, questionInfo) { | ||
if (questionInfo === void 0) { questionInfo = []; } | ||
this.cancelValue = cancelValue; | ||
this.questionInfo = []; | ||
this.questionInfo = questionInfo; | ||
} | ||
@@ -48,5 +51,5 @@ Inquiry.prototype.importRuleForStringWithMultipleChoices = function (key, rule) { | ||
if (rule.cancelValue !== undefined) { | ||
choices.push(new inquirer.Separator()); | ||
choices = immunity.appendToArray(choices, new inquirer.Separator()); | ||
if (rule.cancelValue.constructor === String) { | ||
choices.push({ | ||
choices = immunity.appendToArray(choices, { | ||
name: rule.cancelValue, | ||
@@ -58,3 +61,3 @@ value: this.cancelValue, | ||
else { | ||
choices.push({ | ||
choices = immunity.appendToArray(choices, { | ||
name: rule.cancelValue.name, | ||
@@ -66,3 +69,3 @@ value: this.cancelValue, | ||
} | ||
this.questionInfo.push({ | ||
return new Inquiry(this.cancelValue, immunity.appendToArray(this.questionInfo, { | ||
key: key, | ||
@@ -76,10 +79,9 @@ rule: rule, | ||
} | ||
}); | ||
})); | ||
}; | ||
Inquiry.prototype.importRuleForString = function (key, rule) { | ||
if (rule.values !== undefined) { | ||
this.importRuleForStringWithMultipleChoices(key, rule); | ||
return; | ||
return this.importRuleForStringWithMultipleChoices(key, rule); | ||
} | ||
this.questionInfo.push({ | ||
return new Inquiry(this.cancelValue, immunity.appendToArray(this.questionInfo, { | ||
key: key, | ||
@@ -93,6 +95,6 @@ rule: rule, | ||
} | ||
}); | ||
})); | ||
}; | ||
Inquiry.prototype.importRuleForBoolean = function (key, rule) { | ||
this.questionInfo.push({ | ||
return new Inquiry(this.cancelValue, immunity.appendToArray(this.questionInfo, { | ||
key: key, | ||
@@ -106,38 +108,40 @@ rule: rule, | ||
} | ||
}); | ||
})); | ||
}; | ||
Inquiry.prototype.importRule = function (key, rule) { | ||
if (rule.uiHidden) { | ||
return; | ||
return this; | ||
} | ||
if (rule.type === String) { | ||
this.importRuleForString(key, rule); | ||
return this.importRuleForString(key, rule); | ||
} | ||
else if (rule.type === Boolean) { | ||
this.importRuleForBoolean(key, rule); | ||
if (rule.type === Boolean) { | ||
return this.importRuleForBoolean(key, rule); | ||
} | ||
return this; | ||
}; | ||
Inquiry.prototype.importRules = function (rules) { | ||
var instance = this; | ||
for (var _i = 0, _a = Object.keys(rules); _i < _a.length; _i++) { | ||
var ruleKey = _a[_i]; | ||
var rule = rules[ruleKey]; | ||
this.importRule(ruleKey, rule); | ||
instance = instance.importRule(ruleKey, rule); | ||
} | ||
return instance; | ||
}; | ||
Inquiry.prototype.prompt = function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var argv, _i, _a, questionInfo, result; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
var argv, _i, _a, questionInfo, result, _b, _c, _d; | ||
return __generator(this, function (_e) { | ||
switch (_e.label) { | ||
case 0: | ||
argv = {}; | ||
_i = 0, _a = this.questionInfo; | ||
_b.label = 1; | ||
_e.label = 1; | ||
case 1: | ||
if (!(_i < _a.length)) | ||
return [3 /*break*/, 4]; | ||
if (!(_i < _a.length)) return [3 /*break*/, 4]; | ||
questionInfo = _a[_i]; | ||
return [4 /*yield*/, inquirer.prompt(questionInfo.inquirerInput)]; | ||
case 2: | ||
result = _b.sent(); | ||
result = _e.sent(); | ||
if ((questionInfo.rule.cancelValue !== undefined) && (result.value === this.cancelValue)) { | ||
@@ -150,11 +154,17 @@ return [2 /*return*/, { | ||
if (result.value.length === 0) { | ||
argv[questionInfo.key] = questionInfo.rule.default; | ||
immunity.appendToObject(argv, (_b = {}, | ||
_b[questionInfo.key] = questionInfo.rule.default, | ||
_b)); | ||
} | ||
else if (questionInfo.rule.max === undefined || questionInfo.rule.max > 1) { | ||
argv[questionInfo.key] = [result.value]; | ||
immunity.appendToObject(argv, (_c = {}, | ||
_c[questionInfo.key] = [result.value], | ||
_c)); | ||
} | ||
else { | ||
argv[questionInfo.key] = result.value; | ||
immunity.appendToObject(argv, (_d = {}, | ||
_d[questionInfo.key] = result.value, | ||
_d)); | ||
} | ||
_b.label = 3; | ||
_e.label = 3; | ||
case 3: | ||
@@ -174,4 +184,3 @@ _i++; | ||
exports.Inquiry = Inquiry; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = Inquiry; | ||
//# sourceMappingURL=Inquiry.js.map |
@@ -5,2 +5,7 @@ export declare type RuleTypes = StringConstructor | BooleanConstructor; | ||
}; | ||
export declare type ValidateMethod = (value: any) => ValidationResult; | ||
export declare type ValidationResult = { | ||
result: Boolean; | ||
message?: String; | ||
}; | ||
export interface Rule { | ||
@@ -13,3 +18,3 @@ type: RuleTypes; | ||
values?: (string | any)[]; | ||
cancelValue?: string; | ||
cancelValue?: any; | ||
'default': string[]; | ||
@@ -20,4 +25,4 @@ uiHidden?: boolean; | ||
max?: number; | ||
validate?: Function; | ||
validate?: ValidateMethod; | ||
} | ||
export default Rule; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=Rule.js.map |
@@ -1,2 +0,2 @@ | ||
export declare function alignedString(input: any, initial?: string): string; | ||
export declare function alignedString(input: (number | string)[], initial?: string): string; | ||
export default alignedString; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function alignedString(input, initial) { | ||
@@ -15,4 +16,3 @@ if (initial === void 0) { initial = ''; } | ||
exports.alignedString = alignedString; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = alignedString; | ||
//# sourceMappingURL=alignedString.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function pathNotation(sourceObj, targetPath, defaultValue, delimiter) { | ||
@@ -17,4 +18,3 @@ if (defaultValue === void 0) { defaultValue = undefined; } | ||
exports.pathNotation = pathNotation; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = pathNotation; | ||
//# sourceMappingURL=pathNotation.js.map |
@@ -19,3 +19,3 @@ { | ||
], | ||
"version": "0.1.20", | ||
"version": "0.1.23", | ||
"homepage": "", | ||
@@ -54,9 +54,10 @@ "author": "Eser Ozvataf <eser@ozvataf.com>", | ||
"colors": "^1.1.2", | ||
"evangelist": "^0.0.2", | ||
"inquirer": "^2.0.0", | ||
"yargs-parser": "^4.2.0" | ||
"evangelist": "^0.0.4", | ||
"immunity": "^0.0.11", | ||
"inquirer": "^3.0.6", | ||
"yargs-parser": "^5.0.0" | ||
}, | ||
"devDependencies": { | ||
"@types/es6-shim": "^0.31.32" | ||
"@types/es6-shim": "^0.31.33" | ||
} | ||
} |
@@ -28,3 +28,3 @@ # [consultant](https://github.com/eserozvataf/consultant) | ||
const page = consultant.createPage('Homepage', { | ||
const rules = { | ||
makefile: { | ||
@@ -84,18 +84,17 @@ type: String, | ||
} | ||
}); | ||
}; | ||
// string parsing | ||
let input1 = consultant.input.fromString(rules, 'eser testing --makefile testfile.js'); | ||
console.log(input1.validate()); | ||
// command line parsing | ||
const argv = consultant.parse(process.argv.slice(2)); | ||
console.log(page.validate('main', argv)); | ||
let input2 = consultant.input.fromCommandLine(rules); | ||
console.log(input2.validate()); | ||
// command line user interface | ||
page.inquiry('main') | ||
.then((answers) => { | ||
console.log(answers); | ||
consultant.input.fromInquiry(rules) | ||
.then((input3) => { | ||
console.log(input3); | ||
}); | ||
// help | ||
const help = []; | ||
page.help('main', help); | ||
console.log(help); | ||
``` | ||
@@ -102,0 +101,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
39356
30
547
0
5
143
+ Addedimmunity@^0.0.11
+ Addedansi-escapes@3.2.0(transitive)
+ Addedansi-styles@3.2.1(transitive)
+ Addedcall-bind@1.0.7(transitive)
+ Addedchalk@2.4.2(transitive)
+ Addedchardet@0.4.2(transitive)
+ Addedcli-cursor@2.1.0(transitive)
+ Addedcolor-convert@1.9.3(transitive)
+ Addedcolor-name@1.1.3(transitive)
+ Addeddefine-data-property@1.1.4(transitive)
+ Addeddefine-properties@1.2.1(transitive)
+ Addedes-define-property@1.0.0(transitive)
+ Addedes-errors@1.3.0(transitive)
+ Addedevangelist@0.0.4(transitive)
+ Addedexternal-editor@2.2.0(transitive)
+ Addedfunction-bind@1.1.2(transitive)
+ Addedget-intrinsic@1.2.4(transitive)
+ Addedgopd@1.0.1(transitive)
+ Addedhas-flag@3.0.0(transitive)
+ Addedhas-property-descriptors@1.0.2(transitive)
+ Addedhas-proto@1.0.3(transitive)
+ Addedhas-symbols@1.0.3(transitive)
+ Addedhasown@2.0.2(transitive)
+ Addediconv-lite@0.4.24(transitive)
+ Addedimmunity@0.0.11(transitive)
+ Addedinquirer@3.3.0(transitive)
+ Addedmimic-fn@1.2.0(transitive)
+ Addedmute-stream@0.0.7(transitive)
+ Addedobject-keys@1.1.1(transitive)
+ Addedobject.assign@4.1.5(transitive)
+ Addedonetime@2.0.1(transitive)
+ Addedrestore-cursor@2.0.0(transitive)
+ Addedrx-lite@4.0.8(transitive)
+ Addedrx-lite-aggregates@4.0.8(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedset-function-length@1.2.2(transitive)
+ Addedsignal-exit@3.0.7(transitive)
+ Addedsupports-color@5.5.0(transitive)
+ Addedtmp@0.0.33(transitive)
+ Addedyargs-parser@5.0.1(transitive)
- Removedansi-escapes@1.4.0(transitive)
- Removedansi-regex@2.1.1(transitive)
- Removedansi-styles@2.2.1(transitive)
- Removedbuffer-from@1.1.2(transitive)
- Removedchalk@1.1.3(transitive)
- Removedcli-cursor@1.0.2(transitive)
- Removedconcat-stream@1.6.2(transitive)
- Removedcore-util-is@1.0.3(transitive)
- Removedevangelist@0.0.2(transitive)
- Removedexit-hook@1.1.1(transitive)
- Removedextend@3.0.2(transitive)
- Removedexternal-editor@1.1.1(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedinherits@2.0.4(transitive)
- Removedinquirer@2.0.0(transitive)
- Removedisarray@1.0.0(transitive)
- Removedmute-stream@0.0.6(transitive)
- Removedonetime@1.1.0(transitive)
- Removedos-shim@0.1.3(transitive)
- Removedpinkie@2.0.4(transitive)
- Removedpinkie-promise@2.0.1(transitive)
- Removedprocess-nextick-args@2.0.1(transitive)
- Removedreadable-stream@2.3.8(transitive)
- Removedrestore-cursor@1.0.1(transitive)
- Removedrx@4.1.0(transitive)
- Removedsafe-buffer@5.1.2(transitive)
- Removedspawn-sync@1.0.15(transitive)
- Removedstring_decoder@1.1.1(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedsupports-color@2.0.0(transitive)
- Removedtmp@0.0.29(transitive)
- Removedtypedarray@0.0.6(transitive)
- Removedutil-deprecate@1.0.2(transitive)
- Removedyargs-parser@4.2.1(transitive)
Updatedevangelist@^0.0.4
Updatedinquirer@^3.0.6
Updatedyargs-parser@^5.0.0