@udraft/core
Version:
uDraft is a language and stack agnostic code-generation tool that simplifies full-stack development by converting a single YAML file into code for rapid development.
170 lines • 6.55 kB
JavaScript
"use strict";
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.URenderer = void 0;
const queries_1 = require("../shortcuts/queries");
const attributes_1 = require("../shortcuts/attributes");
class URenderer {
constructor(name) {
this._selection = {};
this._contents = [];
this._outputs = [];
this._name = name;
}
$name() {
return this._name;
}
$draft() {
return this._draft;
}
$selection() {
return Object.assign({}, this._selection);
}
$paths() {
return [...(this._selection.paths || [])];
}
$contents() {
return [...this._contents];
}
$outputs() {
return [...this._outputs];
}
$path(key) {
return this.$paths().find((f) => f.key == key) || null;
}
$content(key) {
return this.$contents().find((f) => f.key == key) || null;
}
$output(key) {
return this.$outputs().find((f) => f.key == key) || null;
}
$modules(where) {
const modules = this.$draft()
.$modules()
.filter((mod) => (where ? where(mod) : true));
return modules;
}
$features(where) {
const modules = this.$draft().$modules();
const features = [];
modules.forEach((mod) => {
let foundFeatures = mod.$features();
if (where)
foundFeatures = foundFeatures.filter((feature) => where(mod, feature));
features.push(...foundFeatures);
});
return features;
}
$models(where) {
const modules = this.$draft().$modules();
let models = [];
modules.forEach((mod) => {
let foundModels = [...mod.$models()];
mod.$features().forEach((feature) => {
const root = [];
if (feature.$input())
root.push(feature.$input());
if (feature.$output())
root.push(feature.$output());
const modelIsAlreadyRegistered = (model) => foundModels.some((m) => m.$name() == model.$name()) ||
models.some((m) => m.$name() == model.$name());
const deepSearch = (model, tail = []) => {
if (tail.includes(model.$name()))
return;
tail.push(model.$name());
const modelRootModule = (0, queries_1.$attr)(model, (0, attributes_1._rootModule)());
const isAlreadyRegistered = modelIsAlreadyRegistered(model);
if (!isAlreadyRegistered ||
(modelRootModule && modelRootModule.$name() == mod.$name())) {
if (isAlreadyRegistered) {
foundModels = foundModels.filter((m) => m.$name() != model.$name());
models = models.filter((m) => m.$name() != model.$name());
}
foundModels.push(model);
model.$fields().forEach((field) => {
if (field.$type() == "nested" || field.$type() == "reference") {
const referencedModel = (0, queries_1.$attr)(field, (0, attributes_1._ref)());
if (referencedModel && referencedModel.$name() != model.$name())
deepSearch(referencedModel, tail);
}
});
}
};
root.forEach((model) => deepSearch(model));
});
if (where)
foundModels = foundModels.filter((model) => where(mod, model));
models.push(...foundModels);
});
return models;
}
$resolveRelativePath(from, to) {
const toFileName = to.split("/").slice(-1)[0];
const fromParts = from
.split("/")
.filter((v) => !!v)
.slice(0, -1);
const toParts = to
.split("/")
.filter((v) => !!v)
.slice(0, -1);
for (let i = 0; i <= fromParts.length; i++) {
const pathIsSplitting = fromParts.slice(0, i).join("/") !== toParts.slice(0, i).join("/");
if (pathIsSplitting ||
(i == fromParts.length && fromParts.length < toParts.length)) {
let returns = "";
if (pathIsSplitting)
for (let j = 0; j <= fromParts.length - i; j++)
returns += "../";
return `${returns ? returns : "./"}${toParts
.slice(pathIsSplitting ? i - 1 : i)
.join("/")}/${toFileName}`;
}
}
return `./${toFileName}`;
}
init(draft) {
return __awaiter(this, void 0, void 0, function* () {
this._draft = draft;
this._selection = yield this.select();
return this;
});
}
run(contents) {
return __awaiter(this, void 0, void 0, function* () {
this._contents = contents;
this._outputs = yield this.render();
this._outputs.forEach((output) => {
var _a;
output.meta = ((_a = this.$content(output.key)) === null || _a === void 0 ? void 0 : _a.meta) || {};
});
return this;
});
}
select() {
return __awaiter(this, void 0, void 0, function* () {
return {};
});
}
render() {
return __awaiter(this, void 0, void 0, function* () {
return [];
});
}
clear() {
this._draft = undefined;
this._selection = {};
this._contents = [];
this._outputs = [];
}
}
exports.URenderer = URenderer;
//# sourceMappingURL=renderer.js.map