@uniformdev/transformer 1.1.11 → 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/cli/index.js +588 -5
- package/dist/cli/index.js.map +1 -1
- package/dist/index.d.ts +78 -1
- package/dist/index.js +475 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
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);
|
|
@@ -417,13 +425,20 @@ interface ContentTypeField {
|
|
|
417
425
|
localizable?: boolean;
|
|
418
426
|
typeConfig?: Record<string, unknown>;
|
|
419
427
|
}
|
|
428
|
+
interface DataResourceDefinition {
|
|
429
|
+
type: string;
|
|
430
|
+
variables?: Record<string, string>;
|
|
431
|
+
}
|
|
420
432
|
interface EntryDefinition {
|
|
421
433
|
entry: {
|
|
422
434
|
_id: string;
|
|
423
435
|
_name: string;
|
|
424
436
|
type: string;
|
|
425
437
|
fields: Record<string, ParameterValue>;
|
|
438
|
+
_dataResources?: Record<string, DataResourceDefinition>;
|
|
439
|
+
[key: string]: unknown;
|
|
426
440
|
};
|
|
441
|
+
[key: string]: unknown;
|
|
427
442
|
}
|
|
428
443
|
interface FlattenedInstance {
|
|
429
444
|
instance: ComponentInstance;
|
|
@@ -451,6 +466,7 @@ declare class CompositionConverterService {
|
|
|
451
466
|
generateEntryFromFlattenedInstance(inst: FlattenedInstance): EntryDefinition;
|
|
452
467
|
findFlattenTargets(slots: Record<string, ComponentInstance[]>, targetType: string, compositionId: string, compositionName: string, strict: boolean): FlattenedInstance[];
|
|
453
468
|
private walkSlots;
|
|
469
|
+
private transformContentReferences;
|
|
454
470
|
private compareTypes;
|
|
455
471
|
private truncate;
|
|
456
472
|
}
|
|
@@ -466,4 +482,65 @@ declare class CompositionConverterService {
|
|
|
466
482
|
*/
|
|
467
483
|
declare function computeGuidHash(guidOrSeed: string): string;
|
|
468
484
|
|
|
469
|
-
|
|
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
|
@@ -1767,6 +1767,9 @@ var CompositionConverterService = class {
|
|
|
1767
1767
|
value: instances.map((inst) => inst.determinisiticId)
|
|
1768
1768
|
};
|
|
1769
1769
|
}
|
|
1770
|
+
if (flattenComponentIds.length > 0) {
|
|
1771
|
+
this.transformContentReferences(entry);
|
|
1772
|
+
}
|
|
1770
1773
|
const entryFilePath = this.fileSystem.joinPath(entriesDirFull, `${compositionId}.json`);
|
|
1771
1774
|
this.logger.action(
|
|
1772
1775
|
whatIf,
|
|
@@ -1889,13 +1892,29 @@ var CompositionConverterService = class {
|
|
|
1889
1892
|
// --- Entry Generation ---
|
|
1890
1893
|
generateEntryFromComposition(composition) {
|
|
1891
1894
|
const comp = composition.composition;
|
|
1895
|
+
const compositionSpecificKeys = /* @__PURE__ */ new Set(["_id", "_name", "type", "parameters", "slots", "_overrides"]);
|
|
1896
|
+
const extraRootProps = {};
|
|
1897
|
+
for (const [key, value] of Object.entries(comp)) {
|
|
1898
|
+
if (!compositionSpecificKeys.has(key) && value != null) {
|
|
1899
|
+
extraRootProps[key] = value;
|
|
1900
|
+
}
|
|
1901
|
+
}
|
|
1902
|
+
const wrapperKeys = /* @__PURE__ */ new Set(["composition"]);
|
|
1903
|
+
const extraWrapperProps = {};
|
|
1904
|
+
for (const [key, value] of Object.entries(composition)) {
|
|
1905
|
+
if (!wrapperKeys.has(key) && value != null) {
|
|
1906
|
+
extraWrapperProps[key] = value;
|
|
1907
|
+
}
|
|
1908
|
+
}
|
|
1892
1909
|
return {
|
|
1893
1910
|
entry: {
|
|
1894
1911
|
_id: comp._id,
|
|
1895
1912
|
_name: comp._name ?? comp._id,
|
|
1896
1913
|
type: comp.type,
|
|
1897
|
-
fields: { ...comp.parameters ?? {} }
|
|
1898
|
-
|
|
1914
|
+
fields: { ...comp.parameters ?? {} },
|
|
1915
|
+
...extraRootProps
|
|
1916
|
+
},
|
|
1917
|
+
...extraWrapperProps
|
|
1899
1918
|
};
|
|
1900
1919
|
}
|
|
1901
1920
|
generateEntryFromFlattenedInstance(inst) {
|
|
@@ -1949,6 +1968,30 @@ var CompositionConverterService = class {
|
|
|
1949
1968
|
}
|
|
1950
1969
|
}
|
|
1951
1970
|
}
|
|
1971
|
+
// --- Content Reference Transformation ---
|
|
1972
|
+
transformContentReferences(entry) {
|
|
1973
|
+
const dataResources = {};
|
|
1974
|
+
for (const [fieldName, field] of Object.entries(entry.entry.fields)) {
|
|
1975
|
+
if (field.type === "contentReference" && Array.isArray(field.value)) {
|
|
1976
|
+
const entryIds = field.value;
|
|
1977
|
+
const resourceKey = `ref-${entry.entry._id}-${fieldName}`;
|
|
1978
|
+
field.value = `\${#jptr:/${resourceKey}/entries}`;
|
|
1979
|
+
dataResources[resourceKey] = {
|
|
1980
|
+
type: "uniformContentInternalReference",
|
|
1981
|
+
variables: {
|
|
1982
|
+
locale: "${locale}",
|
|
1983
|
+
entryIds: entryIds.join(",")
|
|
1984
|
+
}
|
|
1985
|
+
};
|
|
1986
|
+
}
|
|
1987
|
+
}
|
|
1988
|
+
if (Object.keys(dataResources).length > 0) {
|
|
1989
|
+
entry.entry._dataResources = {
|
|
1990
|
+
...entry.entry._dataResources ?? {},
|
|
1991
|
+
...dataResources
|
|
1992
|
+
};
|
|
1993
|
+
}
|
|
1994
|
+
}
|
|
1952
1995
|
// --- Utilities ---
|
|
1953
1996
|
compareTypes(type1, type2, strict) {
|
|
1954
1997
|
if (strict) {
|
|
@@ -2011,6 +2054,434 @@ function formatAsBracedUuid(uuid) {
|
|
|
2011
2054
|
return `{${clean}}`;
|
|
2012
2055
|
}
|
|
2013
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
|
+
|
|
2014
2485
|
// src/cli/logger.ts
|
|
2015
2486
|
import chalk from "chalk";
|
|
2016
2487
|
var Logger = class {
|
|
@@ -2043,10 +2514,12 @@ export {
|
|
|
2043
2514
|
CompositionConverterService,
|
|
2044
2515
|
CompositionService,
|
|
2045
2516
|
DuplicateIdError,
|
|
2517
|
+
FieldRemoverService,
|
|
2046
2518
|
FileNotFoundError,
|
|
2047
2519
|
FileSystemService,
|
|
2048
2520
|
InvalidYamlError,
|
|
2049
2521
|
Logger,
|
|
2522
|
+
ParameterRemoverService,
|
|
2050
2523
|
PropertyNotFoundError,
|
|
2051
2524
|
PropertyPropagatorService,
|
|
2052
2525
|
SlotAlreadyExistsError,
|