Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | 3x 3x 3x 3x 3x 9x 9x 18x 9x 9x 9x 9x 9x 9x 9x 3x 14x 14x 4x 4x 4x 4x 4x 4x 1x 3x 6x 3x 3x 6x 3x 5x 5x 5x | "use strict"; import AppError from "@lecstor/app-error"; import _get from "lodash/get"; import _isFunction from "lodash/isFunction"; import { Prompt } from "makitso-prompt"; import { Stores } from "../plugins/stores"; import { keyPressAutoComplete } from "./keypress-autocomplete"; import { Commands, ContextSchema } from "../types"; /** * @param {String} schemaPath - the dotted path to the property schema * @returns {Error} - PropertySchemaNotDefined */ function PropNotSetError(schemaPath: string) { return AppError("PropertySchemaNotDefined", { status: 400, message: `There is no schema defined for the property "${schemaPath}"`, meta: { propSchemaPath: schemaPath } }); } /** * @param {String} propRef - the dotted path to the property * @returns {Error} - PropBadFormatError */ function PropBadFormatError(propRef: string) { return AppError("PropertyBadlyFormatted", { status: 400, message: `"Property ${propRef}" is invalid. A property reference is a dotted string with exactly two or three parts.`, meta: { propRef } }); } /** * @param {String} storeRef - a store idetifier * @returns {Error} - StoreNotFoundError */ function StoreNotFoundError(storeRef: string) { return AppError("StoreNotFound", { status: 400, message: `The requested context store "${storeRef}" does not exist.`, meta: { storeRef } }); } /** * @param {String} propRef - the dotted path to the property * @returns {Object} - property meta */ function splitProp(propRef: string) { const splitPropRef = propRef.split("."); Iif (splitPropRef.length < 2 || splitPropRef.length > 3) { throw PropBadFormatError(propRef); } const [plugin, property, variant = "default"] = splitPropRef; const schemaPath = [plugin, property].join("."); const propertyPath = [schemaPath, variant].join("."); return { schemaPath, propertyPath, variant }; } /** * @typedef {Object} PropertyMeta * @property {String} store - a store identifier * @property {Object} prompt - a question definition * @property {String} prompt.type - question type * @property {String} prompt.name - question name * @property {String} prompt.message - the question * @property {String} schemaPath - dotted path to the property schema * @property {String} propertyPath - dotted path to the property in the store * @property {String} variant - the difference between the schemaPath and the propertyPath */ /** * * @param {Object} schema - context schema * @param {String} propRef - the dotted path to the property * @returns {PropertyMeta} property metadata */ function getPropMeta(schema: ContextSchema, propRef: string) { const propMeta = splitProp(propRef); const propSchema = _get(schema, propMeta.schemaPath); Iif (!propSchema) { throw PropNotSetError(propMeta.schemaPath); } return { ...propSchema, ...propMeta }; } /** * Get an instance of Context * * @param {Object} arg0 - * @param {Object} arg0.commands - Commands definition. * @param {Object} arg0.schema - Context schema. * @param {Object} arg0.stores - Object of store objects. * @returns {Object} An instance of Context. */ type ContextArgs = { commands: Commands; prompt?: Prompt; schema: ContextSchema; stores: Stores; }; export function Context(arg0: ContextArgs) { const { commands, schema, stores, prompt } = arg0; return { commands, schema, stores, prompt, /** * Get a context property * * @param {string} prop - The property to get. * @returns {Promise} - The value of the property */ get: async function(prop: string) { const meta = getPropMeta(this.schema, prop); Iif (!meta.ask) { return this.stores[meta.store].get(meta); } const variant = (meta.variant !== "default" ? meta.variant : false) || ""; const { default: mDefault, footer, header, maskInput = false, prompt: mPrompt, storedValueIs, suggest } = meta.ask; const value = await this.stores[meta.store].get(meta); // use stored value without prompting if (value && storedValueIs === "response") { return value; } const prompt = this.prompt || new Prompt(); if (Esuggest) { const suggestList = _isFunction(suggest) ? await suggest({ property: prop }) : suggest; const complete = keyPressAutoComplete(suggestList); Object.assign(prompt, { keyPressers: [...prompt.keyPressers, complete] }); } const thisPrompt = { header: header ? header.replace("{variant}", variant) : "", prompt: mPrompt ? mPrompt.replace("{variant}", variant) : "", footer: footer ? footer.replace("{variant}", variant) : "", default: value && storedValueIs === "default" ? value : mDefault, maskInput }; const answer = await prompt.start(thisPrompt); await this.set(prop, answer); return answer; }, /** * Set a context property * * @param {string} prop - The property to set. * @param {*} value - The value of the property. * @returns {Promise} - The value of the property */ set: async function(prop: string, value: unknown) { const meta = getPropMeta(this.schema, prop); Iif (!this.stores[meta.store]) { throw StoreNotFoundError(meta.store); } return this.stores[meta.store].set(meta, value); }, /** * Delete a property from context * * @param {string} prop - The property to delete. * @param {string} [variant="default"] - The name of the variant of this property type. * @returns {Promise} - The value of the property */ delete: async function(prop: string) { const meta = getPropMeta(this.schema, prop); return this.stores[meta.store].delete(meta); }, /** * get a store object * * @param {String} store - a store identifier * @returns {Object} store */ getStore: async function(store: string) { return this.stores[store]; }, /** * list available store identifiers * * @returns {String[]} store identifiers */ listStores: async function() { return Object.keys(this.stores); }, /** * get the schema * @returns {Object} a copy of the schema plain object */ getSchema: function() { return JSON.parse(JSON.stringify(this.schema)); } }; } |