UNPKG

@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.

218 lines 11.7 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); 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 }); const Case = __importStar(require("case")); const renderer_1 = require("../entities/renderer"); const queries_1 = require("../shortcuts/queries"); const attributes_1 = require("../shortcuts/attributes"); const KEYS = { draft: "draft", }; class TSDraftRenderer extends renderer_1.URenderer { constructor(options) { super("ts@draft"); this._draftPath = "src/core/draft.ts"; if (options === null || options === void 0 ? void 0 : options.draftPath) this._draftPath = options.draftPath; } select() { return __awaiter(this, void 0, void 0, function* () { const models = this.$models(); const features = this.$features(); const modules = this.$modules(); const paths = [ { key: KEYS.draft, path: this._draftPath, }, ]; return { paths, modules, features: features, models: models, }; }); } render() { return __awaiter(this, void 0, void 0, function* () { const output = []; const models = this.$selection().models || []; const features = this.$selection().features || []; const modules = this.$selection().modules || []; // type UAttribute = { name: string; value?: any }; let content = `export type UAttribute = { name: string; value?: any };\n` + `export type UField = {name: string; type: string; attributes: UAttribute[], ref?: MODELS; isArray: boolean; };\n` + `export type UModel = {name: string; module: MODULES; attributes: UAttribute[]; fields?: UField[]; enum?: Record<string, any> };\n` + `export type UFeature = {name: string; module: MODULES; attributes: UAttribute[]; input?: UModel; output?: UModel; };\n` + `export type UModule = {name: string; attributes: UAttribute[]; features: Record<string, UFeature>; models: Record<string, UModel> };\n`; content += `export enum MODULES {\n${modules .map((mod) => ` ${Case.pascal(mod.$name())} = "${mod.$name()}",`) .join("\n")}\n};\n\nexport enum FEATURES {\n${features .map((feature) => { var _a, _b, _c; return ` ${Case.pascal((_b = (_a = (0, queries_1.$attr)(feature, (0, attributes_1._rootModule)())) === null || _a === void 0 ? void 0 : _a.$name()) !== null && _b !== void 0 ? _b : "") + Case.pascal(feature.$name())} = "${((_c = (0, queries_1.$attr)(feature, (0, attributes_1._rootModule)())) === null || _c === void 0 ? void 0 : _c.$name()) + "." + feature.$name()}",`; }) .join("\n")}\n};\n\nexport enum MODELS {\n${models .map((model) => ` ${Case.pascal(model.$name())} = "${model.$name()}",`) .join("\n")}\n};\n\n`; const attrToStr = (attributes, tab = " ") => { if (attributes.length == 0) return `[]`; return ("[\n" + attributes .filter((a) => !["enum", "rootModule", "ref", "array"].includes(a.$name())) .map((a) => { let value = ""; if (a.$value() !== null) { if (a.$name() == "regex") { value = a.$value().toString(); } else value = JSON.stringify(a.$value(), null, 2) .split("\n") .map((line, i) => (i > 0 ? tab + " " : "") + line) .join("\n"); } return value ? `${tab}{\n${tab} name: "${a.$name()}"${",\n" + tab + " value: " + value}\n${tab}},` : `${tab}{ name: "${a.$name()}" },`; }) .join("\n") + `\n${tab.slice(2)}]`); }; const modelToStr = (model, tab = " ") => { let fields = ""; let enumValues = ""; let enumAttr = (0, queries_1.$attr)(model, (0, attributes_1._enum)()); let rootModule = (0, queries_1.$attr)(model, (0, attributes_1._rootModule)()); if (!enumAttr) { fields = `fields: [\n${model .$fields() .map((field) => { let ref = ""; const refAttr = (0, queries_1.$attr)(field, (0, attributes_1._ref)()); if (refAttr) { ref = `MODELS.${Case.pascal(refAttr.$name())}`; } return `${tab} {\n${tab} name: "${field.$name()}",\n${tab} isArray: ${(0, queries_1.$attr)(field, (0, attributes_1._array)())},\n${tab} type: "${field.$type()}",\n${ref ? `${tab} ref: ${ref},\n` : ""}${tab} attributes: ${attrToStr(field.$attributes(), tab + " ")},\n${tab} },`; }) .join("\n")}\n${tab} ]`; } else { enumValues = `enum: {\n${Object.keys(enumAttr) .map((k) => `${tab} "${k}": ${JSON.stringify(enumAttr[k])},`) .join("\n")}\n${tab} }`; } return `{\n${tab} name: "${model.$name()}",\n${!!rootModule ? `${tab} module: MODULES.${Case.pascal(rootModule.$name())},` : ""}\n${tab} attributes: ${attrToStr(model.$attributes(), tab + " ")},\n${tab} ${fields || enumValues},\n${tab}}`; }; const featureToStr = (feature, tab = " ") => { let input = ""; let output = ""; const modelInput = feature.$input(); const modelOutput = feature.$output(); const rootModule = (0, queries_1.$attr)(feature, (0, attributes_1._rootModule)()); if (modelInput) { input = `\n${tab} input: uModels["${modelInput.$name()}"],`; } if (modelOutput) { output = `\n${tab} output: uModels["${modelOutput.$name()}"],`; } return `{\n${tab} name: "${feature.$name()}",\n${!!rootModule ? `${tab} module: MODULES.${Case.pascal(rootModule.$name())},` : ""}\n${tab} attributes: ${attrToStr(feature.$attributes(), tab + " ")},${input}${output}\n }`; }; content += `export const uModel = (name: MODELS | string) =>\n uModels[name as keyof typeof uModels] as any as UModel;\n`; content += `export const uFeature = (name: FEATURES | string) =>\n uFeatures[name as keyof typeof uFeatures] as any as UFeature;\n`; content += `export const uModule = (name: MODULES | string) =>\n uModules[name as keyof typeof uModules] as any as UModule;\n`; content += `export const uAttribute = <Type>( src: { attributes: UAttribute[] }, name: string, defaultValue?: Type, defaultWhenNotPresent?: Type ): Type | null | undefined => { const attr = src.attributes.find((x) => x.name === name); if (attr) { if (attr.value !== undefined) return attr.value as Type; else return defaultValue ?? null; } return defaultWhenNotPresent ?? undefined; };\n\n`; content += `const uModels = {\n${models .map((model) => ` "${model.$name()}": ` + modelToStr(model, " ") + ",") .join("\n")}\n};\n\n`; content += `const uFeatures = {\n${features .map((feature) => { var _a; return ` "${((_a = (0, queries_1.$attr)(feature, (0, attributes_1._rootModule)())) === null || _a === void 0 ? void 0 : _a.$name()) + "." + feature.$name()}": ` + featureToStr(feature, " ") + ","; }) .join("\n")}\n};\n\n`; content += `const uModules = {\n${modules .map((mod) => ` "${mod.$name()}": {\n name: "${mod.$name()}",\n attributes: ${attrToStr(mod.$attributes(), " ")},\n features: {\n${mod .$features() .map((feature) => { var _a; return ` "${feature.$name()}": uFeatures["${((_a = (0, queries_1.$attr)(feature, (0, attributes_1._rootModule)())) === null || _a === void 0 ? void 0 : _a.$name()) + "." + feature.$name()}"],`; }) .join("\n")}\n },\n models: {\n${models .filter((model) => (0, queries_1.$attr)(model, (0, attributes_1._rootModule)()) == mod) .map((model) => ` ["${model.$name()}"]: uModels["${model.$name()}"],`) .join("\n")}\n },\n },`) .join("\n")}\n};\n\n`; content += `export const uDraft = {\n attributes: ${attrToStr(this.$draft().$attributes(), " ")},\n modules: uModules,\n features: uFeatures,\n models: uModels,\n};\n\n`; output.push({ key: KEYS.draft, content, }); return output; }); } } exports.default = TSDraftRenderer; //# sourceMappingURL=ts-draft-renderer.js.map