@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/index.d.ts CHANGED
@@ -119,7 +119,6 @@ interface AddComponentPatternOptions extends GlobalOptions {
119
119
  parentComponentType: string;
120
120
  slot: string;
121
121
  componentPatternId: string;
122
- parameters?: string;
123
122
  }
124
123
  interface PropagateRootComponentSlotOptions extends GlobalOptions {
125
124
  compositionType: string;
@@ -367,14 +366,15 @@ declare class ComponentAdderService {
367
366
  private logger;
368
367
  constructor(fileSystem: FileSystemService, componentService: ComponentService, logger: Logger);
369
368
  private compareIds;
369
+ private parseParentComponentTypes;
370
+ private matchesAnyParentType;
370
371
  private parseParameters;
371
372
  private generateId;
372
- private cloneComponentInstance;
373
373
  private regenerateInstanceIds;
374
374
  private createComponentInstance;
375
375
  private addComponentToSlot;
376
376
  addComponent(options: AddComponentInternalOptions): Promise<AddComponentResult>;
377
- addComponentPattern(options: AddComponentInternalOptions): Promise<AddComponentResult>;
377
+ addComponentPattern(options: Omit<AddComponentInternalOptions, 'parameters'>): Promise<AddComponentResult>;
378
378
  private loadComponentPattern;
379
379
  private normalizeComponentPattern;
380
380
  private addComponentToDirectory;
package/dist/index.js CHANGED
@@ -1114,6 +1114,12 @@ var ComponentAdderService = class {
1114
1114
  }
1115
1115
  return id1.toLowerCase() === id2.toLowerCase();
1116
1116
  }
1117
+ parseParentComponentTypes(parentComponentType) {
1118
+ return parentComponentType.split("|").map((t) => t.trim()).filter((t) => t.length > 0);
1119
+ }
1120
+ matchesAnyParentType(instanceType, parentTypes, strict) {
1121
+ return parentTypes.some((pt) => this.compareIds(instanceType, pt, strict));
1122
+ }
1117
1123
  parseParameters(parameterString) {
1118
1124
  const result = {};
1119
1125
  if (!parameterString) return result;
@@ -1129,25 +1135,9 @@ var ComponentAdderService = class {
1129
1135
  generateId(baseName) {
1130
1136
  return `${baseName}-${randomUUID().slice(0, 8)}`;
1131
1137
  }
1132
- cloneComponentInstance(instance) {
1133
- const clone = JSON.parse(JSON.stringify(instance));
1134
- if (clone._id) {
1135
- clone._id = this.generateId(clone.type);
1136
- }
1137
- if (clone.slots) {
1138
- for (const slotInstances of Object.values(clone.slots)) {
1139
- if (Array.isArray(slotInstances)) {
1140
- for (const nestedInstance of slotInstances) {
1141
- this.regenerateInstanceIds(nestedInstance);
1142
- }
1143
- }
1144
- }
1145
- }
1146
- return clone;
1147
- }
1148
1138
  regenerateInstanceIds(instance) {
1149
1139
  if (instance._id) {
1150
- instance._id = this.generateId(instance.type);
1140
+ instance._id = instance._pattern ? randomUUID() : this.generateId(instance.type);
1151
1141
  }
1152
1142
  if (instance.slots) {
1153
1143
  for (const slotInstances of Object.values(instance.slots)) {
@@ -1203,18 +1193,9 @@ var ComponentAdderService = class {
1203
1193
  const fullCompositionPatternsDir = this.fileSystem.resolvePath(rootDir, compositionPatternsDir);
1204
1194
  const fullComponentPatternsDir = this.fileSystem.resolvePath(rootDir, componentPatternsDir);
1205
1195
  const findOptions = { strict };
1206
- this.logger.info(`Loading parent component: ${parentComponentType}`);
1207
- const { component: parentComponent, filePath: parentComponentFilePath } = await this.componentService.loadComponent(fullComponentsDir, parentComponentType, findOptions);
1208
- if (!parentComponent.slots) {
1209
- parentComponent.slots = [];
1210
- }
1211
- let slotDef = parentComponent.slots.find(
1212
- (s) => this.compareIds(s.id, slot, strict)
1213
- );
1214
- if (!slotDef) {
1215
- this.logger.info(`Slot "${slot}" not found on component "${parentComponentType}", creating it`);
1216
- slotDef = { id: slot, name: slot, allowedComponents: [] };
1217
- parentComponent.slots.push(slotDef);
1196
+ const parentTypes = this.parseParentComponentTypes(parentComponentType);
1197
+ if (parentTypes.length === 0) {
1198
+ throw new TransformError("parentComponentType cannot be empty");
1218
1199
  }
1219
1200
  this.logger.info(`Validating new component: ${newComponentType}`);
1220
1201
  try {
@@ -1228,35 +1209,50 @@ var ComponentAdderService = class {
1228
1209
  throw error;
1229
1210
  }
1230
1211
  let allowedComponentsUpdated = false;
1231
- const slotIndex = parentComponent.slots?.findIndex(
1232
- (s) => this.compareIds(s.id, slotDef.id, strict)
1233
- );
1234
- if (slotIndex !== void 0 && slotIndex >= 0) {
1235
- const actualSlot = parentComponent.slots[slotIndex];
1236
- if (!actualSlot.allowedComponents) {
1237
- actualSlot.allowedComponents = [];
1212
+ for (const parentType of parentTypes) {
1213
+ this.logger.info(`Loading parent component: ${parentType}`);
1214
+ const { component: parentComponent, filePath: parentComponentFilePath } = await this.componentService.loadComponent(fullComponentsDir, parentType, findOptions);
1215
+ if (!parentComponent.slots) {
1216
+ parentComponent.slots = [];
1238
1217
  }
1239
- const isAlreadyAllowed = actualSlot.allowedComponents.some(
1240
- (c) => this.compareIds(c, newComponentType, strict)
1218
+ let slotDef = parentComponent.slots.find(
1219
+ (s) => this.compareIds(s.id, slot, strict)
1241
1220
  );
1242
- if (!isAlreadyAllowed) {
1243
- this.logger.action(
1244
- whatIf,
1245
- "UPDATE",
1246
- `Adding "${newComponentType}" to allowedComponents for slot "${slot}" on component "${parentComponentType}"`
1221
+ if (!slotDef) {
1222
+ this.logger.info(`Slot "${slot}" not found on component "${parentType}", creating it`);
1223
+ slotDef = { id: slot, name: slot, allowedComponents: [] };
1224
+ parentComponent.slots.push(slotDef);
1225
+ }
1226
+ const slotIndex = parentComponent.slots?.findIndex(
1227
+ (s) => this.compareIds(s.id, slotDef.id, strict)
1228
+ );
1229
+ if (slotIndex !== void 0 && slotIndex >= 0) {
1230
+ const actualSlot = parentComponent.slots[slotIndex];
1231
+ if (!actualSlot.allowedComponents) {
1232
+ actualSlot.allowedComponents = [];
1233
+ }
1234
+ const isAlreadyAllowed = actualSlot.allowedComponents.some(
1235
+ (c) => this.compareIds(c, newComponentType, strict)
1247
1236
  );
1248
- actualSlot.allowedComponents.push(newComponentType);
1249
- if (!whatIf) {
1250
- await this.componentService.saveComponent(parentComponentFilePath, parentComponent);
1237
+ if (!isAlreadyAllowed) {
1238
+ this.logger.action(
1239
+ whatIf,
1240
+ "UPDATE",
1241
+ `Adding "${newComponentType}" to allowedComponents for slot "${slot}" on component "${parentType}"`
1242
+ );
1243
+ actualSlot.allowedComponents.push(newComponentType);
1244
+ if (!whatIf) {
1245
+ await this.componentService.saveComponent(parentComponentFilePath, parentComponent);
1246
+ }
1247
+ allowedComponentsUpdated = true;
1251
1248
  }
1252
- allowedComponentsUpdated = true;
1253
1249
  }
1254
1250
  }
1255
1251
  const parsedParams = this.parseParameters(paramString);
1256
1252
  const newInstance = this.createComponentInstance(newComponentType, parsedParams);
1257
1253
  const compositionsResult = await this.addComponentToDirectory(
1258
1254
  fullCompositionsDir,
1259
- parentComponentType,
1255
+ parentTypes,
1260
1256
  slot,
1261
1257
  newInstance,
1262
1258
  whatIf,
@@ -1265,7 +1261,7 @@ var ComponentAdderService = class {
1265
1261
  );
1266
1262
  const compositionPatternsResult = await this.addComponentToDirectory(
1267
1263
  fullCompositionPatternsDir,
1268
- parentComponentType,
1264
+ parentTypes,
1269
1265
  slot,
1270
1266
  newInstance,
1271
1267
  whatIf,
@@ -1274,7 +1270,7 @@ var ComponentAdderService = class {
1274
1270
  );
1275
1271
  const componentPatternsResult = await this.addComponentToDirectory(
1276
1272
  fullComponentPatternsDir,
1277
- parentComponentType,
1273
+ parentTypes,
1278
1274
  slot,
1279
1275
  newInstance,
1280
1276
  whatIf,
@@ -1283,7 +1279,7 @@ var ComponentAdderService = class {
1283
1279
  );
1284
1280
  this.logger.info("");
1285
1281
  this.logger.info(
1286
- `Summary: ${allowedComponentsUpdated ? "1 component definition updated, " : ""}${compositionsResult} composition(s), ${compositionPatternsResult} composition pattern(s), ${componentPatternsResult} component pattern(s) updated. ${compositionsResult + compositionPatternsResult + componentPatternsResult} instance(s) added.`
1282
+ `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.`
1287
1283
  );
1288
1284
  return {
1289
1285
  allowedComponentsUpdated,
@@ -1314,18 +1310,9 @@ var ComponentAdderService = class {
1314
1310
  const fullCompositionPatternsDir = this.fileSystem.resolvePath(rootDir, compositionPatternsDir);
1315
1311
  const fullComponentPatternsDir = this.fileSystem.resolvePath(rootDir, componentPatternsDir);
1316
1312
  const findOptions = { strict };
1317
- this.logger.info(`Loading parent component: ${parentComponentType}`);
1318
- const { component: parentComponent } = await this.componentService.loadComponent(fullComponentsDir, parentComponentType, findOptions);
1319
- if (!parentComponent.slots) {
1320
- parentComponent.slots = [];
1321
- }
1322
- let slotDef = parentComponent.slots.find(
1323
- (s) => this.compareIds(s.id, slot, strict)
1324
- );
1325
- if (!slotDef) {
1326
- this.logger.info(`Slot "${slot}" not found on component "${parentComponentType}", creating it`);
1327
- slotDef = { id: slot, name: slot, allowedComponents: [] };
1328
- parentComponent.slots.push(slotDef);
1313
+ const parentTypes = this.parseParentComponentTypes(parentComponentType);
1314
+ if (parentTypes.length === 0) {
1315
+ throw new TransformError("parentComponentType cannot be empty");
1329
1316
  }
1330
1317
  this.logger.info(`Loading component pattern: ${componentPatternId}`);
1331
1318
  const pattern = await this.loadComponentPattern(
@@ -1339,38 +1326,55 @@ var ComponentAdderService = class {
1339
1326
  `Component pattern "${componentPatternId}" has no valid definition or missing type`
1340
1327
  );
1341
1328
  }
1342
- let allowedComponentsUpdated = false;
1343
1329
  const componentTypeInPattern = patternDefinition.type;
1344
- const slotIndex = parentComponent.slots?.findIndex(
1345
- (s) => this.compareIds(s.id, slotDef.id, strict)
1346
- );
1347
- if (slotIndex !== void 0 && slotIndex >= 0) {
1348
- const actualSlot = parentComponent.slots[slotIndex];
1349
- if (!actualSlot.allowedComponents) {
1350
- actualSlot.allowedComponents = [];
1330
+ let allowedComponentsUpdated = false;
1331
+ for (const parentType of parentTypes) {
1332
+ this.logger.info(`Loading parent component: ${parentType}`);
1333
+ const { component: parentComponent, filePath: parentComponentFilePath } = await this.componentService.loadComponent(fullComponentsDir, parentType, findOptions);
1334
+ if (!parentComponent.slots) {
1335
+ parentComponent.slots = [];
1351
1336
  }
1352
- const isAlreadyAllowed = actualSlot.allowedComponents.some(
1353
- (c) => this.compareIds(c, componentTypeInPattern, strict)
1337
+ let slotDef = parentComponent.slots.find(
1338
+ (s) => this.compareIds(s.id, slot, strict)
1354
1339
  );
1355
- if (!isAlreadyAllowed) {
1356
- this.logger.action(
1357
- whatIf,
1358
- "UPDATE",
1359
- `Adding "${componentTypeInPattern}" to allowedComponents for slot "${slot}" on component "${parentComponentType}"`
1340
+ if (!slotDef) {
1341
+ this.logger.info(`Slot "${slot}" not found on component "${parentType}", creating it`);
1342
+ slotDef = { id: slot, name: slot, allowedComponents: [] };
1343
+ parentComponent.slots.push(slotDef);
1344
+ }
1345
+ const slotIndex = parentComponent.slots?.findIndex(
1346
+ (s) => this.compareIds(s.id, slotDef.id, strict)
1347
+ );
1348
+ if (slotIndex !== void 0 && slotIndex >= 0) {
1349
+ const actualSlot = parentComponent.slots[slotIndex];
1350
+ if (!actualSlot.allowedComponents) {
1351
+ actualSlot.allowedComponents = [];
1352
+ }
1353
+ const isAlreadyAllowed = actualSlot.allowedComponents.some(
1354
+ (c) => this.compareIds(c, componentTypeInPattern, strict)
1360
1355
  );
1361
- actualSlot.allowedComponents.push(componentTypeInPattern);
1362
- if (!whatIf) {
1363
- await this.componentService.loadComponent(fullComponentsDir, parentComponentType, findOptions).then(
1364
- ({ filePath }) => this.componentService.saveComponent(filePath, parentComponent)
1356
+ if (!isAlreadyAllowed) {
1357
+ this.logger.action(
1358
+ whatIf,
1359
+ "UPDATE",
1360
+ `Adding "${componentTypeInPattern}" to allowedComponents for slot "${slot}" on component "${parentType}"`
1365
1361
  );
1362
+ actualSlot.allowedComponents.push(componentTypeInPattern);
1363
+ if (!whatIf) {
1364
+ await this.componentService.saveComponent(parentComponentFilePath, parentComponent);
1365
+ }
1366
+ allowedComponentsUpdated = true;
1366
1367
  }
1367
- allowedComponentsUpdated = true;
1368
1368
  }
1369
1369
  }
1370
- const newInstance = this.cloneComponentInstance(patternDefinition);
1370
+ const newInstance = {
1371
+ type: componentTypeInPattern,
1372
+ _pattern: pattern.componentPatternId,
1373
+ _id: randomUUID()
1374
+ };
1371
1375
  const compositionsResult = await this.addComponentToDirectory(
1372
1376
  fullCompositionsDir,
1373
- parentComponentType,
1377
+ parentTypes,
1374
1378
  slot,
1375
1379
  newInstance,
1376
1380
  whatIf,
@@ -1379,7 +1383,7 @@ var ComponentAdderService = class {
1379
1383
  );
1380
1384
  const compositionPatternsResult = await this.addComponentToDirectory(
1381
1385
  fullCompositionPatternsDir,
1382
- parentComponentType,
1386
+ parentTypes,
1383
1387
  slot,
1384
1388
  newInstance,
1385
1389
  whatIf,
@@ -1388,7 +1392,7 @@ var ComponentAdderService = class {
1388
1392
  );
1389
1393
  const componentPatternsResult = await this.addComponentToDirectory(
1390
1394
  fullComponentPatternsDir,
1391
- parentComponentType,
1395
+ parentTypes,
1392
1396
  slot,
1393
1397
  newInstance,
1394
1398
  whatIf,
@@ -1397,7 +1401,7 @@ var ComponentAdderService = class {
1397
1401
  );
1398
1402
  this.logger.info("");
1399
1403
  this.logger.info(
1400
- `Summary: ${allowedComponentsUpdated ? "1 component definition updated, " : ""}${compositionsResult} composition(s), ${compositionPatternsResult} composition pattern(s), ${componentPatternsResult} component pattern(s) updated. ${compositionsResult + compositionPatternsResult + componentPatternsResult} instance(s) added.`
1404
+ `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.`
1401
1405
  );
1402
1406
  return {
1403
1407
  allowedComponentsUpdated,
@@ -1448,7 +1452,7 @@ var ComponentAdderService = class {
1448
1452
  }
1449
1453
  return raw;
1450
1454
  }
1451
- async addComponentToDirectory(directory, parentComponentType, slot, newInstance, whatIf, strict, dirType) {
1455
+ async addComponentToDirectory(directory, parentTypes, slot, newInstance, whatIf, strict, dirType) {
1452
1456
  const exists = await this.fileSystem.fileExists(directory);
1453
1457
  if (!exists) {
1454
1458
  this.logger.detail(`${dirType} directory does not exist, skipping`);
@@ -1481,7 +1485,7 @@ var ComponentAdderService = class {
1481
1485
  }
1482
1486
  const rootInstance = composition.composition;
1483
1487
  let modified = false;
1484
- if (this.compareIds(rootInstance.type, parentComponentType, strict)) {
1488
+ if (this.matchesAnyParentType(rootInstance.type, parentTypes, strict)) {
1485
1489
  if (!rootInstance.slots) {
1486
1490
  rootInstance.slots = {};
1487
1491
  }
@@ -1493,7 +1497,7 @@ var ComponentAdderService = class {
1493
1497
  if (rootInstance.slots) {
1494
1498
  const nestedModified = this.addComponentToNestedSlots(
1495
1499
  rootInstance.slots,
1496
- parentComponentType,
1500
+ parentTypes,
1497
1501
  slot,
1498
1502
  newInstance,
1499
1503
  strict
@@ -1527,12 +1531,12 @@ var ComponentAdderService = class {
1527
1531
  }
1528
1532
  return filesModified;
1529
1533
  }
1530
- addComponentToNestedSlots(slots, parentComponentType, slot, newInstance, strict) {
1534
+ addComponentToNestedSlots(slots, parentTypes, slot, newInstance, strict) {
1531
1535
  let modified = false;
1532
1536
  for (const slotInstances of Object.values(slots)) {
1533
1537
  if (!Array.isArray(slotInstances)) continue;
1534
1538
  for (const instance of slotInstances) {
1535
- if (this.compareIds(instance.type, parentComponentType, strict)) {
1539
+ if (this.matchesAnyParentType(instance.type, parentTypes, strict)) {
1536
1540
  if (!instance.slots) {
1537
1541
  instance.slots = {};
1538
1542
  }
@@ -1544,7 +1548,7 @@ var ComponentAdderService = class {
1544
1548
  if (instance.slots) {
1545
1549
  const nestedModified = this.addComponentToNestedSlots(
1546
1550
  instance.slots,
1547
- parentComponentType,
1551
+ parentTypes,
1548
1552
  slot,
1549
1553
  newInstance,
1550
1554
  strict