@featurevisor/core 3.6.0 → 3.7.0

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.
Files changed (98) hide show
  1. package/lib/benchmark/index.d.ts +2 -1
  2. package/lib/benchmark/index.js +24 -3
  3. package/lib/benchmark/index.js.map +1 -1
  4. package/lib/builder/applyContextToConditions.js +3 -4
  5. package/lib/builder/applyContextToConditions.js.map +1 -1
  6. package/lib/builder/applyContextToDatafile.js +22 -6
  7. package/lib/builder/applyContextToDatafile.js.map +1 -1
  8. package/lib/builder/applyContextToSegments.js +3 -4
  9. package/lib/builder/applyContextToSegments.js.map +1 -1
  10. package/lib/builder/buildDatafile.d.ts +8 -0
  11. package/lib/builder/buildDatafile.js +230 -48
  12. package/lib/builder/buildDatafile.js.map +1 -1
  13. package/lib/builder/buildProject.d.ts +1 -0
  14. package/lib/builder/buildProject.js +16 -2
  15. package/lib/builder/buildProject.js.map +1 -1
  16. package/lib/builder/buildRuntimeDatafiles.js +19 -12
  17. package/lib/builder/buildRuntimeDatafiles.js.map +1 -1
  18. package/lib/builder/hashes.d.ts +5 -2
  19. package/lib/builder/hashes.js +76 -10
  20. package/lib/builder/hashes.js.map +1 -1
  21. package/lib/cli/options.js +14 -10
  22. package/lib/cli/options.js.map +1 -1
  23. package/lib/config/projectConfig.d.ts +6 -0
  24. package/lib/config/projectConfig.js +29 -1
  25. package/lib/config/projectConfig.js.map +1 -1
  26. package/lib/datasource/datasource.d.ts +7 -1
  27. package/lib/datasource/datasource.js +71 -1
  28. package/lib/datasource/datasource.js.map +1 -1
  29. package/lib/datasource/filesystemAdapter.js +14 -2
  30. package/lib/datasource/filesystemAdapter.js.map +1 -1
  31. package/lib/datasource/requiredFeatures.d.ts +9 -2
  32. package/lib/datasource/requiredFeatures.js +43 -6
  33. package/lib/datasource/requiredFeatures.js.map +1 -1
  34. package/lib/datasource/variableOverrides.d.ts +5 -0
  35. package/lib/datasource/variableOverrides.js +21 -0
  36. package/lib/datasource/variableOverrides.js.map +1 -0
  37. package/lib/evaluate/index.d.ts +2 -1
  38. package/lib/evaluate/index.js +37 -5
  39. package/lib/evaluate/index.js.map +1 -1
  40. package/lib/find-usage/index.d.ts +13 -3
  41. package/lib/find-usage/index.js +162 -24
  42. package/lib/find-usage/index.js.map +1 -1
  43. package/lib/generate-code/index.js +2 -2
  44. package/lib/generate-code/index.js.map +1 -1
  45. package/lib/generate-code/typescript.js +86 -17
  46. package/lib/generate-code/typescript.js.map +1 -1
  47. package/lib/info/index.js +31 -19
  48. package/lib/info/index.js.map +1 -1
  49. package/lib/linter/checkCircularDependency.d.ts +2 -2
  50. package/lib/linter/checkCircularDependency.js +2 -2
  51. package/lib/linter/checkCircularDependency.js.map +1 -1
  52. package/lib/linter/featureSchema.d.ts +109 -20
  53. package/lib/linter/featureSchema.js +101 -37
  54. package/lib/linter/featureSchema.js.map +1 -1
  55. package/lib/linter/lintProject.d.ts +1 -1
  56. package/lib/linter/lintProject.js +103 -5
  57. package/lib/linter/lintProject.js.map +1 -1
  58. package/lib/linter/requiredFeaturesSchema.d.ts +7 -0
  59. package/lib/linter/requiredFeaturesSchema.js +34 -0
  60. package/lib/linter/requiredFeaturesSchema.js.map +1 -0
  61. package/lib/linter/targetSchema.d.ts +2 -0
  62. package/lib/linter/targetSchema.js +7 -5
  63. package/lib/linter/targetSchema.js.map +1 -1
  64. package/lib/linter/testSchema.d.ts +1 -50
  65. package/lib/linter/testSchema.js +86 -16
  66. package/lib/linter/testSchema.js.map +1 -1
  67. package/lib/linter/variableSchema.d.ts +45 -0
  68. package/lib/linter/variableSchema.js +274 -0
  69. package/lib/linter/variableSchema.js.map +1 -0
  70. package/lib/list/index.js +68 -9
  71. package/lib/list/index.js.map +1 -1
  72. package/lib/list/matrix.d.ts +2 -1
  73. package/lib/list/matrix.js +44 -0
  74. package/lib/list/matrix.js.map +1 -1
  75. package/lib/promoter/index.js +260 -32
  76. package/lib/promoter/index.js.map +1 -1
  77. package/lib/targeting.d.ts +3 -1
  78. package/lib/targeting.js +29 -0
  79. package/lib/targeting.js.map +1 -1
  80. package/lib/tester/testFeature.js +13 -4
  81. package/lib/tester/testFeature.js.map +1 -1
  82. package/lib/tester/testProject.d.ts +1 -1
  83. package/lib/tester/testProject.js +8 -3
  84. package/lib/tester/testProject.js.map +1 -1
  85. package/lib/tester/testVariable.d.ts +3 -0
  86. package/lib/tester/testVariable.js +72 -0
  87. package/lib/tester/testVariable.js.map +1 -0
  88. package/lib/utils/extractKeys.js +17 -0
  89. package/lib/utils/extractKeys.js.map +1 -1
  90. package/lib/utils/git.js +10 -3
  91. package/lib/utils/git.js.map +1 -1
  92. package/lib/utils/index.d.ts +1 -0
  93. package/lib/utils/index.js +1 -0
  94. package/lib/utils/index.js.map +1 -1
  95. package/lib/utils/schemaReferences.d.ts +7 -0
  96. package/lib/utils/schemaReferences.js +27 -0
  97. package/lib/utils/schemaReferences.js.map +1 -0
  98. package/package.json +5 -5
@@ -40,6 +40,8 @@ const path = __importStar(require("path"));
40
40
  const linter_1 = require("../linter");
41
41
  const cliFormat_1 = require("../tester/cliFormat");
42
42
  const prettyDuration_1 = require("../tester/prettyDuration");
43
+ const schemaReferences_1 = require("../utils/schemaReferences");
44
+ const requiredFeatures_1 = require("../datasource/requiredFeatures");
43
45
  function isPromotable(entity) {
44
46
  return entity?.promotable !== false;
45
47
  }
@@ -118,6 +120,90 @@ function matchesTarget(featureKey, feature, target) {
118
120
  : false;
119
121
  return included && !excluded;
120
122
  }
123
+ function variableMatchesTarget(variableKey, variable, target) {
124
+ const tags = variable.tags || [];
125
+ if (variable.archived === true)
126
+ return false;
127
+ if (target.tag && !tags.includes(target.tag))
128
+ return false;
129
+ if (target.tags && !matchesTags(tags, target.tags))
130
+ return false;
131
+ if (target.includeVariables && !matchesPattern(variableKey, toArray(target.includeVariables))) {
132
+ return false;
133
+ }
134
+ if (target.excludeVariables && matchesPattern(variableKey, toArray(target.excludeVariables))) {
135
+ return false;
136
+ }
137
+ return true;
138
+ }
139
+ function getPromotableVariableOverrides(overrides) {
140
+ const result = [];
141
+ for (const override of overrides || []) {
142
+ if (!isPromotable(override))
143
+ continue;
144
+ result.push(override);
145
+ result.push(...getPromotableVariableOverrides(override.overrides));
146
+ }
147
+ return result;
148
+ }
149
+ function getAllPromotableVariableOverrides(variable) {
150
+ const groups = Array.isArray(variable.overrides)
151
+ ? [variable.overrides]
152
+ : Object.values(variable.overrides || {});
153
+ return groups.flatMap(getPromotableVariableOverrides);
154
+ }
155
+ function mergeVariableOverrideArray(destination, source, policy, conflicts, variableKey, pathSegments = ["overrides"]) {
156
+ if (!source)
157
+ return destination;
158
+ const result = [...(destination || [])];
159
+ for (const sourceOverride of source) {
160
+ if (!isPromotable(sourceOverride))
161
+ continue;
162
+ const index = result.findIndex((entry) => entry.key === sourceOverride.key);
163
+ if (index !== -1 && !isPromotable(result[index]))
164
+ continue;
165
+ const sourceWithoutOverrides = { ...sourceOverride, overrides: undefined };
166
+ const destinationWithoutOverrides = index === -1 ? undefined : { ...result[index], overrides: undefined };
167
+ const localConflicts = [];
168
+ const merged = deepMergeWithPolicy(destinationWithoutOverrides, sourceWithoutOverrides, policy, localConflicts, [...pathSegments, sourceOverride.key]);
169
+ merged.overrides = mergeVariableOverrideArray(index === -1 ? undefined : result[index].overrides, sourceOverride.overrides, policy, conflicts, variableKey, [...pathSegments, sourceOverride.key, "overrides"]);
170
+ conflicts.push(...localConflicts.map((conflict) => ({
171
+ type: "variable",
172
+ key: variableKey,
173
+ ...conflict,
174
+ })));
175
+ if (index === -1)
176
+ result.push(merged);
177
+ else
178
+ result[index] = merged;
179
+ }
180
+ return result;
181
+ }
182
+ function mergeVariable(key, destination, source, policy, conflicts) {
183
+ const sourceWithoutOverrides = { ...source, overrides: undefined };
184
+ const destinationWithoutOverrides = destination
185
+ ? { ...destination, overrides: undefined }
186
+ : undefined;
187
+ const localConflicts = [];
188
+ const merged = deepMergeWithPolicy(destinationWithoutOverrides, sourceWithoutOverrides, policy, localConflicts);
189
+ conflicts.push(...localConflicts.map((conflict) => ({ type: "variable", key, ...conflict })));
190
+ if (Array.isArray(source.overrides) || Array.isArray(destination?.overrides)) {
191
+ merged.overrides = mergeVariableOverrideArray(Array.isArray(destination?.overrides) ? destination.overrides : undefined, Array.isArray(source.overrides) ? source.overrides : undefined, policy, conflicts, key);
192
+ }
193
+ else if (source.overrides || destination?.overrides) {
194
+ const environments = new Set([
195
+ ...Object.keys(destination?.overrides || {}),
196
+ ...Object.keys(source.overrides || {}),
197
+ ]);
198
+ const overrides = {};
199
+ for (const environment of environments) {
200
+ overrides[environment] =
201
+ mergeVariableOverrideArray(destination?.overrides?.[environment], source.overrides?.[environment], policy, conflicts, key) || [];
202
+ }
203
+ merged.overrides = Object.keys(overrides).length > 0 ? overrides : undefined;
204
+ }
205
+ return merged;
206
+ }
121
207
  function withoutKey(entity) {
122
208
  const { key: _key, ...rest } = entity; // eslint-disable-line @typescript-eslint/no-unused-vars
123
209
  return rest;
@@ -205,20 +291,6 @@ function collectConditionDependencies(value, segments, attributes) {
205
291
  if ("not" in value)
206
292
  value.not.forEach((entry) => collectConditionDependencies(entry, segments, attributes));
207
293
  }
208
- function collectSchemaReferences(value, schemas) {
209
- if (!value || typeof value !== "object") {
210
- return;
211
- }
212
- if (Array.isArray(value)) {
213
- value.forEach((entry) => collectSchemaReferences(entry, schemas));
214
- return;
215
- }
216
- const record = value;
217
- if (typeof record.schema === "string") {
218
- schemas.add(record.schema);
219
- }
220
- Object.values(record).forEach((entry) => collectSchemaReferences(entry, schemas));
221
- }
222
294
  function collectFeatureDependencies(feature, features, featureKeys, segmentKeys, attributeKeys, schemaKeys) {
223
295
  if (typeof feature.bucketBy === "string") {
224
296
  attributeKeys.add(feature.bucketBy);
@@ -229,9 +301,11 @@ function collectFeatureDependencies(feature, features, featureKeys, segmentKeys,
229
301
  else if (feature.bucketBy && "or" in feature.bucketBy) {
230
302
  feature.bucketBy.or.forEach((attributeKey) => attributeKeys.add(attributeKey));
231
303
  }
232
- collectSchemaReferences(feature.variablesSchema, schemaKeys);
233
- for (const required of feature.required || []) {
234
- const requiredKey = typeof required === "string" ? required : required.key;
304
+ Object.values(feature.variablesSchema || {}).forEach((schema) => {
305
+ (0, schemaReferences_1.extractSchemaReferences)(schema).forEach((key) => schemaKeys.add(key));
306
+ });
307
+ for (const required of (0, requiredFeatures_1.normalizeFeatureRequirements)(feature)) {
308
+ const requiredKey = (0, requiredFeatures_1.getRequiredFeatureKey)(required);
235
309
  if (!featureKeys.has(requiredKey)) {
236
310
  featureKeys.add(requiredKey);
237
311
  const requiredFeature = features[requiredKey];
@@ -249,12 +323,44 @@ function collectFeatureDependencies(feature, features, featureKeys, segmentKeys,
249
323
  collectGroupSegmentKeys(rule?.segments, segmentKeys);
250
324
  Object.values(rule?.variableOverrides || {}).forEach((overrides) => {
251
325
  (overrides || []).forEach((override) => {
326
+ if (!isPromotable(override))
327
+ return;
252
328
  collectGroupSegmentKeys(override.segments, segmentKeys);
253
329
  collectConditionDependencies(override.conditions, segmentKeys, attributeKeys);
330
+ (0, requiredFeatures_1.normalizeRequiredFeatures)(override.requiredFeatures).forEach((required) => {
331
+ const requiredKey = (0, requiredFeatures_1.getRequiredFeatureKey)(required);
332
+ if (!featureKeys.has(requiredKey)) {
333
+ featureKeys.add(requiredKey);
334
+ const requiredFeature = features[requiredKey];
335
+ if (requiredFeature) {
336
+ collectFeatureDependencies(requiredFeature, features, featureKeys, segmentKeys, attributeKeys, schemaKeys);
337
+ }
338
+ }
339
+ });
254
340
  });
255
341
  });
256
342
  }
257
343
  }
344
+ for (const variation of feature.variations || []) {
345
+ Object.values(variation.variableOverrides || {}).forEach((overrides) => {
346
+ overrides.forEach((override) => {
347
+ if (!isPromotable(override))
348
+ return;
349
+ collectGroupSegmentKeys(override.segments, segmentKeys);
350
+ collectConditionDependencies(override.conditions, segmentKeys, attributeKeys);
351
+ (0, requiredFeatures_1.normalizeRequiredFeatures)(override.requiredFeatures).forEach((required) => {
352
+ const requiredKey = (0, requiredFeatures_1.getRequiredFeatureKey)(required);
353
+ if (!featureKeys.has(requiredKey)) {
354
+ featureKeys.add(requiredKey);
355
+ const requiredFeature = features[requiredKey];
356
+ if (requiredFeature) {
357
+ collectFeatureDependencies(requiredFeature, features, featureKeys, segmentKeys, attributeKeys, schemaKeys);
358
+ }
359
+ }
360
+ });
361
+ });
362
+ });
363
+ }
258
364
  for (const force of Object.values((feature.force || {}))) {
259
365
  const forceEntries = Array.isArray(force) ? force : [force];
260
366
  for (const forceEntry of forceEntries) {
@@ -263,11 +369,64 @@ function collectFeatureDependencies(feature, features, featureKeys, segmentKeys,
263
369
  }
264
370
  }
265
371
  }
372
+ function hasOverridePromotion(overrides) {
373
+ return Object.values(overrides || {}).some((entries) => entries.some((entry) => typeof entry.promotable !== "undefined"));
374
+ }
375
+ function mergeFeatureVariableOverrideArray(destination, source, policy, conflicts, pathSegments) {
376
+ if (typeof source === "undefined")
377
+ return destination;
378
+ const usesPromotion = [...(destination || []), ...source].some((override) => typeof override.promotable !== "undefined");
379
+ if (!usesPromotion) {
380
+ return deepMergeWithPolicy(destination, source, policy, conflicts, pathSegments);
381
+ }
382
+ const allKeyed = [...(destination || []), ...source].every((override) => typeof override.key === "string");
383
+ if (!allKeyed) {
384
+ throw new Error("Cannot merge protected feature variable overrides when keys are missing. Add stable keys to every override in the list.");
385
+ }
386
+ const result = [...(destination || [])];
387
+ for (const sourceOverride of source) {
388
+ if (!isPromotable(sourceOverride))
389
+ continue;
390
+ const index = result.findIndex((entry) => entry.key === sourceOverride.key);
391
+ if (index !== -1 && !isPromotable(result[index]))
392
+ continue;
393
+ const merged = deepMergeWithPolicy(index === -1 ? undefined : result[index], sourceOverride, policy, conflicts, [...pathSegments, sourceOverride.key]);
394
+ if (index === -1)
395
+ result.push(merged);
396
+ else
397
+ result[index] = merged;
398
+ }
399
+ return result;
400
+ }
401
+ function mergeFeatureVariableOverrideRecord(destination, source, policy, conflicts, pathSegments) {
402
+ if (!source)
403
+ return destination;
404
+ const variableKeys = new Set([...Object.keys(destination || {}), ...Object.keys(source)]);
405
+ const result = {};
406
+ for (const variableKey of variableKeys) {
407
+ const merged = mergeFeatureVariableOverrideArray(destination?.[variableKey], source[variableKey], policy, conflicts, [...pathSegments, variableKey]);
408
+ if (merged)
409
+ result[variableKey] = merged;
410
+ }
411
+ return Object.keys(result).length > 0 ? result : undefined;
412
+ }
413
+ function mergeRule(destination, source, policy, conflicts, pathSegments) {
414
+ if (!hasOverridePromotion(source.variableOverrides) &&
415
+ !hasOverridePromotion(destination?.variableOverrides)) {
416
+ return deepMergeWithPolicy(destination, source, policy, conflicts, pathSegments);
417
+ }
418
+ const merged = deepMergeWithPolicy(destination ? { ...destination, variableOverrides: undefined } : undefined, { ...source, variableOverrides: undefined }, policy, conflicts, pathSegments);
419
+ merged.variableOverrides = mergeFeatureVariableOverrideRecord(destination?.variableOverrides, source.variableOverrides, policy, conflicts, [...pathSegments, "variableOverrides"]);
420
+ return merged;
421
+ }
266
422
  function mergeRuleArray(destination, source, policy, conflicts, pathSegments) {
267
423
  if (typeof source === "undefined")
268
424
  return destination;
269
- if (typeof destination === "undefined")
270
- return source.filter((rule) => isPromotable(rule));
425
+ if (typeof destination === "undefined") {
426
+ return source
427
+ .filter((rule) => isPromotable(rule))
428
+ .map((rule) => mergeRule(undefined, rule, policy, conflicts, [...pathSegments, rule.key]));
429
+ }
271
430
  const mergedRuleKeys = new Set();
272
431
  const mergedRules = [];
273
432
  for (const sourceRule of source) {
@@ -280,10 +439,7 @@ function mergeRuleArray(destination, source, policy, conflicts, pathSegments) {
280
439
  mergedRules.push(destinationRule);
281
440
  continue;
282
441
  }
283
- mergedRules.push(deepMergeWithPolicy(destinationRule, sourceRule, policy, conflicts, [
284
- ...pathSegments,
285
- sourceRule.key,
286
- ]));
442
+ mergedRules.push(mergeRule(destinationRule, sourceRule, policy, conflicts, [...pathSegments, sourceRule.key]));
287
443
  }
288
444
  for (const destinationRule of destination) {
289
445
  if (!mergedRuleKeys.has(destinationRule.key)) {
@@ -315,13 +471,40 @@ function mergeRules(destination, source, policy, conflicts) {
315
471
  }
316
472
  return source;
317
473
  }
474
+ function mergeVariations(destination, source, policy, conflicts) {
475
+ if (typeof source === "undefined")
476
+ return destination;
477
+ const usesPromotion = [...(destination || []), ...source].some((variation) => hasOverridePromotion(variation.variableOverrides));
478
+ if (!usesPromotion) {
479
+ return deepMergeWithPolicy(destination, source, policy, conflicts, ["variations"]);
480
+ }
481
+ const mergedValues = new Set();
482
+ const result = [];
483
+ for (const sourceVariation of source) {
484
+ const destinationVariation = destination?.find((variation) => variation.value === sourceVariation.value);
485
+ const pathSegments = ["variations", sourceVariation.value];
486
+ const merged = deepMergeWithPolicy(destinationVariation ? { ...destinationVariation, variableOverrides: undefined } : undefined, { ...sourceVariation, variableOverrides: undefined }, policy, conflicts, pathSegments);
487
+ merged.variableOverrides = mergeFeatureVariableOverrideRecord(destinationVariation?.variableOverrides, sourceVariation.variableOverrides, policy, conflicts, [...pathSegments, "variableOverrides"]);
488
+ result.push(merged);
489
+ mergedValues.add(sourceVariation.value);
490
+ }
491
+ for (const destinationVariation of destination || []) {
492
+ if (!mergedValues.has(destinationVariation.value))
493
+ result.push(destinationVariation);
494
+ }
495
+ return result;
496
+ }
318
497
  function mergeFeature(featureKey, destination, source, policy, conflicts) {
319
- const sourceWithoutRules = { ...source, rules: undefined };
320
- const destinationWithoutRules = destination ? { ...destination, rules: undefined } : undefined;
498
+ const sourceWithoutRules = { ...source, rules: undefined, variations: undefined };
499
+ const destinationWithoutRules = destination
500
+ ? { ...destination, rules: undefined, variations: undefined }
501
+ : undefined;
321
502
  const featureConflicts = [];
322
503
  const mergedFeature = deepMergeWithPolicy(destinationWithoutRules, sourceWithoutRules, policy, featureConflicts);
323
504
  const ruleConflicts = [];
324
505
  const mergedRules = mergeRules(destination?.rules, source.rules, policy, ruleConflicts);
506
+ const variationConflicts = [];
507
+ const mergedVariations = mergeVariations(destination?.variations, source.variations, policy, variationConflicts);
325
508
  conflicts.push(...featureConflicts.map((conflict) => ({
326
509
  type: "feature",
327
510
  key: featureKey,
@@ -330,10 +513,15 @@ function mergeFeature(featureKey, destination, source, policy, conflicts) {
330
513
  type: "feature",
331
514
  key: featureKey,
332
515
  ...conflict,
516
+ })), ...variationConflicts.map((conflict) => ({
517
+ type: "feature",
518
+ key: featureKey,
519
+ ...conflict,
333
520
  })));
334
521
  return {
335
522
  ...mergedFeature,
336
523
  rules: mergedRules,
524
+ variations: mergedVariations,
337
525
  };
338
526
  }
339
527
  function assertionsAreKeyed(assertions) {
@@ -411,22 +599,24 @@ async function getPromotionPlan(sourceDatasource, destinationDatasource, options
411
599
  excludeFeatures.length === 0 &&
412
600
  targetSelectors.length === 0 &&
413
601
  tagSelectors.length === 0;
414
- const [featureKeys, segmentKeys, attributeKeys, groupKeys, schemaKeys, targetKeys, testKeys] = await Promise.all([
602
+ const [featureKeys, segmentKeys, attributeKeys, groupKeys, schemaKeys, targetKeys, variableKeys, testKeys,] = await Promise.all([
415
603
  sourceDatasource.listFeatures(),
416
604
  sourceDatasource.listSegments(),
417
605
  sourceDatasource.listAttributes(),
418
606
  sourceDatasource.listGroups(),
419
607
  sourceDatasource.listSchemas(),
420
608
  sourceDatasource.listTargets(),
609
+ sourceDatasource.listVariables(),
421
610
  sourceDatasource.listTests(),
422
611
  ]);
423
- const [features, segments, attributes, groups, schemas, targets, tests] = await Promise.all([
612
+ const [features, segments, attributes, groups, schemas, targets, variables, tests] = await Promise.all([
424
613
  safeRead(featureKeys, (key) => sourceDatasource.readFeature(key)),
425
614
  safeRead(segmentKeys, (key) => sourceDatasource.readSegment(key)),
426
615
  safeRead(attributeKeys, (key) => sourceDatasource.readAttribute(key)),
427
616
  safeRead(groupKeys, (key) => sourceDatasource.readGroup(key)),
428
617
  safeRead(schemaKeys, (key) => sourceDatasource.readSchema(key)),
429
618
  safeRead(targetKeys, (key) => sourceDatasource.readTarget(key)),
619
+ safeRead(variableKeys, (key) => sourceDatasource.readVariable(key)),
430
620
  safeRead(testKeys, (key) => sourceDatasource.readTest(key)),
431
621
  ]);
432
622
  const promotedFeatureKeys = new Set();
@@ -435,6 +625,7 @@ async function getPromotionPlan(sourceDatasource, destinationDatasource, options
435
625
  const promotedGroupKeys = new Set();
436
626
  const promotedSchemaKeys = new Set();
437
627
  const promotedTargetKeys = new Set();
628
+ const promotedVariableKeys = new Set();
438
629
  const selectedTargets = [];
439
630
  for (const key of targetSelectors) {
440
631
  const target = targets[key];
@@ -451,6 +642,7 @@ async function getPromotionPlan(sourceDatasource, destinationDatasource, options
451
642
  groupKeys.forEach((key) => promotedGroupKeys.add(key));
452
643
  schemaKeys.forEach((key) => promotedSchemaKeys.add(key));
453
644
  targetKeys.forEach((key) => promotedTargetKeys.add(key));
645
+ variableKeys.forEach((key) => promotedVariableKeys.add(key));
454
646
  }
455
647
  else {
456
648
  for (const key of featureKeys) {
@@ -464,8 +656,33 @@ async function getPromotionPlan(sourceDatasource, destinationDatasource, options
464
656
  promotedFeatureKeys.add(key);
465
657
  }
466
658
  }
467
- if (promotedFeatureKeys.size === 0 && !options.allowEmpty) {
468
- throw new Error("No source features matched the promotion filters.");
659
+ for (const key of variableKeys) {
660
+ const variable = variables[key];
661
+ const hasVariableSelectors = tagSelectors.length > 0 || selectedTargets.length > 0;
662
+ const matchesSelectedTags = tagSelectors.length === 0 ||
663
+ tagSelectors.some((tag) => (variable.tags || []).includes(tag));
664
+ const matchesSelectedTargets = selectedTargets.length === 0 ||
665
+ selectedTargets.some((target) => variableMatchesTarget(key, variable, target));
666
+ if (hasVariableSelectors && matchesSelectedTags && matchesSelectedTargets) {
667
+ promotedVariableKeys.add(key);
668
+ }
669
+ }
670
+ for (const key of promotedVariableKeys) {
671
+ const variable = variables[key];
672
+ const promotableOverrides = getAllPromotableVariableOverrides(variable);
673
+ const required = [
674
+ ...(0, requiredFeatures_1.normalizeRequiredFeatures)(variable.requiredFeatures),
675
+ ...promotableOverrides.flatMap((override) => (0, requiredFeatures_1.normalizeRequiredFeatures)(override.requiredFeatures)),
676
+ ];
677
+ required.forEach((entry) => promotedFeatureKeys.add((0, requiredFeatures_1.getRequiredFeatureKey)(entry)));
678
+ (0, schemaReferences_1.extractSchemaReferences)(variable).forEach((schemaKey) => promotedSchemaKeys.add(schemaKey));
679
+ promotableOverrides.forEach((override) => {
680
+ collectGroupSegmentKeys(override.segments, promotedSegmentKeys);
681
+ collectConditionDependencies(override.conditions, promotedSegmentKeys, promotedAttributeKeys);
682
+ });
683
+ }
684
+ if (promotedFeatureKeys.size === 0 && promotedVariableKeys.size === 0 && !options.allowEmpty) {
685
+ throw new Error("No source features or variables matched the promotion filters.");
469
686
  }
470
687
  }
471
688
  let previousFeatureCount = -1;
@@ -507,14 +724,16 @@ async function getPromotionPlan(sourceDatasource, destinationDatasource, options
507
724
  if (!schema)
508
725
  continue;
509
726
  const beforeSize = promotedSchemaKeys.size;
510
- collectSchemaReferences(schema, promotedSchemaKeys);
727
+ (0, schemaReferences_1.extractSchemaReferences)(schema).forEach((key) => promotedSchemaKeys.add(key));
511
728
  if (promotedSchemaKeys.size > beforeSize) {
512
729
  pendingSchemas.push(...Array.from(promotedSchemaKeys).filter((key) => !pendingSchemas.includes(key)));
513
730
  }
514
731
  }
515
732
  const promotedTestKeys = testKeys.filter((key) => {
516
733
  const test = tests[key];
517
- return promotedFeatureKeys.has(test.feature) || promotedSegmentKeys.has(test.segment);
734
+ return (promotedFeatureKeys.has(test.feature) ||
735
+ promotedSegmentKeys.has(test.segment) ||
736
+ promotedVariableKeys.has(test.variable));
518
737
  });
519
738
  const plans = [];
520
739
  async function plan(type, key, source, readDestinationEntity, merge = (destination, sourceValue, conflicts) => {
@@ -568,6 +787,10 @@ async function getPromotionPlan(sourceDatasource, destinationDatasource, options
568
787
  if (features[key])
569
788
  await plan("feature", key, features[key], (entryKey) => destinationDatasource.readFeature(entryKey), (destination, source, conflicts) => mergeFeature(key, destination, source, options.conflicts, conflicts));
570
789
  }
790
+ for (const key of Array.from(promotedVariableKeys).sort()) {
791
+ if (variables[key])
792
+ await plan("variable", key, variables[key], (entryKey) => destinationDatasource.readVariable(entryKey), (destination, source, conflicts) => mergeVariable(key, destination, source, options.conflicts, conflicts));
793
+ }
571
794
  for (const key of Array.from(promotedTargetKeys).sort()) {
572
795
  if (targets[key])
573
796
  await plan("target", key, targets[key], (entryKey) => destinationDatasource.readTarget(entryKey));
@@ -595,6 +818,8 @@ async function writePlan(destinationDatasource, plans) {
595
818
  await destinationDatasource.writeTarget(plan.key, plan.merged);
596
819
  if (plan.type === "feature")
597
820
  await destinationDatasource.writeFeature(plan.key, plan.merged);
821
+ if (plan.type === "variable")
822
+ await destinationDatasource.writeVariable(plan.key, plan.merged);
598
823
  if (plan.type === "test")
599
824
  await destinationDatasource.writeTest(plan.key, plan.merged);
600
825
  }
@@ -641,6 +866,7 @@ function getEntityFilePath(projectConfig, type, key) {
641
866
  group: projectConfig.groupsDirectoryPath,
642
867
  schema: projectConfig.schemasDirectoryPath,
643
868
  target: projectConfig.targetsDirectoryPath,
869
+ variable: projectConfig.variablesDirectoryPath,
644
870
  test: projectConfig.testsDirectoryPath,
645
871
  };
646
872
  const extension = projectConfig.parser.extension || "yml";
@@ -705,6 +931,7 @@ function stringifyMarkdownAudit(result) {
705
931
  `- Attributes: ${result.dependencies.attribute}`,
706
932
  `- Segments: ${result.dependencies.segment}`,
707
933
  `- Features: ${result.dependencies.feature}`,
934
+ `- Variables: ${result.dependencies.variable}`,
708
935
  `- Groups: ${result.dependencies.group}`,
709
936
  `- Schemas: ${result.dependencies.schema}`,
710
937
  `- Targets: ${result.dependencies.target}`,
@@ -799,6 +1026,7 @@ async function promoteProjectSets(projectConfig, datasource, options) {
799
1026
  group: plans.filter((plan) => plan.type === "group").length,
800
1027
  schema: plans.filter((plan) => plan.type === "schema").length,
801
1028
  target: plans.filter((plan) => plan.type === "target").length,
1029
+ variable: plans.filter((plan) => plan.type === "variable").length,
802
1030
  test: plans.filter((plan) => plan.type === "test").length,
803
1031
  };
804
1032
  const result = {
@@ -865,7 +1093,7 @@ function printPromoteResult(result, options = {}) {
865
1093
  }
866
1094
  console.log(` Conflict policy: ${result.filters.conflicts}`);
867
1095
  console.log("");
868
- console.log(` Dependencies: ${result.dependencies.attribute} attributes, ${result.dependencies.segment} segments, ${result.dependencies.feature} features, ${result.dependencies.group} groups, ${result.dependencies.schema} schemas, ${result.dependencies.target} targets, ${result.dependencies.test} tests`);
1096
+ console.log(` Dependencies: ${result.dependencies.attribute} attributes, ${result.dependencies.segment} segments, ${result.dependencies.feature} features, ${result.dependencies.variable} variables, ${result.dependencies.group} groups, ${result.dependencies.schema} schemas, ${result.dependencies.target} targets, ${result.dependencies.test} tests`);
869
1097
  console.log(` Created: ${result.files.created.length}`);
870
1098
  console.log(` Updated: ${result.files.updated.length}`);
871
1099
  console.log(` Unchanged: ${result.files.unchanged.length}`);