@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/cli/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
3
|
// src/cli/index.ts
|
|
4
|
-
import { Command as
|
|
4
|
+
import { Command as Command13 } from "commander";
|
|
5
5
|
|
|
6
6
|
// src/cli/commands/propagate-root-component-property.ts
|
|
7
7
|
import { Command } from "commander";
|
|
@@ -3696,6 +3696,9 @@ var CompositionConverterService = class {
|
|
|
3696
3696
|
value: instances.map((inst) => inst.determinisiticId)
|
|
3697
3697
|
};
|
|
3698
3698
|
}
|
|
3699
|
+
if (flattenComponentIds.length > 0) {
|
|
3700
|
+
this.transformContentReferences(entry);
|
|
3701
|
+
}
|
|
3699
3702
|
const entryFilePath = this.fileSystem.joinPath(entriesDirFull, `${compositionId}.json`);
|
|
3700
3703
|
this.logger.action(
|
|
3701
3704
|
whatIf,
|
|
@@ -3818,13 +3821,29 @@ var CompositionConverterService = class {
|
|
|
3818
3821
|
// --- Entry Generation ---
|
|
3819
3822
|
generateEntryFromComposition(composition) {
|
|
3820
3823
|
const comp = composition.composition;
|
|
3824
|
+
const compositionSpecificKeys = /* @__PURE__ */ new Set(["_id", "_name", "type", "parameters", "slots", "_overrides"]);
|
|
3825
|
+
const extraRootProps = {};
|
|
3826
|
+
for (const [key, value] of Object.entries(comp)) {
|
|
3827
|
+
if (!compositionSpecificKeys.has(key) && value != null) {
|
|
3828
|
+
extraRootProps[key] = value;
|
|
3829
|
+
}
|
|
3830
|
+
}
|
|
3831
|
+
const wrapperKeys = /* @__PURE__ */ new Set(["composition"]);
|
|
3832
|
+
const extraWrapperProps = {};
|
|
3833
|
+
for (const [key, value] of Object.entries(composition)) {
|
|
3834
|
+
if (!wrapperKeys.has(key) && value != null) {
|
|
3835
|
+
extraWrapperProps[key] = value;
|
|
3836
|
+
}
|
|
3837
|
+
}
|
|
3821
3838
|
return {
|
|
3822
3839
|
entry: {
|
|
3823
3840
|
_id: comp._id,
|
|
3824
3841
|
_name: comp._name ?? comp._id,
|
|
3825
3842
|
type: comp.type,
|
|
3826
|
-
fields: { ...comp.parameters ?? {} }
|
|
3827
|
-
|
|
3843
|
+
fields: { ...comp.parameters ?? {} },
|
|
3844
|
+
...extraRootProps
|
|
3845
|
+
},
|
|
3846
|
+
...extraWrapperProps
|
|
3828
3847
|
};
|
|
3829
3848
|
}
|
|
3830
3849
|
generateEntryFromFlattenedInstance(inst) {
|
|
@@ -3878,6 +3897,30 @@ var CompositionConverterService = class {
|
|
|
3878
3897
|
}
|
|
3879
3898
|
}
|
|
3880
3899
|
}
|
|
3900
|
+
// --- Content Reference Transformation ---
|
|
3901
|
+
transformContentReferences(entry) {
|
|
3902
|
+
const dataResources = {};
|
|
3903
|
+
for (const [fieldName, field] of Object.entries(entry.entry.fields)) {
|
|
3904
|
+
if (field.type === "contentReference" && Array.isArray(field.value)) {
|
|
3905
|
+
const entryIds = field.value;
|
|
3906
|
+
const resourceKey = `ref-${entry.entry._id}-${fieldName}`;
|
|
3907
|
+
field.value = `\${#jptr:/${resourceKey}/entries}`;
|
|
3908
|
+
dataResources[resourceKey] = {
|
|
3909
|
+
type: "uniformContentInternalReference",
|
|
3910
|
+
variables: {
|
|
3911
|
+
locale: "${locale}",
|
|
3912
|
+
entryIds: entryIds.join(",")
|
|
3913
|
+
}
|
|
3914
|
+
};
|
|
3915
|
+
}
|
|
3916
|
+
}
|
|
3917
|
+
if (Object.keys(dataResources).length > 0) {
|
|
3918
|
+
entry.entry._dataResources = {
|
|
3919
|
+
...entry.entry._dataResources ?? {},
|
|
3920
|
+
...dataResources
|
|
3921
|
+
};
|
|
3922
|
+
}
|
|
3923
|
+
}
|
|
3881
3924
|
// --- Utilities ---
|
|
3882
3925
|
compareTypes(type1, type2, strict) {
|
|
3883
3926
|
if (strict) {
|
|
@@ -4007,10 +4050,548 @@ function createConvertCompositionsToEntriesCommand() {
|
|
|
4007
4050
|
return command;
|
|
4008
4051
|
}
|
|
4009
4052
|
|
|
4053
|
+
// src/cli/commands/remove-parameter.ts
|
|
4054
|
+
import { Command as Command11 } from "commander";
|
|
4055
|
+
|
|
4056
|
+
// src/core/services/parameter-remover.service.ts
|
|
4057
|
+
var ParameterRemoverService = class {
|
|
4058
|
+
constructor(fileSystem, componentService, logger) {
|
|
4059
|
+
this.fileSystem = fileSystem;
|
|
4060
|
+
this.componentService = componentService;
|
|
4061
|
+
this.logger = logger;
|
|
4062
|
+
}
|
|
4063
|
+
compareIds(id1, id2, strict) {
|
|
4064
|
+
if (strict) {
|
|
4065
|
+
return id1 === id2;
|
|
4066
|
+
}
|
|
4067
|
+
return id1.toLowerCase() === id2.toLowerCase();
|
|
4068
|
+
}
|
|
4069
|
+
async remove(options) {
|
|
4070
|
+
const {
|
|
4071
|
+
rootDir,
|
|
4072
|
+
componentsDir,
|
|
4073
|
+
compositionsDir,
|
|
4074
|
+
compositionPatternsDir,
|
|
4075
|
+
componentPatternsDir,
|
|
4076
|
+
componentType,
|
|
4077
|
+
parameterId,
|
|
4078
|
+
whatIf,
|
|
4079
|
+
strict
|
|
4080
|
+
} = options;
|
|
4081
|
+
const findOptions = { strict };
|
|
4082
|
+
const fullComponentsDir = this.fileSystem.resolvePath(rootDir, componentsDir);
|
|
4083
|
+
const fullCompositionsDir = this.fileSystem.resolvePath(rootDir, compositionsDir);
|
|
4084
|
+
const fullCompositionPatternsDir = this.fileSystem.resolvePath(rootDir, compositionPatternsDir);
|
|
4085
|
+
const fullComponentPatternsDir = this.fileSystem.resolvePath(rootDir, componentPatternsDir);
|
|
4086
|
+
this.logger.info(`Loading component: ${componentType}`);
|
|
4087
|
+
const { component, filePath: componentFilePath } = await this.componentService.loadComponent(fullComponentsDir, componentType, findOptions);
|
|
4088
|
+
const param = this.componentService.findParameter(component, parameterId, findOptions);
|
|
4089
|
+
if (!param) {
|
|
4090
|
+
throw new PropertyNotFoundError(parameterId, componentType);
|
|
4091
|
+
}
|
|
4092
|
+
this.logger.action(
|
|
4093
|
+
whatIf,
|
|
4094
|
+
"REMOVE",
|
|
4095
|
+
`Parameter "${parameterId}" from component/${this.fileSystem.getBasename(componentFilePath)}`
|
|
4096
|
+
);
|
|
4097
|
+
let modifiedComponent = this.componentService.removeParameter(component, parameterId, findOptions);
|
|
4098
|
+
const beforeGroupCount = modifiedComponent.parameters?.filter(
|
|
4099
|
+
(p) => this.componentService.isGroupParameter(p)
|
|
4100
|
+
).length ?? 0;
|
|
4101
|
+
modifiedComponent = this.componentService.removeEmptyGroups(modifiedComponent);
|
|
4102
|
+
const afterGroupCount = modifiedComponent.parameters?.filter(
|
|
4103
|
+
(p) => this.componentService.isGroupParameter(p)
|
|
4104
|
+
).length ?? 0;
|
|
4105
|
+
if (afterGroupCount < beforeGroupCount) {
|
|
4106
|
+
const removedCount = beforeGroupCount - afterGroupCount;
|
|
4107
|
+
this.logger.action(
|
|
4108
|
+
whatIf,
|
|
4109
|
+
"REMOVE",
|
|
4110
|
+
`${removedCount} empty group(s) from component/${this.fileSystem.getBasename(componentFilePath)}`
|
|
4111
|
+
);
|
|
4112
|
+
}
|
|
4113
|
+
if (!whatIf) {
|
|
4114
|
+
await this.componentService.saveComponent(componentFilePath, modifiedComponent);
|
|
4115
|
+
}
|
|
4116
|
+
const compositionsResult = await this.removeParameterInDirectory(
|
|
4117
|
+
fullCompositionsDir,
|
|
4118
|
+
componentType,
|
|
4119
|
+
parameterId,
|
|
4120
|
+
whatIf,
|
|
4121
|
+
strict,
|
|
4122
|
+
"composition"
|
|
4123
|
+
);
|
|
4124
|
+
const compositionPatternsResult = await this.removeParameterInDirectory(
|
|
4125
|
+
fullCompositionPatternsDir,
|
|
4126
|
+
componentType,
|
|
4127
|
+
parameterId,
|
|
4128
|
+
whatIf,
|
|
4129
|
+
strict,
|
|
4130
|
+
"compositionPattern"
|
|
4131
|
+
);
|
|
4132
|
+
const componentPatternsResult = await this.removeParameterInDirectory(
|
|
4133
|
+
fullComponentPatternsDir,
|
|
4134
|
+
componentType,
|
|
4135
|
+
parameterId,
|
|
4136
|
+
whatIf,
|
|
4137
|
+
strict,
|
|
4138
|
+
"componentPattern"
|
|
4139
|
+
);
|
|
4140
|
+
const totalFiles = compositionsResult.filesModified + compositionPatternsResult.filesModified + componentPatternsResult.filesModified;
|
|
4141
|
+
const totalInstances = compositionsResult.instancesUpdated + compositionPatternsResult.instancesUpdated + componentPatternsResult.instancesUpdated;
|
|
4142
|
+
this.logger.info("");
|
|
4143
|
+
this.logger.info(
|
|
4144
|
+
`Summary: 1 component definition updated, ${totalFiles} file(s) (${totalInstances} instance(s)) updated.`
|
|
4145
|
+
);
|
|
4146
|
+
return {
|
|
4147
|
+
componentDefinitionUpdated: true,
|
|
4148
|
+
compositionsModified: compositionsResult.filesModified,
|
|
4149
|
+
compositionPatternsModified: compositionPatternsResult.filesModified,
|
|
4150
|
+
componentPatternsModified: componentPatternsResult.filesModified,
|
|
4151
|
+
instancesUpdated: totalInstances
|
|
4152
|
+
};
|
|
4153
|
+
}
|
|
4154
|
+
async removeParameterInDirectory(directory, componentType, parameterId, whatIf, strict, label) {
|
|
4155
|
+
let files;
|
|
4156
|
+
try {
|
|
4157
|
+
files = await this.fileSystem.findFiles(directory, "**/*.{json,yaml,yml}");
|
|
4158
|
+
} catch {
|
|
4159
|
+
return { filesModified: 0, instancesUpdated: 0 };
|
|
4160
|
+
}
|
|
4161
|
+
if (files.length === 0) {
|
|
4162
|
+
return { filesModified: 0, instancesUpdated: 0 };
|
|
4163
|
+
}
|
|
4164
|
+
let filesModified = 0;
|
|
4165
|
+
let instancesUpdated = 0;
|
|
4166
|
+
for (const filePath of files) {
|
|
4167
|
+
let composition;
|
|
4168
|
+
try {
|
|
4169
|
+
composition = await this.fileSystem.readFile(filePath);
|
|
4170
|
+
} catch {
|
|
4171
|
+
continue;
|
|
4172
|
+
}
|
|
4173
|
+
if (!composition?.composition) {
|
|
4174
|
+
continue;
|
|
4175
|
+
}
|
|
4176
|
+
const count = this.removeParameterFromTree(
|
|
4177
|
+
composition.composition,
|
|
4178
|
+
componentType,
|
|
4179
|
+
parameterId,
|
|
4180
|
+
strict
|
|
4181
|
+
);
|
|
4182
|
+
const overridesCount = this.removeParameterFromOverrides(
|
|
4183
|
+
composition,
|
|
4184
|
+
componentType,
|
|
4185
|
+
parameterId,
|
|
4186
|
+
strict
|
|
4187
|
+
);
|
|
4188
|
+
const totalCount = count + overridesCount;
|
|
4189
|
+
if (totalCount > 0) {
|
|
4190
|
+
const relativePath = filePath.replace(directory, "").replace(/^[/\\]/, "");
|
|
4191
|
+
this.logger.action(
|
|
4192
|
+
whatIf,
|
|
4193
|
+
"UPDATE",
|
|
4194
|
+
`${label}/${relativePath} (${totalCount} instance(s) of ${componentType})`
|
|
4195
|
+
);
|
|
4196
|
+
if (!whatIf) {
|
|
4197
|
+
await this.fileSystem.writeFile(filePath, composition);
|
|
4198
|
+
}
|
|
4199
|
+
filesModified++;
|
|
4200
|
+
instancesUpdated += totalCount;
|
|
4201
|
+
}
|
|
4202
|
+
}
|
|
4203
|
+
return { filesModified, instancesUpdated };
|
|
4204
|
+
}
|
|
4205
|
+
removeParameterFromTree(node, componentType, parameterId, strict) {
|
|
4206
|
+
let count = 0;
|
|
4207
|
+
if (this.compareIds(node.type, componentType, strict) && node.parameters) {
|
|
4208
|
+
const removed = this.removeKeyFromMap(node.parameters, parameterId, strict);
|
|
4209
|
+
if (removed) {
|
|
4210
|
+
count++;
|
|
4211
|
+
}
|
|
4212
|
+
}
|
|
4213
|
+
if (node.slots) {
|
|
4214
|
+
for (const slotInstances of Object.values(node.slots)) {
|
|
4215
|
+
if (!Array.isArray(slotInstances)) continue;
|
|
4216
|
+
for (const instance of slotInstances) {
|
|
4217
|
+
count += this.removeParameterFromTree(instance, componentType, parameterId, strict);
|
|
4218
|
+
}
|
|
4219
|
+
}
|
|
4220
|
+
}
|
|
4221
|
+
return count;
|
|
4222
|
+
}
|
|
4223
|
+
removeParameterFromOverrides(composition, componentType, parameterId, strict) {
|
|
4224
|
+
if (!composition.composition._overrides) {
|
|
4225
|
+
return 0;
|
|
4226
|
+
}
|
|
4227
|
+
let count = 0;
|
|
4228
|
+
if (this.compareIds(composition.composition.type, componentType, strict)) {
|
|
4229
|
+
const rootId = composition.composition._id;
|
|
4230
|
+
const rootOverrides = composition.composition._overrides[rootId];
|
|
4231
|
+
if (rootOverrides?.parameters) {
|
|
4232
|
+
const removed = this.removeKeyFromMap(rootOverrides.parameters, parameterId, strict);
|
|
4233
|
+
if (removed) {
|
|
4234
|
+
count++;
|
|
4235
|
+
}
|
|
4236
|
+
}
|
|
4237
|
+
}
|
|
4238
|
+
const counter = { count: 0 };
|
|
4239
|
+
this.removeOverridesForMatchingInstances(
|
|
4240
|
+
composition.composition,
|
|
4241
|
+
composition.composition._overrides,
|
|
4242
|
+
componentType,
|
|
4243
|
+
parameterId,
|
|
4244
|
+
strict,
|
|
4245
|
+
counter
|
|
4246
|
+
);
|
|
4247
|
+
count += counter.count;
|
|
4248
|
+
return count;
|
|
4249
|
+
}
|
|
4250
|
+
removeOverridesForMatchingInstances(node, overrides, componentType, parameterId, strict, counter) {
|
|
4251
|
+
if (node.slots) {
|
|
4252
|
+
for (const slotInstances of Object.values(node.slots)) {
|
|
4253
|
+
if (!Array.isArray(slotInstances)) continue;
|
|
4254
|
+
for (const instance of slotInstances) {
|
|
4255
|
+
if (this.compareIds(instance.type, componentType, strict) && instance._id) {
|
|
4256
|
+
const instanceOverrides = overrides[instance._id];
|
|
4257
|
+
if (instanceOverrides?.parameters) {
|
|
4258
|
+
const removed = this.removeKeyFromMap(instanceOverrides.parameters, parameterId, strict);
|
|
4259
|
+
if (removed) {
|
|
4260
|
+
counter.count++;
|
|
4261
|
+
}
|
|
4262
|
+
}
|
|
4263
|
+
}
|
|
4264
|
+
this.removeOverridesForMatchingInstances(
|
|
4265
|
+
instance,
|
|
4266
|
+
overrides,
|
|
4267
|
+
componentType,
|
|
4268
|
+
parameterId,
|
|
4269
|
+
strict,
|
|
4270
|
+
counter
|
|
4271
|
+
);
|
|
4272
|
+
}
|
|
4273
|
+
}
|
|
4274
|
+
}
|
|
4275
|
+
}
|
|
4276
|
+
removeKeyFromMap(map, key, strict) {
|
|
4277
|
+
const matchingKey = Object.keys(map).find(
|
|
4278
|
+
(k) => this.compareIds(k, key, strict)
|
|
4279
|
+
);
|
|
4280
|
+
if (!matchingKey) {
|
|
4281
|
+
return false;
|
|
4282
|
+
}
|
|
4283
|
+
delete map[matchingKey];
|
|
4284
|
+
return true;
|
|
4285
|
+
}
|
|
4286
|
+
};
|
|
4287
|
+
|
|
4288
|
+
// src/cli/commands/remove-parameter.ts
|
|
4289
|
+
function createRemoveParameterCommand() {
|
|
4290
|
+
const command = new Command11("remove-parameter");
|
|
4291
|
+
command.description(
|
|
4292
|
+
"Removes a parameter definition from a component and deletes all corresponding parameter values from compositions, composition patterns, and component patterns."
|
|
4293
|
+
).option("--componentType <type>", "The component type that owns the parameter to remove").option("--parameterId <id>", "The parameter ID to remove").hook("preAction", (thisCommand) => {
|
|
4294
|
+
const opts = thisCommand.opts();
|
|
4295
|
+
const requiredOptions = [
|
|
4296
|
+
{ name: "componentType", flag: "--componentType" },
|
|
4297
|
+
{ name: "parameterId", flag: "--parameterId" }
|
|
4298
|
+
];
|
|
4299
|
+
const missing = requiredOptions.filter((opt) => !opts[opt.name]).map((opt) => opt.flag);
|
|
4300
|
+
if (missing.length > 0) {
|
|
4301
|
+
console.error(`error: missing required options: ${missing.join(", ")}`);
|
|
4302
|
+
process.exit(1);
|
|
4303
|
+
}
|
|
4304
|
+
}).action(async (opts, cmd) => {
|
|
4305
|
+
const globalOpts = cmd.optsWithGlobals();
|
|
4306
|
+
const options = {
|
|
4307
|
+
...globalOpts,
|
|
4308
|
+
componentType: opts.componentType,
|
|
4309
|
+
parameterId: opts.parameterId
|
|
4310
|
+
};
|
|
4311
|
+
const logger = new Logger();
|
|
4312
|
+
const fileSystem = new FileSystemService();
|
|
4313
|
+
const componentService = new ComponentService(fileSystem);
|
|
4314
|
+
const remover = new ParameterRemoverService(fileSystem, componentService, logger);
|
|
4315
|
+
try {
|
|
4316
|
+
const result = await remover.remove({
|
|
4317
|
+
rootDir: options.rootDir,
|
|
4318
|
+
componentsDir: options.componentsDir,
|
|
4319
|
+
compositionsDir: options.compositionsDir,
|
|
4320
|
+
compositionPatternsDir: options.compositionPatternsDir,
|
|
4321
|
+
componentPatternsDir: options.componentPatternsDir,
|
|
4322
|
+
componentType: options.componentType,
|
|
4323
|
+
parameterId: options.parameterId,
|
|
4324
|
+
whatIf: options.whatIf ?? false,
|
|
4325
|
+
strict: options.strict ?? false
|
|
4326
|
+
});
|
|
4327
|
+
logger.success(
|
|
4328
|
+
`Removed parameter: ${result.compositionsModified} composition(s), ${result.compositionPatternsModified} composition pattern(s), ${result.componentPatternsModified} component pattern(s) updated`
|
|
4329
|
+
);
|
|
4330
|
+
} catch (error) {
|
|
4331
|
+
if (error instanceof TransformError) {
|
|
4332
|
+
logger.error(error.message);
|
|
4333
|
+
process.exit(1);
|
|
4334
|
+
}
|
|
4335
|
+
throw error;
|
|
4336
|
+
}
|
|
4337
|
+
});
|
|
4338
|
+
return command;
|
|
4339
|
+
}
|
|
4340
|
+
|
|
4341
|
+
// src/cli/commands/remove-field.ts
|
|
4342
|
+
import { Command as Command12 } from "commander";
|
|
4343
|
+
|
|
4344
|
+
// src/core/services/field-remover.service.ts
|
|
4345
|
+
var FieldRemoverService = class {
|
|
4346
|
+
constructor(fileSystem, logger) {
|
|
4347
|
+
this.fileSystem = fileSystem;
|
|
4348
|
+
this.logger = logger;
|
|
4349
|
+
}
|
|
4350
|
+
compareIds(id1, id2, strict) {
|
|
4351
|
+
if (strict) {
|
|
4352
|
+
return id1 === id2;
|
|
4353
|
+
}
|
|
4354
|
+
return id1.toLowerCase() === id2.toLowerCase();
|
|
4355
|
+
}
|
|
4356
|
+
async remove(options) {
|
|
4357
|
+
const {
|
|
4358
|
+
rootDir,
|
|
4359
|
+
compositionsDir,
|
|
4360
|
+
compositionPatternsDir,
|
|
4361
|
+
componentPatternsDir,
|
|
4362
|
+
componentType,
|
|
4363
|
+
parameterId,
|
|
4364
|
+
whatIf,
|
|
4365
|
+
strict
|
|
4366
|
+
} = options;
|
|
4367
|
+
const fullCompositionsDir = this.fileSystem.resolvePath(rootDir, compositionsDir);
|
|
4368
|
+
const fullCompositionPatternsDir = this.fileSystem.resolvePath(rootDir, compositionPatternsDir);
|
|
4369
|
+
const fullComponentPatternsDir = this.fileSystem.resolvePath(rootDir, componentPatternsDir);
|
|
4370
|
+
this.logger.info(`Removing field "${parameterId}" from instances of ${componentType}`);
|
|
4371
|
+
const compositionsResult = await this.removeFieldInDirectory(
|
|
4372
|
+
fullCompositionsDir,
|
|
4373
|
+
componentType,
|
|
4374
|
+
parameterId,
|
|
4375
|
+
whatIf,
|
|
4376
|
+
strict,
|
|
4377
|
+
"composition"
|
|
4378
|
+
);
|
|
4379
|
+
const compositionPatternsResult = await this.removeFieldInDirectory(
|
|
4380
|
+
fullCompositionPatternsDir,
|
|
4381
|
+
componentType,
|
|
4382
|
+
parameterId,
|
|
4383
|
+
whatIf,
|
|
4384
|
+
strict,
|
|
4385
|
+
"compositionPattern"
|
|
4386
|
+
);
|
|
4387
|
+
const componentPatternsResult = await this.removeFieldInDirectory(
|
|
4388
|
+
fullComponentPatternsDir,
|
|
4389
|
+
componentType,
|
|
4390
|
+
parameterId,
|
|
4391
|
+
whatIf,
|
|
4392
|
+
strict,
|
|
4393
|
+
"componentPattern"
|
|
4394
|
+
);
|
|
4395
|
+
const totalFiles = compositionsResult.filesModified + compositionPatternsResult.filesModified + componentPatternsResult.filesModified;
|
|
4396
|
+
const totalInstances = compositionsResult.instancesUpdated + compositionPatternsResult.instancesUpdated + componentPatternsResult.instancesUpdated;
|
|
4397
|
+
this.logger.info("");
|
|
4398
|
+
this.logger.info(
|
|
4399
|
+
`Summary: ${totalFiles} file(s) (${totalInstances} instance(s)) updated.`
|
|
4400
|
+
);
|
|
4401
|
+
return {
|
|
4402
|
+
compositionsModified: compositionsResult.filesModified,
|
|
4403
|
+
compositionPatternsModified: compositionPatternsResult.filesModified,
|
|
4404
|
+
componentPatternsModified: componentPatternsResult.filesModified,
|
|
4405
|
+
instancesUpdated: totalInstances
|
|
4406
|
+
};
|
|
4407
|
+
}
|
|
4408
|
+
async removeFieldInDirectory(directory, componentType, parameterId, whatIf, strict, label) {
|
|
4409
|
+
let files;
|
|
4410
|
+
try {
|
|
4411
|
+
files = await this.fileSystem.findFiles(directory, "**/*.{json,yaml,yml}");
|
|
4412
|
+
} catch {
|
|
4413
|
+
return { filesModified: 0, instancesUpdated: 0 };
|
|
4414
|
+
}
|
|
4415
|
+
if (files.length === 0) {
|
|
4416
|
+
return { filesModified: 0, instancesUpdated: 0 };
|
|
4417
|
+
}
|
|
4418
|
+
let filesModified = 0;
|
|
4419
|
+
let instancesUpdated = 0;
|
|
4420
|
+
for (const filePath of files) {
|
|
4421
|
+
let composition;
|
|
4422
|
+
try {
|
|
4423
|
+
composition = await this.fileSystem.readFile(filePath);
|
|
4424
|
+
} catch {
|
|
4425
|
+
continue;
|
|
4426
|
+
}
|
|
4427
|
+
if (!composition?.composition) {
|
|
4428
|
+
continue;
|
|
4429
|
+
}
|
|
4430
|
+
const treeCount = this.removeFieldFromTree(
|
|
4431
|
+
composition.composition,
|
|
4432
|
+
componentType,
|
|
4433
|
+
parameterId,
|
|
4434
|
+
strict
|
|
4435
|
+
);
|
|
4436
|
+
const overridesCount = this.removeFieldFromOverrides(
|
|
4437
|
+
composition,
|
|
4438
|
+
componentType,
|
|
4439
|
+
parameterId,
|
|
4440
|
+
strict
|
|
4441
|
+
);
|
|
4442
|
+
const totalCount = treeCount + overridesCount;
|
|
4443
|
+
if (totalCount > 0) {
|
|
4444
|
+
const relativePath = filePath.replace(directory, "").replace(/^[/\\]/, "");
|
|
4445
|
+
this.logger.action(
|
|
4446
|
+
whatIf,
|
|
4447
|
+
"UPDATE",
|
|
4448
|
+
`${label}/${relativePath} (${totalCount} instance(s) of ${componentType})`
|
|
4449
|
+
);
|
|
4450
|
+
if (!whatIf) {
|
|
4451
|
+
await this.fileSystem.writeFile(filePath, composition);
|
|
4452
|
+
}
|
|
4453
|
+
filesModified++;
|
|
4454
|
+
instancesUpdated += totalCount;
|
|
4455
|
+
}
|
|
4456
|
+
}
|
|
4457
|
+
return { filesModified, instancesUpdated };
|
|
4458
|
+
}
|
|
4459
|
+
removeFieldFromTree(node, componentType, parameterId, strict) {
|
|
4460
|
+
let count = 0;
|
|
4461
|
+
if (this.compareIds(node.type, componentType, strict) && node.parameters) {
|
|
4462
|
+
const removed = this.removeKeyFromMap(node.parameters, parameterId, strict);
|
|
4463
|
+
if (removed) {
|
|
4464
|
+
count++;
|
|
4465
|
+
}
|
|
4466
|
+
}
|
|
4467
|
+
if (node.slots) {
|
|
4468
|
+
for (const slotInstances of Object.values(node.slots)) {
|
|
4469
|
+
if (!Array.isArray(slotInstances)) continue;
|
|
4470
|
+
for (const instance of slotInstances) {
|
|
4471
|
+
count += this.removeFieldFromTree(instance, componentType, parameterId, strict);
|
|
4472
|
+
}
|
|
4473
|
+
}
|
|
4474
|
+
}
|
|
4475
|
+
return count;
|
|
4476
|
+
}
|
|
4477
|
+
removeFieldFromOverrides(composition, componentType, parameterId, strict) {
|
|
4478
|
+
if (!composition.composition._overrides) {
|
|
4479
|
+
return 0;
|
|
4480
|
+
}
|
|
4481
|
+
let count = 0;
|
|
4482
|
+
if (this.compareIds(composition.composition.type, componentType, strict)) {
|
|
4483
|
+
const rootId = composition.composition._id;
|
|
4484
|
+
const rootOverrides = composition.composition._overrides[rootId];
|
|
4485
|
+
if (rootOverrides?.parameters) {
|
|
4486
|
+
const removed = this.removeKeyFromMap(rootOverrides.parameters, parameterId, strict);
|
|
4487
|
+
if (removed) {
|
|
4488
|
+
count++;
|
|
4489
|
+
}
|
|
4490
|
+
}
|
|
4491
|
+
}
|
|
4492
|
+
count += this.removeOverridesForMatchingInstances(
|
|
4493
|
+
composition.composition,
|
|
4494
|
+
composition.composition._overrides,
|
|
4495
|
+
componentType,
|
|
4496
|
+
parameterId,
|
|
4497
|
+
strict
|
|
4498
|
+
);
|
|
4499
|
+
return count;
|
|
4500
|
+
}
|
|
4501
|
+
removeOverridesForMatchingInstances(node, overrides, componentType, parameterId, strict) {
|
|
4502
|
+
let count = 0;
|
|
4503
|
+
if (node.slots) {
|
|
4504
|
+
for (const slotInstances of Object.values(node.slots)) {
|
|
4505
|
+
if (!Array.isArray(slotInstances)) continue;
|
|
4506
|
+
for (const instance of slotInstances) {
|
|
4507
|
+
if (this.compareIds(instance.type, componentType, strict) && instance._id) {
|
|
4508
|
+
const instanceOverrides = overrides[instance._id];
|
|
4509
|
+
if (instanceOverrides?.parameters) {
|
|
4510
|
+
const removed = this.removeKeyFromMap(instanceOverrides.parameters, parameterId, strict);
|
|
4511
|
+
if (removed) {
|
|
4512
|
+
count++;
|
|
4513
|
+
}
|
|
4514
|
+
}
|
|
4515
|
+
}
|
|
4516
|
+
count += this.removeOverridesForMatchingInstances(
|
|
4517
|
+
instance,
|
|
4518
|
+
overrides,
|
|
4519
|
+
componentType,
|
|
4520
|
+
parameterId,
|
|
4521
|
+
strict
|
|
4522
|
+
);
|
|
4523
|
+
}
|
|
4524
|
+
}
|
|
4525
|
+
}
|
|
4526
|
+
return count;
|
|
4527
|
+
}
|
|
4528
|
+
removeKeyFromMap(map, key, strict) {
|
|
4529
|
+
const matchingKey = Object.keys(map).find(
|
|
4530
|
+
(k) => this.compareIds(k, key, strict)
|
|
4531
|
+
);
|
|
4532
|
+
if (!matchingKey) {
|
|
4533
|
+
return false;
|
|
4534
|
+
}
|
|
4535
|
+
delete map[matchingKey];
|
|
4536
|
+
return true;
|
|
4537
|
+
}
|
|
4538
|
+
};
|
|
4539
|
+
|
|
4540
|
+
// src/cli/commands/remove-field.ts
|
|
4541
|
+
function createRemoveFieldCommand() {
|
|
4542
|
+
const command = new Command12("remove-field");
|
|
4543
|
+
command.description(
|
|
4544
|
+
"Removes parameter values from component instances in compositions, composition patterns, and component patterns without modifying the component definition."
|
|
4545
|
+
).option("--componentType <type>", "The component type whose instances should have the parameter value removed").option("--parameterId <id>", "The parameter value key to remove from matching instances").hook("preAction", (thisCommand) => {
|
|
4546
|
+
const opts = thisCommand.opts();
|
|
4547
|
+
const requiredOptions = [
|
|
4548
|
+
{ name: "componentType", flag: "--componentType" },
|
|
4549
|
+
{ name: "parameterId", flag: "--parameterId" }
|
|
4550
|
+
];
|
|
4551
|
+
const missing = requiredOptions.filter((opt) => !opts[opt.name]).map((opt) => opt.flag);
|
|
4552
|
+
if (missing.length > 0) {
|
|
4553
|
+
console.error(`error: missing required options: ${missing.join(", ")}`);
|
|
4554
|
+
process.exit(1);
|
|
4555
|
+
}
|
|
4556
|
+
}).action(async (opts, cmd) => {
|
|
4557
|
+
const globalOpts = cmd.optsWithGlobals();
|
|
4558
|
+
const options = {
|
|
4559
|
+
...globalOpts,
|
|
4560
|
+
componentType: opts.componentType,
|
|
4561
|
+
parameterId: opts.parameterId
|
|
4562
|
+
};
|
|
4563
|
+
const logger = new Logger();
|
|
4564
|
+
const fileSystem = new FileSystemService();
|
|
4565
|
+
const remover = new FieldRemoverService(fileSystem, logger);
|
|
4566
|
+
try {
|
|
4567
|
+
const result = await remover.remove({
|
|
4568
|
+
rootDir: options.rootDir,
|
|
4569
|
+
compositionsDir: options.compositionsDir,
|
|
4570
|
+
compositionPatternsDir: options.compositionPatternsDir,
|
|
4571
|
+
componentPatternsDir: options.componentPatternsDir,
|
|
4572
|
+
componentType: options.componentType,
|
|
4573
|
+
parameterId: options.parameterId,
|
|
4574
|
+
whatIf: options.whatIf ?? false,
|
|
4575
|
+
strict: options.strict ?? false
|
|
4576
|
+
});
|
|
4577
|
+
logger.success(
|
|
4578
|
+
`Removed field: ${result.compositionsModified} composition(s), ${result.compositionPatternsModified} composition pattern(s), ${result.componentPatternsModified} component pattern(s) updated`
|
|
4579
|
+
);
|
|
4580
|
+
} catch (error) {
|
|
4581
|
+
if (error instanceof TransformError) {
|
|
4582
|
+
logger.error(error.message);
|
|
4583
|
+
process.exit(1);
|
|
4584
|
+
}
|
|
4585
|
+
throw error;
|
|
4586
|
+
}
|
|
4587
|
+
});
|
|
4588
|
+
return command;
|
|
4589
|
+
}
|
|
4590
|
+
|
|
4010
4591
|
// package.json
|
|
4011
4592
|
var package_default = {
|
|
4012
4593
|
name: "@uniformdev/transformer",
|
|
4013
|
-
version: "1.1.
|
|
4594
|
+
version: "1.1.13",
|
|
4014
4595
|
description: "CLI tool for transforming Uniform.dev serialization files offline",
|
|
4015
4596
|
type: "module",
|
|
4016
4597
|
bin: {
|
|
@@ -4079,7 +4660,7 @@ var package_default = {
|
|
|
4079
4660
|
};
|
|
4080
4661
|
|
|
4081
4662
|
// src/cli/index.ts
|
|
4082
|
-
var program = new
|
|
4663
|
+
var program = new Command13();
|
|
4083
4664
|
var appVersion = package_default.version;
|
|
4084
4665
|
console.error(`uniform-transform v${appVersion}`);
|
|
4085
4666
|
program.name("uniform-transform").description("CLI tool for transforming Uniform.dev serialization files offline").version(appVersion);
|
|
@@ -4102,5 +4683,7 @@ program.addCommand(createAddComponentCommand());
|
|
|
4102
4683
|
program.addCommand(createAddComponentPatternCommand());
|
|
4103
4684
|
program.addCommand(createPropagateRootComponentSlotCommand());
|
|
4104
4685
|
program.addCommand(createConvertCompositionsToEntriesCommand());
|
|
4686
|
+
program.addCommand(createRemoveParameterCommand());
|
|
4687
|
+
program.addCommand(createRemoveFieldCommand());
|
|
4105
4688
|
program.parse();
|
|
4106
4689
|
//# sourceMappingURL=index.js.map
|