@embeddable.com/sdk-react 2.2.0 → 2.2.2
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.
- package/lib/{plugin.d.ts → extractComponentsConfigPlugin.d.ts} +1 -2
- package/lib/index.esm.js +539 -30
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +539 -30
- package/lib/index.js.map +1 -1
- package/lib/loadComponentMeta.d.ts +1 -0
- package/lib/validate/componentMetaValidator.d.ts +2 -0
- package/lib/validate/editorMetaValidator.d.ts +1 -0
- package/lib/validate/schema/componentMetaSchema.d.ts +362 -0
- package/lib/validate/schema/editorMetaSchema.d.ts +55 -0
- package/lib/validate/schema/embeddableTypeSchema.d.ts +23 -0
- package/lib/validate/validateComponentMetaPlugin.d.ts +7 -0
- package/package.json +5 -2
package/lib/index.js
CHANGED
|
@@ -10,6 +10,8 @@ var parser = require('@babel/parser');
|
|
|
10
10
|
var generator = require('@babel/generator');
|
|
11
11
|
var traverse = require('@babel/traverse');
|
|
12
12
|
require('node:child_process');
|
|
13
|
+
var zod = require('zod');
|
|
14
|
+
var core = require('@embeddable.com/core');
|
|
13
15
|
|
|
14
16
|
function _interopNamespaceDefault(e) {
|
|
15
17
|
var n = Object.create(null);
|
|
@@ -46,16 +48,42 @@ var createContext = (pluginRoot, coreCtx) => {
|
|
|
46
48
|
};
|
|
47
49
|
};
|
|
48
50
|
|
|
49
|
-
/**
|
|
50
|
-
* TODO: for some reason code from @embeddable.com/extract-components-config
|
|
51
|
-
* is being cached. Thus we use this code duplication in place. Please investigate and fix.
|
|
52
|
-
*/
|
|
53
51
|
// @ts-ignore
|
|
54
52
|
const babelTraverse = traverse.default;
|
|
55
53
|
// @ts-ignore
|
|
56
54
|
const babelGenerate = generator.default;
|
|
57
55
|
const CORE_TYPES_IMPORT_REGEX = /@embeddable\.com\/core$/;
|
|
58
|
-
|
|
56
|
+
const EMB_TYPE_FILE_REGEX = /^(.*)\.type\.emb\.[jt]s$/;
|
|
57
|
+
const loadComponentMeta = async (moduleId, code) => {
|
|
58
|
+
const ast = parser.parse(code, {
|
|
59
|
+
sourceType: "module",
|
|
60
|
+
});
|
|
61
|
+
babelTraverse(ast, {
|
|
62
|
+
ImportDeclaration: (path) => {
|
|
63
|
+
const isNativeTypesImport = CORE_TYPES_IMPORT_REGEX.test(path.node.source.value);
|
|
64
|
+
const isCustomTypeImport = EMB_TYPE_FILE_REGEX.test(path.node.source.value);
|
|
65
|
+
if (!(isNativeTypesImport || isCustomTypeImport)) {
|
|
66
|
+
path.remove();
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
ExportDefaultDeclaration: (path) => {
|
|
70
|
+
path.remove();
|
|
71
|
+
},
|
|
72
|
+
});
|
|
73
|
+
const tempFilePath = moduleId
|
|
74
|
+
.replace(".emb.", ".emb-temp.")
|
|
75
|
+
.replace(/\.ts$/, ".js");
|
|
76
|
+
await fs__namespace.writeFile(tempFilePath, babelGenerate(ast).code);
|
|
77
|
+
const module = await import(tempFilePath);
|
|
78
|
+
await fs__namespace.rm(tempFilePath);
|
|
79
|
+
return module.meta;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* TODO: for some reason code from @embeddable.com/extract-components-config
|
|
84
|
+
* is being cached. Thus we use this code duplication in place. Please investigate and fix.
|
|
85
|
+
*/
|
|
86
|
+
var extractComponentsConfigPlugin = ({ globalKey, outputDir, fileName, componentFileRegex, searchEntry = "", }) => {
|
|
59
87
|
const configs = [];
|
|
60
88
|
return {
|
|
61
89
|
name: "extract-components-config",
|
|
@@ -63,32 +91,12 @@ var extractComponentsConfigPlugin = ({ globalKey, outputDir, fileName, component
|
|
|
63
91
|
var _a;
|
|
64
92
|
if (componentFileRegex.test(moduleInfo.id) &&
|
|
65
93
|
((_a = moduleInfo.code) === null || _a === void 0 ? void 0 : _a.includes(searchEntry))) {
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
});
|
|
69
|
-
babelTraverse(ast, {
|
|
70
|
-
ImportDeclaration: (path) => {
|
|
71
|
-
const isNativeTypesImport = CORE_TYPES_IMPORT_REGEX.test(path.node.source.value);
|
|
72
|
-
const isCustomTypeImport = typeFileRegex.test(path.node.source.value);
|
|
73
|
-
if (!(isNativeTypesImport || isCustomTypeImport)) {
|
|
74
|
-
path.remove();
|
|
75
|
-
}
|
|
76
|
-
},
|
|
77
|
-
ExportDefaultDeclaration: (path) => {
|
|
78
|
-
path.remove();
|
|
79
|
-
},
|
|
80
|
-
});
|
|
81
|
-
const tempFilePath = moduleInfo.id
|
|
82
|
-
.replace(".emb.", ".emb-temp.")
|
|
83
|
-
.replace(/\.ts$/, ".js");
|
|
84
|
-
await fs__namespace.writeFile(tempFilePath, babelGenerate(ast).code);
|
|
85
|
-
const module = await import(tempFilePath);
|
|
86
|
-
const configJSON = JSON.stringify(module.meta, (_key, value) => typeof value === "object" &&
|
|
94
|
+
const meta = await loadComponentMeta(moduleInfo.id, moduleInfo.code);
|
|
95
|
+
const configJSON = JSON.stringify(meta, (_key, value) => typeof value === "object" &&
|
|
87
96
|
value !== null &&
|
|
88
97
|
"__embeddableType" in value
|
|
89
98
|
? value.toString()
|
|
90
99
|
: value);
|
|
91
|
-
await fs__namespace.rm(tempFilePath);
|
|
92
100
|
configs.push(configJSON);
|
|
93
101
|
}
|
|
94
102
|
},
|
|
@@ -124,9 +132,511 @@ var findFiles = async (initialSrcDir, regex) => {
|
|
|
124
132
|
return filesList;
|
|
125
133
|
};
|
|
126
134
|
|
|
135
|
+
var util;
|
|
136
|
+
(function (util) {
|
|
137
|
+
util.assertEqual = (val) => val;
|
|
138
|
+
function assertIs(_arg) { }
|
|
139
|
+
util.assertIs = assertIs;
|
|
140
|
+
function assertNever(_x) {
|
|
141
|
+
throw new Error();
|
|
142
|
+
}
|
|
143
|
+
util.assertNever = assertNever;
|
|
144
|
+
util.arrayToEnum = (items) => {
|
|
145
|
+
const obj = {};
|
|
146
|
+
for (const item of items) {
|
|
147
|
+
obj[item] = item;
|
|
148
|
+
}
|
|
149
|
+
return obj;
|
|
150
|
+
};
|
|
151
|
+
util.getValidEnumValues = (obj) => {
|
|
152
|
+
const validKeys = util.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
|
|
153
|
+
const filtered = {};
|
|
154
|
+
for (const k of validKeys) {
|
|
155
|
+
filtered[k] = obj[k];
|
|
156
|
+
}
|
|
157
|
+
return util.objectValues(filtered);
|
|
158
|
+
};
|
|
159
|
+
util.objectValues = (obj) => {
|
|
160
|
+
return util.objectKeys(obj).map(function (e) {
|
|
161
|
+
return obj[e];
|
|
162
|
+
});
|
|
163
|
+
};
|
|
164
|
+
util.objectKeys = typeof Object.keys === "function" // eslint-disable-line ban/ban
|
|
165
|
+
? (obj) => Object.keys(obj) // eslint-disable-line ban/ban
|
|
166
|
+
: (object) => {
|
|
167
|
+
const keys = [];
|
|
168
|
+
for (const key in object) {
|
|
169
|
+
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
170
|
+
keys.push(key);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return keys;
|
|
174
|
+
};
|
|
175
|
+
util.find = (arr, checker) => {
|
|
176
|
+
for (const item of arr) {
|
|
177
|
+
if (checker(item))
|
|
178
|
+
return item;
|
|
179
|
+
}
|
|
180
|
+
return undefined;
|
|
181
|
+
};
|
|
182
|
+
util.isInteger = typeof Number.isInteger === "function"
|
|
183
|
+
? (val) => Number.isInteger(val) // eslint-disable-line ban/ban
|
|
184
|
+
: (val) => typeof val === "number" && isFinite(val) && Math.floor(val) === val;
|
|
185
|
+
function joinValues(array, separator = " | ") {
|
|
186
|
+
return array
|
|
187
|
+
.map((val) => (typeof val === "string" ? `'${val}'` : val))
|
|
188
|
+
.join(separator);
|
|
189
|
+
}
|
|
190
|
+
util.joinValues = joinValues;
|
|
191
|
+
util.jsonStringifyReplacer = (_, value) => {
|
|
192
|
+
if (typeof value === "bigint") {
|
|
193
|
+
return value.toString();
|
|
194
|
+
}
|
|
195
|
+
return value;
|
|
196
|
+
};
|
|
197
|
+
})(util || (util = {}));
|
|
198
|
+
var objectUtil;
|
|
199
|
+
(function (objectUtil) {
|
|
200
|
+
objectUtil.mergeShapes = (first, second) => {
|
|
201
|
+
return {
|
|
202
|
+
...first,
|
|
203
|
+
...second, // second overwrites first
|
|
204
|
+
};
|
|
205
|
+
};
|
|
206
|
+
})(objectUtil || (objectUtil = {}));
|
|
207
|
+
util.arrayToEnum([
|
|
208
|
+
"string",
|
|
209
|
+
"nan",
|
|
210
|
+
"number",
|
|
211
|
+
"integer",
|
|
212
|
+
"float",
|
|
213
|
+
"boolean",
|
|
214
|
+
"date",
|
|
215
|
+
"bigint",
|
|
216
|
+
"symbol",
|
|
217
|
+
"function",
|
|
218
|
+
"undefined",
|
|
219
|
+
"null",
|
|
220
|
+
"array",
|
|
221
|
+
"object",
|
|
222
|
+
"unknown",
|
|
223
|
+
"promise",
|
|
224
|
+
"void",
|
|
225
|
+
"never",
|
|
226
|
+
"map",
|
|
227
|
+
"set",
|
|
228
|
+
]);
|
|
229
|
+
|
|
230
|
+
const ZodIssueCode = util.arrayToEnum([
|
|
231
|
+
"invalid_type",
|
|
232
|
+
"invalid_literal",
|
|
233
|
+
"custom",
|
|
234
|
+
"invalid_union",
|
|
235
|
+
"invalid_union_discriminator",
|
|
236
|
+
"invalid_enum_value",
|
|
237
|
+
"unrecognized_keys",
|
|
238
|
+
"invalid_arguments",
|
|
239
|
+
"invalid_return_type",
|
|
240
|
+
"invalid_date",
|
|
241
|
+
"invalid_string",
|
|
242
|
+
"too_small",
|
|
243
|
+
"too_big",
|
|
244
|
+
"invalid_intersection_types",
|
|
245
|
+
"not_multiple_of",
|
|
246
|
+
"not_finite",
|
|
247
|
+
]);
|
|
248
|
+
class ZodError extends Error {
|
|
249
|
+
constructor(issues) {
|
|
250
|
+
super();
|
|
251
|
+
this.issues = [];
|
|
252
|
+
this.addIssue = (sub) => {
|
|
253
|
+
this.issues = [...this.issues, sub];
|
|
254
|
+
};
|
|
255
|
+
this.addIssues = (subs = []) => {
|
|
256
|
+
this.issues = [...this.issues, ...subs];
|
|
257
|
+
};
|
|
258
|
+
const actualProto = new.target.prototype;
|
|
259
|
+
if (Object.setPrototypeOf) {
|
|
260
|
+
// eslint-disable-next-line ban/ban
|
|
261
|
+
Object.setPrototypeOf(this, actualProto);
|
|
262
|
+
}
|
|
263
|
+
else {
|
|
264
|
+
this.__proto__ = actualProto;
|
|
265
|
+
}
|
|
266
|
+
this.name = "ZodError";
|
|
267
|
+
this.issues = issues;
|
|
268
|
+
}
|
|
269
|
+
get errors() {
|
|
270
|
+
return this.issues;
|
|
271
|
+
}
|
|
272
|
+
format(_mapper) {
|
|
273
|
+
const mapper = _mapper ||
|
|
274
|
+
function (issue) {
|
|
275
|
+
return issue.message;
|
|
276
|
+
};
|
|
277
|
+
const fieldErrors = { _errors: [] };
|
|
278
|
+
const processError = (error) => {
|
|
279
|
+
for (const issue of error.issues) {
|
|
280
|
+
if (issue.code === "invalid_union") {
|
|
281
|
+
issue.unionErrors.map(processError);
|
|
282
|
+
}
|
|
283
|
+
else if (issue.code === "invalid_return_type") {
|
|
284
|
+
processError(issue.returnTypeError);
|
|
285
|
+
}
|
|
286
|
+
else if (issue.code === "invalid_arguments") {
|
|
287
|
+
processError(issue.argumentsError);
|
|
288
|
+
}
|
|
289
|
+
else if (issue.path.length === 0) {
|
|
290
|
+
fieldErrors._errors.push(mapper(issue));
|
|
291
|
+
}
|
|
292
|
+
else {
|
|
293
|
+
let curr = fieldErrors;
|
|
294
|
+
let i = 0;
|
|
295
|
+
while (i < issue.path.length) {
|
|
296
|
+
const el = issue.path[i];
|
|
297
|
+
const terminal = i === issue.path.length - 1;
|
|
298
|
+
if (!terminal) {
|
|
299
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
300
|
+
// if (typeof el === "string") {
|
|
301
|
+
// curr[el] = curr[el] || { _errors: [] };
|
|
302
|
+
// } else if (typeof el === "number") {
|
|
303
|
+
// const errorArray: any = [];
|
|
304
|
+
// errorArray._errors = [];
|
|
305
|
+
// curr[el] = curr[el] || errorArray;
|
|
306
|
+
// }
|
|
307
|
+
}
|
|
308
|
+
else {
|
|
309
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
310
|
+
curr[el]._errors.push(mapper(issue));
|
|
311
|
+
}
|
|
312
|
+
curr = curr[el];
|
|
313
|
+
i++;
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
};
|
|
318
|
+
processError(this);
|
|
319
|
+
return fieldErrors;
|
|
320
|
+
}
|
|
321
|
+
toString() {
|
|
322
|
+
return this.message;
|
|
323
|
+
}
|
|
324
|
+
get message() {
|
|
325
|
+
return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
|
|
326
|
+
}
|
|
327
|
+
get isEmpty() {
|
|
328
|
+
return this.issues.length === 0;
|
|
329
|
+
}
|
|
330
|
+
flatten(mapper = (issue) => issue.message) {
|
|
331
|
+
const fieldErrors = {};
|
|
332
|
+
const formErrors = [];
|
|
333
|
+
for (const sub of this.issues) {
|
|
334
|
+
if (sub.path.length > 0) {
|
|
335
|
+
fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
|
|
336
|
+
fieldErrors[sub.path[0]].push(mapper(sub));
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
formErrors.push(mapper(sub));
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
return { formErrors, fieldErrors };
|
|
343
|
+
}
|
|
344
|
+
get formErrors() {
|
|
345
|
+
return this.flatten();
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
ZodError.create = (issues) => {
|
|
349
|
+
const error = new ZodError(issues);
|
|
350
|
+
return error;
|
|
351
|
+
};
|
|
352
|
+
|
|
353
|
+
var errorUtil;
|
|
354
|
+
(function (errorUtil) {
|
|
355
|
+
errorUtil.errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
356
|
+
errorUtil.toString = (message) => typeof message === "string" ? message : message === null || message === void 0 ? void 0 : message.message;
|
|
357
|
+
})(errorUtil || (errorUtil = {}));
|
|
358
|
+
var ZodFirstPartyTypeKind;
|
|
359
|
+
(function (ZodFirstPartyTypeKind) {
|
|
360
|
+
ZodFirstPartyTypeKind["ZodString"] = "ZodString";
|
|
361
|
+
ZodFirstPartyTypeKind["ZodNumber"] = "ZodNumber";
|
|
362
|
+
ZodFirstPartyTypeKind["ZodNaN"] = "ZodNaN";
|
|
363
|
+
ZodFirstPartyTypeKind["ZodBigInt"] = "ZodBigInt";
|
|
364
|
+
ZodFirstPartyTypeKind["ZodBoolean"] = "ZodBoolean";
|
|
365
|
+
ZodFirstPartyTypeKind["ZodDate"] = "ZodDate";
|
|
366
|
+
ZodFirstPartyTypeKind["ZodSymbol"] = "ZodSymbol";
|
|
367
|
+
ZodFirstPartyTypeKind["ZodUndefined"] = "ZodUndefined";
|
|
368
|
+
ZodFirstPartyTypeKind["ZodNull"] = "ZodNull";
|
|
369
|
+
ZodFirstPartyTypeKind["ZodAny"] = "ZodAny";
|
|
370
|
+
ZodFirstPartyTypeKind["ZodUnknown"] = "ZodUnknown";
|
|
371
|
+
ZodFirstPartyTypeKind["ZodNever"] = "ZodNever";
|
|
372
|
+
ZodFirstPartyTypeKind["ZodVoid"] = "ZodVoid";
|
|
373
|
+
ZodFirstPartyTypeKind["ZodArray"] = "ZodArray";
|
|
374
|
+
ZodFirstPartyTypeKind["ZodObject"] = "ZodObject";
|
|
375
|
+
ZodFirstPartyTypeKind["ZodUnion"] = "ZodUnion";
|
|
376
|
+
ZodFirstPartyTypeKind["ZodDiscriminatedUnion"] = "ZodDiscriminatedUnion";
|
|
377
|
+
ZodFirstPartyTypeKind["ZodIntersection"] = "ZodIntersection";
|
|
378
|
+
ZodFirstPartyTypeKind["ZodTuple"] = "ZodTuple";
|
|
379
|
+
ZodFirstPartyTypeKind["ZodRecord"] = "ZodRecord";
|
|
380
|
+
ZodFirstPartyTypeKind["ZodMap"] = "ZodMap";
|
|
381
|
+
ZodFirstPartyTypeKind["ZodSet"] = "ZodSet";
|
|
382
|
+
ZodFirstPartyTypeKind["ZodFunction"] = "ZodFunction";
|
|
383
|
+
ZodFirstPartyTypeKind["ZodLazy"] = "ZodLazy";
|
|
384
|
+
ZodFirstPartyTypeKind["ZodLiteral"] = "ZodLiteral";
|
|
385
|
+
ZodFirstPartyTypeKind["ZodEnum"] = "ZodEnum";
|
|
386
|
+
ZodFirstPartyTypeKind["ZodEffects"] = "ZodEffects";
|
|
387
|
+
ZodFirstPartyTypeKind["ZodNativeEnum"] = "ZodNativeEnum";
|
|
388
|
+
ZodFirstPartyTypeKind["ZodOptional"] = "ZodOptional";
|
|
389
|
+
ZodFirstPartyTypeKind["ZodNullable"] = "ZodNullable";
|
|
390
|
+
ZodFirstPartyTypeKind["ZodDefault"] = "ZodDefault";
|
|
391
|
+
ZodFirstPartyTypeKind["ZodCatch"] = "ZodCatch";
|
|
392
|
+
ZodFirstPartyTypeKind["ZodPromise"] = "ZodPromise";
|
|
393
|
+
ZodFirstPartyTypeKind["ZodBranded"] = "ZodBranded";
|
|
394
|
+
ZodFirstPartyTypeKind["ZodPipeline"] = "ZodPipeline";
|
|
395
|
+
ZodFirstPartyTypeKind["ZodReadonly"] = "ZodReadonly";
|
|
396
|
+
})(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
|
|
397
|
+
|
|
398
|
+
const errorFormatter = (issues) => {
|
|
399
|
+
const errors = [];
|
|
400
|
+
for (let issue of issues) {
|
|
401
|
+
if (issue.code === ZodIssueCode.invalid_union && issue.unionErrors.length) {
|
|
402
|
+
const error = issue.unionErrors[issue.unionErrors.length - 1];
|
|
403
|
+
issue = error.issues[error.issues.length - 1];
|
|
404
|
+
}
|
|
405
|
+
const path = formatErrorPath(issue.path);
|
|
406
|
+
const message = issue.message;
|
|
407
|
+
errors.push(`"${path}": ${message}`);
|
|
408
|
+
}
|
|
409
|
+
return errors;
|
|
410
|
+
};
|
|
411
|
+
const formatErrorPath = (path) => {
|
|
412
|
+
let formatted = "";
|
|
413
|
+
for (const pathElement of path) {
|
|
414
|
+
if (formatted.length === 0) {
|
|
415
|
+
formatted = `${pathElement}`;
|
|
416
|
+
}
|
|
417
|
+
else {
|
|
418
|
+
formatted +=
|
|
419
|
+
typeof pathElement === "number"
|
|
420
|
+
? `[${pathElement}]`
|
|
421
|
+
: `.${pathElement}`;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
return formatted;
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
const embeddableTypeSchema = zod.z
|
|
428
|
+
.object({
|
|
429
|
+
typeConfig: zod.z.object({
|
|
430
|
+
label: zod.z.string(),
|
|
431
|
+
toString: zod.z.function(),
|
|
432
|
+
}),
|
|
433
|
+
})
|
|
434
|
+
.or(zod.z.enum(core.ALL_NATIVE_TYPES));
|
|
435
|
+
|
|
436
|
+
const editorMetaSchema = zod.z
|
|
437
|
+
.object({
|
|
438
|
+
name: zod.z.string(),
|
|
439
|
+
label: zod.z.string(),
|
|
440
|
+
type: embeddableTypeSchema,
|
|
441
|
+
})
|
|
442
|
+
.strict();
|
|
443
|
+
|
|
444
|
+
const editorMetaValidator = (meta) => {
|
|
445
|
+
const result = editorMetaSchema.safeParse(meta);
|
|
446
|
+
if (result.success)
|
|
447
|
+
return [];
|
|
448
|
+
return errorFormatter(result.error.issues);
|
|
449
|
+
};
|
|
450
|
+
|
|
451
|
+
const componentMetaSchema = zod.z
|
|
452
|
+
.object({
|
|
453
|
+
name: zod.z.string(),
|
|
454
|
+
label: zod.z.string(),
|
|
455
|
+
inputs: zod.z
|
|
456
|
+
.object({
|
|
457
|
+
name: zod.z.string(),
|
|
458
|
+
label: zod.z.string(),
|
|
459
|
+
description: zod.z.string().optional(),
|
|
460
|
+
defaultValue: zod.z.any().optional(),
|
|
461
|
+
config: zod.z.record(zod.z.string(), zod.z.any()).optional(),
|
|
462
|
+
type: embeddableTypeSchema,
|
|
463
|
+
array: zod.z.boolean().optional(),
|
|
464
|
+
})
|
|
465
|
+
.strict()
|
|
466
|
+
.array()
|
|
467
|
+
.optional(),
|
|
468
|
+
events: zod.z
|
|
469
|
+
.object({
|
|
470
|
+
name: zod.z.string(),
|
|
471
|
+
label: zod.z.string(),
|
|
472
|
+
properties: zod.z
|
|
473
|
+
.object({
|
|
474
|
+
name: zod.z.string(),
|
|
475
|
+
type: embeddableTypeSchema,
|
|
476
|
+
label: zod.z.string().optional(),
|
|
477
|
+
})
|
|
478
|
+
.array()
|
|
479
|
+
.optional(),
|
|
480
|
+
})
|
|
481
|
+
.strict()
|
|
482
|
+
.array()
|
|
483
|
+
.optional(),
|
|
484
|
+
variables: zod.z
|
|
485
|
+
.object({
|
|
486
|
+
name: zod.z.string(),
|
|
487
|
+
type: embeddableTypeSchema,
|
|
488
|
+
defaultValue: zod.z.any().optional(),
|
|
489
|
+
inputs: zod.z.array(zod.z.string()).optional(),
|
|
490
|
+
events: zod.z
|
|
491
|
+
.array(zod.z.object({
|
|
492
|
+
name: zod.z.string(),
|
|
493
|
+
property: zod.z.string(),
|
|
494
|
+
}))
|
|
495
|
+
.optional(),
|
|
496
|
+
})
|
|
497
|
+
.strict()
|
|
498
|
+
.array()
|
|
499
|
+
.optional(),
|
|
500
|
+
})
|
|
501
|
+
.strict();
|
|
502
|
+
|
|
503
|
+
const componentMetaValidator = (meta) => {
|
|
504
|
+
const result = componentMetaSchema.safeParse(meta);
|
|
505
|
+
if (!result.success)
|
|
506
|
+
return errorFormatter(result.error.issues);
|
|
507
|
+
return validateVariables(meta);
|
|
508
|
+
};
|
|
509
|
+
const validateVariables = (meta) => {
|
|
510
|
+
const variables = meta.variables;
|
|
511
|
+
if (!variables)
|
|
512
|
+
return [];
|
|
513
|
+
const errors = [];
|
|
514
|
+
// all inputs should be defined
|
|
515
|
+
errors.push(...validateVariableInputs(meta));
|
|
516
|
+
// all events and properties should be valid
|
|
517
|
+
errors.push(...validateVariableEvents(meta));
|
|
518
|
+
return errors;
|
|
519
|
+
};
|
|
520
|
+
const validateVariableInputs = (meta) => {
|
|
521
|
+
const variables = meta.variables;
|
|
522
|
+
const definedInputs = meta.inputs;
|
|
523
|
+
if (!variables)
|
|
524
|
+
return [];
|
|
525
|
+
const errors = [];
|
|
526
|
+
// all inputs should be defined
|
|
527
|
+
variables.forEach((variableConfig, idx) => {
|
|
528
|
+
const inputs = variableConfig.inputs;
|
|
529
|
+
if (inputs) {
|
|
530
|
+
inputs.forEach((input, inputIdx) => {
|
|
531
|
+
const definedInput = definedInputs === null || definedInputs === void 0 ? void 0 : definedInputs.find((d) => d.name === input);
|
|
532
|
+
if (!definedInput) {
|
|
533
|
+
const path = formatErrorPath(["variables", idx, "inputs", inputIdx]);
|
|
534
|
+
errors.push(`${path}: input "${input}" is not defined`);
|
|
535
|
+
}
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
});
|
|
539
|
+
return errors;
|
|
540
|
+
};
|
|
541
|
+
const validateVariableEvents = (meta) => {
|
|
542
|
+
const variables = meta.variables;
|
|
543
|
+
const definedEvents = meta.events;
|
|
544
|
+
if (!variables)
|
|
545
|
+
return [];
|
|
546
|
+
const errors = [];
|
|
547
|
+
variables.forEach((variableConfig, idx) => {
|
|
548
|
+
const events = variableConfig.events;
|
|
549
|
+
if (events) {
|
|
550
|
+
events.forEach((event, eventIdx) => {
|
|
551
|
+
const definedEvent = definedEvents === null || definedEvents === void 0 ? void 0 : definedEvents.find((d) => d.name === event.name);
|
|
552
|
+
if (!definedEvent) {
|
|
553
|
+
const path = formatErrorPath([
|
|
554
|
+
"variables",
|
|
555
|
+
idx,
|
|
556
|
+
"events",
|
|
557
|
+
eventIdx,
|
|
558
|
+
"name",
|
|
559
|
+
]);
|
|
560
|
+
errors.push(`${path}: event "${event.name}" is not defined`);
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
const definedProperties = definedEvent.properties;
|
|
564
|
+
const definedProperty = definedProperties === null || definedProperties === void 0 ? void 0 : definedProperties.find((p) => p.name === event.property);
|
|
565
|
+
if (!definedProperty) {
|
|
566
|
+
const path = formatErrorPath([
|
|
567
|
+
"variables",
|
|
568
|
+
idx,
|
|
569
|
+
"events",
|
|
570
|
+
eventIdx,
|
|
571
|
+
"property",
|
|
572
|
+
]);
|
|
573
|
+
errors.push(`${path}: property "${event.property}" is not defined`);
|
|
574
|
+
return;
|
|
575
|
+
}
|
|
576
|
+
if (definedProperty.type !== variableConfig.type) {
|
|
577
|
+
const path = formatErrorPath(["variables", idx]);
|
|
578
|
+
errors.push(`${path}: the type of the variable "${variableConfig.name}" and the type of the property "${event.property}" do not match`);
|
|
579
|
+
}
|
|
580
|
+
});
|
|
581
|
+
}
|
|
582
|
+
});
|
|
583
|
+
return errors;
|
|
584
|
+
};
|
|
585
|
+
|
|
586
|
+
const EDITOR = "editor";
|
|
587
|
+
const COMPONENT = "component";
|
|
588
|
+
const validators = {
|
|
589
|
+
[EDITOR]: editorMetaValidator,
|
|
590
|
+
[COMPONENT]: componentMetaValidator,
|
|
591
|
+
};
|
|
592
|
+
const getModuleType = (moduleInfo) => {
|
|
593
|
+
var _a, _b;
|
|
594
|
+
if ((_a = moduleInfo.code) === null || _a === void 0 ? void 0 : _a.includes("defineComponent")) {
|
|
595
|
+
return COMPONENT;
|
|
596
|
+
}
|
|
597
|
+
else if ((_b = moduleInfo.code) === null || _b === void 0 ? void 0 : _b.includes("defineEditor")) {
|
|
598
|
+
return EDITOR;
|
|
599
|
+
}
|
|
600
|
+
else {
|
|
601
|
+
return null;
|
|
602
|
+
}
|
|
603
|
+
};
|
|
604
|
+
var validateComponentMetaPlugin = (componentFileRegex) => {
|
|
605
|
+
const metaConfigs = [];
|
|
606
|
+
return {
|
|
607
|
+
name: "validate-component-meta",
|
|
608
|
+
moduleParsed: async (moduleInfo) => {
|
|
609
|
+
const moduleType = componentFileRegex.test(moduleInfo.id)
|
|
610
|
+
? getModuleType(moduleInfo)
|
|
611
|
+
: null;
|
|
612
|
+
if (moduleType) {
|
|
613
|
+
const meta = await loadComponentMeta(moduleInfo.id, moduleInfo.code);
|
|
614
|
+
metaConfigs.push({
|
|
615
|
+
moduleId: moduleInfo.id,
|
|
616
|
+
meta,
|
|
617
|
+
moduleType,
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
},
|
|
621
|
+
buildEnd: function () {
|
|
622
|
+
const errors = [];
|
|
623
|
+
for (const metaConfig of metaConfigs) {
|
|
624
|
+
const validator = validators[metaConfig.moduleType];
|
|
625
|
+
const validationResult = validator(metaConfig.meta);
|
|
626
|
+
if (validationResult && validationResult.length) {
|
|
627
|
+
errors.push(`\nComponent meta is not valid ${metaConfig.moduleId}:\n${validationResult.join("\n")}`);
|
|
628
|
+
}
|
|
629
|
+
}
|
|
630
|
+
if (errors.length) {
|
|
631
|
+
// @ts-ignore
|
|
632
|
+
this.error(errors.join("\n"));
|
|
633
|
+
}
|
|
634
|
+
},
|
|
635
|
+
};
|
|
636
|
+
};
|
|
637
|
+
|
|
127
638
|
const oraP = import('ora');
|
|
128
639
|
const EMB_FILE_REGEX = /^(.*)(?<!\.type|\.options)\.emb\.[jt]s$/;
|
|
129
|
-
const EMB_TYPE_FILE_REGEX = /^(.*)\.type\.emb\.[jt]s$/;
|
|
130
640
|
var generate = async (ctx) => {
|
|
131
641
|
const ora = (await oraP).default;
|
|
132
642
|
const progress = ora("React: building components...").start();
|
|
@@ -146,7 +656,6 @@ async function runViteBuild(ctx) {
|
|
|
146
656
|
fileName: "embeddable-components-meta.js",
|
|
147
657
|
outputDir: ".embeddable-build",
|
|
148
658
|
componentFileRegex: EMB_FILE_REGEX,
|
|
149
|
-
typeFileRegex: EMB_TYPE_FILE_REGEX,
|
|
150
659
|
searchEntry: "defineComponent",
|
|
151
660
|
}),
|
|
152
661
|
extractComponentsConfigPlugin({
|
|
@@ -154,9 +663,9 @@ async function runViteBuild(ctx) {
|
|
|
154
663
|
fileName: "embeddable-editors-meta.js",
|
|
155
664
|
outputDir: ".embeddable-build",
|
|
156
665
|
componentFileRegex: EMB_FILE_REGEX,
|
|
157
|
-
typeFileRegex: EMB_TYPE_FILE_REGEX,
|
|
158
666
|
searchEntry: "defineEditor",
|
|
159
667
|
}),
|
|
668
|
+
validateComponentMetaPlugin(EMB_FILE_REGEX),
|
|
160
669
|
],
|
|
161
670
|
build: {
|
|
162
671
|
sourcemap: true,
|