makitso
Advanced tools
Comparing version 1.0.4 to 1.1.0
@@ -0,1 +1,3 @@ | ||
const Prompt = require("makitso-prompt"); | ||
const Makitso = require("../"); | ||
@@ -46,25 +48,18 @@ | ||
async function initMakitso() { | ||
const makitso = Makitso({ options }); | ||
await makitso.registerPlugins({ commands: testCommands }); | ||
makitso.prompt.start = jest.fn(); | ||
return makitso; | ||
} | ||
function nextCommand(makitso, command) { | ||
makitso.prompt.start | ||
async function initMakitso({ command }) { | ||
const prompt = Prompt(); | ||
prompt.start = jest | ||
.fn() | ||
.mockReturnValueOnce(Promise.resolve(command)) | ||
.mockReturnValue(comQuit); | ||
return Makitso({ | ||
options, | ||
plugins: { commands: testCommands }, | ||
commandPrompt: prompt | ||
}); | ||
} | ||
describe("Makitso", () => { | ||
it("creates the Makitso instance", async () => { | ||
const makitso = Makitso({ options }); | ||
expect(makitso.registerPlugins).toBeDefined(); | ||
}); | ||
it("invokes a command with args", async () => { | ||
const makitso = await initMakitso(); | ||
nextCommand(makitso, "basic Uno due tre quattro"); | ||
await makitso.start(); | ||
await initMakitso({ command: "basic Uno due tre quattro" }); | ||
@@ -81,5 +76,3 @@ const arg0 = lastCalled(basicMockAction); | ||
it("invokes a command with a multi arg", async () => { | ||
const makitso = await initMakitso(); | ||
nextCommand(makitso, "multi Uno due tre quattro"); | ||
await makitso.start(); | ||
await initMakitso({ command: "multi Uno due tre quattro" }); | ||
@@ -96,5 +89,3 @@ const arg0 = lastCalled(basicMockAction); | ||
it("invokes a command with an optional arg included", async () => { | ||
const makitso = await initMakitso(); | ||
nextCommand(makitso, "optional Uno due"); | ||
await makitso.start(); | ||
await initMakitso({ command: "optional Uno due" }); | ||
@@ -111,5 +102,3 @@ const arg0 = lastCalled(basicMockAction); | ||
it("invokes a command with an optional arg not included", async () => { | ||
const makitso = await initMakitso(); | ||
nextCommand(makitso, "optional Uno"); | ||
await makitso.start(); | ||
await initMakitso({ command: "optional Uno" }); | ||
@@ -126,5 +115,3 @@ const arg0 = lastCalled(basicMockAction); | ||
it("invokes a command with an optional multi arg included", async () => { | ||
const makitso = await initMakitso(); | ||
nextCommand(makitso, "multiOptional Uno due tre"); | ||
await makitso.start(); | ||
await initMakitso({ command: "multiOptional Uno due tre" }); | ||
@@ -141,5 +128,3 @@ const arg0 = lastCalled(basicMockAction); | ||
it("invokes a command with an optional multi arg not included", async () => { | ||
const makitso = await initMakitso(); | ||
nextCommand(makitso, "multiOptional Uno"); | ||
await makitso.start(); | ||
await initMakitso({ command: "multiOptional Uno" }); | ||
@@ -156,5 +141,3 @@ const arg0 = lastCalled(basicMockAction); | ||
it("invokes a command with options", async () => { | ||
const makitso = await initMakitso(); | ||
nextCommand(makitso, "basic -f quattro --three tre"); | ||
await makitso.start(); | ||
await initMakitso({ command: "basic -f quattro --three tre" }); | ||
const arg0 = lastCalled(basicMockAction); | ||
@@ -193,8 +176,6 @@ expect(arg0.context.getSchema).toBeInstanceOf(Function); | ||
it("removes options that shadow positional args", async () => { | ||
const makitso = await initMakitso(); | ||
nextCommand( | ||
makitso, | ||
"basic Uno due tre -o UnoDue --one UnoDueDue -f quattro" | ||
); | ||
await makitso.start(); | ||
await initMakitso({ | ||
command: "basic Uno due tre -o UnoDue --one UnoDueDue -f quattro" | ||
}); | ||
const arg0 = lastCalled(basicMockAction); | ||
@@ -201,0 +182,0 @@ expect(arg0.context.getSchema).toBeInstanceOf(Function); |
@@ -6,4 +6,3 @@ const Prompt = require("makitso-prompt"); | ||
const prompt = Prompt(); | ||
prompt.start = jest.fn(); | ||
let prompt; | ||
@@ -15,2 +14,6 @@ function nextInput(username) { | ||
describe("get", () => { | ||
beforeEach(() => { | ||
prompt = Prompt(); | ||
prompt.start = jest.fn(); | ||
}); | ||
it("prompts for a missing value, stores, and returns the answer", async () => { | ||
@@ -17,0 +20,0 @@ nextInput("lecstor"); |
@@ -5,3 +5,7 @@ "use strict"; | ||
const _get = require("lodash/get"); | ||
const _isFunction = require("lodash/isFunction"); | ||
const Prompt = require("makitso-prompt"); | ||
const AutoComplete = require("./keypress-autocomplete"); | ||
/** | ||
@@ -93,12 +97,11 @@ * @param {String} schemaPath - the dotted path to the property schema | ||
* @param {Object} arg0.stores - Object of store objects. | ||
* @param {Object} arg0.prompt - Instance of makitso-prompt | ||
* @returns {Object} An instance of Context. | ||
*/ | ||
function Context(arg0 = {}) { | ||
const { commands, prompt, schema, stores } = arg0; | ||
const { commands, schema, stores, prompt } = arg0; | ||
return { | ||
commands, | ||
prompt, | ||
schema, | ||
stores, | ||
prompt, | ||
@@ -118,4 +121,11 @@ /** | ||
const variant = (meta.variant !== "default" ? meta.variant : false) || ""; | ||
const { header, prompt: mPrompt, footer } = meta.ask; | ||
const { storedValueIs, default: mDefault, maskInput = false } = meta.ask; | ||
const { | ||
default: mDefault, | ||
footer, | ||
header, | ||
maskInput = false, | ||
prompt: mPrompt, | ||
storedValueIs, | ||
suggest | ||
} = meta.ask; | ||
@@ -128,2 +138,13 @@ let value = await this.stores[meta.store].get(meta); | ||
const prompt = this.prompt || Prompt(); | ||
if (suggest) { | ||
const suggestList = _isFunction(suggest) | ||
? await suggest({ property: prop }) | ||
: suggest; | ||
const complete = AutoComplete(suggestList); | ||
Object.assign(prompt, { | ||
keyPressers: [...prompt.keyPressers, complete] | ||
}); | ||
} | ||
const thisPrompt = { | ||
@@ -130,0 +151,0 @@ header: header ? header.replace("{variant}", variant) : "", |
@@ -224,3 +224,3 @@ "use strict"; | ||
function Makitso(args = {}) { | ||
const { plugins, contextPrompt = Prompt() } = args; | ||
const { plugins } = args; | ||
let { commandPrompt } = args; | ||
@@ -238,8 +238,3 @@ | ||
const { schema, stores, commands } = pluginSet; | ||
const context = Context({ | ||
schema, | ||
stores, | ||
commands, | ||
prompt: contextPrompt | ||
}); | ||
const context = Context({ schema, stores, commands }); | ||
return start({ context, commands, prompt: commandPrompt }); | ||
@@ -246,0 +241,0 @@ }); |
{ | ||
"name": "makitso", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "A Framework for building composable interactive commandline apps", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
175066
41
2171