@pubby/sdk
Version:
Pubby Development Kit
172 lines (167 loc) • 6.23 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var tslib = require('tslib');
var MissingParameterError = /** @class */ (function (_super) {
tslib.__extends(MissingParameterError, _super);
function MissingParameterError(param) {
var _this = _super.call(this, "Missing parameter \"" + param + "\"!") || this;
_this.param = param;
return _this;
}
return MissingParameterError;
}(Error));
function regexpFinder(regex, normalizer) {
if (normalizer === void 0) { normalizer = function (text) { return text; }; }
return function (textRef) {
var match = regex.exec(textRef.text);
if (!match)
return null;
var _a = tslib.__read(match, 1), value = _a[0];
textRef.consume(value.length);
return normalizer(value);
};
}
var TextRef = /** @class */ (function () {
function TextRef(text) {
this.text = text;
}
TextRef.prototype.consume = function (count) {
this.text = this.text.substr(count);
return this;
};
TextRef.prototype.trim = function () {
this.text = this.text.trim();
return this;
};
TextRef.prototype.toString = function () {
return this.text;
};
return TextRef;
}());
var ParamsString = /** @class */ (function () {
function ParamsString(types) {
this.types = types;
}
ParamsString.prototype.parseArg = function (arg) {
var ARG_REGEXP = /^([a-z][\w_$]*)(?::(.+))?/gim;
var delimiter = arg.charAt(0);
if (delimiter !== "<" && delimiter !== "[") {
throw new Error("Invalid arg \"" + arg + "\"");
}
arg = arg.substr(1, arg.length - 2);
var match = ARG_REGEXP.exec(arg);
if (!match) {
throw new Error("Invalid arg \"" + arg + "\"");
}
var _a = tslib.__read(match, 3), _ = _a[0], name = _a[1], typeDef = _a[2];
var _b = tslib.__read(typeDef.split(":"), 2), type = _b[0], _c = _b[1], typeArg = _c === void 0 ? "" : _c;
var rest = type.startsWith("...");
if (rest) {
type = type.substr(3);
}
if (!(type in this.types)) {
throw new Error("Invalid type \"" + type + "\" for parameter \"" + name + "\".");
}
return {
name: name,
required: delimiter === "<",
rest: rest,
type: this.types[type],
typeArg: typeArg,
};
};
ParamsString.prototype.defaultRestMatcher = function () {
return function (textRef) {
var value = [];
while (textRef) {
textRef.trim();
var findValue = this.find(textRef);
if ((findValue !== null && findValue !== void 0 ? findValue : null) === null) {
break;
}
value.push(findValue);
}
if (!value.length) {
return null;
}
return value;
};
};
ParamsString.prototype.typedef = function (type) {
var _a;
(_a = type.rest) !== null && _a !== void 0 ? _a : (type.rest = this.defaultRestMatcher());
this.types[type.name] = type;
return this;
};
ParamsString.prototype.parseArgs = function (usageString) {
var _this = this;
usageString = usageString.trim();
if (!usageString)
return [];
var argsString = usageString.split(/\s+/);
return argsString.map(function (arg) { return _this.parseArg(arg); });
};
ParamsString.prototype.parseText = function (params, textRef) {
var e_1, _a;
var argsObject = {};
try {
for (var params_1 = tslib.__values(params), params_1_1 = params_1.next(); !params_1_1.done; params_1_1 = params_1.next()) {
var param = params_1_1.value;
textRef.trim();
var value = param.rest
? param.type.rest(textRef)
: param.type.find(textRef);
if ((value !== null && value !== void 0 ? value : null) !== null) {
argsObject[param.name] = value;
}
else if (param.required) {
throw new MissingParameterError(param.name);
}
else {
argsObject[param.name] = null;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (params_1_1 && !params_1_1.done && (_a = params_1.return)) _a.call(params_1);
}
finally { if (e_1) throw e_1.error; }
}
return argsObject;
};
ParamsString.prototype.getCommand = function (usage, input) {
var _a;
var commands = ["run"];
var command = commands[0];
var usageRef = new TextRef(usage);
var inputRef = new TextRef(input);
var regex = /^<((?:\|?(?:[\w_$]+)(?::(default))?)+)>/g;
var match = regex.exec(usageRef.text);
if (match) {
commands = match[1].split("|").map(function (cmdDef) {
var _a = tslib.__read(cmdDef.split(":"), 2), cmd = _a[0], modifier = _a[1];
if (modifier === "default") {
command = cmd;
}
return cmd;
});
usageRef.consume(match[0].length).trim();
}
var commandMatch = new RegExp("^(" + commands.join("|") + ")\\b").exec(inputRef.text);
if (commandMatch) {
_a = tslib.__read(commandMatch, 1), command = _a[0];
inputRef.consume(command.length);
}
return {
command: command,
args: this.parseText(this.parseArgs(usageRef.text), inputRef),
};
};
return ParamsString;
}());
exports.MissingParameterError = MissingParameterError;
exports.ParamsString = ParamsString;
exports.TextRef = TextRef;
exports.regexpFinder = regexpFinder;