consultant
Advanced tools
Comparing version 0.8.2 to 0.8.3
@@ -19,3 +19,4 @@ "use strict"; | ||
exports.ArgsParser = ArgsParser; | ||
; | ||
exports.default = ArgsParser; | ||
//# sourceMappingURL=ArgsParser.js.map |
@@ -12,4 +12,5 @@ import { Types } from './Types'; | ||
fromInquiry(): Promise<ConsultationResult>; | ||
getRule(predicate: (key: string, rule: Rule) => boolean, node?: Rule): any; | ||
getRuleById(id: string): Promise<any>; | ||
static getRuleInternal(predicate: (key: string, rule: Rule) => boolean, node: Rule): Promise<Rule | undefined>; | ||
getRule(predicate: (key: string, rule: Rule) => boolean): Promise<Rule | undefined>; | ||
getRuleById(id: string): Promise<Rule | undefined>; | ||
help(): Promise<void>; | ||
@@ -16,0 +17,0 @@ helpForId(id: string): Promise<void>; |
@@ -28,3 +28,3 @@ "use strict"; | ||
} | ||
async getRule(predicate, node = this.rules) { | ||
static async getRuleInternal(predicate, node) { | ||
const children = await Rule_1.getRuleChildren(node); | ||
@@ -38,3 +38,3 @@ if (children !== undefined) { | ||
if (child.children !== undefined || child.getChildren !== undefined) { | ||
const result = this.getRule(predicate, child); | ||
const result = await Consultant.getRuleInternal(predicate, child); | ||
if (result !== undefined) { | ||
@@ -48,2 +48,5 @@ return result; | ||
} | ||
async getRule(predicate) { | ||
return Consultant.getRuleInternal(predicate, this.rules); | ||
} | ||
async getRuleById(id) { | ||
@@ -58,2 +61,5 @@ return await this.getRule((key, rule) => rule.id === id); | ||
const rule = await this.getRuleById(id), dumper = new HelpDumper_1.HelpDumper(); | ||
if (rule === undefined) { | ||
throw new Error(`rule id '${id}' is not found.`); | ||
} | ||
await dumper.dump(rule, process.stdout); | ||
@@ -64,3 +70,4 @@ } | ||
exports.Consultant = Consultant; | ||
; | ||
exports.default = Consultant; | ||
//# sourceMappingURL=Consultant.js.map |
@@ -5,2 +5,4 @@ "use strict"; | ||
const Validator_1 = require("./Validator"); | ||
; | ||
; | ||
class Consultation { | ||
@@ -24,3 +26,4 @@ constructor(rules, argv) { | ||
exports.Consultation = Consultation; | ||
; | ||
exports.default = Consultation; | ||
//# sourceMappingURL=Consultation.js.map |
@@ -107,3 +107,4 @@ "use strict"; | ||
exports.HelpDumper = HelpDumper; | ||
; | ||
exports.default = HelpDumper; | ||
//# sourceMappingURL=HelpDumper.js.map |
@@ -5,13 +5,8 @@ "use strict"; | ||
async inquire(rules) { | ||
return Promise.resolve({ | ||
tags: [], | ||
values: {}, | ||
isValid: false, | ||
isCancelled: true, | ||
errors: {} | ||
}); | ||
throw new Error('Inquirer is not implemented yet.'); | ||
} | ||
} | ||
exports.Inquirer = Inquirer; | ||
; | ||
exports.default = Inquirer; | ||
//# sourceMappingURL=Inquirer.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
; | ||
async function getRuleChildren(rule) { | ||
@@ -10,2 +11,3 @@ if (rule.getChildren !== undefined) { | ||
exports.getRuleChildren = getRuleChildren; | ||
; | ||
//# sourceMappingURL=Rule.js.map |
@@ -9,3 +9,4 @@ "use strict"; | ||
})(Types = exports.Types || (exports.Types = {})); | ||
; | ||
exports.default = Types; | ||
//# sourceMappingURL=Types.js.map |
@@ -1,2 +0,2 @@ | ||
export declare function alignedString(input: (number | string)[], initial?: string): string; | ||
export declare function alignedString(input: Array<number | string>, initial?: string): string; | ||
export default alignedString; |
@@ -15,3 +15,4 @@ "use strict"; | ||
exports.alignedString = alignedString; | ||
; | ||
exports.default = alignedString; | ||
//# sourceMappingURL=alignedString.js.map |
@@ -1,2 +0,2 @@ | ||
export declare function pathNotation(sourceObj: any, targetPath: any, defaultValue?: undefined, delimiter?: string): any; | ||
export declare function pathNotation(sourceObj: any, targetPath: string, defaultValue?: any, delimiter?: string): any; | ||
export default pathNotation; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
function pathNotation(sourceObj, targetPath, defaultValue = undefined, delimiter = '.') { | ||
function pathNotation(sourceObj, targetPath, defaultValue, delimiter = '.') { | ||
const targetPath_ = targetPath.split(delimiter); | ||
@@ -8,3 +8,3 @@ let sourceObj_ = sourceObj; | ||
const property = targetPath_.shift(); | ||
if (!(property in sourceObj_)) { | ||
if (property === undefined || !(property in sourceObj_)) { | ||
return defaultValue; | ||
@@ -11,0 +11,0 @@ } |
import { ConsultationResult, ConsultationError } from './Consultation'; | ||
import { Rule, RuleCollection, ValidateMethod } from './Rule'; | ||
export declare type ValidationOutputType = { | ||
commandId?: string; | ||
values: { | ||
[key: string]: any; | ||
}; | ||
errors: { | ||
[key: string]: Array<ConsultationError>; | ||
}; | ||
argvRemainder: { | ||
[key: string]: any; | ||
}; | ||
}; | ||
export declare class Validator { | ||
static getArgvKeys(rule: Rule, key: string, condition: (key: string) => boolean): string[]; | ||
static executeValidatorSingle(validatorFunc: ValidateMethod, childKey: any, value: any): Promise<ConsultationError[]>; | ||
static executeValidator(validatorFunc: ValidateMethod, childKey: any, value: any): Promise<ConsultationError[]>; | ||
static getArgvKeys(rule: Rule, key: string, condition: (key: string) => boolean): Array<string>; | ||
static executeValidatorSingle(validatorFunc: ValidateMethod, childKey: any, value: any): Promise<Array<ConsultationError>>; | ||
static executeValidator(validatorFunc: ValidateMethod, childKey: any, value: any): Promise<Array<ConsultationError>>; | ||
static prepareValue(value: any[], childKey: string, child: Rule): Promise<{ | ||
value: any; | ||
errors: ConsultationError[] | undefined; | ||
errors?: Array<ConsultationError>; | ||
}>; | ||
@@ -14,4 +26,6 @@ processSingleParameter(childKey: string, child: Rule, argv: { | ||
}): Promise<{ | ||
values: any; | ||
errors: ConsultationError[] | undefined; | ||
values: { | ||
[key: string]: any; | ||
}; | ||
errors?: Array<ConsultationError>; | ||
argvRemainder: { | ||
@@ -24,4 +38,8 @@ [key: string]: any; | ||
}): Promise<{ | ||
values: {}; | ||
errors: {}; | ||
values: { | ||
[key: string]: any; | ||
}; | ||
errors?: { | ||
[key: string]: Array<ConsultationError>; | ||
}; | ||
argvRemainder: { | ||
@@ -34,11 +52,6 @@ [key: string]: any; | ||
}): { | ||
commandKey: string; | ||
commandKey?: string; | ||
argvRemainder: { | ||
[key: string]: any; | ||
}; | ||
} | { | ||
commandKey: undefined; | ||
argvRemainder: { | ||
[key: string]: any; | ||
}; | ||
}; | ||
@@ -48,3 +61,3 @@ processCommands(children: RuleCollection, argv: { | ||
}): { | ||
commandKey: string | undefined; | ||
commandKey?: string; | ||
argvRemainder: { | ||
@@ -56,3 +69,3 @@ [key: string]: any; | ||
[key: string]: any; | ||
}): any; | ||
}): Promise<ValidationOutputType>; | ||
validate(rules: Rule, argv: { | ||
@@ -59,0 +72,0 @@ [key: string]: any; |
@@ -214,3 +214,4 @@ "use strict"; | ||
exports.Validator = Validator; | ||
; | ||
exports.default = Validator; | ||
//# sourceMappingURL=Validator.js.map |
{ | ||
"name": "consultant", | ||
"description": "Library for getting parameters from various sources", | ||
"description": "Gathers structural and validateable input from command line or interative menu", | ||
"keywords": [ | ||
@@ -19,3 +19,3 @@ "interactive", | ||
], | ||
"version": "0.8.2", | ||
"version": "0.8.3", | ||
"homepage": "", | ||
@@ -22,0 +22,0 @@ "author": "Eser Ozvataf <eser@ozvataf.com>", |
152
README.md
@@ -11,3 +11,3 @@ # [consultant](https://github.com/eserozvataf/jsmake-libraries) | ||
Consultant is a JavaScript library which allows us getting parameters (or options) from various sources such as command line, string or interactive menu. | ||
Consultant is a JavaScript library which allows you gathering options from various sources such as command line, optstring or interactive menu. | ||
@@ -20,87 +20,103 @@ Provides: | ||
- Parsing command line input like `cmd --parameter=value --option1 --option2`. | ||
- Interactive command line user interface to getting options directly from user. | ||
- Interactive command line user interface to getting options directly from user. | ||
- Prepares an help output based on model definition. | ||
## Example Usage | ||
## Quick start | ||
Execute `npm install consultant` to install consultant and its dependencies into your project directory. | ||
## Usage | ||
```js | ||
import consultant from 'consultant'; | ||
import { Consultant } from 'consultant'; | ||
const 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: (value) => value.length >= 3 || 'minimum 3 chars required' | ||
}, | ||
tasks: { | ||
type: Boolean, | ||
aliases: [ 't' ], | ||
label: 'Tasks', | ||
description: 'Lists defined tasks', | ||
'default': false, | ||
uiHidden: true, | ||
min: 0, | ||
max: 1 | ||
}, | ||
verbosity: { | ||
type: String, | ||
label: 'Verbosity', | ||
description: 'Sets verbosity of log messages [debug, warn, info, error]', | ||
'default': 'info', | ||
values: [ 'debug', 'warn', 'info', 'error' ], | ||
uiHidden: true, | ||
min: 0, | ||
max: 1 | ||
}, | ||
version: { | ||
type: Boolean, | ||
aliases: [ 'v' ], | ||
label: 'Version', | ||
description: 'Displays the jsmake version', | ||
'default': false, | ||
uiHidden: true, | ||
min: 0, | ||
max: 1 | ||
}, | ||
help: { | ||
type: Boolean, | ||
aliases: [ 'h', '?' ], | ||
label: 'Help', | ||
description: 'Displays this help message', | ||
'default': false, | ||
uiHidden: true, | ||
min: 0, | ||
max: 1 | ||
label: 'sample menu', | ||
children: { | ||
plugins: { | ||
type: Consultant.types.command, | ||
aliases: [ 'p' ], | ||
id: 'plugins', | ||
label: 'Plugin commands', | ||
description: 'Add/list plugins', | ||
children: { | ||
add: { | ||
type: Consultant.types.command, | ||
id: 'plugins-add', | ||
label: 'Add', | ||
description: 'Adds a plugin' | ||
}, | ||
repo: { | ||
type: Consultant.types.stringParameter, | ||
label: 'Repo', | ||
description: 'Specifies repo address', | ||
'default': 'http://github.com/eserozvataf/', | ||
min: 0, | ||
max: 1 | ||
} | ||
} | ||
}, | ||
makefile: { | ||
type: Consultant.types.stringParameter, | ||
aliases: [ 'f' ], | ||
label: 'Makefile', | ||
description: 'Load tasks from FILE', | ||
parameter: 'FILE', | ||
'default': [ 'makefile.js' ], | ||
min: 0, | ||
max: undefined, | ||
validate: (value) => value.length >= 3 || 'minimum 3 chars required' | ||
}, | ||
tasks: { | ||
type: Consultant.types.booleanParameter, | ||
aliases: [ 't' ], | ||
label: 'Tasks', | ||
description: 'Lists defined tasks', | ||
'default': false | ||
}, | ||
verbosity: { | ||
type: Consultant.types.stringParameter, | ||
label: 'Verbosity', | ||
description: 'Sets verbosity of log messages [debug, warn, info, error]', | ||
'default': 'info', | ||
values: [ 'debug', 'warn', 'info', 'error' ], | ||
min: 0, | ||
max: 1 | ||
}, | ||
version: { | ||
type: Consultant.types.booleanParameter, | ||
aliases: [ 'v' ], | ||
label: 'Version', | ||
description: 'Displays the jsmake version', | ||
'default': false | ||
}, | ||
help: { | ||
type: Consultant.types.booleanParameter, | ||
aliases: [ 'h', '?' ], | ||
label: 'Help', | ||
description: 'Displays this help message', | ||
'default': false | ||
} | ||
} | ||
}; | ||
const params = new Consultant(rules); | ||
// string parsing | ||
const input1 = consultant.input.fromString('eser testing --makefile testfile.js'); | ||
console.log(input1.validate(rules)); | ||
const input1 = await params.fromString('plugins add --makefile testfile.js'); | ||
console.log(input1.commandId); // plugins-add | ||
console.log(input1.values.makefile); // [ 'testfile.js' ] | ||
// command line parsing | ||
const input2 = consultant.input.fromCommandLine(); | ||
console.log(input2.validate(rules)); | ||
const input2 = await params.fromCommandLine(); | ||
console.log(input2.values.verbosity); // info | ||
// command line user interface | ||
consultant.input.fromInquiry(rules) | ||
.then((input3) => { | ||
console.log(input3.validate(rules)); | ||
}); | ||
const input3 = await params.fromInquiry(); | ||
``` | ||
## Quick start | ||
Execute `npm install consultant` to install consultant and its dependencies into your project directory. | ||
## Todo List | ||
@@ -107,0 +123,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
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
49890
678
157