@frui.ts/generator
Advanced tools
Comparing version 1.0.0-rc.3 to 1.0.0-rc.4
#!/usr/bin/env node | ||
/* Frui.ts generator */ | ||
var re = Object.defineProperty; | ||
var se = (n, e, t) => e in n ? re(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t; | ||
var m = (n, e, t) => (se(n, typeof e != "symbol" ? e + "" : e, t), t); | ||
var ne = Object.defineProperty; | ||
var ie = (n, e, t) => e in n ? ne(n, e, { enumerable: !0, configurable: !0, writable: !0, value: t }) : n[e] = t; | ||
var m = (n, e, t) => (ie(n, typeof e != "symbol" ? e + "" : e, t), t); | ||
import { program as P } from "commander"; | ||
import { upperFirst as ne, camelCase as h, uniqBy as ie, uniq as z, groupBy as oe, isArray as ae } from "lodash-es"; | ||
import { Project as ce, IndentationText as pe, ts as V, ClassDeclaration as $, InterfaceDeclaration as W, FunctionTypeNode as le, ArrowFunction as ue } from "ts-morph"; | ||
import H from "fs"; | ||
import { upperFirst as oe, camelCase as h, uniqBy as ae, uniq as H, groupBy as ce, isArray as pe } from "lodash-es"; | ||
import { Project as le, IndentationText as ue, ts as W, ClassDeclaration as x, InterfaceDeclaration as G, FunctionTypeNode as me, ArrowFunction as he } from "ts-morph"; | ||
import K from "fs"; | ||
import T from "path"; | ||
import { SingleBar as me } from "cli-progress"; | ||
import { SingleBar as fe } from "cli-progress"; | ||
import R from "handlebars"; | ||
import { fileURLToPath as he } from "url"; | ||
import G from "@apidevtools/swagger-parser"; | ||
const fe = "999.0.0", ye = "Frui.ts code generator"; | ||
P.name("fruits-generate").version(fe).description(ye); | ||
import { fileURLToPath as ye } from "url"; | ||
import _ from "@apidevtools/swagger-parser"; | ||
const de = "999.0.0", ge = "Frui.ts code generator"; | ||
P.name("fruits-generate").version(de).description(ge); | ||
P.command("inversify").description("Generate Inversify configuration files").option("-p, --project <fileName>", "TS project file", "./tsconfig.json").option("-c, --config <fileName>", "Custom configuration file").option("-d, --debug", "Output extra debugging").option("--no-decorators", "Do not generate decorators file").option("--decorators-output <relativePath>", "Decorators output file path", "src/di.decorators.ts").option("--no-registry", "Do not generate registry file").option("--registry-output <relativePath>", "Registry output file path", "src/di.registry.ts").action(async (n) => { | ||
@@ -24,3 +24,3 @@ const e = { | ||
console.log("Processing", e.config), n.decorators && (e.decorators = { output: n.decoratorsOutput }), n.registry && (e.registry = { output: n.registryOutput }); | ||
const t = await Promise.resolve().then(() => Pe), r = new t.default(e); | ||
const t = await Promise.resolve().then(() => Re), r = new t.default(e); | ||
await r.init(), await r.run(); | ||
@@ -36,3 +36,3 @@ }); | ||
console.log("Processing", e.config); | ||
const t = await Promise.resolve().then(() => De), r = new t.default(e); | ||
const t = await Promise.resolve().then(() => Ie), r = new t.default(e); | ||
await r.init(), await r.run(); | ||
@@ -47,3 +47,3 @@ }); | ||
console.log("Processing", e.config); | ||
const t = await Promise.resolve().then(() => vt), r = new t.default(e); | ||
const t = await Promise.resolve().then(() => Ft), r = new t.default(e); | ||
await r.init(), await r.run(); | ||
@@ -55,9 +55,9 @@ }); | ||
function g(n) { | ||
return ne(h(n)); | ||
return oe(h(n)); | ||
} | ||
async function de(n) { | ||
const e = T.join(process.cwd(), n), t = await H.promises.readFile(e, "utf8"); | ||
async function be(n) { | ||
const e = T.join(process.cwd(), n), t = await K.promises.readFile(e, "utf8"); | ||
return JSON.parse(t); | ||
} | ||
function ge(n, e) { | ||
function we(n, e) { | ||
if (n === e) | ||
@@ -71,4 +71,4 @@ return "./"; | ||
} | ||
const be = "// WARNING: This file has been generated. Do not edit it manually, your changes might get lost."; | ||
function we(n, e) { | ||
const Oe = "// WARNING: This file has been generated. Do not edit it manually, your changes might get lost."; | ||
function Ee(n, e) { | ||
return { | ||
@@ -79,3 +79,3 @@ format: `${n.padEnd(e)} [{bar}] {percentage}% | ETA: {eta}s | {value}/{total}` | ||
function F(n, e = 0) { | ||
return new me(we(n, e)); | ||
return new fe(Ee(n, e)); | ||
} | ||
@@ -90,6 +90,6 @@ class b { | ||
const e = F("Parsing"); | ||
e.start(2, 0), this.project = new ce({ | ||
e.start(2, 0), this.project = new le({ | ||
tsConfigFilePath: this.params.project, | ||
manipulationSettings: { | ||
indentationText: pe.TwoSpaces | ||
indentationText: ue.TwoSpaces | ||
} | ||
@@ -99,3 +99,3 @@ }), e.increment(), this.params.debug && this.logDiagnostics(); | ||
if (this.params.config) { | ||
const r = await de(this.params.config); | ||
const r = await be(this.params.config); | ||
this.config = Object.assign({}, t, r); | ||
@@ -112,6 +112,6 @@ } else | ||
const r = e.getSourceFile(t); | ||
return !((s = r == null ? void 0 : r.getStatementByKind(V.SyntaxKind.SingleLineCommentTrivia | V.SyntaxKind.MultiLineCommentTrivia)) != null && s.getText().includes("generator:ignore")); | ||
return !((s = r == null ? void 0 : r.getStatementByKind(W.SyntaxKind.SingleLineCommentTrivia | W.SyntaxKind.MultiLineCommentTrivia)) != null && s.getText().includes("generator:ignore")); | ||
} | ||
writeGeneratedHeader(e) { | ||
e.insertText(0, (t) => t.writeLine(be)); | ||
e.insertText(0, (t) => t.writeLine(Oe)); | ||
} | ||
@@ -132,3 +132,3 @@ getDiagnostics() { | ||
} | ||
const Ee = "Factory", ve = [ | ||
const ve = "Factory", Fe = [ | ||
{ | ||
@@ -155,7 +155,7 @@ pattern: "Service$", | ||
} | ||
], Fe = { | ||
factoryName: Ee, | ||
rules: ve | ||
], je = { | ||
factoryName: ve, | ||
rules: Fe | ||
}; | ||
class Oe { | ||
class Te { | ||
analyze(e, t) { | ||
@@ -166,3 +166,3 @@ const r = []; | ||
const a = i[0]; | ||
if (a instanceof $ && !a.isAbstract()) { | ||
if (a instanceof x && !a.isAbstract()) { | ||
const c = a.getName(); | ||
@@ -178,3 +178,3 @@ if (c) { | ||
}); | ||
}), ie(r, (s) => s.declaration); | ||
}), ae(r, (s) => s.declaration); | ||
} | ||
@@ -192,3 +192,3 @@ } | ||
} | ||
function A(n) { | ||
function I(n) { | ||
let e = n.getSymbol() ?? n.getAliasSymbol(); | ||
@@ -203,3 +203,3 @@ if (n.isUnion()) { | ||
} | ||
class je { | ||
class Pe { | ||
constructor(e, t, r) { | ||
@@ -231,3 +231,3 @@ m(this, "decorators"); | ||
else { | ||
const s = K(e); | ||
const s = J(e); | ||
this.registerConstructor(e, t, s); | ||
@@ -282,3 +282,3 @@ } | ||
a.isArray() && (a = a.getArrayElementTypeOrThrow(), c = !0); | ||
const p = A(a); | ||
const p = I(a); | ||
if (p) { | ||
@@ -297,3 +297,3 @@ const u = this.getConstructorParameterDecorator( | ||
const i = s ? "multiInject" : "inject"; | ||
if (r instanceof $) { | ||
if (r instanceof x) { | ||
const o = v(r, this.decoratorsFile); | ||
@@ -307,3 +307,3 @@ return { | ||
} | ||
if (r instanceof W) | ||
if (r instanceof G) | ||
return { | ||
@@ -315,5 +315,5 @@ importStatements: [], | ||
}; | ||
if (r instanceof le) { | ||
const o = A(r.getReturnType()); | ||
if (o instanceof $ || o instanceof W) | ||
if (r instanceof me) { | ||
const o = I(r.getReturnType()); | ||
if (o instanceof x || o instanceof G) | ||
return { | ||
@@ -326,5 +326,5 @@ importStatements: [], | ||
} | ||
if (r instanceof ue) { | ||
const o = A(r.getReturnType()); | ||
if (o instanceof $) { | ||
if (r instanceof he) { | ||
const o = I(r.getReturnType()); | ||
if (o instanceof x) { | ||
const a = this.factoryName && o.getStaticMethod(this.factoryName); | ||
@@ -344,3 +344,3 @@ if (a) { | ||
} | ||
function K(n) { | ||
function J(n) { | ||
const e = n.getConstructors()[0]; | ||
@@ -350,5 +350,5 @@ if (e) | ||
const t = n.getBaseClass(); | ||
return t ? K(t) : void 0; | ||
return t ? J(t) : void 0; | ||
} | ||
class Te extends b { | ||
class Se extends b { | ||
constructor() { | ||
@@ -363,5 +363,5 @@ super(...arguments); | ||
t.start(4, 0); | ||
const r = this.parseRules(), s = new Oe().analyze(this.project, r); | ||
const r = this.parseRules(), s = new Te().analyze(this.project, r); | ||
t.increment(); | ||
const i = new je( | ||
const i = new Pe( | ||
this.ensureDecoratorsFile(), | ||
@@ -386,3 +386,3 @@ this.ensureRegistryFile(), | ||
getDefaultConfig() { | ||
return Promise.resolve(Fe); | ||
return Promise.resolve(je); | ||
} | ||
@@ -417,9 +417,9 @@ ensureDecoratorsFile() { | ||
} | ||
const Pe = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({ | ||
const Re = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({ | ||
__proto__: null, | ||
default: Te | ||
}, Symbol.toStringTag, { value: "Module" })), Se = "**/views/**/*.tsx", Re = { | ||
viewsPattern: Se | ||
default: Se | ||
}, Symbol.toStringTag, { value: "Module" })), $e = "**/views/**/*.tsx", xe = { | ||
viewsPattern: $e | ||
}; | ||
class $e { | ||
class Ne { | ||
analyze(e, t) { | ||
@@ -432,3 +432,3 @@ const r = []; | ||
} | ||
class xe { | ||
class De { | ||
constructor(e) { | ||
@@ -446,3 +446,3 @@ this.targetFile = e; | ||
} | ||
class Ne extends b { | ||
class Ae extends b { | ||
constructor() { | ||
@@ -455,5 +455,5 @@ super(...arguments); | ||
t.start(5, 0); | ||
const r = new $e().analyze(this.project, this.config.viewsPattern); | ||
const r = new Ne().analyze(this.project, this.config.viewsPattern); | ||
t.increment(); | ||
const i = new xe(this.ensureViewsFile()).process(r); | ||
const i = new De(this.ensureViewsFile()).process(r); | ||
t.increment(), this.viewsFile.addImportDeclarations(i.importStatements), t.increment(), this.viewsFile.addStatements(i.statements), t.increment(), await this.saveFile(this.viewsFile), t.increment(), t.stop(); | ||
@@ -465,9 +465,9 @@ } | ||
getDefaultConfig() { | ||
return Promise.resolve(Re); | ||
return Promise.resolve(xe); | ||
} | ||
} | ||
const De = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({ | ||
const Ie = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({ | ||
__proto__: null, | ||
default: Ne | ||
}, Symbol.toStringTag, { value: "Module" })), Ae = "https://fruits-demo.herokuapp.com/api/swagger-json", Ie = "src/entities", qe = "src/repositories", Ce = !0, Le = { | ||
default: Ae | ||
}, Symbol.toStringTag, { value: "Module" })), qe = "https://fruits-demo.herokuapp.com/api/swagger-json", Ce = "src/entities", Le = "src/repositories", Be = !0, Me = { | ||
entities: { | ||
@@ -492,7 +492,7 @@ EnumValue: !1, | ||
} | ||
}, Be = "enum", Me = "native", Ve = { | ||
}, Ve = "enum", We = "native", Ge = { | ||
rules: { | ||
readOnly: !1 | ||
} | ||
}, We = "/api/", Ge = "class-transformer", _e = { | ||
}, _e = "/api/", ke = "class-transformer", Ue = { | ||
enumEntity: "@enumEntity.hbs", | ||
@@ -510,17 +510,17 @@ enumEntityFile: "@enumEntityFile.hbs", | ||
unionEntityFile: "@unionEntityFile.hbs" | ||
}, ke = !1, Ue = { | ||
api: Ae, | ||
entitiesPath: Ie, | ||
repositoriesPath: qe, | ||
conversion: Ce, | ||
observable: Le, | ||
enums: Be, | ||
dates: Me, | ||
validations: Ve, | ||
endpointUrlPrefix: We, | ||
templatesFolder: Ge, | ||
templates: _e, | ||
optionalAsNullable: ke | ||
}, ze = !1, He = { | ||
api: qe, | ||
entitiesPath: Ce, | ||
repositoriesPath: Le, | ||
conversion: Be, | ||
observable: Me, | ||
enums: Ve, | ||
dates: We, | ||
validations: Ge, | ||
endpointUrlPrefix: _e, | ||
templatesFolder: ke, | ||
templates: Ue, | ||
optionalAsNullable: ze | ||
}; | ||
class w { | ||
class O { | ||
constructor(e) { | ||
@@ -531,3 +531,3 @@ m(this, "externalName"); | ||
} | ||
class D extends w { | ||
class A extends O { | ||
constructor(e, t) { | ||
@@ -537,3 +537,3 @@ super(e), this.items = t; | ||
} | ||
class y extends w { | ||
class y extends O { | ||
constructor(e, t) { | ||
@@ -547,3 +547,3 @@ super(e), this.properties = t; | ||
} | ||
class L extends y { | ||
class B extends y { | ||
constructor(e, t, r) { | ||
@@ -553,3 +553,3 @@ super(e, r), this.baseEntities = t; | ||
} | ||
class B extends w { | ||
class M extends O { | ||
constructor(e, t) { | ||
@@ -559,3 +559,3 @@ super(e), this.entities = t; | ||
} | ||
class ze { | ||
class Ke { | ||
constructor(e, t) { | ||
@@ -590,5 +590,5 @@ this.parentDirectory = e, this.templates = t; | ||
} | ||
var J = /* @__PURE__ */ ((n) => (n[n.required = 0] = "required", n[n.number = 1] = "number", n[n.date = 2] = "date", n[n.maxLength = 3] = "maxLength", n[n.minLength = 4] = "minLength", n[n.pattern = 5] = "pattern", n[n.nullable = 6] = "nullable", n[n.readOnly = 7] = "readOnly", n[n.format = 8] = "format", n))(J || {}); | ||
const l = J; | ||
class Q { | ||
var Q = /* @__PURE__ */ ((n) => (n[n.required = 0] = "required", n[n.number = 1] = "number", n[n.date = 2] = "date", n[n.maxLength = 3] = "maxLength", n[n.minLength = 4] = "minLength", n[n.pattern = 5] = "pattern", n[n.nullable = 6] = "nullable", n[n.readOnly = 7] = "readOnly", n[n.format = 8] = "format", n))(Q || {}); | ||
const l = Q; | ||
class X { | ||
constructor(e) { | ||
@@ -605,3 +605,3 @@ m(this, "globalExcludedProperties"); | ||
} | ||
const N = class N extends Q { | ||
const D = class D extends X { | ||
constructor(e) { | ||
@@ -619,9 +619,9 @@ super(typeof e == "object" ? e : void 0), this.config = e; | ||
const i = t.includes(s.name), o = (r = s.restrictions) == null ? void 0 : r.has(l.readOnly); | ||
!i && !o && s.addTag(N.OBSERVABLE, !0); | ||
!i && !o && s.addTag(D.OBSERVABLE, !0); | ||
} | ||
} | ||
}; | ||
m(N, "OBSERVABLE", Symbol("observable")); | ||
let x = N; | ||
class d extends w { | ||
m(D, "OBSERVABLE", Symbol("observable")); | ||
let N = D; | ||
class d extends O { | ||
constructor(e, t, r = !1) { | ||
@@ -631,3 +631,3 @@ super(e), this.referencedEntity = t, this.isArray = r; | ||
} | ||
class He { | ||
class Je { | ||
constructor(e, t, r) { | ||
@@ -660,3 +660,3 @@ this.parentDirectory = e, this.config = t, this.templates = r; | ||
r == null || r.forEach((a) => s.push(a.name)); | ||
const i = z(s).sort().map( | ||
const i = H(s).sort().map( | ||
(a) => this.templates.entityImport({ | ||
@@ -690,3 +690,3 @@ entity: a, | ||
example: s.example, | ||
isObservable: (a = s.tags) == null ? void 0 : a.get(x.OBSERVABLE), | ||
isObservable: (a = s.tags) == null ? void 0 : a.get(N.OBSERVABLE), | ||
conversions: this.config.conversion !== !1 && this.getPropertyTypeConversions(s), | ||
@@ -701,4 +701,4 @@ readonly: (c = s.restrictions) == null ? void 0 : c.has(l.readOnly), | ||
}), | ||
validationEntity: this.config.validations !== !1 && _(e) && {}, | ||
useBaseClassValidation: !!(t != null && t.some((s) => _(s))) | ||
validationEntity: this.config.validations !== !1 && k(e) && {}, | ||
useBaseClassValidation: !!(t != null && t.some((s) => k(s))) | ||
}; | ||
@@ -710,3 +710,3 @@ return this.templates.objectEntityContent(r); | ||
let t = e.type.type; | ||
t instanceof d && (t = t.referencedEntity.type), !(t instanceof D) && (typeof t == "object" && (yield `@Type(() => ${e.type.getTypeName()})`), t === "Date" && (this.config.dates === "date-fns" ? (yield "@Transform(value => (value ? new Date(value) : undefined), { toClassOnly: true })", ((r = e.restrictions) == null ? void 0 : r.get(l.format)) === "date" ? yield '@Transform(value => (value ? formatISO(value, { representation: "date" }) : undefined), { toPlainOnly: true })' : yield "@Transform(value => (value ? formatISO(value) : undefined), { toPlainOnly: true })") : yield "@Type(() => Date)")); | ||
t instanceof d && (t = t.referencedEntity.type), !(t instanceof A) && (typeof t == "object" && (yield `@Type(() => ${e.type.getTypeName()})`), t === "Date" && (this.config.dates === "date-fns" ? (yield "@Transform(value => (value ? new Date(value) : undefined), { toClassOnly: true })", ((r = e.restrictions) == null ? void 0 : r.get(l.format)) === "date" ? yield '@Transform(value => (value ? formatISO(value, { representation: "date" }) : undefined), { toPlainOnly: true })' : yield "@Transform(value => (value ? formatISO(value) : undefined), { toPlainOnly: true })") : yield "@Type(() => Date)")); | ||
} | ||
@@ -735,6 +735,6 @@ getPropertyValidations(e) { | ||
const i = JSON.stringify(t); | ||
return Qe(s, i) ? `${Ke(s) ?? r}: ${i}` : void 0; | ||
return Ye(s, i) ? `${Qe(s) ?? r}: ${i}` : void 0; | ||
} | ||
} | ||
function _(n) { | ||
function k(n) { | ||
return n.properties.some((e) => { | ||
@@ -745,3 +745,3 @@ var t; | ||
} | ||
function Ke(n) { | ||
function Qe(n) { | ||
if (typeof n == "string") | ||
@@ -752,14 +752,14 @@ return n; | ||
} | ||
const k = /* @__PURE__ */ new Map(); | ||
function Je(n) { | ||
const e = k.get(n); | ||
const U = /* @__PURE__ */ new Map(); | ||
function Xe(n) { | ||
const e = U.get(n); | ||
if (e) | ||
return e; | ||
const t = new RegExp(n); | ||
return k.set(n, t), t; | ||
return U.set(n, t), t; | ||
} | ||
function Qe(n, e) { | ||
return typeof n == "object" && n.filter ? !!Je(n.filter).exec(e) : !0; | ||
function Ye(n, e) { | ||
return typeof n == "object" && n.filter ? !!Xe(n.filter).exec(e) : !0; | ||
} | ||
class Xe { | ||
class Ze { | ||
constructor(e, t, r) { | ||
@@ -790,6 +790,6 @@ this.parentDirectory = e, this.config = t, this.templates = r; | ||
getRequiredImports(e) { | ||
return z( | ||
return H( | ||
e.flatMap((t) => { | ||
var r, s; | ||
return [t.queryParam, (r = t.requestBody) == null ? void 0 : r.typeReference, (s = U(t)) == null ? void 0 : s.typeReference]; | ||
return [t.queryParam, (r = t.requestBody) == null ? void 0 : r.typeReference, (s = z(t)) == null ? void 0 : s.typeReference]; | ||
}).filter((t) => !!t && t.isImportRequired) | ||
@@ -803,5 +803,5 @@ ).map((t) => { | ||
var s; | ||
const t = U(e), r = { | ||
const t = z(e), r = { | ||
action: e, | ||
path: Ye(e), | ||
path: et(e), | ||
parameters: this.getMethodParameters(e), | ||
@@ -826,3 +826,3 @@ requestContentType: (s = e.requestBody) == null ? void 0 : s.contentType, | ||
} | ||
function U(n) { | ||
function z(n) { | ||
if (!n.responses) | ||
@@ -833,3 +833,3 @@ return; | ||
} | ||
function Ye(n) { | ||
function et(n) { | ||
let e = n.path; | ||
@@ -840,3 +840,3 @@ for (const t of n.pathParams) | ||
} | ||
class Ze { | ||
class tt { | ||
constructor(e, t) { | ||
@@ -871,3 +871,3 @@ this.parentDirectory = e, this.templates = t; | ||
} | ||
class et { | ||
class rt { | ||
constructor(e, t) { | ||
@@ -921,3 +921,3 @@ this.parentDirectory = e, this.templates = t; | ||
} | ||
class tt { | ||
class st { | ||
constructor(e, t, r) { | ||
@@ -956,12 +956,12 @@ this.project = e, this.config = r, this.initTemplates(); | ||
R.registerPartial("generatedEntityHeader", await this.readTemplate("generatedEntityHeader")); | ||
const i = this.project.createDirectory(this.config.entitiesPath), o = this.config.enums === "enum" ? new ze(i, s) : new Ze(i, s), a = new He(i, this.config, s), c = new et(i, s); | ||
const i = this.project.createDirectory(this.config.entitiesPath), o = this.config.enums === "enum" ? new Ke(i, s) : new tt(i, s), a = new Je(i, this.config, s), c = new rt(i, s); | ||
t.increment(1); | ||
for (const { type: p } of e) { | ||
if (p instanceof D) | ||
if (p instanceof A) | ||
o.write(p); | ||
else if (p instanceof L) { | ||
else if (p instanceof B) { | ||
const u = p.baseEntities.map((E) => E.type).filter((E) => E instanceof y); | ||
a.write(p, u); | ||
} else | ||
p instanceof y ? a.write(p) : p instanceof B && c.write(p); | ||
p instanceof y ? a.write(p) : p instanceof M && c.write(p); | ||
t.increment(); | ||
@@ -974,3 +974,3 @@ } | ||
return; | ||
const t = Object.entries(oe(e, (c) => c.repository)), r = F("Generating repositories"), s = Math.ceil(t.length * 0.1 + 1); | ||
const t = Object.entries(ce(e, (c) => c.repository)), r = F("Generating repositories"), s = Math.ceil(t.length * 0.1 + 1); | ||
r.start(1 + t.length + s, 0); | ||
@@ -980,6 +980,6 @@ const i = { | ||
repositoryFile: await this.readTemplate("repositoryFile") | ||
}, o = this.project.createDirectory(this.config.repositoriesPath), a = new Xe( | ||
}, o = this.project.createDirectory(this.config.repositoriesPath), a = new Ze( | ||
o, | ||
{ | ||
entitiesRelativePath: ge(this.config.repositoriesPath, this.config.entitiesPath) | ||
entitiesRelativePath: we(this.config.repositoriesPath, this.config.entitiesPath) | ||
}, | ||
@@ -997,3 +997,3 @@ i | ||
return R.compile(`// missing template ${e}`); | ||
const r = this.getFullPath(t), s = await H.promises.readFile(r); | ||
const r = this.getFullPath(t), s = await K.promises.readFile(r); | ||
return R.compile(s.toString()); | ||
@@ -1004,3 +1004,3 @@ } | ||
const t = "./openapi/templates/", r = T.join(t, this.config.templatesFolder ?? ".", "/"); | ||
return T.resolve(st, e.replace("@", r)); | ||
return T.resolve(it, e.replace("@", r)); | ||
} else | ||
@@ -1010,4 +1010,4 @@ return T.resolve(process.cwd(), e); | ||
} | ||
const rt = he(import.meta.url), st = T.dirname(rt); | ||
class nt { | ||
const nt = ye(import.meta.url), it = T.dirname(nt); | ||
class ot { | ||
constructor(e) { | ||
@@ -1025,3 +1025,3 @@ this.config = e; | ||
} | ||
class X { | ||
class Y { | ||
constructor(e) { | ||
@@ -1031,24 +1031,24 @@ this.aliases = e; | ||
formatNames(e) { | ||
e.type instanceof y ? this.formatEntity(e.type) : e.type && typeof e.type != "string" && O(g, e.type); | ||
e.type instanceof y ? this.formatEntity(e.type) : e.type && typeof e.type != "string" && j(g, e.type); | ||
} | ||
formatEntity(e) { | ||
var r; | ||
O(g, e); | ||
j(g, e); | ||
const t = (r = this.aliases) == null ? void 0 : r[e.name]; | ||
t && (e.name = t); | ||
for (const s of e.properties) | ||
O(h, s); | ||
j(h, s); | ||
} | ||
static toPascalCase(e) { | ||
O(g, e); | ||
j(g, e); | ||
} | ||
static toCamelCase(e) { | ||
O(h, e); | ||
j(h, e); | ||
} | ||
} | ||
function O(n, e) { | ||
function j(n, e) { | ||
const t = n(e.name); | ||
t !== e.name && (e.externalName = e.name, e.name = t); | ||
} | ||
class it extends Q { | ||
class at extends X { | ||
constructor(e) { | ||
@@ -1080,22 +1080,22 @@ super(typeof e == "object" ? e : void 0), this.config = e; | ||
} | ||
function ot(n) { | ||
function ct(n) { | ||
return n.swagger === "2.0"; | ||
} | ||
function at(n) { | ||
function pt(n) { | ||
var e; | ||
return !!((e = n.openapi) != null && e.startsWith("3")); | ||
} | ||
function j(n) { | ||
function w(n) { | ||
return !!n.$ref; | ||
} | ||
function I(n) { | ||
function q(n) { | ||
return !!n.$ref; | ||
} | ||
function ct(n) { | ||
function lt(n) { | ||
return !!n.type; | ||
} | ||
function pt(n) { | ||
function ut(n) { | ||
return !n.$ref; | ||
} | ||
class Y extends w { | ||
class Z extends O { | ||
constructor(t, r) { | ||
@@ -1117,3 +1117,3 @@ super(t); | ||
} | ||
class Z extends w { | ||
class ee extends O { | ||
constructor(e, t) { | ||
@@ -1123,3 +1123,3 @@ super(e), this.fullName = t; | ||
} | ||
class ee { | ||
class te { | ||
constructor(e) { | ||
@@ -1143,3 +1143,3 @@ this.type = e; | ||
} | ||
class lt { | ||
class mt { | ||
constructor(e) { | ||
@@ -1154,7 +1154,7 @@ m(this, "types", /* @__PURE__ */ new Map()); | ||
const r = this.parseSchemaObject(e, t); | ||
I(t) && this.setTypeReference(e, new d(e, r)); | ||
q(t) && this.setTypeReference(e, new d(e, r)); | ||
} | ||
} | ||
parseSchemaObject(e, t) { | ||
if (I(t)) | ||
if (q(t)) | ||
return this.parseReferenceObject(t); | ||
@@ -1165,3 +1165,3 @@ if (t.enum) | ||
case "array": { | ||
if (ae(t.items)) | ||
if (pe(t.items)) | ||
throw new Error("Multiple 'items' types in array are not supported yet."); | ||
@@ -1197,7 +1197,7 @@ if (!t.items) | ||
parseReferenceObject(e) { | ||
const t = ut(e.$ref), r = mt(e.$ref) ? void 0 : new Z(t, e.$ref); | ||
const t = ht(e.$ref), r = ft(e.$ref) ? void 0 : new ee(t, e.$ref); | ||
return this.setTypeReference(t, r); | ||
} | ||
parseEnum(e, t) { | ||
const r = t.enum ? new D(e, t.enum) : void 0; | ||
const r = t.enum ? new A(e, t.enum) : void 0; | ||
return this.setTypeReference(e, r); | ||
@@ -1210,6 +1210,6 @@ } | ||
const r = t.allOf, s = r.filter( | ||
(c) => !I(c) && c.type === "object" && !c.allOf && !c.oneOf | ||
(c) => !q(c) && c.type === "object" && !c.allOf && !c.oneOf | ||
); | ||
t.properties && s.push(t); | ||
const i = r.filter((c) => !s.includes(c)).map((c, p) => this.parseSchemaObject(`${e}Parent${p + 1}`, c)), o = s.flatMap((c) => this.extractObjectProperties(e, c)), a = new L(e, i, o); | ||
const i = r.filter((c) => !s.includes(c)).map((c, p) => this.parseSchemaObject(`${e}Parent${p + 1}`, c)), o = s.flatMap((c) => this.extractObjectProperties(e, c)), a = new B(e, i, o); | ||
return s.forEach( | ||
@@ -1225,3 +1225,3 @@ (c) => { | ||
(o, a) => this.parseSchemaObject(`${e}Option${a + 1}`, o) | ||
), i = new B(e, s); | ||
), i = new M(e, s); | ||
return this.setTypeReference(e, i); | ||
@@ -1238,4 +1238,4 @@ } | ||
parseEntityProperty(e, t, r) { | ||
const s = e + g(t), i = this.parseSchemaObject(s, r), o = new Y(t, i); | ||
return pt(r) && (o.description = r.description, o.example = r.example, r.maxLength && o.addRestriction(l.maxLength, r.maxLength), r.minLength && o.addRestriction(l.minLength, r.minLength), r.pattern && o.addRestriction(l.pattern, r.pattern), r.format && o.addRestriction(l.format, r.format)), o; | ||
const s = e + g(t), i = this.parseSchemaObject(s, r), o = new Z(t, i); | ||
return ut(r) && (o.description = r.description, o.example = r.example, r.maxLength && o.addRestriction(l.maxLength, r.maxLength), r.minLength && o.addRestriction(l.minLength, r.minLength), r.pattern && o.addRestriction(l.pattern, r.pattern), r.format && o.addRestriction(l.format, r.format)), o; | ||
} | ||
@@ -1246,15 +1246,15 @@ setTypeReference(e, t) { | ||
return r.type = r.type ?? t, r; | ||
const s = new ee(t); | ||
const s = new te(t); | ||
return this.types.set(e, s), s; | ||
} | ||
} | ||
const q = "#/definitions/"; | ||
function ut(n) { | ||
const e = n.indexOf(q, 0); | ||
return e >= 0 ? n.substring(e + q.length) : n; | ||
const C = "#/definitions/"; | ||
function ht(n) { | ||
const e = n.indexOf(C, 0); | ||
return e >= 0 ? n.substring(e + C.length) : n; | ||
} | ||
function mt(n) { | ||
return n.startsWith(q); | ||
function ft(n) { | ||
return n.startsWith(C); | ||
} | ||
class ht extends w { | ||
class yt extends O { | ||
constructor(t) { | ||
@@ -1272,3 +1272,3 @@ super(t); | ||
} | ||
class ft { | ||
class dt { | ||
constructor(e, t) { | ||
@@ -1284,3 +1284,3 @@ m(this, "types", /* @__PURE__ */ new Map()); | ||
const s = this.parseSchemaObject(t, r); | ||
j(r) && this.setTypeReference(t, new d(t, s)); | ||
w(r) && this.setTypeReference(t, new d(t, s)); | ||
} | ||
@@ -1290,15 +1290,16 @@ this.parsePaths(this.apiDocument.paths); | ||
parseSchemaObject(e, t) { | ||
if (console.debug("Parsing object", e), j(t)) | ||
return this.parseReferenceObject(t); | ||
if (t.enum) | ||
return this.parseEnum(e, t); | ||
switch (t.type) { | ||
const r = this.fixSchemaObject(t); | ||
if (console.debug("Parsing object", e), w(r)) | ||
return this.parseReferenceObject(r); | ||
if (r.enum) | ||
return this.parseEnum(e, r); | ||
switch (r.type) { | ||
case "array": { | ||
const r = `${e}Item`, s = this.parseSchemaObject(r, t.items), i = new d(e, s, !0); | ||
return this.setTypeReference(e, i); | ||
const s = `${e}Item`, i = this.parseSchemaObject(s, r.items), o = new d(e, i, !0); | ||
return this.setTypeReference(e, o); | ||
} | ||
case "object": | ||
return this.parseObject(e, t); | ||
return this.parseObject(e, r); | ||
case "string": | ||
switch (t.format) { | ||
switch (r.format) { | ||
case "binary": | ||
@@ -1317,14 +1318,48 @@ return this.setTypeReference(e, "Blob"); | ||
case "boolean": | ||
return this.setTypeReference(e, t.type); | ||
return this.setTypeReference(e, r.type); | ||
} | ||
if (t.oneOf || t.anyOf || t.allOf) | ||
return this.parseObject(e, t); | ||
if (r.oneOf || r.anyOf || r.allOf) | ||
return this.parseObject(e, r); | ||
throw new Error(`Could not parse object '${e}'`); | ||
} | ||
// eslint-disable-next-line sonarjs/cognitive-complexity | ||
fixSchemaObject(e) { | ||
var t, r; | ||
if (w(e)) | ||
return e; | ||
if (re(e.type)) | ||
return { ...e, type: "object", nullable: !0 }; | ||
if ((t = e.oneOf) != null && t.some((s) => $(s))) { | ||
e.nullable = !0; | ||
const s = e.oneOf.filter((i) => !$(i)); | ||
if (s.length > 1) | ||
e.oneOf = s; | ||
else | ||
return { ...e, ...s[0] }; | ||
} | ||
if ((r = e.anyOf) != null && r.some((s) => $(s))) { | ||
e.nullable = !0; | ||
const s = e.anyOf.filter((i) => !$(i)); | ||
if (s.length > 1) | ||
e.anyOf = s; | ||
else | ||
return { ...e, ...s[0] }; | ||
} | ||
if (Array.isArray(e.type) && e.type.length > 0) { | ||
let s = e.type; | ||
if (e.type.includes("null") && (e.nullable = !0, s = s.filter((i) => i !== "null")), s.length == 1) | ||
e.type = s[0]; | ||
else | ||
return { | ||
oneOf: s.map((i) => ({ ...e, type: i })) | ||
}; | ||
} | ||
return e; | ||
} | ||
parseReferenceObject(e) { | ||
const t = yt(e.$ref), r = dt(e.$ref) ? void 0 : new Z(t, e.$ref); | ||
const t = gt(e.$ref), r = bt(e.$ref) ? void 0 : new ee(t, e.$ref); | ||
return this.setTypeReference(t, r); | ||
} | ||
parseEnum(e, t) { | ||
const r = t.enum ? new D(e, t.enum) : void 0; | ||
const r = t.enum ? new A(e, t.enum) : void 0; | ||
return this.setTypeReference(e, r); | ||
@@ -1337,6 +1372,6 @@ } | ||
const r = t.allOf, s = r.filter( | ||
(c) => !j(c) && c.type === "object" && !c.allOf && !c.oneOf | ||
(c) => !w(c) && c.type === "object" && !c.allOf && !c.oneOf | ||
); | ||
t.properties && s.push(t); | ||
const i = r.filter((c) => !s.includes(c)).map((c, p) => this.parseSchemaObject(`${e}Parent${p + 1}`, c)), o = s.flatMap((c) => this.extractObjectProperties(e, c)), a = new L(e, i, o); | ||
const i = r.filter((c) => !s.includes(c)).map((c, p) => this.parseSchemaObject(`${e}Parent${p + 1}`, c)), o = s.flatMap((c) => this.extractObjectProperties(e, c)), a = new B(e, i, o); | ||
return s.forEach( | ||
@@ -1350,3 +1385,3 @@ (c) => { | ||
parseOneOfObject(e, t) { | ||
const s = t.oneOf.map((o, a) => this.parseSchemaObject(`${e}Option${a + 1}`, o)), i = new B(e, s); | ||
const s = t.oneOf.map((o, a) => this.parseSchemaObject(`${e}Option${a + 1}`, o)), i = new M(e, s); | ||
return this.setTypeReference(e, i); | ||
@@ -1363,4 +1398,4 @@ } | ||
parseEntityProperty(e, t, r) { | ||
const s = e + g(t), i = this.parseSchemaObject(s, r), o = new Y(t, i); | ||
return ct(r) && (o.description = r.description, o.example = r.example, r.maxLength && o.addRestriction(l.maxLength, r.maxLength), r.minLength && o.addRestriction(l.minLength, r.minLength), r.pattern && o.addRestriction(l.pattern, r.pattern), r.nullable !== void 0 && o.addRestriction(l.nullable, r.nullable), r.readOnly && o.addRestriction(l.readOnly, r.readOnly), r.format && o.addRestriction(l.format, r.format)), o; | ||
const s = e + g(t), i = this.parseSchemaObject(s, r), o = new Z(t, i); | ||
return lt(r) && (o.description = r.description, o.example = r.example, r.maxLength && o.addRestriction(l.maxLength, r.maxLength), r.minLength && o.addRestriction(l.minLength, r.minLength), r.pattern && o.addRestriction(l.pattern, r.pattern), r.nullable !== void 0 && o.addRestriction(l.nullable, r.nullable), r.readOnly && o.addRestriction(l.readOnly, r.readOnly), r.format && o.addRestriction(l.format, r.format)), o; | ||
} | ||
@@ -1371,3 +1406,3 @@ setTypeReference(e, t) { | ||
return r.type = r.type ?? t, r; | ||
const s = new ee(t); | ||
const s = new te(t); | ||
return this.types.set(e, s), s; | ||
@@ -1382,10 +1417,10 @@ } | ||
})) | ||
).filter((t) => gt(t.method)).map((t) => this.parseEndpoint(t)); | ||
).filter((t) => wt(t.method)).map((t) => this.parseEndpoint(t)); | ||
} | ||
parseEndpoint({ path: e, method: t, action: r }) { | ||
var c, p, u, E, M; | ||
var c, p, u, E, V; | ||
console.debug("Parsing endpoint", t, e), e = (c = this.config) != null && c.endpointUrlPrefix ? e.replace(this.config.endpointUrlPrefix, "") : e; | ||
const s = r.operationId ?? h(t + "-" + e.replace(/\{(\D*?)\}/, "By-$1")), i = new ht(s); | ||
const s = r.operationId ?? h(t + "-" + e.replace(/\{(\D*?)\}/, "By-$1")), i = new yt(s); | ||
i.path = e, i.method = t, i.repository = ((p = r.tags) == null ? void 0 : p[0]) ?? "General", i.description = ((u = r.description) == null ? void 0 : u.trim()) ?? ((E = r.summary) == null ? void 0 : E.trim()); | ||
const o = ((M = r.parameters) == null ? void 0 : M.filter( | ||
const o = ((V = r.parameters) == null ? void 0 : V.filter( | ||
(f) => !!f.schema | ||
@@ -1395,3 +1430,3 @@ )) ?? []; | ||
const S = this.parseEndpointParameter(s, f); | ||
return X.toCamelCase(S), S; | ||
return Y.toCamelCase(S), S; | ||
}); | ||
@@ -1404,8 +1439,8 @@ const a = o.filter((f) => f.in === "query").map((f) => this.parseEndpointParameter(s, f)); | ||
return r.requestBody && (i.requestBody = this.getRequestBodyType(s + "Request", r.requestBody)), i.responses = {}, r.responses && Object.entries(r.responses).forEach(([f, S]) => { | ||
const te = this.getResponseBodyType(`${s}Response${f}`, S); | ||
i.responses[f] = te; | ||
const se = this.getResponseBodyType(`${s}Response${f}`, S); | ||
i.responses[f] = se; | ||
}), i; | ||
} | ||
parseEndpointParameter(e, t) { | ||
const r = this.parseEntityProperty(e, bt(t.name), t.schema); | ||
const r = this.parseEntityProperty(e, Ot(t.name), t.schema); | ||
return r.description = t.description, t.required && r.addRestriction(l.required, !0), r; | ||
@@ -1415,3 +1450,3 @@ } | ||
var r, s; | ||
if (j(t)) { | ||
if (w(t)) { | ||
const i = t.$ref.substr(t.$ref.lastIndexOf("/") + 1), o = (s = (r = this.apiDocument.components) == null ? void 0 : r.requestBodies) == null ? void 0 : s[i]; | ||
@@ -1426,3 +1461,3 @@ return o ? this.getRequestBodyType(i, o) : void 0; | ||
var r, s, i; | ||
if (j(t)) { | ||
if (w(t)) { | ||
const o = t.$ref.substr(t.$ref.lastIndexOf("/") + 1), a = (s = (r = this.apiDocument.components) == null ? void 0 : r.responses) == null ? void 0 : s[o]; | ||
@@ -1438,11 +1473,11 @@ return a ? this.getResponseBodyType(o, a) : void 0; | ||
} | ||
const C = "#/components/schemas/"; | ||
function yt(n) { | ||
const e = n.indexOf(C, 0); | ||
return e >= 0 ? n.substring(e + C.length) : n; | ||
const L = "#/components/schemas/"; | ||
function gt(n) { | ||
const e = n.indexOf(L, 0); | ||
return e >= 0 ? n.substring(e + L.length) : n; | ||
} | ||
function dt(n) { | ||
return n.startsWith(C); | ||
function bt(n) { | ||
return n.startsWith(L); | ||
} | ||
function gt(n) { | ||
function wt(n) { | ||
switch (n) { | ||
@@ -1462,10 +1497,16 @@ case "get": | ||
} | ||
function bt(n) { | ||
function Ot(n) { | ||
return n.endsWith("[]") ? n.substring(0, n.length - 2) : n; | ||
} | ||
class wt { | ||
function re(n) { | ||
return n === "null"; | ||
} | ||
function $(n) { | ||
return !w(n) && re(n.type); | ||
} | ||
class Et { | ||
async process(e, t) { | ||
const r = F("Analysing"); | ||
r.start(2, 0); | ||
const s = t != null && t.bundleReferences ? await G.bundle(e) : await G.parse(e); | ||
const s = t != null && t.bundleReferences ? await _.bundle(e) : await _.parse(e); | ||
r.increment(); | ||
@@ -1476,8 +1517,8 @@ const i = this.parseModel(s, t); | ||
parseModel(e, t) { | ||
if (ot(e)) { | ||
const r = new lt(e); | ||
if (ct(e)) { | ||
const r = new mt(e); | ||
return r.parse(), r; | ||
} | ||
if (at(e)) { | ||
const r = new ft(e, t); | ||
if (pt(e)) { | ||
const r = new dt(e, t); | ||
return r.parse(), r; | ||
@@ -1488,3 +1529,3 @@ } | ||
} | ||
class Et extends b { | ||
class vt extends b { | ||
async run() { | ||
@@ -1495,20 +1536,20 @@ if (!this.config.api) { | ||
} | ||
const e = new wt(), { types: t, endpoints: r } = await e.process(this.config.api, this.config), s = new X(this.config.aliases), i = new x(this.config.observable), o = new nt(this.config), a = ( | ||
const e = new Et(), { types: t, endpoints: r } = await e.process(this.config.api, this.config), s = new Y(this.config.aliases), i = new N(this.config.observable), o = new ot(this.config), a = ( | ||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing | ||
this.config.validations && Object.assign({}, this.config.observable || void 0, this.config.validations) | ||
), c = new it(a); | ||
), c = new at(a); | ||
t.forEach((u) => { | ||
s.formatNames(u), i.format(u), c.format(u), o.format(u); | ||
}); | ||
const p = new tt(this.project, this.params, this.config); | ||
const p = new st(this.project, this.params, this.config); | ||
await p.generateEntities(Array.from(t.values())), await p.generateRepositories(r); | ||
} | ||
getDefaultConfig() { | ||
return Promise.resolve(Ue); | ||
return Promise.resolve(He); | ||
} | ||
} | ||
const vt = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({ | ||
const Ft = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({ | ||
__proto__: null, | ||
default: Et | ||
default: vt | ||
}, Symbol.toStringTag, { value: "Module" })); | ||
//# sourceMappingURL=generator.js.map |
@@ -6,3 +6,3 @@ { | ||
}, | ||
"version": "1.0.0-rc.3", | ||
"version": "1.0.0-rc.4", | ||
"description": "Frui.ts code generator", | ||
@@ -60,3 +60,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "4db51a5b0ed16c0768277482482372c579678bd8" | ||
"gitHead": "fdbf53a07c25a0d5fc30f34f164ebf560eef0962" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
207999
1472
0