@cms0/cms0 0.2.20 → 0.2.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (33) hide show
  1. package/README.md +11 -0
  2. package/dist/cjs/custom-types/registry.cjs +6 -0
  3. package/dist/cjs/index.cjs +400 -65
  4. package/dist/cjs/libs/cli/config-loader.cjs +45 -4
  5. package/dist/cjs/libs/cli/publisher.cjs +24 -11
  6. package/dist/esm/custom-types/registry.js +6 -0
  7. package/dist/esm/index.js +401 -66
  8. package/dist/esm/libs/cli/config-loader.js +45 -4
  9. package/dist/esm/libs/cli/publisher.js +24 -11
  10. package/dist/types/custom-types/index.d.ts +2 -1
  11. package/dist/types/custom-types/index.d.ts.map +1 -1
  12. package/dist/types/custom-types/registry.d.ts.map +1 -1
  13. package/dist/types/index.d.ts +131 -7
  14. package/dist/types/index.d.ts.map +1 -1
  15. package/dist/types/libs/cli/config-loader.d.ts.map +1 -1
  16. package/dist/types/libs/cli/publisher.d.ts.map +1 -1
  17. package/package.json +13 -9
  18. package/dist/cjs/index-old-1.cjs +0 -866
  19. package/dist/cjs/index-old.cjs +0 -1016
  20. package/dist/cjs/libs/cli/descriptor-builder.cjs +0 -273
  21. package/dist/cjs/utils/index.cjs +0 -2
  22. package/dist/esm/index-old-1.js +0 -862
  23. package/dist/esm/index-old.js +0 -1012
  24. package/dist/esm/libs/cli/descriptor-builder.js +0 -268
  25. package/dist/esm/utils/index.js +0 -1
  26. package/dist/types/index-old-1.d.ts +0 -175
  27. package/dist/types/index-old-1.d.ts.map +0 -1
  28. package/dist/types/index-old.d.ts +0 -151
  29. package/dist/types/index-old.d.ts.map +0 -1
  30. package/dist/types/libs/cli/descriptor-builder.d.ts +0 -5
  31. package/dist/types/libs/cli/descriptor-builder.d.ts.map +0 -1
  32. package/dist/types/utils/index.d.ts +0 -2
  33. package/dist/types/utils/index.d.ts.map +0 -1
@@ -1,273 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.buildDescriptor = buildDescriptor;
7
- // Build a schema descriptor by inspecting user types with ts-morph.
8
- const fs_1 = __importDefault(require("fs"));
9
- const path_1 = __importDefault(require("path"));
10
- const ts_morph_1 = require("ts-morph");
11
- const config_loader_js_1 = require("./config-loader.cjs");
12
- function unwrapOptional(type) {
13
- let optional = false;
14
- let nullable = false;
15
- if (type.isUnion()) {
16
- const unionTypes = type.getUnionTypes();
17
- const filtered = unionTypes.filter((t) => {
18
- if (t.isNull()) {
19
- nullable = true;
20
- return false;
21
- }
22
- if (t.isUndefined()) {
23
- optional = true;
24
- return false;
25
- }
26
- return true;
27
- });
28
- if (filtered.length === 1) {
29
- return { base: filtered[0], optional, nullable };
30
- }
31
- }
32
- return { base: type, optional, nullable };
33
- }
34
- function getDescriptorForType(type, modelMap, warnings, ctx, opts) {
35
- const { base, optional, nullable } = unwrapOptional(type);
36
- const isOptional = !!opts?.optional || optional;
37
- const isNullable = !!opts?.nullable || nullable;
38
- if (base.isString()) {
39
- return {
40
- kind: "primitive",
41
- type: "string",
42
- optional: isOptional,
43
- nullable: isNullable,
44
- };
45
- }
46
- if (base.isNumber()) {
47
- return {
48
- kind: "primitive",
49
- type: "number",
50
- optional: isOptional,
51
- nullable: isNullable,
52
- };
53
- }
54
- if (base.isBoolean()) {
55
- return {
56
- kind: "primitive",
57
- type: "boolean",
58
- optional: isOptional,
59
- nullable: isNullable,
60
- };
61
- }
62
- if (base.isArray()) {
63
- const elem = base.getArrayElementTypeOrThrow();
64
- return {
65
- kind: "array",
66
- type: "array",
67
- items: getDescriptorForType(elem, modelMap, warnings, `${ctx}[]`),
68
- optional: isOptional,
69
- nullable: isNullable,
70
- };
71
- }
72
- const aliasSymbol = base.getAliasSymbol();
73
- if (aliasSymbol) {
74
- const name = aliasSymbol.getName();
75
- if (modelMap[name]) {
76
- return {
77
- kind: "modelRef",
78
- model: name,
79
- optional: isOptional,
80
- nullable: isNullable,
81
- };
82
- }
83
- }
84
- if (base.isObject() && !base.isInterface()) {
85
- const props = {};
86
- for (const prop of base.getProperties()) {
87
- const propDecl = prop.getDeclarations()[0];
88
- const analyzed = unwrapOptional(propDecl.getType());
89
- props[prop.getName()] = getDescriptorForType(analyzed.base, modelMap, warnings, `${ctx}.${prop.getName()}`, {
90
- optional: prop.isOptional() || analyzed.optional,
91
- nullable: analyzed.nullable,
92
- });
93
- }
94
- return {
95
- kind: "object",
96
- type: "object",
97
- properties: props,
98
- optional: isOptional,
99
- nullable: isNullable,
100
- };
101
- }
102
- // unsupported or unknown type; fall back to string and record warning
103
- warnings.add(`cms0: unsupported type at ${ctx}; falling back to string`);
104
- return {
105
- kind: "primitive",
106
- type: "string",
107
- optional: isOptional,
108
- nullable: isNullable,
109
- };
110
- }
111
- function collectModels(sourceFiles, modelMap, warnings) {
112
- sourceFiles.forEach((sf) => {
113
- sf.getTypeAliases().forEach((alias) => {
114
- if (!alias.hasExportKeyword())
115
- return;
116
- const name = alias.getName();
117
- const type = alias.getType();
118
- if (type.isObject()) {
119
- const props = {};
120
- for (const prop of type.getProperties()) {
121
- const decl = prop.getDeclarations()[0];
122
- const propType = decl.getType();
123
- props[prop.getName()] = getDescriptorForType(propType, modelMap, warnings, `model ${name}.${prop.getName()}`);
124
- }
125
- modelMap[name] = { kind: "model", properties: props };
126
- }
127
- });
128
- });
129
- }
130
- function readCms0Options(invoc) {
131
- const arg = invoc.getArguments()[0];
132
- if (!arg || !ts_morph_1.Node.isObjectLiteralExpression(arg))
133
- return {};
134
- const options = {};
135
- const localesProp = arg.getProperty("locales");
136
- if (localesProp && ts_morph_1.Node.isPropertyAssignment(localesProp)) {
137
- const init = localesProp.getInitializer();
138
- if (init && ts_morph_1.Node.isArrayLiteralExpression(init)) {
139
- const values = init
140
- .getElements()
141
- .map((el) => (ts_morph_1.Node.isStringLiteral(el) ? el.getLiteralValue() : null))
142
- .filter((val) => Boolean(val));
143
- if (values.length)
144
- options.locales = values;
145
- }
146
- }
147
- const defaultProp = arg.getProperty("defaultLocale");
148
- if (defaultProp && ts_morph_1.Node.isPropertyAssignment(defaultProp)) {
149
- const init = defaultProp.getInitializer();
150
- if (init && ts_morph_1.Node.isStringLiteral(init)) {
151
- options.defaultLocale = init.getLiteralValue();
152
- }
153
- }
154
- return options;
155
- }
156
- function findRootInvocation(sourceFiles) {
157
- for (const sf of sourceFiles) {
158
- const invoc = sf
159
- ?.getDescendantsOfKind(ts_morph_1.SyntaxKind.CallExpression)
160
- .find((call) => call.getExpression().getText() === "cms0");
161
- if (invoc) {
162
- const typeArgs = invoc.getTypeArguments();
163
- if (typeArgs.length > 0) {
164
- return {
165
- rootType: typeArgs[0].getType(),
166
- ...readCms0Options(invoc),
167
- };
168
- }
169
- }
170
- }
171
- return undefined;
172
- }
173
- function buildDescriptor(resolved) {
174
- const tsconfig = resolved.tsconfigPath ?? (0, config_loader_js_1.findTsConfig)(resolved.entryFile);
175
- const project = tsconfig
176
- ? new ts_morph_1.Project({ tsConfigFilePath: tsconfig })
177
- : new ts_morph_1.Project();
178
- const warnings = new Set();
179
- const entryDir = path_1.default.dirname(path_1.default.resolve(resolved.entryFile));
180
- if (!project.getSourceFile(resolved.entryFile) &&
181
- fs_1.default.existsSync(resolved.entryFile)) {
182
- project.addSourceFileAtPath(resolved.entryFile);
183
- }
184
- const sourceFiles = project.getSourceFiles().filter((sf) => {
185
- const filePath = path_1.default.resolve(sf.getFilePath());
186
- const rel = path_1.default.relative(entryDir, filePath);
187
- return rel && !rel.startsWith("..") && !path_1.default.isAbsolute(rel);
188
- });
189
- const modelMap = {};
190
- collectModels(sourceFiles, modelMap, warnings);
191
- const invocation = findRootInvocation(sourceFiles);
192
- if (!invocation) {
193
- throw new Error("Could not locate cms0<T>() invocation in project sources.");
194
- }
195
- const rootType = invocation.rootType;
196
- const roots = {};
197
- for (const prop of rootType.getProperties()) {
198
- const name = prop.getName();
199
- const decl = prop.getDeclarations()[0];
200
- const analyzed = unwrapOptional(decl.getType());
201
- const propType = analyzed.base;
202
- const propOptional = prop.isOptional() || analyzed.optional;
203
- const propNullable = analyzed.nullable;
204
- if (propType.isArray()) {
205
- const elem = propType.getArrayElementTypeOrThrow();
206
- const itemDesc = getDescriptorForType(elem, modelMap, warnings, `root ${name}[]`);
207
- roots[name] = {
208
- type: "array",
209
- items: itemDesc,
210
- optional: propOptional,
211
- nullable: propNullable,
212
- };
213
- }
214
- else if (propType.isObject() && !propType.isArray()) {
215
- const objDesc = getDescriptorForType(propType, modelMap, warnings, `root ${name}`, {
216
- optional: propOptional,
217
- nullable: propNullable,
218
- });
219
- if (objDesc.kind === "object") {
220
- roots[name] = {
221
- type: "object",
222
- properties: objDesc.properties,
223
- optional: propOptional,
224
- nullable: propNullable,
225
- };
226
- }
227
- else if (objDesc.kind === "modelRef") {
228
- roots[name] = {
229
- type: "object",
230
- properties: {},
231
- optional: propOptional,
232
- nullable: propNullable,
233
- };
234
- }
235
- else {
236
- roots[name] = {
237
- type: "object",
238
- properties: {},
239
- optional: propOptional,
240
- nullable: propNullable,
241
- };
242
- }
243
- }
244
- else {
245
- roots[name] = {
246
- type: "object",
247
- properties: {
248
- [name]: getDescriptorForType(propType, modelMap, warnings, `root ${name}.${name}`, {
249
- optional: propOptional,
250
- nullable: propNullable,
251
- }),
252
- },
253
- optional: propOptional,
254
- nullable: propNullable,
255
- };
256
- }
257
- }
258
- if (warnings.size) {
259
- warnings.forEach((w) => console.warn(w));
260
- }
261
- const locales = invocation.locales;
262
- const defaultLocale = invocation.defaultLocale;
263
- return {
264
- models: modelMap,
265
- roots,
266
- metadata: locales || defaultLocale
267
- ? {
268
- locales,
269
- defaultLocale,
270
- }
271
- : undefined,
272
- };
273
- }
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });