@uniformdev/transformer 1.1.2 → 1.1.4

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/cli/index.js CHANGED
@@ -2422,6 +2422,12 @@ var ComponentAdderService = class {
2422
2422
  }
2423
2423
  return id1.toLowerCase() === id2.toLowerCase();
2424
2424
  }
2425
+ parseParentComponentTypes(parentComponentType) {
2426
+ return parentComponentType.split("|").map((t) => t.trim()).filter((t) => t.length > 0);
2427
+ }
2428
+ matchesAnyParentType(instanceType, parentTypes, strict) {
2429
+ return parentTypes.some((pt) => this.compareIds(instanceType, pt, strict));
2430
+ }
2425
2431
  parseParameters(parameterString) {
2426
2432
  const result = {};
2427
2433
  if (!parameterString) return result;
@@ -2437,25 +2443,9 @@ var ComponentAdderService = class {
2437
2443
  generateId(baseName) {
2438
2444
  return `${baseName}-${randomUUID().slice(0, 8)}`;
2439
2445
  }
2440
- cloneComponentInstance(instance) {
2441
- const clone = JSON.parse(JSON.stringify(instance));
2442
- if (clone._id) {
2443
- clone._id = this.generateId(clone.type);
2444
- }
2445
- if (clone.slots) {
2446
- for (const slotInstances of Object.values(clone.slots)) {
2447
- if (Array.isArray(slotInstances)) {
2448
- for (const nestedInstance of slotInstances) {
2449
- this.regenerateInstanceIds(nestedInstance);
2450
- }
2451
- }
2452
- }
2453
- }
2454
- return clone;
2455
- }
2456
2446
  regenerateInstanceIds(instance) {
2457
2447
  if (instance._id) {
2458
- instance._id = this.generateId(instance.type);
2448
+ instance._id = instance._pattern ? randomUUID() : this.generateId(instance.type);
2459
2449
  }
2460
2450
  if (instance.slots) {
2461
2451
  for (const slotInstances of Object.values(instance.slots)) {
@@ -2511,18 +2501,9 @@ var ComponentAdderService = class {
2511
2501
  const fullCompositionPatternsDir = this.fileSystem.resolvePath(rootDir, compositionPatternsDir);
2512
2502
  const fullComponentPatternsDir = this.fileSystem.resolvePath(rootDir, componentPatternsDir);
2513
2503
  const findOptions = { strict };
2514
- this.logger.info(`Loading parent component: ${parentComponentType}`);
2515
- const { component: parentComponent, filePath: parentComponentFilePath } = await this.componentService.loadComponent(fullComponentsDir, parentComponentType, findOptions);
2516
- if (!parentComponent.slots) {
2517
- parentComponent.slots = [];
2518
- }
2519
- let slotDef = parentComponent.slots.find(
2520
- (s) => this.compareIds(s.id, slot, strict)
2521
- );
2522
- if (!slotDef) {
2523
- this.logger.info(`Slot "${slot}" not found on component "${parentComponentType}", creating it`);
2524
- slotDef = { id: slot, name: slot, allowedComponents: [] };
2525
- parentComponent.slots.push(slotDef);
2504
+ const parentTypes = this.parseParentComponentTypes(parentComponentType);
2505
+ if (parentTypes.length === 0) {
2506
+ throw new TransformError("parentComponentType cannot be empty");
2526
2507
  }
2527
2508
  this.logger.info(`Validating new component: ${newComponentType}`);
2528
2509
  try {
@@ -2536,35 +2517,50 @@ var ComponentAdderService = class {
2536
2517
  throw error;
2537
2518
  }
2538
2519
  let allowedComponentsUpdated = false;
2539
- const slotIndex = parentComponent.slots?.findIndex(
2540
- (s) => this.compareIds(s.id, slotDef.id, strict)
2541
- );
2542
- if (slotIndex !== void 0 && slotIndex >= 0) {
2543
- const actualSlot = parentComponent.slots[slotIndex];
2544
- if (!actualSlot.allowedComponents) {
2545
- actualSlot.allowedComponents = [];
2520
+ for (const parentType of parentTypes) {
2521
+ this.logger.info(`Loading parent component: ${parentType}`);
2522
+ const { component: parentComponent, filePath: parentComponentFilePath } = await this.componentService.loadComponent(fullComponentsDir, parentType, findOptions);
2523
+ if (!parentComponent.slots) {
2524
+ parentComponent.slots = [];
2525
+ }
2526
+ let slotDef = parentComponent.slots.find(
2527
+ (s) => this.compareIds(s.id, slot, strict)
2528
+ );
2529
+ if (!slotDef) {
2530
+ this.logger.info(`Slot "${slot}" not found on component "${parentType}", creating it`);
2531
+ slotDef = { id: slot, name: slot, allowedComponents: [] };
2532
+ parentComponent.slots.push(slotDef);
2546
2533
  }
2547
- const isAlreadyAllowed = actualSlot.allowedComponents.some(
2548
- (c) => this.compareIds(c, newComponentType, strict)
2534
+ const slotIndex = parentComponent.slots?.findIndex(
2535
+ (s) => this.compareIds(s.id, slotDef.id, strict)
2549
2536
  );
2550
- if (!isAlreadyAllowed) {
2551
- this.logger.action(
2552
- whatIf,
2553
- "UPDATE",
2554
- `Adding "${newComponentType}" to allowedComponents for slot "${slot}" on component "${parentComponentType}"`
2537
+ if (slotIndex !== void 0 && slotIndex >= 0) {
2538
+ const actualSlot = parentComponent.slots[slotIndex];
2539
+ if (!actualSlot.allowedComponents) {
2540
+ actualSlot.allowedComponents = [];
2541
+ }
2542
+ const isAlreadyAllowed = actualSlot.allowedComponents.some(
2543
+ (c) => this.compareIds(c, newComponentType, strict)
2555
2544
  );
2556
- actualSlot.allowedComponents.push(newComponentType);
2557
- if (!whatIf) {
2558
- await this.componentService.saveComponent(parentComponentFilePath, parentComponent);
2545
+ if (!isAlreadyAllowed) {
2546
+ this.logger.action(
2547
+ whatIf,
2548
+ "UPDATE",
2549
+ `Adding "${newComponentType}" to allowedComponents for slot "${slot}" on component "${parentType}"`
2550
+ );
2551
+ actualSlot.allowedComponents.push(newComponentType);
2552
+ if (!whatIf) {
2553
+ await this.componentService.saveComponent(parentComponentFilePath, parentComponent);
2554
+ }
2555
+ allowedComponentsUpdated = true;
2559
2556
  }
2560
- allowedComponentsUpdated = true;
2561
2557
  }
2562
2558
  }
2563
2559
  const parsedParams = this.parseParameters(paramString);
2564
2560
  const newInstance = this.createComponentInstance(newComponentType, parsedParams);
2565
2561
  const compositionsResult = await this.addComponentToDirectory(
2566
2562
  fullCompositionsDir,
2567
- parentComponentType,
2563
+ parentTypes,
2568
2564
  slot,
2569
2565
  newInstance,
2570
2566
  whatIf,
@@ -2573,7 +2569,7 @@ var ComponentAdderService = class {
2573
2569
  );
2574
2570
  const compositionPatternsResult = await this.addComponentToDirectory(
2575
2571
  fullCompositionPatternsDir,
2576
- parentComponentType,
2572
+ parentTypes,
2577
2573
  slot,
2578
2574
  newInstance,
2579
2575
  whatIf,
@@ -2582,7 +2578,7 @@ var ComponentAdderService = class {
2582
2578
  );
2583
2579
  const componentPatternsResult = await this.addComponentToDirectory(
2584
2580
  fullComponentPatternsDir,
2585
- parentComponentType,
2581
+ parentTypes,
2586
2582
  slot,
2587
2583
  newInstance,
2588
2584
  whatIf,
@@ -2591,7 +2587,7 @@ var ComponentAdderService = class {
2591
2587
  );
2592
2588
  this.logger.info("");
2593
2589
  this.logger.info(
2594
- `Summary: ${allowedComponentsUpdated ? "1 component definition updated, " : ""}${compositionsResult} composition(s), ${compositionPatternsResult} composition pattern(s), ${componentPatternsResult} component pattern(s) updated. ${compositionsResult + compositionPatternsResult + componentPatternsResult} instance(s) added.`
2590
+ `Summary: ${allowedComponentsUpdated ? `${parentTypes.length} component definition(s) updated, ` : ""}${compositionsResult} composition(s), ${compositionPatternsResult} composition pattern(s), ${componentPatternsResult} component pattern(s) updated. ${compositionsResult + compositionPatternsResult + componentPatternsResult} instance(s) added.`
2595
2591
  );
2596
2592
  return {
2597
2593
  allowedComponentsUpdated,
@@ -2622,18 +2618,9 @@ var ComponentAdderService = class {
2622
2618
  const fullCompositionPatternsDir = this.fileSystem.resolvePath(rootDir, compositionPatternsDir);
2623
2619
  const fullComponentPatternsDir = this.fileSystem.resolvePath(rootDir, componentPatternsDir);
2624
2620
  const findOptions = { strict };
2625
- this.logger.info(`Loading parent component: ${parentComponentType}`);
2626
- const { component: parentComponent } = await this.componentService.loadComponent(fullComponentsDir, parentComponentType, findOptions);
2627
- if (!parentComponent.slots) {
2628
- parentComponent.slots = [];
2629
- }
2630
- let slotDef = parentComponent.slots.find(
2631
- (s) => this.compareIds(s.id, slot, strict)
2632
- );
2633
- if (!slotDef) {
2634
- this.logger.info(`Slot "${slot}" not found on component "${parentComponentType}", creating it`);
2635
- slotDef = { id: slot, name: slot, allowedComponents: [] };
2636
- parentComponent.slots.push(slotDef);
2621
+ const parentTypes = this.parseParentComponentTypes(parentComponentType);
2622
+ if (parentTypes.length === 0) {
2623
+ throw new TransformError("parentComponentType cannot be empty");
2637
2624
  }
2638
2625
  this.logger.info(`Loading component pattern: ${componentPatternId}`);
2639
2626
  const pattern = await this.loadComponentPattern(
@@ -2647,38 +2634,55 @@ var ComponentAdderService = class {
2647
2634
  `Component pattern "${componentPatternId}" has no valid definition or missing type`
2648
2635
  );
2649
2636
  }
2650
- let allowedComponentsUpdated = false;
2651
2637
  const componentTypeInPattern = patternDefinition.type;
2652
- const slotIndex = parentComponent.slots?.findIndex(
2653
- (s) => this.compareIds(s.id, slotDef.id, strict)
2654
- );
2655
- if (slotIndex !== void 0 && slotIndex >= 0) {
2656
- const actualSlot = parentComponent.slots[slotIndex];
2657
- if (!actualSlot.allowedComponents) {
2658
- actualSlot.allowedComponents = [];
2638
+ let allowedComponentsUpdated = false;
2639
+ for (const parentType of parentTypes) {
2640
+ this.logger.info(`Loading parent component: ${parentType}`);
2641
+ const { component: parentComponent, filePath: parentComponentFilePath } = await this.componentService.loadComponent(fullComponentsDir, parentType, findOptions);
2642
+ if (!parentComponent.slots) {
2643
+ parentComponent.slots = [];
2644
+ }
2645
+ let slotDef = parentComponent.slots.find(
2646
+ (s) => this.compareIds(s.id, slot, strict)
2647
+ );
2648
+ if (!slotDef) {
2649
+ this.logger.info(`Slot "${slot}" not found on component "${parentType}", creating it`);
2650
+ slotDef = { id: slot, name: slot, allowedComponents: [] };
2651
+ parentComponent.slots.push(slotDef);
2659
2652
  }
2660
- const isAlreadyAllowed = actualSlot.allowedComponents.some(
2661
- (c) => this.compareIds(c, componentTypeInPattern, strict)
2653
+ const slotIndex = parentComponent.slots?.findIndex(
2654
+ (s) => this.compareIds(s.id, slotDef.id, strict)
2662
2655
  );
2663
- if (!isAlreadyAllowed) {
2664
- this.logger.action(
2665
- whatIf,
2666
- "UPDATE",
2667
- `Adding "${componentTypeInPattern}" to allowedComponents for slot "${slot}" on component "${parentComponentType}"`
2656
+ if (slotIndex !== void 0 && slotIndex >= 0) {
2657
+ const actualSlot = parentComponent.slots[slotIndex];
2658
+ if (!actualSlot.allowedComponents) {
2659
+ actualSlot.allowedComponents = [];
2660
+ }
2661
+ const isAlreadyAllowed = actualSlot.allowedComponents.some(
2662
+ (c) => this.compareIds(c, componentTypeInPattern, strict)
2668
2663
  );
2669
- actualSlot.allowedComponents.push(componentTypeInPattern);
2670
- if (!whatIf) {
2671
- await this.componentService.loadComponent(fullComponentsDir, parentComponentType, findOptions).then(
2672
- ({ filePath }) => this.componentService.saveComponent(filePath, parentComponent)
2664
+ if (!isAlreadyAllowed) {
2665
+ this.logger.action(
2666
+ whatIf,
2667
+ "UPDATE",
2668
+ `Adding "${componentTypeInPattern}" to allowedComponents for slot "${slot}" on component "${parentType}"`
2673
2669
  );
2670
+ actualSlot.allowedComponents.push(componentTypeInPattern);
2671
+ if (!whatIf) {
2672
+ await this.componentService.saveComponent(parentComponentFilePath, parentComponent);
2673
+ }
2674
+ allowedComponentsUpdated = true;
2674
2675
  }
2675
- allowedComponentsUpdated = true;
2676
2676
  }
2677
2677
  }
2678
- const newInstance = this.cloneComponentInstance(patternDefinition);
2678
+ const newInstance = {
2679
+ type: componentTypeInPattern,
2680
+ _pattern: pattern.componentPatternId,
2681
+ _id: randomUUID()
2682
+ };
2679
2683
  const compositionsResult = await this.addComponentToDirectory(
2680
2684
  fullCompositionsDir,
2681
- parentComponentType,
2685
+ parentTypes,
2682
2686
  slot,
2683
2687
  newInstance,
2684
2688
  whatIf,
@@ -2687,7 +2691,7 @@ var ComponentAdderService = class {
2687
2691
  );
2688
2692
  const compositionPatternsResult = await this.addComponentToDirectory(
2689
2693
  fullCompositionPatternsDir,
2690
- parentComponentType,
2694
+ parentTypes,
2691
2695
  slot,
2692
2696
  newInstance,
2693
2697
  whatIf,
@@ -2696,7 +2700,7 @@ var ComponentAdderService = class {
2696
2700
  );
2697
2701
  const componentPatternsResult = await this.addComponentToDirectory(
2698
2702
  fullComponentPatternsDir,
2699
- parentComponentType,
2703
+ parentTypes,
2700
2704
  slot,
2701
2705
  newInstance,
2702
2706
  whatIf,
@@ -2705,7 +2709,7 @@ var ComponentAdderService = class {
2705
2709
  );
2706
2710
  this.logger.info("");
2707
2711
  this.logger.info(
2708
- `Summary: ${allowedComponentsUpdated ? "1 component definition updated, " : ""}${compositionsResult} composition(s), ${compositionPatternsResult} composition pattern(s), ${componentPatternsResult} component pattern(s) updated. ${compositionsResult + compositionPatternsResult + componentPatternsResult} instance(s) added.`
2712
+ `Summary: ${allowedComponentsUpdated ? `${parentTypes.length} component definition(s) updated, ` : ""}${compositionsResult} composition(s), ${compositionPatternsResult} composition pattern(s), ${componentPatternsResult} component pattern(s) updated. ${compositionsResult + compositionPatternsResult + componentPatternsResult} instance(s) added.`
2709
2713
  );
2710
2714
  return {
2711
2715
  allowedComponentsUpdated,
@@ -2756,7 +2760,7 @@ var ComponentAdderService = class {
2756
2760
  }
2757
2761
  return raw;
2758
2762
  }
2759
- async addComponentToDirectory(directory, parentComponentType, slot, newInstance, whatIf, strict, dirType) {
2763
+ async addComponentToDirectory(directory, parentTypes, slot, newInstance, whatIf, strict, dirType) {
2760
2764
  const exists = await this.fileSystem.fileExists(directory);
2761
2765
  if (!exists) {
2762
2766
  this.logger.detail(`${dirType} directory does not exist, skipping`);
@@ -2789,7 +2793,7 @@ var ComponentAdderService = class {
2789
2793
  }
2790
2794
  const rootInstance = composition.composition;
2791
2795
  let modified = false;
2792
- if (this.compareIds(rootInstance.type, parentComponentType, strict)) {
2796
+ if (this.matchesAnyParentType(rootInstance.type, parentTypes, strict)) {
2793
2797
  if (!rootInstance.slots) {
2794
2798
  rootInstance.slots = {};
2795
2799
  }
@@ -2801,7 +2805,7 @@ var ComponentAdderService = class {
2801
2805
  if (rootInstance.slots) {
2802
2806
  const nestedModified = this.addComponentToNestedSlots(
2803
2807
  rootInstance.slots,
2804
- parentComponentType,
2808
+ parentTypes,
2805
2809
  slot,
2806
2810
  newInstance,
2807
2811
  strict
@@ -2835,12 +2839,12 @@ var ComponentAdderService = class {
2835
2839
  }
2836
2840
  return filesModified;
2837
2841
  }
2838
- addComponentToNestedSlots(slots, parentComponentType, slot, newInstance, strict) {
2842
+ addComponentToNestedSlots(slots, parentTypes, slot, newInstance, strict) {
2839
2843
  let modified = false;
2840
2844
  for (const slotInstances of Object.values(slots)) {
2841
2845
  if (!Array.isArray(slotInstances)) continue;
2842
2846
  for (const instance of slotInstances) {
2843
- if (this.compareIds(instance.type, parentComponentType, strict)) {
2847
+ if (this.matchesAnyParentType(instance.type, parentTypes, strict)) {
2844
2848
  if (!instance.slots) {
2845
2849
  instance.slots = {};
2846
2850
  }
@@ -2852,7 +2856,7 @@ var ComponentAdderService = class {
2852
2856
  if (instance.slots) {
2853
2857
  const nestedModified = this.addComponentToNestedSlots(
2854
2858
  instance.slots,
2855
- parentComponentType,
2859
+ parentTypes,
2856
2860
  slot,
2857
2861
  newInstance,
2858
2862
  strict
@@ -2927,7 +2931,7 @@ function createAddComponentCommand() {
2927
2931
  import { Command as Command8 } from "commander";
2928
2932
  function createAddComponentPatternCommand() {
2929
2933
  const command = new Command8("add-component-pattern");
2930
- command.description("Adds a component pattern instance to existing component slots across all compositions.").option("--parentComponentType <type>", "The component type that owns the slot").option("--slot <slotId>", "The slot ID where the component pattern will be added").option("--componentPatternId <id>", "The component pattern ID to add").option("--parameters <params>", "Pipe-separated parameter assignments (key1:value1|key2:value2)").hook("preAction", (thisCommand) => {
2934
+ command.description("Adds a component pattern instance to existing component slots across all compositions.").option("--parentComponentType <type>", "The component type that owns the slot").option("--slot <slotId>", "The slot ID where the component pattern will be added").option("--componentPatternId <id>", "The component pattern ID to add").hook("preAction", (thisCommand) => {
2931
2935
  const opts = thisCommand.opts();
2932
2936
  const requiredOptions = [
2933
2937
  { name: "parentComponentType", flag: "--parentComponentType" },
@@ -2945,8 +2949,7 @@ function createAddComponentPatternCommand() {
2945
2949
  ...globalOpts,
2946
2950
  parentComponentType: opts.parentComponentType,
2947
2951
  slot: opts.slot,
2948
- componentPatternId: opts.componentPatternId,
2949
- parameters: opts.parameters
2952
+ componentPatternId: opts.componentPatternId
2950
2953
  };
2951
2954
  const logger = new Logger();
2952
2955
  const fileSystem = new FileSystemService();
@@ -2962,7 +2965,6 @@ function createAddComponentPatternCommand() {
2962
2965
  parentComponentType: options.parentComponentType,
2963
2966
  slot: options.slot,
2964
2967
  componentPatternId: options.componentPatternId,
2965
- parameters: options.parameters,
2966
2968
  whatIf: options.whatIf ?? false,
2967
2969
  strict: options.strict ?? false
2968
2970
  });