@kizenapps/packager 0.1.0

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/dist/index.js ADDED
@@ -0,0 +1,749 @@
1
+ // src/lib/minify.ts
2
+ var minifyFile = async (file) => {
3
+ if (file.path.endsWith(".js")) {
4
+ if (!file.content) {
5
+ return "";
6
+ }
7
+ const { minify } = await import("@node-minify/core");
8
+ const { uglifyJs } = await import("@node-minify/uglify-js");
9
+ const result = await minify({
10
+ compressor: uglifyJs,
11
+ content: file.content,
12
+ options: {
13
+ mangle: true,
14
+ parse: {
15
+ bare_returns: true
16
+ }
17
+ }
18
+ });
19
+ return result;
20
+ }
21
+ return file.content;
22
+ };
23
+ var minifyFiles = async (files) => {
24
+ return Promise.all(
25
+ files.map(async (file) => {
26
+ const minified = await minifyFile(file);
27
+ return { ...file, minified };
28
+ })
29
+ );
30
+ };
31
+
32
+ // src/lib/apiNames.ts
33
+ var sanitizeToAPIName = (name) => {
34
+ return name.toLowerCase().replace(/[^a-z0-9\-\s]/g, "").replace(/\s+/g, "_");
35
+ };
36
+ var scriptRuntimeToApiName = (runtime) => {
37
+ const result = runtime.replace(/\s+/g, "-").replace(/\./g, "-");
38
+ return result;
39
+ };
40
+
41
+ // src/lib/image.ts
42
+ var imagetoUint8Array = (base64Image) => {
43
+ const byteCharacters = atob(base64Image);
44
+ const byteNumbers = new Array(byteCharacters.length);
45
+ for (let i = 0; i < byteCharacters.length; i++) {
46
+ byteNumbers[i] = byteCharacters.charCodeAt(i);
47
+ }
48
+ return new Uint8Array(byteNumbers);
49
+ };
50
+ var zipToUint8Array = (zip) => {
51
+ return Uint8Array.from(zip);
52
+ };
53
+
54
+ // src/lib/structure.ts
55
+ var thumbnailImageExtensions = ["png"];
56
+ var triggerImageExtensions = [...thumbnailImageExtensions, "svg"];
57
+ var EVENT_SCRIPTS_DIRECTORY_NAME = "eventScripts";
58
+ var FLOATING_FRAMES_DIRECTORY_NAME = "floatingFrames";
59
+ var DATA_ADORNMENTS_DIRECTORY_NAME = "dataAdornments";
60
+ var TOOLBAR_ITEMS_DIRECTORY_NAME = "toolbarItems";
61
+ var ROUTABLE_PAGES_DIRECTORY_NAME = "pages";
62
+ var BROWSER_ROUTE_SCRIPTS_DIRECTORY_NAME = "routeScripts";
63
+ var JS_ACTIONS_DIRECTORY_NAME = "actions";
64
+ var OBJECT_SETTINGS_ITEMS_DIRECTORY_NAME = "objectSettingsItems";
65
+ var AUTOMATION_STEPS_DIRECTORY_NAME = "automationSteps";
66
+ var SETUP_ASSISTANT_DIRECTORY_NAME = "setupAssistant";
67
+ var USER_SETUP_ASSISTANT_DIRECTORY_NAME = "userSetupAssistant";
68
+ var CALENDAR_SOURCES_DIRECTORY_NAME = "calendarSources";
69
+ var THUMBNAIL_FILE_NAMES = thumbnailImageExtensions.map(
70
+ (ext) => `thumbnail.${ext}`
71
+ );
72
+ var RELEASE_NOTES_EXTENSION = "md";
73
+ var SCRIPT_EXTENSION = "js";
74
+ var MAIN_SCRIPT_FILE = `script.${SCRIPT_EXTENSION}`;
75
+ var MESSAGE_SCRIPT_FILE = `message.${SCRIPT_EXTENSION}`;
76
+ var CALLBACK_SCRIPT_FILE = `callback.${SCRIPT_EXTENSION}`;
77
+ var AUTOMATION_SCRIPT_FILE = `script.py`;
78
+ var CONFIG_FILE_NAME = "config.json";
79
+ var STYLES_FILE_NAME = "styles.css";
80
+ var MANIFEST_FILE_NAME = "kizen.json";
81
+ var KZN_FILE_NAME = "import.kzn";
82
+ var maybeParseConfig = (content, path) => {
83
+ if (!content) {
84
+ throw new Error(`No content provided for config file: ${path}`);
85
+ }
86
+ try {
87
+ return JSON.parse(content);
88
+ } catch {
89
+ throw new Error(`Failed to parse config content: ${path}`);
90
+ }
91
+ };
92
+ var getMinimizedConfig = (config, entry, splitPath, consideredFiles) => {
93
+ const result = config["minimized_config"] ?? {};
94
+ if (result["customIconFile"]) {
95
+ let basePath = splitPath.slice(0, -1).join("/");
96
+ if (entry.endsWith("/")) {
97
+ basePath = `${entry}${basePath}`;
98
+ } else {
99
+ basePath = `${entry}/${basePath}`;
100
+ }
101
+ const filePath = `${basePath}/${result["customIconFile"]}`;
102
+ const file = consideredFiles.find((f) => f.path === filePath);
103
+ if (file?.base64Image) {
104
+ let extension = filePath.split(".").pop()?.toLowerCase();
105
+ if (extension === "svg") {
106
+ extension = "svg+xml";
107
+ }
108
+ result["customIcon"] = `data:image/${extension};base64,${file.base64Image}`;
109
+ }
110
+ }
111
+ return result;
112
+ };
113
+ var getAdornmentIcon = (config, entry, splitPath, consideredFiles) => {
114
+ if (config["customIconFile"]) {
115
+ let basePath = splitPath.slice(0, -1).join("/");
116
+ if (entry.endsWith("/")) {
117
+ basePath = `${entry}${basePath}`;
118
+ } else {
119
+ basePath = `${entry}/${basePath}`;
120
+ }
121
+ const filePath = `${basePath}/${config["customIconFile"]}`;
122
+ const file = consideredFiles.find((f) => f.path === filePath);
123
+ if (file?.base64Image) {
124
+ let extension = filePath.split(".").pop()?.toLowerCase();
125
+ if (extension === "svg") {
126
+ extension = "svg+xml";
127
+ }
128
+ return `data:image/${extension};base64,${file.base64Image}`;
129
+ }
130
+ }
131
+ return "";
132
+ };
133
+
134
+ // src/lib/package.ts
135
+ var replacementVar = "__kizen_state";
136
+ var prepend = "const __kizen_utils = {};\n";
137
+ var convertToSelfInvokingFunction = (fn, variable, args = {}, prependCode = "") => {
138
+ const functionString = fn.replace(/\n$/, "").replace(/;$/, "");
139
+ const inner = `(${functionString})({ state: {{${variable}}}, args: ${JSON.stringify(args)}, utils: __kizen_utils })`;
140
+ const full = `(function() { ${prependCode}
141
+ return ${inner}; })()`;
142
+ return full;
143
+ };
144
+ var mapFields = (fields, scriptsByKey) => {
145
+ return fields.map((field) => {
146
+ if (field["type"] === "container" && field["fields"] && Array.isArray(field["fields"])) {
147
+ return {
148
+ ...field,
149
+ fields: mapFields(
150
+ field["fields"],
151
+ scriptsByKey
152
+ )
153
+ };
154
+ }
155
+ const fieldKey = field["key"];
156
+ const scripts = scriptsByKey[fieldKey];
157
+ if (scripts) {
158
+ const result = { ...field };
159
+ for (const fnName of [
160
+ "getFetchUrl",
161
+ "optionMapper",
162
+ "getHeaders",
163
+ "getBody",
164
+ "getContextUrl"
165
+ ]) {
166
+ const fn = scripts[fnName];
167
+ if (fn) {
168
+ result[fnName] = convertToSelfInvokingFunction(
169
+ fn,
170
+ replacementVar,
171
+ {},
172
+ prepend
173
+ );
174
+ }
175
+ }
176
+ return result;
177
+ }
178
+ return field;
179
+ });
180
+ };
181
+ var processSetupAssistantFromFiles = (assistantConfig, scripts) => {
182
+ if (!assistantConfig) {
183
+ return void 0;
184
+ }
185
+ const scriptsByKey = scripts.reduce(
186
+ (acc, script) => {
187
+ if (!acc[script.key]) {
188
+ acc[script.key] = {};
189
+ }
190
+ acc[script.key][script.type] = script.content;
191
+ return acc;
192
+ },
193
+ {}
194
+ );
195
+ const newConfig = { ...assistantConfig };
196
+ if (newConfig["fields"] && Array.isArray(newConfig["fields"])) {
197
+ newConfig["fields"] = mapFields(
198
+ newConfig["fields"],
199
+ scriptsByKey
200
+ );
201
+ }
202
+ return newConfig;
203
+ };
204
+ var processOnePluginFromManifest = (manifest, files) => {
205
+ const entry = manifest.entry;
206
+ const releaseNotesDirectory = manifest.release_notes_directory;
207
+ const apiName = manifest.api_name;
208
+ const consideredFiles = files.filter(
209
+ (f) => f.path !== MANIFEST_FILE_NAME && f.path.startsWith(entry)
210
+ );
211
+ const releaseNotesFiles = releaseNotesDirectory ? files.filter((f) => f.path.startsWith(releaseNotesDirectory)) : [];
212
+ const { version } = manifest;
213
+ const releaseNotes = releaseNotesFiles.find(
214
+ (file) => file.path.endsWith(`${version}.${RELEASE_NOTES_EXTENSION}`)
215
+ );
216
+ const floatingFrames = {};
217
+ const dataAdornments = {};
218
+ const toolbarItems = {};
219
+ const routablePages = {};
220
+ const routeScripts = {};
221
+ const jsActions = {};
222
+ const objectSettingsItems = {};
223
+ const automationSteps = {};
224
+ const calendarSources = {};
225
+ let thumbnail = null;
226
+ let kznFile = null;
227
+ let hasBlockingCondition = false;
228
+ const requiredConfigFiles = {};
229
+ let setupAssistantJSON = null;
230
+ const setupAssistantFunctions = [];
231
+ let userSetupAssistantJSON = null;
232
+ const userSetupAssistantFunctions = [];
233
+ for (const file of consideredFiles) {
234
+ let strippedPath = file.path.replace(entry, "");
235
+ strippedPath = strippedPath.startsWith("/") ? strippedPath.substring(1) : strippedPath;
236
+ const splitPath = strippedPath.split("/");
237
+ if (splitPath[0] === SETUP_ASSISTANT_DIRECTORY_NAME) {
238
+ if (splitPath[1] === "assistant.json") {
239
+ setupAssistantJSON = maybeParseConfig(file.content, file.path);
240
+ } else {
241
+ const assistantKey = splitPath[1];
242
+ if (assistantKey && splitPath[2]?.endsWith(`.${SCRIPT_EXTENSION}`)) {
243
+ setupAssistantFunctions.push({
244
+ key: assistantKey,
245
+ type: splitPath[2].replace(`.${SCRIPT_EXTENSION}`, ""),
246
+ content: file.content
247
+ });
248
+ }
249
+ }
250
+ } else if (splitPath[0] === USER_SETUP_ASSISTANT_DIRECTORY_NAME) {
251
+ if (splitPath[1] === "assistant.json") {
252
+ userSetupAssistantJSON = maybeParseConfig(file.content, file.path);
253
+ } else {
254
+ const assistantKey = splitPath[1];
255
+ if (assistantKey && splitPath[2]?.endsWith(`.${SCRIPT_EXTENSION}`)) {
256
+ userSetupAssistantFunctions.push({
257
+ key: assistantKey,
258
+ type: splitPath[2].replace(`.${SCRIPT_EXTENSION}`, ""),
259
+ content: file.content
260
+ });
261
+ }
262
+ }
263
+ } else if (splitPath[0] === FLOATING_FRAMES_DIRECTORY_NAME) {
264
+ const floatingFrameName = splitPath[1];
265
+ if (!floatingFrameName) continue;
266
+ if (!floatingFrames[floatingFrameName]) {
267
+ requiredConfigFiles[`frame:${floatingFrameName}`] = true;
268
+ floatingFrames[floatingFrameName] = {
269
+ name: "",
270
+ api_name: "",
271
+ title: "",
272
+ type: "script",
273
+ css: "",
274
+ event_scripts: {},
275
+ default_position: "bottom-right",
276
+ header_color: "",
277
+ header_text_color: "",
278
+ height: 0,
279
+ width: 0,
280
+ ignore: [],
281
+ match: [],
282
+ message_handler: "",
283
+ minimized_style: "circle",
284
+ minimized_config: {},
285
+ script: "",
286
+ html: "",
287
+ when: ""
288
+ };
289
+ }
290
+ const frame = floatingFrames[floatingFrameName];
291
+ if (splitPath[2] === EVENT_SCRIPTS_DIRECTORY_NAME) {
292
+ if (splitPath[3]?.endsWith(`.${SCRIPT_EXTENSION}`)) {
293
+ const functionName = splitPath[3].replace(`.${SCRIPT_EXTENSION}`, "");
294
+ frame.event_scripts[functionName] = file.minified;
295
+ }
296
+ } else if (splitPath[2] === CONFIG_FILE_NAME) {
297
+ requiredConfigFiles[`frame:${floatingFrameName}`] = false;
298
+ const config = maybeParseConfig(file.content, file.path);
299
+ frame.name = config["name"] || "";
300
+ frame.api_name = config["api_name"] || sanitizeToAPIName(floatingFrameName);
301
+ frame.title = config["title"] || "";
302
+ frame.default_position = config["default_position"] || "bottom-right";
303
+ frame.header_color = config["header_color"] || "";
304
+ frame.header_text_color = config["header_text_color"] || "";
305
+ frame.height = config["height"] || 0;
306
+ frame.width = config["width"] || 0;
307
+ frame.ignore = config["ignore"] || [];
308
+ frame.match = config["match"] || [];
309
+ frame.html = config["html"] || "";
310
+ frame.minimized_style = config["minimized_style"] || "circle";
311
+ frame.minimized_config = getMinimizedConfig(
312
+ config,
313
+ entry,
314
+ splitPath,
315
+ consideredFiles
316
+ );
317
+ frame.when = config["when"] || "";
318
+ } else if (splitPath[2] === MESSAGE_SCRIPT_FILE) {
319
+ frame.message_handler = file.minified;
320
+ } else if (splitPath[2] === MAIN_SCRIPT_FILE) {
321
+ frame.script = file.minified;
322
+ } else if (splitPath[2] === STYLES_FILE_NAME) {
323
+ frame.css = file.minified;
324
+ }
325
+ } else if (splitPath[0] === DATA_ADORNMENTS_DIRECTORY_NAME) {
326
+ const dataAdornmentName = splitPath[1];
327
+ if (!dataAdornmentName) continue;
328
+ if (!dataAdornments[dataAdornmentName]) {
329
+ requiredConfigFiles[`dataAdornment:${dataAdornmentName}`] = true;
330
+ dataAdornments[dataAdornmentName] = {
331
+ config: { icon: "", color: "", tooltip: "" },
332
+ field_type: "phonenumber",
333
+ script: "",
334
+ when: ""
335
+ };
336
+ }
337
+ const adornment = dataAdornments[dataAdornmentName];
338
+ if (splitPath[2] === CONFIG_FILE_NAME) {
339
+ requiredConfigFiles[`dataAdornment:${dataAdornmentName}`] = false;
340
+ const config = maybeParseConfig(file.content, file.path);
341
+ adornment.config.icon = config["icon"] || "";
342
+ adornment.config.color = config["color"] || "";
343
+ adornment.config.tooltip = config["tooltip"] || "";
344
+ adornment.config.customIcon = getAdornmentIcon(
345
+ config,
346
+ entry,
347
+ splitPath,
348
+ consideredFiles
349
+ );
350
+ adornment.field_type = config["field_type"] || "phonenumber";
351
+ adornment.when = config["when"] || "";
352
+ if (config["when"]) hasBlockingCondition = true;
353
+ } else if (splitPath[2] === MAIN_SCRIPT_FILE) {
354
+ adornment.script = file.minified;
355
+ }
356
+ } else if (splitPath[0] === TOOLBAR_ITEMS_DIRECTORY_NAME) {
357
+ const toolbarItemName = splitPath[1];
358
+ if (!toolbarItemName) continue;
359
+ if (!toolbarItems[toolbarItemName]) {
360
+ requiredConfigFiles[`toolbarItem:${toolbarItemName}`] = true;
361
+ toolbarItems[toolbarItemName] = {
362
+ api_name: "",
363
+ label: "",
364
+ icon: "",
365
+ color: "",
366
+ script: "",
367
+ when: ""
368
+ };
369
+ }
370
+ const item = toolbarItems[toolbarItemName];
371
+ if (splitPath[2] === CONFIG_FILE_NAME) {
372
+ requiredConfigFiles[`toolbarItem:${toolbarItemName}`] = false;
373
+ const config = maybeParseConfig(file.content, file.path);
374
+ item.api_name = config["api_name"] || sanitizeToAPIName(toolbarItemName);
375
+ item.label = config["label"] || "";
376
+ item.icon = config["icon"] || "";
377
+ item.color = config["color"] || "";
378
+ item.when = config["when"] || "";
379
+ if (config["when"]) hasBlockingCondition = true;
380
+ } else if (splitPath[2] === MAIN_SCRIPT_FILE) {
381
+ item.script = file.minified;
382
+ }
383
+ } else if (splitPath[0] === ROUTABLE_PAGES_DIRECTORY_NAME) {
384
+ const pageName = splitPath[1];
385
+ if (!pageName) continue;
386
+ if (!routablePages[pageName]) {
387
+ requiredConfigFiles[`page:${pageName}`] = true;
388
+ routablePages[pageName] = {
389
+ name: pageName,
390
+ api_name: "",
391
+ event_scripts: {},
392
+ script: "",
393
+ callback: "",
394
+ css: "",
395
+ is_toolbar_item: false,
396
+ toolbar_color: "",
397
+ toolbar_icon: "",
398
+ type: "script",
399
+ html: "",
400
+ iframe_url: ""
401
+ };
402
+ }
403
+ const page = routablePages[pageName];
404
+ if (splitPath[2] === EVENT_SCRIPTS_DIRECTORY_NAME) {
405
+ if (splitPath[3]?.endsWith(SCRIPT_EXTENSION)) {
406
+ const functionName = splitPath[3].replace(`.${SCRIPT_EXTENSION}`, "");
407
+ page.event_scripts[functionName] = file.minified;
408
+ }
409
+ } else if (splitPath[2] === CONFIG_FILE_NAME) {
410
+ requiredConfigFiles[`page:${pageName}`] = false;
411
+ const config = maybeParseConfig(file.content, file.path);
412
+ page.api_name = config["api_name"] || sanitizeToAPIName(pageName);
413
+ page.name = config["name"] || pageName;
414
+ page.is_toolbar_item = config["is_toolbar_item"] || false;
415
+ page.toolbar_color = config["toolbar_color"] || "";
416
+ page.toolbar_icon = config["toolbar_icon"] || "";
417
+ } else if (splitPath[2] === MAIN_SCRIPT_FILE) {
418
+ page.script = file.minified;
419
+ } else if (splitPath[2] === CALLBACK_SCRIPT_FILE) {
420
+ page.callback = file.minified;
421
+ } else if (splitPath[2] === STYLES_FILE_NAME) {
422
+ page.css = file.minified;
423
+ }
424
+ } else if (splitPath[0] === AUTOMATION_STEPS_DIRECTORY_NAME) {
425
+ const stepName = splitPath[1];
426
+ if (!stepName) continue;
427
+ if (!automationSteps[stepName]) {
428
+ requiredConfigFiles[`automationStep:${stepName}`] = true;
429
+ automationSteps[stepName] = {
430
+ name: "",
431
+ action_step_api_name: "",
432
+ overall_description: "",
433
+ action_description: "",
434
+ action_type: "",
435
+ script_runtime: scriptRuntimeToApiName("python 3.12"),
436
+ secrets: [],
437
+ inputs: [],
438
+ outputs: [],
439
+ script: "",
440
+ when: ""
441
+ };
442
+ }
443
+ const step = automationSteps[stepName];
444
+ if (splitPath[2] === CONFIG_FILE_NAME) {
445
+ requiredConfigFiles[`automationStep:${stepName}`] = false;
446
+ const config = maybeParseConfig(file.content, file.path);
447
+ step.name = config["name"] || stepName;
448
+ step.action_step_api_name = config["api_name"] || sanitizeToAPIName(stepName);
449
+ step.overall_description = config["plugin_description"] || "";
450
+ step.action_description = config["action_description"] || "";
451
+ step.action_type = config["action_type"] || "";
452
+ step.secrets = config["secrets"] || [];
453
+ step.inputs = config["inputs"] || [];
454
+ step.outputs = config["outputs"] || [];
455
+ step.script_runtime = scriptRuntimeToApiName(
456
+ config["runtime"] || "python 3.12"
457
+ );
458
+ step.when = config["when"] || "";
459
+ if (config["when"]) hasBlockingCondition = true;
460
+ } else if (splitPath[2] === AUTOMATION_SCRIPT_FILE) {
461
+ step.script = file.content;
462
+ }
463
+ } else if (splitPath[0] === BROWSER_ROUTE_SCRIPTS_DIRECTORY_NAME) {
464
+ const routeName = splitPath[1];
465
+ if (!routeName) continue;
466
+ if (!routeScripts[routeName]) {
467
+ requiredConfigFiles[`route:${routeName}`] = true;
468
+ routeScripts[routeName] = {
469
+ script: "",
470
+ api_name: "",
471
+ hint_object_name: "",
472
+ routes: [],
473
+ name: routeName
474
+ };
475
+ }
476
+ const route = routeScripts[routeName];
477
+ if (splitPath[2] === CONFIG_FILE_NAME) {
478
+ requiredConfigFiles[`route:${routeName}`] = false;
479
+ const config = maybeParseConfig(file.content, file.path);
480
+ route.api_name = config["api_name"] || sanitizeToAPIName(routeName);
481
+ route.hint_object_name = config["hint_object_name"] || "";
482
+ route.routes = config["routes"] || [];
483
+ route.name = config["name"] || routeName;
484
+ } else if (splitPath[2] === MAIN_SCRIPT_FILE) {
485
+ route.script = file.minified;
486
+ }
487
+ } else if (splitPath[0] === JS_ACTIONS_DIRECTORY_NAME) {
488
+ const actionName = splitPath[1];
489
+ if (!actionName) continue;
490
+ if (!jsActions[actionName]) {
491
+ requiredConfigFiles[`action:${actionName}`] = true;
492
+ jsActions[actionName] = {
493
+ script: "",
494
+ name: "",
495
+ hint_object_name: "",
496
+ api_name: ""
497
+ };
498
+ }
499
+ const action = jsActions[actionName];
500
+ if (splitPath[2] === CONFIG_FILE_NAME) {
501
+ requiredConfigFiles[`action:${actionName}`] = false;
502
+ const config = maybeParseConfig(file.content, file.path);
503
+ action.name = config["name"] || actionName;
504
+ action.hint_object_name = config["hint_object_name"] || "";
505
+ action.api_name = config["api_name"] || sanitizeToAPIName(actionName);
506
+ } else if (splitPath[2] === MAIN_SCRIPT_FILE) {
507
+ action.script = file.minified;
508
+ }
509
+ } else if (splitPath[0] === OBJECT_SETTINGS_ITEMS_DIRECTORY_NAME) {
510
+ const itemName = splitPath[1];
511
+ if (!itemName) continue;
512
+ if (!objectSettingsItems[itemName]) {
513
+ requiredConfigFiles[`objectSettingsItem:${itemName}`] = true;
514
+ objectSettingsItems[itemName] = {
515
+ api_name: "",
516
+ label: "",
517
+ script: "",
518
+ when: ""
519
+ };
520
+ }
521
+ const settingsItem = objectSettingsItems[itemName];
522
+ if (splitPath[2] === CONFIG_FILE_NAME) {
523
+ requiredConfigFiles[`objectSettingsItem:${itemName}`] = false;
524
+ const config = maybeParseConfig(file.content, file.path);
525
+ settingsItem.api_name = config["api_name"] || sanitizeToAPIName(itemName);
526
+ settingsItem.label = config["label"] || "";
527
+ settingsItem.when = config["when"] || "";
528
+ } else if (splitPath[2] === MAIN_SCRIPT_FILE) {
529
+ settingsItem.script = file.minified;
530
+ }
531
+ } else if (splitPath[0] === CALENDAR_SOURCES_DIRECTORY_NAME) {
532
+ const itemName = splitPath[1];
533
+ if (!itemName) continue;
534
+ if (!calendarSources[itemName]) {
535
+ requiredConfigFiles[`calendarSource:${itemName}`] = true;
536
+ calendarSources[itemName] = {
537
+ api_name: "",
538
+ name: "",
539
+ calendars_script: "",
540
+ events_script: "",
541
+ when: ""
542
+ };
543
+ }
544
+ const cal = calendarSources[itemName];
545
+ if (splitPath[2] === CONFIG_FILE_NAME) {
546
+ requiredConfigFiles[`calendarSource:${itemName}`] = false;
547
+ const config = maybeParseConfig(file.content, file.path);
548
+ cal.api_name = config["api_name"] || sanitizeToAPIName(itemName);
549
+ cal.name = config["name"] || "";
550
+ cal.when = config["when"] || "";
551
+ if (config["when"]) hasBlockingCondition = true;
552
+ } else if (splitPath[2] === "calendars.js") {
553
+ cal.calendars_script = file.minified;
554
+ } else if (splitPath[2] === "events.js") {
555
+ cal.events_script = file.minified;
556
+ }
557
+ } else if (splitPath[0] === KZN_FILE_NAME) {
558
+ if (!file.binaryData) {
559
+ throw new Error(
560
+ `KZN file ${file.path} does not contain binary data. Cannot proceed.`
561
+ );
562
+ }
563
+ if (kznFile) {
564
+ throw new Error(
565
+ `Multiple KZN files found for plugin ${apiName}. Only one is allowed.`
566
+ );
567
+ }
568
+ kznFile = zipToUint8Array(file.binaryData);
569
+ } else if (THUMBNAIL_FILE_NAMES.includes(splitPath[0] ?? "")) {
570
+ if (file.base64Image) {
571
+ if (thumbnail) {
572
+ throw new Error(
573
+ `Multiple thumbnail files found for plugin ${apiName}. Only one is allowed.`
574
+ );
575
+ }
576
+ thumbnail = imagetoUint8Array(file.base64Image);
577
+ }
578
+ }
579
+ }
580
+ const requiredFileKeys = Object.keys(requiredConfigFiles).filter(
581
+ (key) => requiredConfigFiles[key] === true
582
+ );
583
+ if (requiredFileKeys.length > 0) {
584
+ throw new Error(
585
+ `The following required files are missing for plugin ${apiName}: ${requiredFileKeys.join(" config.json, ")} config.json. Please ensure these files are present in the repository.`
586
+ );
587
+ }
588
+ const setupAssistantContent = processSetupAssistantFromFiles(
589
+ setupAssistantJSON,
590
+ setupAssistantFunctions
591
+ );
592
+ const userSetupAssistantContent = processSetupAssistantFromFiles(
593
+ userSetupAssistantJSON,
594
+ userSetupAssistantFunctions
595
+ );
596
+ const updatedManifest = {
597
+ ...manifest,
598
+ block_loading_for_setup: hasBlockingCondition
599
+ };
600
+ const resolvedSetupAssistant = manifest.setup_assistant ?? setupAssistantContent;
601
+ if (resolvedSetupAssistant !== void 0) {
602
+ updatedManifest.setup_assistant = resolvedSetupAssistant;
603
+ }
604
+ const resolvedUserSetupAssistant = manifest.user_setup_assistant ?? userSetupAssistantContent;
605
+ if (resolvedUserSetupAssistant !== void 0) {
606
+ updatedManifest.user_setup_assistant = resolvedUserSetupAssistant;
607
+ }
608
+ return {
609
+ [apiName]: {
610
+ manifest: updatedManifest,
611
+ thumbnail,
612
+ floatingFrames,
613
+ dataAdornments,
614
+ toolbarItems,
615
+ routablePages,
616
+ actions: jsActions,
617
+ routeScripts,
618
+ releaseNotes: releaseNotes?.content,
619
+ objectSettingsMenuItems: objectSettingsItems,
620
+ calendarSources,
621
+ automationSteps,
622
+ kznFile
623
+ }
624
+ };
625
+ };
626
+ var packagePlugin = (files, manifests) => {
627
+ const manifestArray = Array.isArray(manifests) ? manifests : [manifests];
628
+ return manifestArray.reduce((acc, manifest) => {
629
+ return { ...acc, ...processOnePluginFromManifest(manifest, files) };
630
+ }, {});
631
+ };
632
+ var parseManifestFromFiles = (files) => {
633
+ const manifestFile = files.find((f) => f.path === MANIFEST_FILE_NAME);
634
+ if (!manifestFile) {
635
+ throw new Error("Missing kizen.json at the root of the plugin directory.");
636
+ }
637
+ try {
638
+ return JSON.parse(manifestFile.content);
639
+ } catch {
640
+ throw new Error("kizen.json could not be parsed.");
641
+ }
642
+ };
643
+
644
+ // src/lib/transform.ts
645
+ var formatBaseConfig = (packagedPlugin) => {
646
+ const assistantConfig = packagedPlugin.manifest.setup_assistant;
647
+ const actionsByApiName = Object.values(packagedPlugin.actions).reduce(
648
+ (acc, action) => {
649
+ acc[action.api_name] = action;
650
+ return acc;
651
+ },
652
+ {}
653
+ );
654
+ const userAssistantConfig = packagedPlugin.manifest.user_setup_assistant;
655
+ return {
656
+ ...packagedPlugin.manifest.base_config,
657
+ setup_assistant: assistantConfig ? {
658
+ ...assistantConfig,
659
+ actions: (assistantConfig.actions ?? []).map((apiName) => {
660
+ const action = actionsByApiName[apiName];
661
+ if (!action) {
662
+ throw new Error(
663
+ `Action with API name ${apiName} not found in packaged plugin for setup assistant. Allowed actions: ${Object.keys(actionsByApiName).join(", ")}`
664
+ );
665
+ }
666
+ return action;
667
+ })
668
+ } : void 0,
669
+ user_setup_assistant: userAssistantConfig,
670
+ block_loading_for_setup: packagedPlugin.manifest.block_loading_for_setup
671
+ };
672
+ };
673
+ var transformDeployablePlugin = (packagedPlugin) => {
674
+ const {
675
+ manifest,
676
+ floatingFrames,
677
+ dataAdornments,
678
+ routablePages,
679
+ toolbarItems,
680
+ actions,
681
+ routeScripts,
682
+ thumbnail,
683
+ releaseNotes,
684
+ objectSettingsMenuItems,
685
+ automationSteps,
686
+ calendarSources,
687
+ kznFile
688
+ } = packagedPlugin;
689
+ return {
690
+ ...manifest,
691
+ api_name: manifest.api_name,
692
+ artifacts: {
693
+ data_adornments: Object.values(dataAdornments),
694
+ floating_frames: Object.values(floatingFrames),
695
+ routable_pages: Object.values(routablePages),
696
+ toolbar_items: Object.values(toolbarItems),
697
+ js_action_templates: Object.values(actions),
698
+ route_scripts: Object.values(routeScripts),
699
+ object_settings_menu_items: Object.values(objectSettingsMenuItems),
700
+ automation_action_configs: Object.values(automationSteps),
701
+ calendar_sources: Object.values(calendarSources)
702
+ },
703
+ thumbnail,
704
+ kznFile,
705
+ releaseNotes,
706
+ base_config: formatBaseConfig(packagedPlugin),
707
+ setup_assistant: void 0,
708
+ services: manifest.services ?? []
709
+ };
710
+ };
711
+ export {
712
+ AUTOMATION_SCRIPT_FILE,
713
+ AUTOMATION_STEPS_DIRECTORY_NAME,
714
+ BROWSER_ROUTE_SCRIPTS_DIRECTORY_NAME,
715
+ CALENDAR_SOURCES_DIRECTORY_NAME,
716
+ CALLBACK_SCRIPT_FILE,
717
+ CONFIG_FILE_NAME,
718
+ DATA_ADORNMENTS_DIRECTORY_NAME,
719
+ EVENT_SCRIPTS_DIRECTORY_NAME,
720
+ FLOATING_FRAMES_DIRECTORY_NAME,
721
+ JS_ACTIONS_DIRECTORY_NAME,
722
+ KZN_FILE_NAME,
723
+ MAIN_SCRIPT_FILE,
724
+ MANIFEST_FILE_NAME,
725
+ MESSAGE_SCRIPT_FILE,
726
+ OBJECT_SETTINGS_ITEMS_DIRECTORY_NAME,
727
+ RELEASE_NOTES_EXTENSION,
728
+ ROUTABLE_PAGES_DIRECTORY_NAME,
729
+ SCRIPT_EXTENSION,
730
+ SETUP_ASSISTANT_DIRECTORY_NAME,
731
+ STYLES_FILE_NAME,
732
+ THUMBNAIL_FILE_NAMES,
733
+ TOOLBAR_ITEMS_DIRECTORY_NAME,
734
+ USER_SETUP_ASSISTANT_DIRECTORY_NAME,
735
+ getAdornmentIcon,
736
+ getMinimizedConfig,
737
+ imagetoUint8Array,
738
+ maybeParseConfig,
739
+ minifyFiles,
740
+ packagePlugin,
741
+ parseManifestFromFiles,
742
+ sanitizeToAPIName,
743
+ scriptRuntimeToApiName,
744
+ thumbnailImageExtensions,
745
+ transformDeployablePlugin,
746
+ triggerImageExtensions,
747
+ zipToUint8Array
748
+ };
749
+ //# sourceMappingURL=index.js.map