@cms0/shared 0.1.1 → 0.2.14

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.
@@ -0,0 +1,404 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isPrimitiveDescriptor = isPrimitiveDescriptor;
4
+ exports.isEnumDescriptor = isEnumDescriptor;
5
+ exports.isUnionDescriptor = isUnionDescriptor;
6
+ exports.isModelRefDescriptor = isModelRefDescriptor;
7
+ exports.isObjectDescriptor = isObjectDescriptor;
8
+ exports.isArrayDescriptor = isArrayDescriptor;
9
+ exports.getModelDescriptor = getModelDescriptor;
10
+ exports.getDescriptorPropertyEntries = getDescriptorPropertyEntries;
11
+ exports.resolveAssetKindFromModelDescriptor = resolveAssetKindFromModelDescriptor;
12
+ exports.resolveDescriptorCapability = resolveDescriptorCapability;
13
+ exports.getDescriptorDefaultValue = getDescriptorDefaultValue;
14
+ exports.normalizeEnumValue = normalizeEnumValue;
15
+ exports.formatEnumOptionValue = formatEnumOptionValue;
16
+ exports.parseEnumOptionValue = parseEnumOptionValue;
17
+ exports.resolveUnionBranchIndex = resolveUnionBranchIndex;
18
+ exports.normalizeUnionValue = normalizeUnionValue;
19
+ exports.getDescriptorDisplayName = getDescriptorDisplayName;
20
+ const union_js_1 = require("./union.js");
21
+ function isPlainRecord(value) {
22
+ return !!value && typeof value === "object" && !Array.isArray(value);
23
+ }
24
+ function isPrimitiveDescriptor(descriptor) {
25
+ if (!descriptor)
26
+ return false;
27
+ const type = descriptor.type;
28
+ return (descriptor.kind === "primitive" ||
29
+ (!descriptor.kind && type !== "object" && type !== "array"));
30
+ }
31
+ function isEnumDescriptor(descriptor) {
32
+ return descriptor?.kind === "enum";
33
+ }
34
+ function isUnionDescriptor(descriptor) {
35
+ return descriptor?.kind === "union";
36
+ }
37
+ function isModelRefDescriptor(descriptor) {
38
+ return descriptor?.kind === "modelRef";
39
+ }
40
+ function isObjectDescriptor(descriptor) {
41
+ return !!descriptor && descriptor.type === "object";
42
+ }
43
+ function isArrayDescriptor(descriptor) {
44
+ return !!descriptor && descriptor.type === "array";
45
+ }
46
+ function getModelDescriptor(fullDescriptor, modelName) {
47
+ if (!fullDescriptor || !modelName)
48
+ return undefined;
49
+ return fullDescriptor.models?.[modelName];
50
+ }
51
+ function getDescriptorPropertyEntries(descriptor) {
52
+ const properties = descriptor.properties ?? {};
53
+ const order = Array.isArray(descriptor.propertyOrder)
54
+ ? descriptor.propertyOrder
55
+ : [];
56
+ const ordered = order
57
+ .filter((key) => Object.prototype.hasOwnProperty.call(properties, key))
58
+ .map((key) => [key, properties[key]]);
59
+ const remaining = Object.entries(properties)
60
+ .filter(([key]) => !order.includes(key))
61
+ .sort(([left], [right]) => left.localeCompare(right));
62
+ return [...ordered, ...remaining];
63
+ }
64
+ function resolveAssetKindFromModelDescriptor(modelName, modelDescriptor) {
65
+ const presentation = modelDescriptor?.presentation;
66
+ if (presentation?.kind === "asset") {
67
+ if (presentation.assetKind === "image")
68
+ return "image";
69
+ if (presentation.assetKind === "video")
70
+ return "video";
71
+ return "file";
72
+ }
73
+ if (modelName === "Image")
74
+ return "image";
75
+ if (modelName === "Video")
76
+ return "video";
77
+ if (modelName === "File")
78
+ return "file";
79
+ return null;
80
+ }
81
+ function resolveDescriptorCapability(descriptor, fullDescriptor) {
82
+ if (isUnionDescriptor(descriptor)) {
83
+ const keys = (0, union_js_1.getUnionBranchKeys)(descriptor);
84
+ const branches = Array.isArray(descriptor.anyOf) ? descriptor.anyOf : [];
85
+ return {
86
+ kind: "union",
87
+ discriminatorKey: descriptor.discriminator?.key,
88
+ branches: branches.map((branch, index) => ({
89
+ index,
90
+ key: keys[index] ?? (0, union_js_1.getUnionBranchKeyAt)(descriptor, index) ?? `branch_${index + 1}`,
91
+ descriptor: branch,
92
+ capability: resolveDescriptorCapability(branch, fullDescriptor),
93
+ label: getDescriptorDisplayName(branch, index),
94
+ })),
95
+ };
96
+ }
97
+ if (isModelRefDescriptor(descriptor)) {
98
+ const modelDescriptor = getModelDescriptor(fullDescriptor, descriptor.model);
99
+ const assetKind = resolveAssetKindFromModelDescriptor(descriptor.model, modelDescriptor);
100
+ if (assetKind) {
101
+ return {
102
+ kind: "asset-ref",
103
+ modelName: descriptor.model,
104
+ assetKind,
105
+ modelDescriptor,
106
+ };
107
+ }
108
+ return {
109
+ kind: "model-ref",
110
+ modelName: descriptor.model,
111
+ modelDescriptor,
112
+ };
113
+ }
114
+ if (isEnumDescriptor(descriptor)) {
115
+ return {
116
+ kind: "enum",
117
+ valueType: descriptor.valueType,
118
+ options: Array.isArray(descriptor.values) ? descriptor.values.slice() : [],
119
+ };
120
+ }
121
+ if (isObjectDescriptor(descriptor)) {
122
+ return {
123
+ kind: "object-inline",
124
+ properties: getDescriptorPropertyEntries(descriptor),
125
+ };
126
+ }
127
+ if (isArrayDescriptor(descriptor)) {
128
+ return {
129
+ kind: "collection-inline",
130
+ itemDescriptor: descriptor.items,
131
+ };
132
+ }
133
+ if (!isPrimitiveDescriptor(descriptor)) {
134
+ return {
135
+ kind: "unsupported",
136
+ reason: "Unsupported descriptor kind.",
137
+ };
138
+ }
139
+ if (descriptor.customType === "LocalizedString") {
140
+ return { kind: "localized-string" };
141
+ }
142
+ if (descriptor.customType === "RichText") {
143
+ return { kind: "rich-text" };
144
+ }
145
+ if (descriptor.customType === "LocalizedRichText") {
146
+ return { kind: "localized-rich-text" };
147
+ }
148
+ return {
149
+ kind: "scalar",
150
+ primitiveType: descriptor.type,
151
+ customType: descriptor.customType,
152
+ };
153
+ }
154
+ function getDescriptorDefaultValue(descriptor, options = {}) {
155
+ const capability = resolveDescriptorCapability(descriptor);
156
+ if (capability.kind === "localized-string") {
157
+ const locales = options.locales?.length ? [...options.locales] : [options.defaultLocale ?? "en"];
158
+ const defaultLocale = options.defaultLocale ?? locales[0] ?? "en";
159
+ return {
160
+ defaultLocale,
161
+ locales: Object.fromEntries(locales.map((locale) => [locale, ""])),
162
+ };
163
+ }
164
+ if (capability.kind === "rich-text") {
165
+ return { value: {}, html: "" };
166
+ }
167
+ if (capability.kind === "localized-rich-text") {
168
+ const locales = options.locales?.length ? [...options.locales] : [options.defaultLocale ?? "en"];
169
+ const defaultLocale = options.defaultLocale ?? locales[0] ?? "en";
170
+ return {
171
+ defaultLocale,
172
+ locales: Object.fromEntries(locales.map((locale) => [locale, { value: {}, html: "" }])),
173
+ };
174
+ }
175
+ if (capability.kind === "model-ref" || capability.kind === "asset-ref") {
176
+ return null;
177
+ }
178
+ if (capability.kind === "enum") {
179
+ return capability.options[0] ?? null;
180
+ }
181
+ if (capability.kind === "union") {
182
+ const firstBranch = capability.branches[0];
183
+ if (!firstBranch)
184
+ return null;
185
+ return (0, union_js_1.createTaggedUnionValue)(descriptor, 0, getDescriptorDefaultValue(firstBranch.descriptor, options));
186
+ }
187
+ if (capability.kind === "object-inline") {
188
+ return Object.fromEntries(capability.properties.map(([key, propertyDescriptor]) => [
189
+ key,
190
+ getDescriptorDefaultValue(propertyDescriptor, options),
191
+ ]));
192
+ }
193
+ if (capability.kind === "collection-inline") {
194
+ return [];
195
+ }
196
+ if (capability.kind === "scalar") {
197
+ if (capability.primitiveType === "string")
198
+ return "";
199
+ if (capability.primitiveType === "number")
200
+ return 0;
201
+ if (capability.primitiveType === "boolean")
202
+ return false;
203
+ return null;
204
+ }
205
+ return null;
206
+ }
207
+ function normalizeEnumValue(descriptor, value) {
208
+ const valueType = (descriptor.valueType ?? "string");
209
+ const values = Array.isArray(descriptor.values) ? descriptor.values : [];
210
+ let normalized = value;
211
+ if (valueType === "string") {
212
+ normalized = value == null ? "" : String(value);
213
+ }
214
+ else if (valueType === "number") {
215
+ if (typeof value === "number" && Number.isFinite(value)) {
216
+ normalized = value;
217
+ }
218
+ else {
219
+ const parsed = Number(value);
220
+ normalized = Number.isFinite(parsed) ? parsed : null;
221
+ }
222
+ }
223
+ else if (valueType === "boolean") {
224
+ if (value === true || value === "true" || value === 1 || value === "1") {
225
+ normalized = true;
226
+ }
227
+ else if (value === false || value === "false" || value === 0 || value === "0") {
228
+ normalized = false;
229
+ }
230
+ else {
231
+ normalized = Boolean(value);
232
+ }
233
+ }
234
+ if (!values.length)
235
+ return normalized;
236
+ if (values.some((entry) => Object.is(entry, normalized))) {
237
+ return normalized;
238
+ }
239
+ return values[0];
240
+ }
241
+ function formatEnumOptionValue(value) {
242
+ return JSON.stringify(value);
243
+ }
244
+ function parseEnumOptionValue(raw, valueType) {
245
+ try {
246
+ const parsed = JSON.parse(raw);
247
+ if (valueType === "string")
248
+ return String(parsed ?? "");
249
+ if (valueType === "number") {
250
+ const num = Number(parsed);
251
+ return Number.isFinite(num) ? num : null;
252
+ }
253
+ return Boolean(parsed);
254
+ }
255
+ catch {
256
+ if (valueType === "number") {
257
+ const num = Number(raw);
258
+ return Number.isFinite(num) ? num : null;
259
+ }
260
+ if (valueType === "boolean") {
261
+ return raw === "true";
262
+ }
263
+ return raw;
264
+ }
265
+ }
266
+ function isUrlLikeString(input) {
267
+ return (typeof input === "string" &&
268
+ (input.includes("://") || input.startsWith("/") || input.startsWith("./")));
269
+ }
270
+ function isUuidLikeId(input) {
271
+ return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(input);
272
+ }
273
+ function normalizeModelRefValue(value) {
274
+ if (typeof value === "string" || typeof value === "number")
275
+ return value;
276
+ if (!isPlainRecord(value))
277
+ return null;
278
+ const id = value.id;
279
+ return typeof id === "string" || typeof id === "number" ? id : null;
280
+ }
281
+ function descriptorMatchScore(descriptor, value) {
282
+ if (isPrimitiveDescriptor(descriptor)) {
283
+ if (descriptor.type === "json")
284
+ return 1;
285
+ if (descriptor.type === "string")
286
+ return typeof value === "string" ? 4 : 0;
287
+ if (descriptor.type === "number")
288
+ return typeof value === "number" ? 4 : 0;
289
+ if (descriptor.type === "boolean")
290
+ return typeof value === "boolean" ? 4 : 0;
291
+ return 0;
292
+ }
293
+ if (isEnumDescriptor(descriptor)) {
294
+ const values = Array.isArray(descriptor.values) ? descriptor.values : [];
295
+ if (!values.length)
296
+ return 2;
297
+ return values.some((entry) => Object.is(entry, value)) ? 6 : 0;
298
+ }
299
+ if (isModelRefDescriptor(descriptor)) {
300
+ if (typeof value === "string") {
301
+ const normalized = value.trim();
302
+ if (!normalized)
303
+ return 0;
304
+ if (isUrlLikeString(normalized))
305
+ return 1;
306
+ return isUuidLikeId(normalized) ? 6 : 2;
307
+ }
308
+ if (typeof value === "number") {
309
+ return Number.isFinite(value) && value !== 0 ? 4 : 0;
310
+ }
311
+ const candidate = normalizeModelRefValue(value);
312
+ if (candidate == null)
313
+ return 0;
314
+ return typeof candidate === "string" && isUuidLikeId(candidate) ? 5 : 2;
315
+ }
316
+ if (isArrayDescriptor(descriptor)) {
317
+ return Array.isArray(value) ? 3 : 0;
318
+ }
319
+ if (isObjectDescriptor(descriptor)) {
320
+ if (!isPlainRecord(value))
321
+ return 0;
322
+ const keys = Object.keys(descriptor.properties ?? {});
323
+ const matched = keys.filter((key) => Object.prototype.hasOwnProperty.call(value, key)).length;
324
+ return 2 + matched;
325
+ }
326
+ if (isUnionDescriptor(descriptor)) {
327
+ const branches = Array.isArray(descriptor.anyOf) ? descriptor.anyOf : [];
328
+ if (!branches.length)
329
+ return 0;
330
+ return Math.max(...branches.map((branch) => descriptorMatchScore(branch, value)));
331
+ }
332
+ return 0;
333
+ }
334
+ function resolveUnionBranchIndex(descriptor, value) {
335
+ const branches = Array.isArray(descriptor.anyOf) ? descriptor.anyOf : [];
336
+ if (!branches.length)
337
+ return 0;
338
+ const discriminatorKey = descriptor.discriminator?.key;
339
+ if (discriminatorKey && isPlainRecord(value)) {
340
+ const discriminatorValue = value[discriminatorKey];
341
+ for (let index = 0; index < branches.length; index += 1) {
342
+ const branch = branches[index];
343
+ const expected = branch?.properties?.[discriminatorKey]?.values?.[0];
344
+ if (expected !== undefined && Object.is(expected, discriminatorValue)) {
345
+ return index;
346
+ }
347
+ }
348
+ }
349
+ let bestIndex = 0;
350
+ let bestScore = -1;
351
+ for (let index = 0; index < branches.length; index += 1) {
352
+ const score = descriptorMatchScore(branches[index], value);
353
+ if (score > bestScore) {
354
+ bestScore = score;
355
+ bestIndex = index;
356
+ }
357
+ }
358
+ return bestIndex;
359
+ }
360
+ function normalizeUnionValue(descriptor, value, options = {}) {
361
+ if (value == null) {
362
+ return null;
363
+ }
364
+ if ((0, union_js_1.isTaggedUnionValue)(value)) {
365
+ return value;
366
+ }
367
+ const branches = Array.isArray(descriptor.anyOf) ? descriptor.anyOf : [];
368
+ if (!branches.length)
369
+ return null;
370
+ const branchIndex = resolveUnionBranchIndex(descriptor, value);
371
+ const branchKey = (0, union_js_1.getUnionBranchKeyAt)(descriptor, branchIndex);
372
+ if (!branchKey)
373
+ return null;
374
+ const branchDescriptor = branches[branchIndex];
375
+ const branchValue = value == null
376
+ ? getDescriptorDefaultValue(branchDescriptor, options)
377
+ : value;
378
+ return (0, union_js_1.createTaggedUnionValue)(descriptor, branchIndex, branchValue);
379
+ }
380
+ function getDescriptorDisplayName(descriptor, index = 0) {
381
+ const customType = descriptor?.customType;
382
+ if (typeof customType === "string" && customType.length > 0) {
383
+ return humanizeVisualText(customType);
384
+ }
385
+ if (isModelRefDescriptor(descriptor)) {
386
+ return humanizeVisualText(descriptor.model || `Branch ${index + 1}`);
387
+ }
388
+ if (isEnumDescriptor(descriptor))
389
+ return "Enum";
390
+ if (isUnionDescriptor(descriptor))
391
+ return "Union";
392
+ const descriptorType = descriptor?.type;
393
+ if (typeof descriptorType === "string") {
394
+ return humanizeVisualText(descriptorType);
395
+ }
396
+ return `Branch ${index + 1}`;
397
+ }
398
+ function humanizeVisualText(value) {
399
+ return value
400
+ .replace(/([a-z0-9])([A-Z])/g, "$1 $2")
401
+ .replace(/[-_]+/g, " ")
402
+ .trim()
403
+ .replace(/\b\w/g, (match) => match.toUpperCase());
404
+ }
package/dist/cjs/index.js CHANGED
@@ -17,5 +17,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
17
17
  exports.delay = void 0;
18
18
  __exportStar(require("./validation.js"), exports);
19
19
  __exportStar(require("./union.js"), exports);
20
+ __exportStar(require("./descriptor-capabilities.js"), exports);
20
21
  const delay = (ms) => new Promise((res) => setTimeout(res, ms));
21
22
  exports.delay = delay;
package/dist/cjs/union.js CHANGED
@@ -7,6 +7,7 @@ exports.getUnionBranchKeyAt = getUnionBranchKeyAt;
7
7
  exports.isTaggedUnionValue = isTaggedUnionValue;
8
8
  exports.encodeTaggedUnionValue = encodeTaggedUnionValue;
9
9
  exports.decodeTaggedUnionValue = decodeTaggedUnionValue;
10
+ exports.createTaggedUnionValue = createTaggedUnionValue;
10
11
  exports.resolveTaggedUnionBranchIndex = resolveTaggedUnionBranchIndex;
11
12
  exports.CMS0_UNION_META_KEY = "__cms0Union";
12
13
  function isObjectRecord(value) {
@@ -154,6 +155,12 @@ function decodeTaggedUnionValue(value) {
154
155
  value: value.value,
155
156
  };
156
157
  }
158
+ function createTaggedUnionValue(descriptor, branchIndex, branchValue) {
159
+ const branchKey = getUnionBranchKeyAt(descriptor, branchIndex);
160
+ if (!branchKey)
161
+ return null;
162
+ return encodeTaggedUnionValue(branchKey, branchValue);
163
+ }
157
164
  function resolveTaggedUnionBranchIndex(descriptor, value) {
158
165
  const decoded = decodeTaggedUnionValue(value);
159
166
  if (!decoded)
@@ -0,0 +1,385 @@
1
+ import { createTaggedUnionValue, getUnionBranchKeyAt, getUnionBranchKeys, isTaggedUnionValue, } from "./union.js";
2
+ function isPlainRecord(value) {
3
+ return !!value && typeof value === "object" && !Array.isArray(value);
4
+ }
5
+ export function isPrimitiveDescriptor(descriptor) {
6
+ if (!descriptor)
7
+ return false;
8
+ const type = descriptor.type;
9
+ return (descriptor.kind === "primitive" ||
10
+ (!descriptor.kind && type !== "object" && type !== "array"));
11
+ }
12
+ export function isEnumDescriptor(descriptor) {
13
+ return descriptor?.kind === "enum";
14
+ }
15
+ export function isUnionDescriptor(descriptor) {
16
+ return descriptor?.kind === "union";
17
+ }
18
+ export function isModelRefDescriptor(descriptor) {
19
+ return descriptor?.kind === "modelRef";
20
+ }
21
+ export function isObjectDescriptor(descriptor) {
22
+ return !!descriptor && descriptor.type === "object";
23
+ }
24
+ export function isArrayDescriptor(descriptor) {
25
+ return !!descriptor && descriptor.type === "array";
26
+ }
27
+ export function getModelDescriptor(fullDescriptor, modelName) {
28
+ if (!fullDescriptor || !modelName)
29
+ return undefined;
30
+ return fullDescriptor.models?.[modelName];
31
+ }
32
+ export function getDescriptorPropertyEntries(descriptor) {
33
+ const properties = descriptor.properties ?? {};
34
+ const order = Array.isArray(descriptor.propertyOrder)
35
+ ? descriptor.propertyOrder
36
+ : [];
37
+ const ordered = order
38
+ .filter((key) => Object.prototype.hasOwnProperty.call(properties, key))
39
+ .map((key) => [key, properties[key]]);
40
+ const remaining = Object.entries(properties)
41
+ .filter(([key]) => !order.includes(key))
42
+ .sort(([left], [right]) => left.localeCompare(right));
43
+ return [...ordered, ...remaining];
44
+ }
45
+ export function resolveAssetKindFromModelDescriptor(modelName, modelDescriptor) {
46
+ const presentation = modelDescriptor?.presentation;
47
+ if (presentation?.kind === "asset") {
48
+ if (presentation.assetKind === "image")
49
+ return "image";
50
+ if (presentation.assetKind === "video")
51
+ return "video";
52
+ return "file";
53
+ }
54
+ if (modelName === "Image")
55
+ return "image";
56
+ if (modelName === "Video")
57
+ return "video";
58
+ if (modelName === "File")
59
+ return "file";
60
+ return null;
61
+ }
62
+ export function resolveDescriptorCapability(descriptor, fullDescriptor) {
63
+ if (isUnionDescriptor(descriptor)) {
64
+ const keys = getUnionBranchKeys(descriptor);
65
+ const branches = Array.isArray(descriptor.anyOf) ? descriptor.anyOf : [];
66
+ return {
67
+ kind: "union",
68
+ discriminatorKey: descriptor.discriminator?.key,
69
+ branches: branches.map((branch, index) => ({
70
+ index,
71
+ key: keys[index] ?? getUnionBranchKeyAt(descriptor, index) ?? `branch_${index + 1}`,
72
+ descriptor: branch,
73
+ capability: resolveDescriptorCapability(branch, fullDescriptor),
74
+ label: getDescriptorDisplayName(branch, index),
75
+ })),
76
+ };
77
+ }
78
+ if (isModelRefDescriptor(descriptor)) {
79
+ const modelDescriptor = getModelDescriptor(fullDescriptor, descriptor.model);
80
+ const assetKind = resolveAssetKindFromModelDescriptor(descriptor.model, modelDescriptor);
81
+ if (assetKind) {
82
+ return {
83
+ kind: "asset-ref",
84
+ modelName: descriptor.model,
85
+ assetKind,
86
+ modelDescriptor,
87
+ };
88
+ }
89
+ return {
90
+ kind: "model-ref",
91
+ modelName: descriptor.model,
92
+ modelDescriptor,
93
+ };
94
+ }
95
+ if (isEnumDescriptor(descriptor)) {
96
+ return {
97
+ kind: "enum",
98
+ valueType: descriptor.valueType,
99
+ options: Array.isArray(descriptor.values) ? descriptor.values.slice() : [],
100
+ };
101
+ }
102
+ if (isObjectDescriptor(descriptor)) {
103
+ return {
104
+ kind: "object-inline",
105
+ properties: getDescriptorPropertyEntries(descriptor),
106
+ };
107
+ }
108
+ if (isArrayDescriptor(descriptor)) {
109
+ return {
110
+ kind: "collection-inline",
111
+ itemDescriptor: descriptor.items,
112
+ };
113
+ }
114
+ if (!isPrimitiveDescriptor(descriptor)) {
115
+ return {
116
+ kind: "unsupported",
117
+ reason: "Unsupported descriptor kind.",
118
+ };
119
+ }
120
+ if (descriptor.customType === "LocalizedString") {
121
+ return { kind: "localized-string" };
122
+ }
123
+ if (descriptor.customType === "RichText") {
124
+ return { kind: "rich-text" };
125
+ }
126
+ if (descriptor.customType === "LocalizedRichText") {
127
+ return { kind: "localized-rich-text" };
128
+ }
129
+ return {
130
+ kind: "scalar",
131
+ primitiveType: descriptor.type,
132
+ customType: descriptor.customType,
133
+ };
134
+ }
135
+ export function getDescriptorDefaultValue(descriptor, options = {}) {
136
+ const capability = resolveDescriptorCapability(descriptor);
137
+ if (capability.kind === "localized-string") {
138
+ const locales = options.locales?.length ? [...options.locales] : [options.defaultLocale ?? "en"];
139
+ const defaultLocale = options.defaultLocale ?? locales[0] ?? "en";
140
+ return {
141
+ defaultLocale,
142
+ locales: Object.fromEntries(locales.map((locale) => [locale, ""])),
143
+ };
144
+ }
145
+ if (capability.kind === "rich-text") {
146
+ return { value: {}, html: "" };
147
+ }
148
+ if (capability.kind === "localized-rich-text") {
149
+ const locales = options.locales?.length ? [...options.locales] : [options.defaultLocale ?? "en"];
150
+ const defaultLocale = options.defaultLocale ?? locales[0] ?? "en";
151
+ return {
152
+ defaultLocale,
153
+ locales: Object.fromEntries(locales.map((locale) => [locale, { value: {}, html: "" }])),
154
+ };
155
+ }
156
+ if (capability.kind === "model-ref" || capability.kind === "asset-ref") {
157
+ return null;
158
+ }
159
+ if (capability.kind === "enum") {
160
+ return capability.options[0] ?? null;
161
+ }
162
+ if (capability.kind === "union") {
163
+ const firstBranch = capability.branches[0];
164
+ if (!firstBranch)
165
+ return null;
166
+ return createTaggedUnionValue(descriptor, 0, getDescriptorDefaultValue(firstBranch.descriptor, options));
167
+ }
168
+ if (capability.kind === "object-inline") {
169
+ return Object.fromEntries(capability.properties.map(([key, propertyDescriptor]) => [
170
+ key,
171
+ getDescriptorDefaultValue(propertyDescriptor, options),
172
+ ]));
173
+ }
174
+ if (capability.kind === "collection-inline") {
175
+ return [];
176
+ }
177
+ if (capability.kind === "scalar") {
178
+ if (capability.primitiveType === "string")
179
+ return "";
180
+ if (capability.primitiveType === "number")
181
+ return 0;
182
+ if (capability.primitiveType === "boolean")
183
+ return false;
184
+ return null;
185
+ }
186
+ return null;
187
+ }
188
+ export function normalizeEnumValue(descriptor, value) {
189
+ const valueType = (descriptor.valueType ?? "string");
190
+ const values = Array.isArray(descriptor.values) ? descriptor.values : [];
191
+ let normalized = value;
192
+ if (valueType === "string") {
193
+ normalized = value == null ? "" : String(value);
194
+ }
195
+ else if (valueType === "number") {
196
+ if (typeof value === "number" && Number.isFinite(value)) {
197
+ normalized = value;
198
+ }
199
+ else {
200
+ const parsed = Number(value);
201
+ normalized = Number.isFinite(parsed) ? parsed : null;
202
+ }
203
+ }
204
+ else if (valueType === "boolean") {
205
+ if (value === true || value === "true" || value === 1 || value === "1") {
206
+ normalized = true;
207
+ }
208
+ else if (value === false || value === "false" || value === 0 || value === "0") {
209
+ normalized = false;
210
+ }
211
+ else {
212
+ normalized = Boolean(value);
213
+ }
214
+ }
215
+ if (!values.length)
216
+ return normalized;
217
+ if (values.some((entry) => Object.is(entry, normalized))) {
218
+ return normalized;
219
+ }
220
+ return values[0];
221
+ }
222
+ export function formatEnumOptionValue(value) {
223
+ return JSON.stringify(value);
224
+ }
225
+ export function parseEnumOptionValue(raw, valueType) {
226
+ try {
227
+ const parsed = JSON.parse(raw);
228
+ if (valueType === "string")
229
+ return String(parsed ?? "");
230
+ if (valueType === "number") {
231
+ const num = Number(parsed);
232
+ return Number.isFinite(num) ? num : null;
233
+ }
234
+ return Boolean(parsed);
235
+ }
236
+ catch {
237
+ if (valueType === "number") {
238
+ const num = Number(raw);
239
+ return Number.isFinite(num) ? num : null;
240
+ }
241
+ if (valueType === "boolean") {
242
+ return raw === "true";
243
+ }
244
+ return raw;
245
+ }
246
+ }
247
+ function isUrlLikeString(input) {
248
+ return (typeof input === "string" &&
249
+ (input.includes("://") || input.startsWith("/") || input.startsWith("./")));
250
+ }
251
+ function isUuidLikeId(input) {
252
+ return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(input);
253
+ }
254
+ function normalizeModelRefValue(value) {
255
+ if (typeof value === "string" || typeof value === "number")
256
+ return value;
257
+ if (!isPlainRecord(value))
258
+ return null;
259
+ const id = value.id;
260
+ return typeof id === "string" || typeof id === "number" ? id : null;
261
+ }
262
+ function descriptorMatchScore(descriptor, value) {
263
+ if (isPrimitiveDescriptor(descriptor)) {
264
+ if (descriptor.type === "json")
265
+ return 1;
266
+ if (descriptor.type === "string")
267
+ return typeof value === "string" ? 4 : 0;
268
+ if (descriptor.type === "number")
269
+ return typeof value === "number" ? 4 : 0;
270
+ if (descriptor.type === "boolean")
271
+ return typeof value === "boolean" ? 4 : 0;
272
+ return 0;
273
+ }
274
+ if (isEnumDescriptor(descriptor)) {
275
+ const values = Array.isArray(descriptor.values) ? descriptor.values : [];
276
+ if (!values.length)
277
+ return 2;
278
+ return values.some((entry) => Object.is(entry, value)) ? 6 : 0;
279
+ }
280
+ if (isModelRefDescriptor(descriptor)) {
281
+ if (typeof value === "string") {
282
+ const normalized = value.trim();
283
+ if (!normalized)
284
+ return 0;
285
+ if (isUrlLikeString(normalized))
286
+ return 1;
287
+ return isUuidLikeId(normalized) ? 6 : 2;
288
+ }
289
+ if (typeof value === "number") {
290
+ return Number.isFinite(value) && value !== 0 ? 4 : 0;
291
+ }
292
+ const candidate = normalizeModelRefValue(value);
293
+ if (candidate == null)
294
+ return 0;
295
+ return typeof candidate === "string" && isUuidLikeId(candidate) ? 5 : 2;
296
+ }
297
+ if (isArrayDescriptor(descriptor)) {
298
+ return Array.isArray(value) ? 3 : 0;
299
+ }
300
+ if (isObjectDescriptor(descriptor)) {
301
+ if (!isPlainRecord(value))
302
+ return 0;
303
+ const keys = Object.keys(descriptor.properties ?? {});
304
+ const matched = keys.filter((key) => Object.prototype.hasOwnProperty.call(value, key)).length;
305
+ return 2 + matched;
306
+ }
307
+ if (isUnionDescriptor(descriptor)) {
308
+ const branches = Array.isArray(descriptor.anyOf) ? descriptor.anyOf : [];
309
+ if (!branches.length)
310
+ return 0;
311
+ return Math.max(...branches.map((branch) => descriptorMatchScore(branch, value)));
312
+ }
313
+ return 0;
314
+ }
315
+ export function resolveUnionBranchIndex(descriptor, value) {
316
+ const branches = Array.isArray(descriptor.anyOf) ? descriptor.anyOf : [];
317
+ if (!branches.length)
318
+ return 0;
319
+ const discriminatorKey = descriptor.discriminator?.key;
320
+ if (discriminatorKey && isPlainRecord(value)) {
321
+ const discriminatorValue = value[discriminatorKey];
322
+ for (let index = 0; index < branches.length; index += 1) {
323
+ const branch = branches[index];
324
+ const expected = branch?.properties?.[discriminatorKey]?.values?.[0];
325
+ if (expected !== undefined && Object.is(expected, discriminatorValue)) {
326
+ return index;
327
+ }
328
+ }
329
+ }
330
+ let bestIndex = 0;
331
+ let bestScore = -1;
332
+ for (let index = 0; index < branches.length; index += 1) {
333
+ const score = descriptorMatchScore(branches[index], value);
334
+ if (score > bestScore) {
335
+ bestScore = score;
336
+ bestIndex = index;
337
+ }
338
+ }
339
+ return bestIndex;
340
+ }
341
+ export function normalizeUnionValue(descriptor, value, options = {}) {
342
+ if (value == null) {
343
+ return null;
344
+ }
345
+ if (isTaggedUnionValue(value)) {
346
+ return value;
347
+ }
348
+ const branches = Array.isArray(descriptor.anyOf) ? descriptor.anyOf : [];
349
+ if (!branches.length)
350
+ return null;
351
+ const branchIndex = resolveUnionBranchIndex(descriptor, value);
352
+ const branchKey = getUnionBranchKeyAt(descriptor, branchIndex);
353
+ if (!branchKey)
354
+ return null;
355
+ const branchDescriptor = branches[branchIndex];
356
+ const branchValue = value == null
357
+ ? getDescriptorDefaultValue(branchDescriptor, options)
358
+ : value;
359
+ return createTaggedUnionValue(descriptor, branchIndex, branchValue);
360
+ }
361
+ export function getDescriptorDisplayName(descriptor, index = 0) {
362
+ const customType = descriptor?.customType;
363
+ if (typeof customType === "string" && customType.length > 0) {
364
+ return humanizeVisualText(customType);
365
+ }
366
+ if (isModelRefDescriptor(descriptor)) {
367
+ return humanizeVisualText(descriptor.model || `Branch ${index + 1}`);
368
+ }
369
+ if (isEnumDescriptor(descriptor))
370
+ return "Enum";
371
+ if (isUnionDescriptor(descriptor))
372
+ return "Union";
373
+ const descriptorType = descriptor?.type;
374
+ if (typeof descriptorType === "string") {
375
+ return humanizeVisualText(descriptorType);
376
+ }
377
+ return `Branch ${index + 1}`;
378
+ }
379
+ function humanizeVisualText(value) {
380
+ return value
381
+ .replace(/([a-z0-9])([A-Z])/g, "$1 $2")
382
+ .replace(/[-_]+/g, " ")
383
+ .trim()
384
+ .replace(/\b\w/g, (match) => match.toUpperCase());
385
+ }
package/dist/esm/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from "./validation.js";
2
2
  export * from "./union.js";
3
+ export * from "./descriptor-capabilities.js";
3
4
  export const delay = (ms) => new Promise((res) => setTimeout(res, ms));
package/dist/esm/union.js CHANGED
@@ -144,6 +144,12 @@ export function decodeTaggedUnionValue(value) {
144
144
  value: value.value,
145
145
  };
146
146
  }
147
+ export function createTaggedUnionValue(descriptor, branchIndex, branchValue) {
148
+ const branchKey = getUnionBranchKeyAt(descriptor, branchIndex);
149
+ if (!branchKey)
150
+ return null;
151
+ return encodeTaggedUnionValue(branchKey, branchValue);
152
+ }
147
153
  export function resolveTaggedUnionBranchIndex(descriptor, value) {
148
154
  const decoded = decodeTaggedUnionValue(value);
149
155
  if (!decoded)
@@ -0,0 +1,90 @@
1
+ import { type Cms0TaggedUnionValue } from "./union.js";
2
+ import type { EnumValue, EnumValueType, FieldDescriptor, FullDescriptor, ModelDescriptor } from "./validation.js";
3
+ export type Cms0AssetKind = "file" | "image" | "video";
4
+ export type Cms0DescriptorCapability = {
5
+ kind: "scalar";
6
+ primitiveType: "string" | "number" | "boolean" | "json";
7
+ customType?: string;
8
+ } | {
9
+ kind: "enum";
10
+ valueType: EnumValueType;
11
+ options: EnumValue[];
12
+ } | {
13
+ kind: "localized-string";
14
+ } | {
15
+ kind: "rich-text";
16
+ } | {
17
+ kind: "localized-rich-text";
18
+ } | {
19
+ kind: "model-ref";
20
+ modelName: string;
21
+ modelDescriptor?: ModelDescriptor;
22
+ } | {
23
+ kind: "asset-ref";
24
+ modelName: string;
25
+ assetKind: Cms0AssetKind;
26
+ modelDescriptor?: ModelDescriptor;
27
+ } | {
28
+ kind: "object-inline";
29
+ properties: Array<[string, FieldDescriptor]>;
30
+ } | {
31
+ kind: "collection-inline";
32
+ itemDescriptor: FieldDescriptor;
33
+ } | {
34
+ kind: "union";
35
+ branches: Array<{
36
+ index: number;
37
+ key: string;
38
+ descriptor: FieldDescriptor;
39
+ capability: Cms0DescriptorCapability;
40
+ label: string;
41
+ }>;
42
+ discriminatorKey?: string;
43
+ } | {
44
+ kind: "unsupported";
45
+ reason?: string;
46
+ };
47
+ export declare function isPrimitiveDescriptor(descriptor: FieldDescriptor | undefined): descriptor is Extract<FieldDescriptor, {
48
+ kind?: "primitive";
49
+ }>;
50
+ export declare function isEnumDescriptor(descriptor: FieldDescriptor | undefined): descriptor is Extract<FieldDescriptor, {
51
+ kind: "enum";
52
+ }>;
53
+ export declare function isUnionDescriptor(descriptor: FieldDescriptor | undefined): descriptor is Extract<FieldDescriptor, {
54
+ kind: "union";
55
+ }>;
56
+ export declare function isModelRefDescriptor(descriptor: FieldDescriptor | undefined): descriptor is Extract<FieldDescriptor, {
57
+ kind: "modelRef";
58
+ }>;
59
+ export declare function isObjectDescriptor(descriptor: FieldDescriptor | undefined): descriptor is Extract<FieldDescriptor, {
60
+ type: "object";
61
+ }>;
62
+ export declare function isArrayDescriptor(descriptor: FieldDescriptor | undefined): descriptor is Extract<FieldDescriptor, {
63
+ type: "array";
64
+ }>;
65
+ export declare function getModelDescriptor(fullDescriptor: FullDescriptor | undefined, modelName: string | undefined): ModelDescriptor | undefined;
66
+ export declare function getDescriptorPropertyEntries(descriptor: Extract<FieldDescriptor, {
67
+ type: "object";
68
+ }>): Array<[string, FieldDescriptor]>;
69
+ export declare function resolveAssetKindFromModelDescriptor(modelName: string | undefined, modelDescriptor: ModelDescriptor | undefined): Cms0AssetKind | null;
70
+ export declare function resolveDescriptorCapability(descriptor: FieldDescriptor, fullDescriptor?: FullDescriptor): Cms0DescriptorCapability;
71
+ export declare function getDescriptorDefaultValue(descriptor: FieldDescriptor, options?: {
72
+ locales?: readonly string[];
73
+ defaultLocale?: string;
74
+ }): unknown;
75
+ export declare function normalizeEnumValue(descriptor: Extract<FieldDescriptor, {
76
+ kind: "enum";
77
+ }>, value: unknown): unknown;
78
+ export declare function formatEnumOptionValue(value: unknown): string;
79
+ export declare function parseEnumOptionValue(raw: string, valueType: "string" | "number" | "boolean"): unknown;
80
+ export declare function resolveUnionBranchIndex(descriptor: Extract<FieldDescriptor, {
81
+ kind: "union";
82
+ }>, value: unknown): number;
83
+ export declare function normalizeUnionValue(descriptor: Extract<FieldDescriptor, {
84
+ kind: "union";
85
+ }>, value: unknown, options?: {
86
+ locales?: readonly string[];
87
+ defaultLocale?: string;
88
+ }): Cms0TaggedUnionValue | null;
89
+ export declare function getDescriptorDisplayName(descriptor: FieldDescriptor, index?: number): string;
90
+ //# sourceMappingURL=descriptor-capabilities.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"descriptor-capabilities.d.ts","sourceRoot":"","sources":["../../src/descriptor-capabilities.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,oBAAoB,EAC1B,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,EACV,SAAS,EACT,aAAa,EACb,eAAe,EACf,cAAc,EACd,eAAe,EAChB,MAAM,iBAAiB,CAAC;AAEzB,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,OAAO,GAAG,OAAO,CAAC;AAEvD,MAAM,MAAM,wBAAwB,GAChC;IACE,IAAI,EAAE,QAAQ,CAAC;IACf,aAAa,EAAE,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,MAAM,CAAC;IACxD,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,GACD;IACE,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,aAAa,CAAC;IACzB,OAAO,EAAE,SAAS,EAAE,CAAC;CACtB,GACD;IACE,IAAI,EAAE,kBAAkB,CAAC;CAC1B,GACD;IACE,IAAI,EAAE,WAAW,CAAC;CACnB,GACD;IACE,IAAI,EAAE,qBAAqB,CAAC;CAC7B,GACD;IACE,IAAI,EAAE,WAAW,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,eAAe,CAAC,EAAE,eAAe,CAAC;CACnC,GACD;IACE,IAAI,EAAE,WAAW,CAAC;I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B,oBAAoB,CAClC,GAAG,EAAE,MAAM,EACX,SAAS,EAAE,QAAQ,GAAG,QAAQ,GAAG,SAAS,GACzC,OAAO,CAmBT;AA0ED,wBAAgB,uBAAuB,CACrC,UAAU,EAAE,OAAO,CAAC,eAAe,EAAE;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,CAAC,EACvD,KAAK,EAAE,OAAO,GACb,MAAM,CA0BR;AAED,wBAAgB,mBAAmB,CACjC,UAAU,EAAE,OAAO,CAAC,eAAe,EAAE;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,CAAC,EACvD,KAAK,EAAE,OAAO,EACd,OAAO,GAAE;IACP,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IAC5B,aAAa,CAAC,EAAE,MAAM,CAAC;CACnB,GACL,oBAAoB,GAAG,IAAI,CAoB7B;AAED,wBAAgB,wBAAwB,CACtC,UAAU,EAAE,eAAe,EAC3B,KAAK,SAAI,GACR,MAAM,CAeR"}
@@ -1,4 +1,5 @@
1
1
  export * from "./validation.js";
2
2
  export * from "./union.js";
3
+ export * from "./descriptor-capabilities.js";
3
4
  export declare const delay: (ms: number) => Promise<unknown>;
4
5
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAE3B,eAAO,MAAM,KAAK,GAAI,IAAI,MAAM,qBAA8C,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,YAAY,CAAC;AAC3B,cAAc,8BAA8B,CAAC;AAE7C,eAAO,MAAM,KAAK,GAAI,IAAI,MAAM,qBAA8C,CAAC"}
@@ -19,6 +19,7 @@ export declare function decodeTaggedUnionValue(value: unknown): {
19
19
  branchKey: string;
20
20
  value: unknown;
21
21
  } | null;
22
+ export declare function createTaggedUnionValue(descriptor: UnionDescriptor, branchIndex: number, branchValue: unknown): Cms0TaggedUnionValue | null;
22
23
  export declare function resolveTaggedUnionBranchIndex(descriptor: UnionDescriptor, value: unknown): number;
23
24
  export {};
24
25
  //# sourceMappingURL=union.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"union.d.ts","sourceRoot":"","sources":["../../src/union.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,iBAAiB,CAAC;AAEvD,eAAO,MAAM,mBAAmB,EAAG,aAAsB,CAAC;AAE1D,MAAM,MAAM,mBAAmB,GAAG;IAChC,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,MAAM,MAAM,oBAAoB,GAAG;IACjC,CAAC,mBAAmB,CAAC,EAAE,mBAAmB,CAAC;IAC3C,KAAK,EAAE,OAAO,CAAC;CAChB,CAAC;AAEF,KAAK,eAAe,GAAG,OAAO,CAAC,eAAe,EAAE;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,CAAC,CAAC;AA8FnE,wBAAgB,sBAAsB,CAAC,QAAQ,EAAE,eAAe,EAAE,GAAG,MAAM,EAAE,CAc5E;AAED,wBAAgB,kBAAkB,CAAC,UAAU,EAAE,eAAe,GAAG,MAAM,EAAE,CAqBxE;AAED,wBAAgB,mBAAmB,CACjC,UAAU,EAAE,eAAe,EAC3B,KAAK,EAAE,MAAM,GACZ,MAAM,GAAG,SAAS,CAGpB;AAED,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,oBAAoB,CAMhF;AAED,wBAAgB,sBAAsB,CACpC,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,OAAO,GACb,oBAAoB,CAKtB;AAED,wBAAgB,sBAAsB,CACpC,KAAK,EAAE,OAAO,GACb;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,OAAO,CAAA;CAAE,GAAG,IAAI,CAO9C;A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IAAI,CAI7B;AAED,wBAAgB,6BAA6B,CAC3C,UAAU,EAAE,eAAe,EAC3B,KAAK,EAAE,OAAO,GACb,MAAM,CAKR"}
@@ -54,6 +54,10 @@ export type ModelDescriptor = {
54
54
  kind: "model";
55
55
  properties: Record<string, FieldDescriptor>;
56
56
  propertyOrder?: string[];
57
+ presentation?: {
58
+ kind?: "default" | "asset";
59
+ assetKind?: "file" | "image" | "video";
60
+ };
57
61
  };
58
62
  export type RootDescriptor = FieldDescriptor;
59
63
  export type FullDescriptor = {
@@ -1 +1 @@
1
- {"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["../../src/validation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAW,MAAM,KAAK,CAAC;AASjC,MAAM,MAAM,aAAa,GAAG,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,MAAM,CAAC;AACrE,MAAM,MAAM,aAAa,GAAG,OAAO,CAAC,aAAa,EAAE,MAAM,CAAC,CAAC;AAC3D,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC;AAElD,KAAK,wBAAwB,GAAG;IAC9B,IAAI,CAAC,EAAE,WAAW,CAAC;IACnB,IAAI,EAAE,aAAa,CAAC;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,KAAK,uBAAuB,GAAG;IAC7B,IAAI,EAAE,UAAU,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB,CAAC;AAEF,KAAK,qBAAqB,GAAG;IAC3B,IAAI,CAAC,EAAE,QAAQ,CAAC;IAChB,IAAI,EAAE,QAAQ,CAAC;IACf,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IAC5C,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AAe,CAAC,CAAC;IAC5C,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,YAAY,CAAC,EAAE;QACb,IAAI,CAAC,EAAE,SAAS,GAAG,OAAO,CAAC;QAC3B,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,GAAG,OAAO,CAAC;KACxC,CAAC;CACH,CAAC;AAEF,MAAM,MAAM,cAAc,GAAG,eAAe,CAAC;AAE7C,MAAM,MAAM,cAAc,GAAG;IAC3B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IACxC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;IACtC,QAAQ,CAAC,EAAE;QACT,OAAO,CAAC,EAAE,MAAM,EAAE,CAAC;QACnB,aAAa,CAAC,EAAE,MAAM,CAAC;QACvB,QAAQ,CAAC,EAAE;YACT,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;YACjB,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC;SACnB,CAAC;KACH,CAAC;CACH,CAAC;AAwIF,wBAAgB,6BAA6B,CAAC,UAAU,EAAE,cAAc;;;EAoBvE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cms0/shared",
3
- "version": "0.1.1",
3
+ "version": "0.2.14",
4
4
  "type": "module",
5
5
  "private": false,
6
6
  "publishConfig": {
@@ -34,11 +34,15 @@
34
34
  "@cms0/typescript-config": "0.0.1"
35
35
  },
36
36
  "scripts": {
37
+ "dev": "pnpm run dev:setup && pnpm run dev:esm",
38
+ "dev:setup": "node -e \"const fs=require('fs');fs.mkdirSync('dist/cjs',{recursive:true});fs.writeFileSync('dist/cjs/package.json','{\\\"type\\\":\\\"commonjs\\\"}\\n');fs.mkdirSync('dist',{recursive:true});fs.copyFileSync('src/responsive-break.css','dist/responsive-break.css');\"",
39
+ "dev:esm": "tsc -w -p tsconfig.json --preserveWatchOutput",
40
+ "dev:cjs": "tsc -w -p tsconfig.cjs.json --preserveWatchOutput",
41
+ "dev:css": "node -e \"const fs=require('node:fs');const path=require('node:path');const source=path.join('src','responsive-break.css');const target=path.join('dist','responsive-break.css');const sync=()=>{fs.mkdirSync(path.dirname(target),{recursive:true});fs.copyFileSync(source,target);console.log('[shared] synced responsive-break.css');};sync();fs.watchFile(source,{interval:250},(curr,prev)=>{if(curr.mtimeMs!==prev.mtimeMs)sync();});const stop=()=>{fs.unwatchFile(source);process.exit(0);};process.on('SIGINT',stop);process.on('SIGTERM',stop);\"",
37
42
  "build": "pnpm run build:clean && pnpm run build:esm && pnpm run build:cjs && pnpm run build:post",
38
43
  "build:clean": "node -e \"require('fs').rmSync('dist', { recursive: true, force: true });\"",
39
44
  "build:esm": "tsc -p tsconfig.json",
40
45
  "build:cjs": "tsc -p tsconfig.cjs.json",
41
- "build:post": "node -e \"const fs=require('fs');fs.mkdirSync('dist/cjs',{recursive:true});fs.writeFileSync('dist/cjs/package.json','{\\\"type\\\":\\\"commonjs\\\"}\\\\n');fs.copyFileSync('src/responsive-break.css','dist/responsive-break.css');\"",
42
- "dev": "tsc -b --watch"
46
+ "build:post": "node -e \"const fs=require('fs');fs.mkdirSync('dist/cjs',{recursive:true});fs.writeFileSync('dist/cjs/package.json','{\\\"type\\\":\\\"commonjs\\\"}\\\\n');fs.copyFileSync('src/responsive-break.css','dist/responsive-break.css');\""
43
47
  }
44
48
  }