@uniformdev/transformer 1.1.12 → 1.1.13

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
@@ -132,6 +132,14 @@ interface ConvertCompositionsToEntriesOptions extends GlobalOptions {
132
132
  compositionTypes: string;
133
133
  flattenComponentIds?: string;
134
134
  }
135
+ interface RemoveParameterOptions extends GlobalOptions {
136
+ componentType: string;
137
+ parameterId: string;
138
+ }
139
+ interface RemoveFieldOptions extends GlobalOptions {
140
+ componentType: string;
141
+ parameterId: string;
142
+ }
135
143
 
136
144
  declare class TransformError extends Error {
137
145
  constructor(message: string);
@@ -474,4 +482,65 @@ declare class CompositionConverterService {
474
482
  */
475
483
  declare function computeGuidHash(guidOrSeed: string): string;
476
484
 
477
- export { type AddComponentOptions, type AddComponentPatternOptions, ComponentAdderService, ComponentAlreadyExistsError, type ComponentDefinition, type ComponentInstance, ComponentNotFoundError, ComponentRenamerService, ComponentService, type Composition, CompositionConverterService, type CompositionOverrides, type CompositionPatternCandidatesOptions, type CompositionRoot, CompositionService, type ConvertCompositionsToEntriesOptions, DuplicateIdError, FileNotFoundError, FileSystemService, type GlobalOptions, InvalidYamlError, Logger, type PackSerializationOptions, type Parameter, type ParameterValue, type PropagateRootComponentPropertyOptions, type PropagateRootComponentSlotOptions, PropertyNotFoundError, PropertyPropagatorService, type RenameComponentOptions, type RenameSlotOptions, SlotAlreadyExistsError, type SlotDefinition, SlotNotFoundError, SlotRenamerService, TransformError, type UnpackSerializationOptions, computeGuidHash };
485
+ interface RemoveParameterInternalOptions {
486
+ rootDir: string;
487
+ componentsDir: string;
488
+ compositionsDir: string;
489
+ compositionPatternsDir: string;
490
+ componentPatternsDir: string;
491
+ componentType: string;
492
+ parameterId: string;
493
+ whatIf: boolean;
494
+ strict: boolean;
495
+ }
496
+ interface RemoveParameterResult {
497
+ componentDefinitionUpdated: boolean;
498
+ compositionsModified: number;
499
+ compositionPatternsModified: number;
500
+ componentPatternsModified: number;
501
+ instancesUpdated: number;
502
+ }
503
+ declare class ParameterRemoverService {
504
+ private fileSystem;
505
+ private componentService;
506
+ private logger;
507
+ constructor(fileSystem: FileSystemService, componentService: ComponentService, logger: Logger);
508
+ private compareIds;
509
+ remove(options: RemoveParameterInternalOptions): Promise<RemoveParameterResult>;
510
+ private removeParameterInDirectory;
511
+ private removeParameterFromTree;
512
+ private removeParameterFromOverrides;
513
+ private removeOverridesForMatchingInstances;
514
+ private removeKeyFromMap;
515
+ }
516
+
517
+ interface RemoveFieldInternalOptions {
518
+ rootDir: string;
519
+ compositionsDir: string;
520
+ compositionPatternsDir: string;
521
+ componentPatternsDir: string;
522
+ componentType: string;
523
+ parameterId: string;
524
+ whatIf: boolean;
525
+ strict: boolean;
526
+ }
527
+ interface RemoveFieldResult {
528
+ compositionsModified: number;
529
+ compositionPatternsModified: number;
530
+ componentPatternsModified: number;
531
+ instancesUpdated: number;
532
+ }
533
+ declare class FieldRemoverService {
534
+ private fileSystem;
535
+ private logger;
536
+ constructor(fileSystem: FileSystemService, logger: Logger);
537
+ private compareIds;
538
+ remove(options: RemoveFieldInternalOptions): Promise<RemoveFieldResult>;
539
+ private removeFieldInDirectory;
540
+ private removeFieldFromTree;
541
+ private removeFieldFromOverrides;
542
+ private removeOverridesForMatchingInstances;
543
+ private removeKeyFromMap;
544
+ }
545
+
546
+ export { type AddComponentOptions, type AddComponentPatternOptions, ComponentAdderService, ComponentAlreadyExistsError, type ComponentDefinition, type ComponentInstance, ComponentNotFoundError, ComponentRenamerService, ComponentService, type Composition, CompositionConverterService, type CompositionOverrides, type CompositionPatternCandidatesOptions, type CompositionRoot, CompositionService, type ConvertCompositionsToEntriesOptions, DuplicateIdError, FieldRemoverService, FileNotFoundError, FileSystemService, type GlobalOptions, InvalidYamlError, Logger, type PackSerializationOptions, type Parameter, ParameterRemoverService, type ParameterValue, type PropagateRootComponentPropertyOptions, type PropagateRootComponentSlotOptions, PropertyNotFoundError, PropertyPropagatorService, type RemoveFieldOptions, type RemoveParameterOptions, type RenameComponentOptions, type RenameSlotOptions, SlotAlreadyExistsError, type SlotDefinition, SlotNotFoundError, SlotRenamerService, TransformError, type UnpackSerializationOptions, computeGuidHash };
package/dist/index.js CHANGED
@@ -2054,6 +2054,434 @@ function formatAsBracedUuid(uuid) {
2054
2054
  return `{${clean}}`;
2055
2055
  }
2056
2056
 
2057
+ // src/core/services/parameter-remover.service.ts
2058
+ var ParameterRemoverService = class {
2059
+ constructor(fileSystem, componentService, logger) {
2060
+ this.fileSystem = fileSystem;
2061
+ this.componentService = componentService;
2062
+ this.logger = logger;
2063
+ }
2064
+ compareIds(id1, id2, strict) {
2065
+ if (strict) {
2066
+ return id1 === id2;
2067
+ }
2068
+ return id1.toLowerCase() === id2.toLowerCase();
2069
+ }
2070
+ async remove(options) {
2071
+ const {
2072
+ rootDir,
2073
+ componentsDir,
2074
+ compositionsDir,
2075
+ compositionPatternsDir,
2076
+ componentPatternsDir,
2077
+ componentType,
2078
+ parameterId,
2079
+ whatIf,
2080
+ strict
2081
+ } = options;
2082
+ const findOptions = { strict };
2083
+ const fullComponentsDir = this.fileSystem.resolvePath(rootDir, componentsDir);
2084
+ const fullCompositionsDir = this.fileSystem.resolvePath(rootDir, compositionsDir);
2085
+ const fullCompositionPatternsDir = this.fileSystem.resolvePath(rootDir, compositionPatternsDir);
2086
+ const fullComponentPatternsDir = this.fileSystem.resolvePath(rootDir, componentPatternsDir);
2087
+ this.logger.info(`Loading component: ${componentType}`);
2088
+ const { component, filePath: componentFilePath } = await this.componentService.loadComponent(fullComponentsDir, componentType, findOptions);
2089
+ const param = this.componentService.findParameter(component, parameterId, findOptions);
2090
+ if (!param) {
2091
+ throw new PropertyNotFoundError(parameterId, componentType);
2092
+ }
2093
+ this.logger.action(
2094
+ whatIf,
2095
+ "REMOVE",
2096
+ `Parameter "${parameterId}" from component/${this.fileSystem.getBasename(componentFilePath)}`
2097
+ );
2098
+ let modifiedComponent = this.componentService.removeParameter(component, parameterId, findOptions);
2099
+ const beforeGroupCount = modifiedComponent.parameters?.filter(
2100
+ (p) => this.componentService.isGroupParameter(p)
2101
+ ).length ?? 0;
2102
+ modifiedComponent = this.componentService.removeEmptyGroups(modifiedComponent);
2103
+ const afterGroupCount = modifiedComponent.parameters?.filter(
2104
+ (p) => this.componentService.isGroupParameter(p)
2105
+ ).length ?? 0;
2106
+ if (afterGroupCount < beforeGroupCount) {
2107
+ const removedCount = beforeGroupCount - afterGroupCount;
2108
+ this.logger.action(
2109
+ whatIf,
2110
+ "REMOVE",
2111
+ `${removedCount} empty group(s) from component/${this.fileSystem.getBasename(componentFilePath)}`
2112
+ );
2113
+ }
2114
+ if (!whatIf) {
2115
+ await this.componentService.saveComponent(componentFilePath, modifiedComponent);
2116
+ }
2117
+ const compositionsResult = await this.removeParameterInDirectory(
2118
+ fullCompositionsDir,
2119
+ componentType,
2120
+ parameterId,
2121
+ whatIf,
2122
+ strict,
2123
+ "composition"
2124
+ );
2125
+ const compositionPatternsResult = await this.removeParameterInDirectory(
2126
+ fullCompositionPatternsDir,
2127
+ componentType,
2128
+ parameterId,
2129
+ whatIf,
2130
+ strict,
2131
+ "compositionPattern"
2132
+ );
2133
+ const componentPatternsResult = await this.removeParameterInDirectory(
2134
+ fullComponentPatternsDir,
2135
+ componentType,
2136
+ parameterId,
2137
+ whatIf,
2138
+ strict,
2139
+ "componentPattern"
2140
+ );
2141
+ const totalFiles = compositionsResult.filesModified + compositionPatternsResult.filesModified + componentPatternsResult.filesModified;
2142
+ const totalInstances = compositionsResult.instancesUpdated + compositionPatternsResult.instancesUpdated + componentPatternsResult.instancesUpdated;
2143
+ this.logger.info("");
2144
+ this.logger.info(
2145
+ `Summary: 1 component definition updated, ${totalFiles} file(s) (${totalInstances} instance(s)) updated.`
2146
+ );
2147
+ return {
2148
+ componentDefinitionUpdated: true,
2149
+ compositionsModified: compositionsResult.filesModified,
2150
+ compositionPatternsModified: compositionPatternsResult.filesModified,
2151
+ componentPatternsModified: componentPatternsResult.filesModified,
2152
+ instancesUpdated: totalInstances
2153
+ };
2154
+ }
2155
+ async removeParameterInDirectory(directory, componentType, parameterId, whatIf, strict, label) {
2156
+ let files;
2157
+ try {
2158
+ files = await this.fileSystem.findFiles(directory, "**/*.{json,yaml,yml}");
2159
+ } catch {
2160
+ return { filesModified: 0, instancesUpdated: 0 };
2161
+ }
2162
+ if (files.length === 0) {
2163
+ return { filesModified: 0, instancesUpdated: 0 };
2164
+ }
2165
+ let filesModified = 0;
2166
+ let instancesUpdated = 0;
2167
+ for (const filePath of files) {
2168
+ let composition;
2169
+ try {
2170
+ composition = await this.fileSystem.readFile(filePath);
2171
+ } catch {
2172
+ continue;
2173
+ }
2174
+ if (!composition?.composition) {
2175
+ continue;
2176
+ }
2177
+ const count = this.removeParameterFromTree(
2178
+ composition.composition,
2179
+ componentType,
2180
+ parameterId,
2181
+ strict
2182
+ );
2183
+ const overridesCount = this.removeParameterFromOverrides(
2184
+ composition,
2185
+ componentType,
2186
+ parameterId,
2187
+ strict
2188
+ );
2189
+ const totalCount = count + overridesCount;
2190
+ if (totalCount > 0) {
2191
+ const relativePath = filePath.replace(directory, "").replace(/^[/\\]/, "");
2192
+ this.logger.action(
2193
+ whatIf,
2194
+ "UPDATE",
2195
+ `${label}/${relativePath} (${totalCount} instance(s) of ${componentType})`
2196
+ );
2197
+ if (!whatIf) {
2198
+ await this.fileSystem.writeFile(filePath, composition);
2199
+ }
2200
+ filesModified++;
2201
+ instancesUpdated += totalCount;
2202
+ }
2203
+ }
2204
+ return { filesModified, instancesUpdated };
2205
+ }
2206
+ removeParameterFromTree(node, componentType, parameterId, strict) {
2207
+ let count = 0;
2208
+ if (this.compareIds(node.type, componentType, strict) && node.parameters) {
2209
+ const removed = this.removeKeyFromMap(node.parameters, parameterId, strict);
2210
+ if (removed) {
2211
+ count++;
2212
+ }
2213
+ }
2214
+ if (node.slots) {
2215
+ for (const slotInstances of Object.values(node.slots)) {
2216
+ if (!Array.isArray(slotInstances)) continue;
2217
+ for (const instance of slotInstances) {
2218
+ count += this.removeParameterFromTree(instance, componentType, parameterId, strict);
2219
+ }
2220
+ }
2221
+ }
2222
+ return count;
2223
+ }
2224
+ removeParameterFromOverrides(composition, componentType, parameterId, strict) {
2225
+ if (!composition.composition._overrides) {
2226
+ return 0;
2227
+ }
2228
+ let count = 0;
2229
+ if (this.compareIds(composition.composition.type, componentType, strict)) {
2230
+ const rootId = composition.composition._id;
2231
+ const rootOverrides = composition.composition._overrides[rootId];
2232
+ if (rootOverrides?.parameters) {
2233
+ const removed = this.removeKeyFromMap(rootOverrides.parameters, parameterId, strict);
2234
+ if (removed) {
2235
+ count++;
2236
+ }
2237
+ }
2238
+ }
2239
+ const counter = { count: 0 };
2240
+ this.removeOverridesForMatchingInstances(
2241
+ composition.composition,
2242
+ composition.composition._overrides,
2243
+ componentType,
2244
+ parameterId,
2245
+ strict,
2246
+ counter
2247
+ );
2248
+ count += counter.count;
2249
+ return count;
2250
+ }
2251
+ removeOverridesForMatchingInstances(node, overrides, componentType, parameterId, strict, counter) {
2252
+ if (node.slots) {
2253
+ for (const slotInstances of Object.values(node.slots)) {
2254
+ if (!Array.isArray(slotInstances)) continue;
2255
+ for (const instance of slotInstances) {
2256
+ if (this.compareIds(instance.type, componentType, strict) && instance._id) {
2257
+ const instanceOverrides = overrides[instance._id];
2258
+ if (instanceOverrides?.parameters) {
2259
+ const removed = this.removeKeyFromMap(instanceOverrides.parameters, parameterId, strict);
2260
+ if (removed) {
2261
+ counter.count++;
2262
+ }
2263
+ }
2264
+ }
2265
+ this.removeOverridesForMatchingInstances(
2266
+ instance,
2267
+ overrides,
2268
+ componentType,
2269
+ parameterId,
2270
+ strict,
2271
+ counter
2272
+ );
2273
+ }
2274
+ }
2275
+ }
2276
+ }
2277
+ removeKeyFromMap(map, key, strict) {
2278
+ const matchingKey = Object.keys(map).find(
2279
+ (k) => this.compareIds(k, key, strict)
2280
+ );
2281
+ if (!matchingKey) {
2282
+ return false;
2283
+ }
2284
+ delete map[matchingKey];
2285
+ return true;
2286
+ }
2287
+ };
2288
+
2289
+ // src/core/services/field-remover.service.ts
2290
+ var FieldRemoverService = class {
2291
+ constructor(fileSystem, logger) {
2292
+ this.fileSystem = fileSystem;
2293
+ this.logger = logger;
2294
+ }
2295
+ compareIds(id1, id2, strict) {
2296
+ if (strict) {
2297
+ return id1 === id2;
2298
+ }
2299
+ return id1.toLowerCase() === id2.toLowerCase();
2300
+ }
2301
+ async remove(options) {
2302
+ const {
2303
+ rootDir,
2304
+ compositionsDir,
2305
+ compositionPatternsDir,
2306
+ componentPatternsDir,
2307
+ componentType,
2308
+ parameterId,
2309
+ whatIf,
2310
+ strict
2311
+ } = options;
2312
+ const fullCompositionsDir = this.fileSystem.resolvePath(rootDir, compositionsDir);
2313
+ const fullCompositionPatternsDir = this.fileSystem.resolvePath(rootDir, compositionPatternsDir);
2314
+ const fullComponentPatternsDir = this.fileSystem.resolvePath(rootDir, componentPatternsDir);
2315
+ this.logger.info(`Removing field "${parameterId}" from instances of ${componentType}`);
2316
+ const compositionsResult = await this.removeFieldInDirectory(
2317
+ fullCompositionsDir,
2318
+ componentType,
2319
+ parameterId,
2320
+ whatIf,
2321
+ strict,
2322
+ "composition"
2323
+ );
2324
+ const compositionPatternsResult = await this.removeFieldInDirectory(
2325
+ fullCompositionPatternsDir,
2326
+ componentType,
2327
+ parameterId,
2328
+ whatIf,
2329
+ strict,
2330
+ "compositionPattern"
2331
+ );
2332
+ const componentPatternsResult = await this.removeFieldInDirectory(
2333
+ fullComponentPatternsDir,
2334
+ componentType,
2335
+ parameterId,
2336
+ whatIf,
2337
+ strict,
2338
+ "componentPattern"
2339
+ );
2340
+ const totalFiles = compositionsResult.filesModified + compositionPatternsResult.filesModified + componentPatternsResult.filesModified;
2341
+ const totalInstances = compositionsResult.instancesUpdated + compositionPatternsResult.instancesUpdated + componentPatternsResult.instancesUpdated;
2342
+ this.logger.info("");
2343
+ this.logger.info(
2344
+ `Summary: ${totalFiles} file(s) (${totalInstances} instance(s)) updated.`
2345
+ );
2346
+ return {
2347
+ compositionsModified: compositionsResult.filesModified,
2348
+ compositionPatternsModified: compositionPatternsResult.filesModified,
2349
+ componentPatternsModified: componentPatternsResult.filesModified,
2350
+ instancesUpdated: totalInstances
2351
+ };
2352
+ }
2353
+ async removeFieldInDirectory(directory, componentType, parameterId, whatIf, strict, label) {
2354
+ let files;
2355
+ try {
2356
+ files = await this.fileSystem.findFiles(directory, "**/*.{json,yaml,yml}");
2357
+ } catch {
2358
+ return { filesModified: 0, instancesUpdated: 0 };
2359
+ }
2360
+ if (files.length === 0) {
2361
+ return { filesModified: 0, instancesUpdated: 0 };
2362
+ }
2363
+ let filesModified = 0;
2364
+ let instancesUpdated = 0;
2365
+ for (const filePath of files) {
2366
+ let composition;
2367
+ try {
2368
+ composition = await this.fileSystem.readFile(filePath);
2369
+ } catch {
2370
+ continue;
2371
+ }
2372
+ if (!composition?.composition) {
2373
+ continue;
2374
+ }
2375
+ const treeCount = this.removeFieldFromTree(
2376
+ composition.composition,
2377
+ componentType,
2378
+ parameterId,
2379
+ strict
2380
+ );
2381
+ const overridesCount = this.removeFieldFromOverrides(
2382
+ composition,
2383
+ componentType,
2384
+ parameterId,
2385
+ strict
2386
+ );
2387
+ const totalCount = treeCount + overridesCount;
2388
+ if (totalCount > 0) {
2389
+ const relativePath = filePath.replace(directory, "").replace(/^[/\\]/, "");
2390
+ this.logger.action(
2391
+ whatIf,
2392
+ "UPDATE",
2393
+ `${label}/${relativePath} (${totalCount} instance(s) of ${componentType})`
2394
+ );
2395
+ if (!whatIf) {
2396
+ await this.fileSystem.writeFile(filePath, composition);
2397
+ }
2398
+ filesModified++;
2399
+ instancesUpdated += totalCount;
2400
+ }
2401
+ }
2402
+ return { filesModified, instancesUpdated };
2403
+ }
2404
+ removeFieldFromTree(node, componentType, parameterId, strict) {
2405
+ let count = 0;
2406
+ if (this.compareIds(node.type, componentType, strict) && node.parameters) {
2407
+ const removed = this.removeKeyFromMap(node.parameters, parameterId, strict);
2408
+ if (removed) {
2409
+ count++;
2410
+ }
2411
+ }
2412
+ if (node.slots) {
2413
+ for (const slotInstances of Object.values(node.slots)) {
2414
+ if (!Array.isArray(slotInstances)) continue;
2415
+ for (const instance of slotInstances) {
2416
+ count += this.removeFieldFromTree(instance, componentType, parameterId, strict);
2417
+ }
2418
+ }
2419
+ }
2420
+ return count;
2421
+ }
2422
+ removeFieldFromOverrides(composition, componentType, parameterId, strict) {
2423
+ if (!composition.composition._overrides) {
2424
+ return 0;
2425
+ }
2426
+ let count = 0;
2427
+ if (this.compareIds(composition.composition.type, componentType, strict)) {
2428
+ const rootId = composition.composition._id;
2429
+ const rootOverrides = composition.composition._overrides[rootId];
2430
+ if (rootOverrides?.parameters) {
2431
+ const removed = this.removeKeyFromMap(rootOverrides.parameters, parameterId, strict);
2432
+ if (removed) {
2433
+ count++;
2434
+ }
2435
+ }
2436
+ }
2437
+ count += this.removeOverridesForMatchingInstances(
2438
+ composition.composition,
2439
+ composition.composition._overrides,
2440
+ componentType,
2441
+ parameterId,
2442
+ strict
2443
+ );
2444
+ return count;
2445
+ }
2446
+ removeOverridesForMatchingInstances(node, overrides, componentType, parameterId, strict) {
2447
+ let count = 0;
2448
+ if (node.slots) {
2449
+ for (const slotInstances of Object.values(node.slots)) {
2450
+ if (!Array.isArray(slotInstances)) continue;
2451
+ for (const instance of slotInstances) {
2452
+ if (this.compareIds(instance.type, componentType, strict) && instance._id) {
2453
+ const instanceOverrides = overrides[instance._id];
2454
+ if (instanceOverrides?.parameters) {
2455
+ const removed = this.removeKeyFromMap(instanceOverrides.parameters, parameterId, strict);
2456
+ if (removed) {
2457
+ count++;
2458
+ }
2459
+ }
2460
+ }
2461
+ count += this.removeOverridesForMatchingInstances(
2462
+ instance,
2463
+ overrides,
2464
+ componentType,
2465
+ parameterId,
2466
+ strict
2467
+ );
2468
+ }
2469
+ }
2470
+ }
2471
+ return count;
2472
+ }
2473
+ removeKeyFromMap(map, key, strict) {
2474
+ const matchingKey = Object.keys(map).find(
2475
+ (k) => this.compareIds(k, key, strict)
2476
+ );
2477
+ if (!matchingKey) {
2478
+ return false;
2479
+ }
2480
+ delete map[matchingKey];
2481
+ return true;
2482
+ }
2483
+ };
2484
+
2057
2485
  // src/cli/logger.ts
2058
2486
  import chalk from "chalk";
2059
2487
  var Logger = class {
@@ -2086,10 +2514,12 @@ export {
2086
2514
  CompositionConverterService,
2087
2515
  CompositionService,
2088
2516
  DuplicateIdError,
2517
+ FieldRemoverService,
2089
2518
  FileNotFoundError,
2090
2519
  FileSystemService,
2091
2520
  InvalidYamlError,
2092
2521
  Logger,
2522
+ ParameterRemoverService,
2093
2523
  PropertyNotFoundError,
2094
2524
  PropertyPropagatorService,
2095
2525
  SlotAlreadyExistsError,