poyka
Version:
A framework for interactively creating, extending and maintaining web application as never easier before while doing it in elegant design patterns.
147 lines • 6.82 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExecFromSheet = void 0;
const DB_1 = require("./DB");
const decova_dotnet_developer_1 = require("decova-dotnet-developer");
const Intellisense_1 = require("./Intellisense");
const TerminalAgent_1 = require("./TerminalAgent");
class ExecFromSheet {
constructor() {
this._sheet = null;
}
HandlePromptText(prompt, vars) {
return __awaiter(this, void 0, void 0, function* () {
const ans = yield TerminalAgent_1.TerminalAgent.AskForTextAsync(prompt.DisplayText);
const hasPattern = new decova_dotnet_developer_1.XString(ans).IsNullOrWhiteSpace() == false;
const pattern = new RegExp(ans);
if (hasPattern && pattern.test(ans.trim()) == false) {
TerminalAgent_1.TerminalAgent.ShowError(`[${prompt.VarName}] doesn't match the pattern /${prompt.Regex}/g!`);
yield this.HandlePromptText(prompt, vars);
}
vars.Ensure(prompt.VarName, ans);
});
}
HandleMcq(prompt, vars) {
return __awaiter(this, void 0, void 0, function* () {
const options = new decova_dotnet_developer_1.List(prompt.Options.map(op => { label: op; }));
const intelli = new Intellisense_1.Intellisense(options, (op) => op.label);
const ans = yield intelli.Prompt();
vars.Ensure(prompt.VarName, ans);
});
}
ShowContinueSkip() {
return __awaiter(this, void 0, void 0, function* () {
const options = new decova_dotnet_developer_1.List(['Continue', 'Skip']);
const intelli = new Intellisense_1.Intellisense(options, (op) => op);
const ans = yield intelli.Prompt();
switch (ans) {
case 'Continue':
return true;
case 'Skip':
default:
return false;
}
});
}
CompileScript(composer, vars) {
return __awaiter(this, void 0, void 0, function* () {
let output = new decova_dotnet_developer_1.XString(composer.trim());
vars === null || vars === void 0 ? void 0 : vars.Items.Items.forEach(kv => {
output = output.ReplaceAll(`<<${kv.Key}>>`, kv.Value);
});
try {
if (output.StartsWith("(")) {
const func = eval(output.Value);
if ((func === null || func === void 0 ? void 0 : func.constructor) != Function) {
throw new decova_dotnet_developer_1.Exception(`The provided script cannot be compiled [${composer}]`);
}
output = func();
}
}
catch (err) {
throw new decova_dotnet_developer_1.Exception(`The provided composer execution raised an exception: [${composer}]`, new decova_dotnet_developer_1.Exception(err));
}
return output.Value;
});
}
HandleCommand(command, vars) {
return __awaiter(this, void 0, void 0, function* () {
TerminalAgent_1.TerminalAgent.Hint(command.DisplayText);
const output = yield this.CompileScript(command.Composer, vars);
const ans = yield this.ShowContinueSkip();
if (ans) {
TerminalAgent_1.TerminalAgent.Exec(output);
}
else {
yield TerminalAgent_1.TerminalAgent.ShowError("Command skipped!");
}
});
}
HandleInstruction(instruction, vars) {
return __awaiter(this, void 0, void 0, function* () {
TerminalAgent_1.TerminalAgent.Hint(instruction.DisplayText);
let output = yield this.CompileScript(instruction.Composer, vars);
TerminalAgent_1.TerminalAgent.Instruct(output);
const ans = yield this.ShowContinueSkip();
if (ans) {
TerminalAgent_1.TerminalAgent.ShowError("Done!");
}
else {
TerminalAgent_1.TerminalAgent.ShowError("Instruction skipped!");
}
});
}
HanldeWalkthrough(wk) {
var _a;
return __awaiter(this, void 0, void 0, function* () {
const vars = new decova_dotnet_developer_1.Dictionary();
for (let step of wk.Steps) {
if (step.IsActive == false)
continue;
if (step.RunOnlyIf) {
if (!this.CompileScript(step.RunOnlyIf, vars)) {
continue;
}
}
switch (step.Type) {
case DB_1.StepType.Prompt:
if (((_a = step.Options) === null || _a === void 0 ? void 0 : _a.length) > 0) {
this.HandleMcq(step, vars);
}
else {
this.HandlePromptText(step, vars);
}
break;
case DB_1.StepType.Command:
this.HandleCommand(step, vars);
break;
case DB_1.StepType.Instruction:
this.HandleInstruction(step, vars);
break;
}
}
;
});
}
TakeControl() {
var _a;
return __awaiter(this, void 0, void 0, function* () {
this._sheet = yield DB_1.DB.GetSheetAsync(false);
const allWks = new decova_dotnet_developer_1.List((_a = this._sheet) === null || _a === void 0 ? void 0 : _a.Walkthroughs);
const intelli = new Intellisense_1.Intellisense(allWks, (op) => op.DisplayText);
let ans = yield intelli.Prompt();
yield this.HanldeWalkthrough(ans);
});
}
}
exports.ExecFromSheet = ExecFromSheet;
//# sourceMappingURL=ExecFromSheet.js.map