@bubblydoo/uxp-toolkit 0.0.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.
Files changed (50) hide show
  1. package/.turbo/turbo-build.log +15 -0
  2. package/CHANGELOG.md +7 -0
  3. package/dist/index.d.ts +271 -0
  4. package/dist/index.js +733 -0
  5. package/package.json +41 -0
  6. package/src/commands-library/getLayerProperties.ts +32 -0
  7. package/src/commands-library/renameLayer.ts +15 -0
  8. package/src/commands-library/renameLayer.uxp-test.ts +32 -0
  9. package/src/core/batchPlay.ts +16 -0
  10. package/src/core/command.ts +130 -0
  11. package/src/core/executeAsModal.ts +101 -0
  12. package/src/core/suspendHistory.ts +15 -0
  13. package/src/core/suspendHistory.uxp-test.ts +18 -0
  14. package/src/core-wrappers/executeAsModalAndSuspendHistory.ts +11 -0
  15. package/src/dom/getFlattenedDomLayersList.ts +43 -0
  16. package/src/dom/photoshopDomLayersToTree.ts +18 -0
  17. package/src/error-sourcemaps/sourcemaps.ts +100 -0
  18. package/src/error-sourcemaps/sourcemaps.uxp-test.ts +24 -0
  19. package/src/errors/ut-error.ts +6 -0
  20. package/src/filesystem/openFileByPath.ts +10 -0
  21. package/src/general-tree/flattenTree.ts +12 -0
  22. package/src/general-tree/layerRef.ts +4 -0
  23. package/src/general-tree/mapTree.ts +11 -0
  24. package/src/general-tree/mapTreeRef.ts +11 -0
  25. package/src/general-tree/treeTypes.ts +7 -0
  26. package/src/index.ts +73 -0
  27. package/src/metadata-storage/metadataStorage.ts +66 -0
  28. package/src/metadata-storage/metadataStorage.uxp-test.ts +35 -0
  29. package/src/node-compat/path/resolvePath.ts +19 -0
  30. package/src/other/applicationInfo.ts +169 -0
  31. package/src/other/applicationInfo.uxp-test.ts +11 -0
  32. package/src/other/clipboard.ts +10 -0
  33. package/src/other/clipboard.uxp-test.ts +17 -0
  34. package/src/other/uxpEntrypoints.ts +9 -0
  35. package/src/ut-tree/getFlattenedLayerDescriptorsList.ts +72 -0
  36. package/src/ut-tree/getLayerEffects.ts +41 -0
  37. package/src/ut-tree/getLayerProperties.ts +35 -0
  38. package/src/ut-tree/photoshopLayerDescriptorsToUTLayers.ts +182 -0
  39. package/src/ut-tree/photoshopLayerDescriptorsToUTLayers.uxp-test.ts +52 -0
  40. package/src/ut-tree/psLayerRef.ts +4 -0
  41. package/src/ut-tree/utLayersToTree.ts +21 -0
  42. package/src/util/utLayerToLayer.ts +41 -0
  43. package/test/fixtures/clipping-layers.psd +0 -0
  44. package/test/fixtures/one-layer.psd +0 -0
  45. package/test/index.ts +21 -0
  46. package/test/meta-tests/executeAsModal.uxp-test.ts +38 -0
  47. package/test/meta-tests/suspendHistory.uxp-test.ts +27 -0
  48. package/tsconfig.json +13 -0
  49. package/tsup.config.ts +9 -0
  50. package/uxp-tests.json +13 -0
package/dist/index.js ADDED
@@ -0,0 +1,733 @@
1
+ // src/core/batchPlay.ts
2
+ import { action } from "photoshop";
3
+ async function batchPlay(actions, options) {
4
+ return action.batchPlay(actions, {
5
+ ...options,
6
+ modalBehavior: "execute",
7
+ dialogOptions: "silent",
8
+ synchronousExecution: false
9
+ });
10
+ }
11
+
12
+ // src/core/command.ts
13
+ import "zod";
14
+
15
+ // src/core/executeAsModal.ts
16
+ import { core } from "photoshop";
17
+ var originalExecuteAsModal = core.executeAsModal;
18
+ async function executeAsModal(commandName, fn, opts) {
19
+ let error;
20
+ let result;
21
+ await originalExecuteAsModal(async (executionContext) => {
22
+ const abortController = new AbortController();
23
+ executionContext.onCancel = () => {
24
+ abortController.abort();
25
+ };
26
+ const extendedExecutionContext = {
27
+ isCancelled: executionContext.isCancelled,
28
+ reportProgress: executionContext.reportProgress,
29
+ hostControl: executionContext.hostControl,
30
+ signal: abortController.signal,
31
+ ...createModifyingBatchPlayContext()
32
+ };
33
+ try {
34
+ result = await fn(extendedExecutionContext);
35
+ } catch (e) {
36
+ console.error("error in executeAsModal");
37
+ console.error(e);
38
+ error = e;
39
+ }
40
+ }, {
41
+ commandName,
42
+ ...opts
43
+ });
44
+ if (error) {
45
+ throw error;
46
+ } else {
47
+ return result;
48
+ }
49
+ }
50
+
51
+ // src/core/command.ts
52
+ import "photoshop";
53
+ function createCommand(obj) {
54
+ return {
55
+ modifying: obj.modifying,
56
+ descriptor: obj.descriptor,
57
+ schema: obj.schema
58
+ };
59
+ }
60
+ async function batchPlayCommandBase(command, options) {
61
+ const [result] = await batchPlay([command.descriptor], options);
62
+ if (result?._obj === "error") {
63
+ throw new Error("Batch play command failed", { cause: result });
64
+ }
65
+ return command.schema.parse(result);
66
+ }
67
+ async function batchPlayCommandsBase(commands, options) {
68
+ const results = await batchPlay(commands.map((command) => command.descriptor), options);
69
+ if (results[0]?._obj === "error") {
70
+ throw new Error("Batch play command failed", { cause: results[0] });
71
+ }
72
+ return commands.map((command, index) => command.schema.parse(results[index]));
73
+ }
74
+ function batchPlayCommand(command, options) {
75
+ return batchPlayCommandBase(command, options);
76
+ }
77
+ function batchPlayCommands(commands, options) {
78
+ return batchPlayCommandsBase(commands, options);
79
+ }
80
+ function createModifyingBatchPlayContext() {
81
+ return {
82
+ batchPlayCommand: batchPlayCommandBase,
83
+ batchPlayCommands: batchPlayCommandsBase
84
+ };
85
+ }
86
+
87
+ // src/core/suspendHistory.ts
88
+ async function suspendHistory(document, historyStateName, fn) {
89
+ let result;
90
+ await document.suspendHistory(async (context) => {
91
+ result = await fn(context);
92
+ }, historyStateName);
93
+ return result;
94
+ }
95
+
96
+ // src/core-wrappers/executeAsModalAndSuspendHistory.ts
97
+ var executeAsModalAndSuspendHistory = async (commandName, document, fn) => {
98
+ return await executeAsModal(commandName, async (ctx) => {
99
+ return await suspendHistory(document, commandName, (suspendHistoryContext) => fn(ctx, suspendHistoryContext));
100
+ });
101
+ };
102
+
103
+ // src/commands-library/getLayerProperties.ts
104
+ import { z as z2 } from "zod";
105
+ var getLayerProperties = async (document) => {
106
+ const command = createCommand({
107
+ modifying: false,
108
+ descriptor: {
109
+ _obj: "multiGet",
110
+ _target: { _ref: [{ _ref: "document", _id: document.id }] },
111
+ extendedReference: [
112
+ ["name", "layerID", "visible"],
113
+ { _obj: "layer", index: 1, count: -1 }
114
+ ]
115
+ },
116
+ schema: z2.object({
117
+ list: z2.array(
118
+ z2.object({
119
+ name: z2.string(),
120
+ layerID: z2.number(),
121
+ visible: z2.boolean().optional()
122
+ })
123
+ )
124
+ })
125
+ });
126
+ const result = await batchPlayCommand(command);
127
+ return [...result.list].reverse();
128
+ };
129
+
130
+ // src/commands-library/renameLayer.ts
131
+ import { z as z3 } from "zod";
132
+ function createRenameLayerCommand(layerRef, newName) {
133
+ return createCommand({
134
+ modifying: true,
135
+ descriptor: {
136
+ _obj: "set",
137
+ _target: [{ _ref: "layer", _id: layerRef.id }, { _ref: "document", _id: layerRef.docId }],
138
+ to: { _obj: "layer", name: newName }
139
+ },
140
+ schema: z3.unknown()
141
+ });
142
+ }
143
+
144
+ // src/dom/getFlattenedDomLayersList.ts
145
+ import { constants } from "photoshop";
146
+ var getFlattenedDomLayersList = (layers) => {
147
+ const allLayers = [];
148
+ const stack = [];
149
+ for (let i = layers.length - 1; i >= 0; i--) {
150
+ stack.push(layers[i]);
151
+ }
152
+ while (stack.length > 0) {
153
+ const layer = stack.pop();
154
+ if (!layer) continue;
155
+ allLayers.push(layer);
156
+ if (layer.kind === constants.LayerKind.GROUP) {
157
+ const children = layer.layers;
158
+ if (children && children.length > 0) {
159
+ for (let i = children.length - 1; i >= 0; i--) {
160
+ stack.push(children[i]);
161
+ }
162
+ }
163
+ }
164
+ }
165
+ return allLayers;
166
+ };
167
+
168
+ // src/dom/photoshopDomLayersToTree.ts
169
+ var photoshopDomLayersToTree = (layers) => {
170
+ const filteredLayers = layers.filter((layer) => layer.parent === null);
171
+ const generateTree = (layers2) => {
172
+ return layers2.map((layer) => ({
173
+ ref: layer,
174
+ name: layer.name,
175
+ ...layer.layers && { children: generateTree(layer.layers) }
176
+ }));
177
+ };
178
+ return generateTree(filteredLayers);
179
+ };
180
+
181
+ // src/filesystem/openFileByPath.ts
182
+ import { app } from "photoshop";
183
+ import { storage } from "uxp";
184
+ async function openFileByPath(path) {
185
+ const fs = storage.localFileSystem;
186
+ const file = await fs.getEntryWithUrl(path);
187
+ const doc = await executeAsModal("Open file", () => app.open(file));
188
+ return doc;
189
+ }
190
+
191
+ // src/general-tree/flattenTree.ts
192
+ function flattenTree(tree) {
193
+ const result = [];
194
+ for (const node of tree) {
195
+ result.push(node);
196
+ if (node.children) {
197
+ result.push(...flattenTree(node.children));
198
+ }
199
+ }
200
+ return result;
201
+ }
202
+
203
+ // src/general-tree/mapTree.ts
204
+ function mapTree(tree, mapFn) {
205
+ return tree.map((node) => {
206
+ return {
207
+ ...node,
208
+ ref: mapFn(node.ref),
209
+ children: node.children ? mapTree(node.children, mapFn) : void 0
210
+ };
211
+ });
212
+ }
213
+
214
+ // src/general-tree/mapTreeRef.ts
215
+ function mapTreeRef(tree, mapFn) {
216
+ return tree.map((node) => {
217
+ return {
218
+ ...node,
219
+ ref: mapFn(node.ref),
220
+ children: node.children ? mapTreeRef(node.children, mapFn) : void 0
221
+ };
222
+ });
223
+ }
224
+
225
+ // src/other/applicationInfo.ts
226
+ import { z as z4 } from "zod";
227
+ async function photoshopGetApplicationInfo() {
228
+ return await batchPlayCommand(photoshopApplicationInfoCommand);
229
+ }
230
+ var photoshopAppInfoSchema = z4.object({
231
+ active: z4.boolean(),
232
+ autoShowHomeScreen: z4.boolean(),
233
+ available: z4.number(),
234
+ buildNumber: z4.string(),
235
+ documentArea: z4.object({
236
+ left: z4.number(),
237
+ top: z4.number(),
238
+ right: z4.number(),
239
+ bottom: z4.number()
240
+ }),
241
+ hostName: z4.string(),
242
+ hostVersion: z4.object({
243
+ versionMajor: z4.number(),
244
+ versionMinor: z4.number(),
245
+ versionFix: z4.number()
246
+ }),
247
+ localeInfo: z4.object({
248
+ decimalPoint: z4.string()
249
+ }),
250
+ osVersion: z4.string(),
251
+ panelList: z4.array(
252
+ z4.object({
253
+ ID: z4.string(),
254
+ name: z4.string(),
255
+ obscured: z4.boolean(),
256
+ visible: z4.boolean()
257
+ })
258
+ )
259
+ });
260
+ var photoshopApplicationInfoCommand = createCommand({
261
+ modifying: false,
262
+ descriptor: {
263
+ _obj: "get",
264
+ _target: [
265
+ {
266
+ _ref: "application",
267
+ _enum: "ordinal",
268
+ _value: "targetEnum"
269
+ }
270
+ ]
271
+ },
272
+ schema: photoshopAppInfoSchema
273
+ });
274
+
275
+ // src/other/clipboard.ts
276
+ async function copyToClipboard(text) {
277
+ await navigator.clipboard.writeText({
278
+ "text/plain": text
279
+ });
280
+ }
281
+ async function readFromClipboard() {
282
+ const clipboard = await navigator.clipboard.readText();
283
+ return clipboard["text/plain"];
284
+ }
285
+
286
+ // src/other/uxpEntrypoints.ts
287
+ import { z as z5 } from "zod";
288
+ var uxpEntrypointsSchema = z5.object({
289
+ _pluginInfo: z5.object({
290
+ id: z5.string(),
291
+ name: z5.string(),
292
+ version: z5.string()
293
+ })
294
+ });
295
+
296
+ // src/ut-tree/getLayerProperties.ts
297
+ import { z as z6 } from "zod";
298
+ function createGetLayerPropertiesCommand(docId) {
299
+ return createCommand({
300
+ modifying: false,
301
+ descriptor: {
302
+ _obj: "multiGet",
303
+ _target: { _ref: [{ _ref: "document", _id: docId }] },
304
+ extendedReference: [
305
+ ["name", "layerID", "visible"],
306
+ { _obj: "layer", index: 1, count: -1 }
307
+ ]
308
+ },
309
+ schema: z6.object({
310
+ list: z6.array(
311
+ z6.object({
312
+ name: z6.string(),
313
+ layerID: z6.number(),
314
+ visible: z6.boolean().optional()
315
+ })
316
+ )
317
+ })
318
+ });
319
+ }
320
+ var getLayerProperties2 = async (documentId) => {
321
+ const command = createGetLayerPropertiesCommand(documentId);
322
+ const result = await batchPlayCommand(command);
323
+ return [...result.list].reverse();
324
+ };
325
+
326
+ // src/ut-tree/getFlattenedLayerDescriptorsList.ts
327
+ import { z as z7 } from "zod";
328
+ var layerDescriptorSchema = z7.object({
329
+ name: z7.string(),
330
+ // id: z.number(),
331
+ layerID: z7.number(),
332
+ // _docId: z.number(),
333
+ mode: z7.object({
334
+ _enum: z7.literal("blendMode"),
335
+ _value: z7.string()
336
+ // passThrough, normal, multiply, screen, overlay, etc.
337
+ }),
338
+ background: z7.boolean(),
339
+ itemIndex: z7.number(),
340
+ visible: z7.boolean(),
341
+ layerKind: z7.number(),
342
+ layerSection: z7.object({
343
+ _value: z7.enum([
344
+ "layerSectionStart",
345
+ "layerSectionEnd",
346
+ "layerSectionContent"
347
+ ]),
348
+ _enum: z7.literal("layerSectionType")
349
+ })
350
+ });
351
+ var getFlattenedLayerDescriptorsList = async (documentId) => {
352
+ const layerProperties = await getLayerProperties2(documentId);
353
+ const commands = layerProperties.map(
354
+ (layerProp) => createCommand({
355
+ modifying: false,
356
+ descriptor: {
357
+ _obj: "get",
358
+ _target: [
359
+ {
360
+ _ref: "layer",
361
+ _id: layerProp.layerID
362
+ }
363
+ ],
364
+ makeVisible: false,
365
+ layerID: [layerProp.layerID],
366
+ _isCommand: false
367
+ },
368
+ schema: layerDescriptorSchema
369
+ })
370
+ );
371
+ const layerDescriptors = await batchPlayCommands(commands);
372
+ return layerDescriptors.map((desc) => {
373
+ return {
374
+ ...desc,
375
+ docId: documentId
376
+ };
377
+ });
378
+ };
379
+
380
+ // src/ut-tree/getLayerEffects.ts
381
+ import { z as z8 } from "zod";
382
+ function createGetLayerCommand(layerRef) {
383
+ return createCommand({
384
+ modifying: false,
385
+ descriptor: {
386
+ _obj: "get",
387
+ _target: [
388
+ { _ref: "layer", _id: layerRef.id },
389
+ { _ref: "document", _id: layerRef.docId }
390
+ ]
391
+ },
392
+ schema: z8.object({
393
+ layerID: z8.number(),
394
+ group: z8.boolean().optional(),
395
+ layerEffects: z8.record(z8.string(), z8.object({
396
+ // "scale" does not have an "enabled" property, that's why it's optional
397
+ enabled: z8.boolean().optional()
398
+ }).or(z8.array(z8.object({
399
+ enabled: z8.boolean()
400
+ })))).optional()
401
+ })
402
+ });
403
+ }
404
+ async function getLayerEffects(layerRef) {
405
+ const result = await batchPlayCommand(createGetLayerCommand(layerRef));
406
+ const data = result.layerEffects || {};
407
+ const effects = {};
408
+ for (const effect in data) {
409
+ if (effect !== "scale") effects[effect] = Array.isArray(data[effect]) ? data[effect].some((e) => e.enabled) : !!data[effect]?.enabled;
410
+ }
411
+ return effects;
412
+ }
413
+
414
+ // src/ut-tree/photoshopLayerDescriptorsToUTLayers.ts
415
+ var layerKindMap = /* @__PURE__ */ new Map([
416
+ [1, "pixel"],
417
+ [2, "adjustment-layer"],
418
+ // All adjustment layers
419
+ [3, "text"],
420
+ [4, "curves"],
421
+ [5, "smartObject"],
422
+ [6, "video"],
423
+ [7, "group"],
424
+ [8, "threeD"],
425
+ [9, "gradientFill"],
426
+ [10, "pattern"],
427
+ [11, "solidColor"],
428
+ [12, "background"]
429
+ // according to the internet but the actual value is undefined
430
+ ]);
431
+ var blendModes = [
432
+ "normal",
433
+ "dissolve",
434
+ "darken",
435
+ "multiply",
436
+ "colorBurn",
437
+ "linearBurn",
438
+ "darkerColor",
439
+ "lighten",
440
+ "screen",
441
+ "colorDodge",
442
+ "linearDodge",
443
+ "lighterColor",
444
+ "overlay",
445
+ "softLight",
446
+ "hardLight",
447
+ "vividLight",
448
+ "linearLight",
449
+ "pinLight",
450
+ "hardMix",
451
+ "difference",
452
+ "exclusion",
453
+ "blendSubtraction",
454
+ "blendDivide",
455
+ "hue",
456
+ "saturation",
457
+ "color",
458
+ "luminosity",
459
+ "passThrough"
460
+ ];
461
+ var getLayerSectionValue = (layer) => {
462
+ if (typeof layer.layerSection === "string") {
463
+ return layer.layerSection;
464
+ }
465
+ if (layer.layerSection && typeof layer.layerSection === "object" && "_value" in layer.layerSection) {
466
+ return layer.layerSection._value;
467
+ }
468
+ return void 0;
469
+ };
470
+ var getLayerKind = (layer) => {
471
+ const kind = layerKindMap.get(layer.layerKind);
472
+ if (!kind) {
473
+ throw new Error(`Unknown layer kind: ${layer.layerKind}`);
474
+ }
475
+ return kind;
476
+ };
477
+ var getBlendMode = (layer) => {
478
+ const mode = layer.mode._value;
479
+ if (!blendModes.includes(mode)) {
480
+ throw new Error(`Unknown blend mode: ${mode}`);
481
+ }
482
+ return mode;
483
+ };
484
+ var determineLayerSection = (layer) => {
485
+ const section = getLayerSectionValue(layer);
486
+ const isGroupEnd = layer.name === "</Layer group>" || layer.name === "</Layer set>" || section === "layerSectionEnd";
487
+ const isGroupStart = section === "layerSectionStart";
488
+ return isGroupStart ? "start" : isGroupEnd ? "end" : "normal";
489
+ };
490
+ var photoshopLayerDescriptorsToUTLayers = async (layers) => {
491
+ const root = [];
492
+ const stack = [{ layers: root }];
493
+ const commands = layers.map((layer) => createGetLayerCommand({ docId: layer.docId, id: layer.layerID }));
494
+ const batchResults = await executeAsModal("Get Layer Effects Data", async (ctx) => {
495
+ return await ctx.batchPlayCommands(commands);
496
+ });
497
+ const effectsMap = /* @__PURE__ */ new Map();
498
+ batchResults.forEach((result, index) => {
499
+ const layerId = layers[index].layerID;
500
+ const data = result.layerEffects;
501
+ const effects = {};
502
+ if (data) {
503
+ for (const effect in data) {
504
+ effects[effect] = Array.isArray(data[effect]) ? data[effect].some((e) => e.enabled) : !!data[effect]?.enabled;
505
+ }
506
+ }
507
+ effectsMap.set(layerId, effects);
508
+ });
509
+ for (const layer of layers) {
510
+ const sectionType = determineLayerSection(layer);
511
+ const isClippingMask = !!batchResults.find((res, index) => {
512
+ return layer.layerID === res.layerID;
513
+ })?.group;
514
+ if (sectionType === "end") {
515
+ if (stack.length > 1) {
516
+ stack.pop();
517
+ }
518
+ continue;
519
+ }
520
+ const node = {
521
+ name: layer.name,
522
+ docId: layer.docId,
523
+ id: layer.layerID,
524
+ visible: layer.visible,
525
+ kind: getLayerKind(layer),
526
+ blendMode: getBlendMode(layer),
527
+ isClippingMask,
528
+ effects: effectsMap.get(layer.layerID) || {}
529
+ };
530
+ const current = stack[stack.length - 1];
531
+ current.layers.push(node);
532
+ if (sectionType === "start") {
533
+ node.layers = [];
534
+ stack.push({ layers: node.layers });
535
+ }
536
+ }
537
+ return root;
538
+ };
539
+
540
+ // src/ut-tree/utLayersToTree.ts
541
+ function utLayersToTree(layer) {
542
+ return layer.map((layer2) => ({
543
+ ref: {
544
+ name: layer2.name,
545
+ docId: layer2.docId,
546
+ id: layer2.id,
547
+ visible: layer2.visible,
548
+ kind: layer2.kind,
549
+ blendMode: layer2.blendMode,
550
+ isClippingMask: layer2.isClippingMask,
551
+ effects: layer2.effects
552
+ },
553
+ name: layer2.name,
554
+ children: layer2.layers ? utLayersToTree(layer2.layers) : void 0
555
+ }));
556
+ }
557
+
558
+ // src/util/utLayerToLayer.ts
559
+ import { app as app2 } from "photoshop";
560
+ function utLayerToDomLayer(layer) {
561
+ const doc = app2.documents.find((d) => d.id === layer.docId);
562
+ if (!doc) {
563
+ throw new Error(`Document with id ${layer.docId} not found.`);
564
+ }
565
+ const allLayers = getFlattenedDomLayersList(doc.layers);
566
+ const domLayer = allLayers.find((l) => l.id === layer.id);
567
+ if (!domLayer) {
568
+ throw new Error(
569
+ `Layer with id ${layer.id} not found in document ${layer.docId}.`
570
+ );
571
+ }
572
+ return domLayer;
573
+ }
574
+ function utLayersToDomLayers(layers) {
575
+ if (layers.length === 0) return [];
576
+ const docId = layers[0].docId;
577
+ if (!layers.every((l) => l.docId === docId)) {
578
+ throw new Error("All layers must be from the same document.");
579
+ }
580
+ const doc = app2.documents.find((d) => d.id === docId);
581
+ if (!doc) {
582
+ throw new Error(`Document with id ${docId} not found.`);
583
+ }
584
+ const allLayers = getFlattenedDomLayersList(doc.layers);
585
+ return layers.map((l) => {
586
+ const domLayer = allLayers.find((dl) => dl.id === l.id);
587
+ if (!domLayer) {
588
+ throw new Error(
589
+ `Layer with id ${l.id} not found in document ${docId}.`
590
+ );
591
+ }
592
+ return domLayer;
593
+ });
594
+ }
595
+
596
+ // src/error-sourcemaps/sourcemaps.ts
597
+ import ErrorStackParser from "error-stack-parser";
598
+ import { SourceMapConsumer } from "source-map-js";
599
+ import { storage as storage2 } from "uxp";
600
+
601
+ // src/node-compat/path/resolvePath.ts
602
+ import { resolve as nativeResolve } from "path";
603
+ function pathResolve(...pathSegments) {
604
+ const urlOrString = nativeResolve(...pathSegments);
605
+ if (typeof urlOrString === "string") {
606
+ return urlOrString;
607
+ }
608
+ if (isUrl(urlOrString)) {
609
+ return urlOrString.toString();
610
+ }
611
+ throw new Error("Unexpected URL object");
612
+ }
613
+ function isUrl(urlOrString) {
614
+ return urlOrString instanceof URL;
615
+ }
616
+
617
+ // src/errors/ut-error.ts
618
+ var UTError = class extends Error {
619
+ constructor(message, opts = {}) {
620
+ super(message, opts);
621
+ this.name = "UTError";
622
+ }
623
+ };
624
+
625
+ // src/error-sourcemaps/sourcemaps.ts
626
+ async function parseUxpErrorSourcemaps(error, opts = {}) {
627
+ const parsedError = parseErrorIntoBasicStackFrames(error);
628
+ const unsourcemappedHeaderLines = opts.unsourcemappedHeaderLines ?? 0;
629
+ const loadedFilesCache = {};
630
+ const fs = storage2.localFileSystem;
631
+ const parsedMappedError = [];
632
+ for (const frame of parsedError) {
633
+ if (!frame.fileName || !frame.lineNumber || !frame.columnNumber) {
634
+ parsedMappedError.push(frame);
635
+ continue;
636
+ }
637
+ const entryPath = "plugin:" + frame.fileName;
638
+ const file = loadedFilesCache[entryPath] ?? await fs.getEntryWithUrl(entryPath);
639
+ loadedFilesCache[entryPath] = file;
640
+ if (!file.isFile) {
641
+ parsedMappedError.push(frame);
642
+ continue;
643
+ }
644
+ const sourcemapFileEntryPath = entryPath + ".map";
645
+ const sourcemapFile = loadedFilesCache[sourcemapFileEntryPath] ?? await fs.getEntryWithUrl(sourcemapFileEntryPath);
646
+ loadedFilesCache[sourcemapFileEntryPath] = sourcemapFile;
647
+ if (!sourcemapFile.isFile) {
648
+ parsedMappedError.push(frame);
649
+ continue;
650
+ }
651
+ const sourcemapContents = await sourcemapFile.read({});
652
+ const sourcemap = JSON.parse(sourcemapContents);
653
+ const smc = new SourceMapConsumer(sourcemap);
654
+ const mappedFrame = smc.originalPositionFor({
655
+ line: frame.lineNumber - unsourcemappedHeaderLines,
656
+ column: frame.columnNumber
657
+ });
658
+ if (mappedFrame.source && mappedFrame.line && mappedFrame.column) {
659
+ parsedMappedError.push({
660
+ ...frame,
661
+ fileName: mappedFrame.source,
662
+ lineNumber: mappedFrame.line,
663
+ columnNumber: mappedFrame.column
664
+ });
665
+ } else {
666
+ parsedMappedError.push(frame);
667
+ }
668
+ }
669
+ return parsedMappedError;
670
+ }
671
+ function parseErrorIntoBasicStackFrames(error) {
672
+ try {
673
+ const frames = ErrorStackParser.parse(error);
674
+ return frames.map((frame) => {
675
+ return {
676
+ functionName: frame.functionName,
677
+ fileName: frame.fileName,
678
+ lineNumber: frame.lineNumber,
679
+ columnNumber: frame.columnNumber
680
+ };
681
+ });
682
+ } catch (e) {
683
+ throw new UTStacktraceParsingError("Failed to parse error stack trace", { cause: e });
684
+ }
685
+ }
686
+ async function getBasicStackFrameAbsoluteFilePath(frame) {
687
+ const pluginFolder = await storage2.localFileSystem.getPluginFolder();
688
+ const absoluteFileName = pathResolve(
689
+ pluginFolder.nativePath,
690
+ "index.js",
691
+ frame.fileName
692
+ ).replace(/^plugin:/, "");
693
+ return `${absoluteFileName}:${frame.lineNumber}:${frame.columnNumber}`;
694
+ }
695
+ var UTStacktraceParsingError = class extends UTError {
696
+ name = "UTStacktraceParsingError";
697
+ constructor(message, opts = {}) {
698
+ super(message, opts);
699
+ }
700
+ };
701
+ export {
702
+ batchPlay,
703
+ batchPlayCommand,
704
+ batchPlayCommands,
705
+ copyToClipboard,
706
+ createCommand,
707
+ createGetLayerCommand as createGetLayerEffectsCommand,
708
+ createGetLayerPropertiesCommand,
709
+ createModifyingBatchPlayContext,
710
+ createRenameLayerCommand,
711
+ executeAsModal,
712
+ executeAsModalAndSuspendHistory,
713
+ flattenTree,
714
+ getBasicStackFrameAbsoluteFilePath,
715
+ getFlattenedDomLayersList,
716
+ getFlattenedLayerDescriptorsList,
717
+ getLayerEffects,
718
+ getLayerProperties,
719
+ getLayerProperties2 as getLayerPropertiesFromUtTree,
720
+ mapTree,
721
+ mapTreeRef,
722
+ openFileByPath,
723
+ parseUxpErrorSourcemaps,
724
+ photoshopDomLayersToTree,
725
+ photoshopGetApplicationInfo,
726
+ photoshopLayerDescriptorsToUTLayers,
727
+ readFromClipboard,
728
+ suspendHistory,
729
+ utLayerToDomLayer,
730
+ utLayersToDomLayers,
731
+ utLayersToTree,
732
+ uxpEntrypointsSchema
733
+ };