@featurevisor/core 3.1.0 → 3.2.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 (208) hide show
  1. package/lib/datasource/datasource.d.ts +1 -1
  2. package/lib/datasource/datasource.js +4 -14
  3. package/lib/datasource/datasource.js.map +1 -1
  4. package/lib/datasource/requiredFeatures.d.ts +7 -0
  5. package/lib/datasource/requiredFeatures.js +38 -0
  6. package/lib/datasource/requiredFeatures.js.map +1 -0
  7. package/lib/linter/checkCircularDependency.d.ts +1 -1
  8. package/lib/linter/checkCircularDependency.js +3 -22
  9. package/lib/linter/checkCircularDependency.js.map +1 -1
  10. package/lib/linter/conditionSchema.js +27 -3
  11. package/lib/linter/conditionSchema.js.map +1 -1
  12. package/lib/tester/testFeature.js +17 -16
  13. package/lib/tester/testFeature.js.map +1 -1
  14. package/package.json +10 -7
  15. package/CHANGELOG.md +0 -2246
  16. package/coverage/clover.xml +0 -5456
  17. package/coverage/coverage-final.json +0 -64
  18. package/coverage/lcov-report/base.css +0 -224
  19. package/coverage/lcov-report/block-navigation.js +0 -87
  20. package/coverage/lcov-report/favicon.png +0 -0
  21. package/coverage/lcov-report/index.html +0 -371
  22. package/coverage/lcov-report/prettify.css +0 -1
  23. package/coverage/lcov-report/prettify.js +0 -2
  24. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  25. package/coverage/lcov-report/sorter.js +0 -210
  26. package/coverage/lcov-report/src/assess-distribution/index.html +0 -116
  27. package/coverage/lcov-report/src/assess-distribution/index.ts.html +0 -754
  28. package/coverage/lcov-report/src/benchmark/index.html +0 -116
  29. package/coverage/lcov-report/src/benchmark/index.ts.html +0 -793
  30. package/coverage/lcov-report/src/builder/allocator.ts.html +0 -220
  31. package/coverage/lcov-report/src/builder/applyContextToConditions.ts.html +0 -502
  32. package/coverage/lcov-report/src/builder/applyContextToDatafile.ts.html +0 -772
  33. package/coverage/lcov-report/src/builder/applyContextToSegments.ts.html +0 -580
  34. package/coverage/lcov-report/src/builder/buildDatafile.ts.html +0 -2053
  35. package/coverage/lcov-report/src/builder/buildProject.ts.html +0 -1117
  36. package/coverage/lcov-report/src/builder/buildRuntimeDatafiles.ts.html +0 -256
  37. package/coverage/lcov-report/src/builder/getFeatureRanges.ts.html +0 -268
  38. package/coverage/lcov-report/src/builder/hashes.ts.html +0 -412
  39. package/coverage/lcov-report/src/builder/index.html +0 -311
  40. package/coverage/lcov-report/src/builder/index.ts.html +0 -91
  41. package/coverage/lcov-report/src/builder/mutateVariables.ts.html +0 -421
  42. package/coverage/lcov-report/src/builder/mutator.ts.html +0 -796
  43. package/coverage/lcov-report/src/builder/revision.ts.html +0 -151
  44. package/coverage/lcov-report/src/builder/traffic.ts.html +0 -661
  45. package/coverage/lcov-report/src/cli/cli.ts.html +0 -400
  46. package/coverage/lcov-report/src/cli/index.html +0 -131
  47. package/coverage/lcov-report/src/cli/plugins.ts.html +0 -214
  48. package/coverage/lcov-report/src/config/index.html +0 -131
  49. package/coverage/lcov-report/src/config/index.ts.html +0 -88
  50. package/coverage/lcov-report/src/config/projectConfig.ts.html +0 -925
  51. package/coverage/lcov-report/src/datasource/adapter.ts.html +0 -253
  52. package/coverage/lcov-report/src/datasource/datasource.ts.html +0 -1000
  53. package/coverage/lcov-report/src/datasource/filesystemAdapter.ts.html +0 -1576
  54. package/coverage/lcov-report/src/datasource/index.html +0 -161
  55. package/coverage/lcov-report/src/datasource/index.ts.html +0 -94
  56. package/coverage/lcov-report/src/evaluate/index.html +0 -116
  57. package/coverage/lcov-report/src/evaluate/index.ts.html +0 -814
  58. package/coverage/lcov-report/src/find-duplicate-segments/findDuplicateSegments.ts.html +0 -313
  59. package/coverage/lcov-report/src/find-duplicate-segments/index.html +0 -131
  60. package/coverage/lcov-report/src/find-duplicate-segments/index.ts.html +0 -307
  61. package/coverage/lcov-report/src/find-usage/index.html +0 -116
  62. package/coverage/lcov-report/src/find-usage/index.ts.html +0 -1780
  63. package/coverage/lcov-report/src/generate-code/index.html +0 -131
  64. package/coverage/lcov-report/src/generate-code/index.ts.html +0 -403
  65. package/coverage/lcov-report/src/generate-code/typescript.ts.html +0 -1909
  66. package/coverage/lcov-report/src/index.html +0 -131
  67. package/coverage/lcov-report/src/info/index.html +0 -116
  68. package/coverage/lcov-report/src/info/index.ts.html +0 -472
  69. package/coverage/lcov-report/src/init/index.html +0 -116
  70. package/coverage/lcov-report/src/init/index.ts.html +0 -280
  71. package/coverage/lcov-report/src/linter/attributeSchema.ts.html +0 -1963
  72. package/coverage/lcov-report/src/linter/checkCircularDependency.ts.html +0 -220
  73. package/coverage/lcov-report/src/linter/checkPercentageExceedingSlot.ts.html +0 -268
  74. package/coverage/lcov-report/src/linter/conditionSchema.ts.html +0 -2224
  75. package/coverage/lcov-report/src/linter/featureSchema.ts.html +0 -5851
  76. package/coverage/lcov-report/src/linter/groupSchema.ts.html +0 -226
  77. package/coverage/lcov-report/src/linter/index.html +0 -326
  78. package/coverage/lcov-report/src/linter/index.ts.html +0 -88
  79. package/coverage/lcov-report/src/linter/lintProject.ts.html +0 -2704
  80. package/coverage/lcov-report/src/linter/mutationNotation.ts.html +0 -1411
  81. package/coverage/lcov-report/src/linter/printError.ts.html +0 -289
  82. package/coverage/lcov-report/src/linter/schema.ts.html +0 -1558
  83. package/coverage/lcov-report/src/linter/segmentSchema.ts.html +0 -139
  84. package/coverage/lcov-report/src/linter/targetSchema.ts.html +0 -223
  85. package/coverage/lcov-report/src/linter/testSchema.ts.html +0 -529
  86. package/coverage/lcov-report/src/linter/zodHelpers.ts.html +0 -133
  87. package/coverage/lcov-report/src/list/index.html +0 -131
  88. package/coverage/lcov-report/src/list/index.ts.html +0 -2065
  89. package/coverage/lcov-report/src/list/matrix.ts.html +0 -721
  90. package/coverage/lcov-report/src/promoter/index.html +0 -116
  91. package/coverage/lcov-report/src/promoter/index.ts.html +0 -3883
  92. package/coverage/lcov-report/src/sets.ts.html +0 -313
  93. package/coverage/lcov-report/src/targeting.ts.html +0 -403
  94. package/coverage/lcov-report/src/tester/cliFormat.ts.html +0 -133
  95. package/coverage/lcov-report/src/tester/helpers.ts.html +0 -313
  96. package/coverage/lcov-report/src/tester/index.html +0 -206
  97. package/coverage/lcov-report/src/tester/prettyDuration.ts.html +0 -187
  98. package/coverage/lcov-report/src/tester/printTestResult.ts.html +0 -337
  99. package/coverage/lcov-report/src/tester/testFeature.ts.html +0 -1186
  100. package/coverage/lcov-report/src/tester/testProject.ts.html +0 -1018
  101. package/coverage/lcov-report/src/tester/testSegment.ts.html +0 -316
  102. package/coverage/lcov-report/src/utils/extractKeys.ts.html +0 -493
  103. package/coverage/lcov-report/src/utils/git.ts.html +0 -559
  104. package/coverage/lcov-report/src/utils/index.html +0 -161
  105. package/coverage/lcov-report/src/utils/index.ts.html +0 -94
  106. package/coverage/lcov-report/src/utils/pretty.ts.html +0 -121
  107. package/coverage/lcov.info +0 -11615
  108. package/jest.config.js +0 -22
  109. package/json-schema/README.md +0 -5
  110. package/json-schema/attribute.json +0 -40
  111. package/json-schema/feature.json +0 -394
  112. package/json-schema/segment.json +0 -137
  113. package/src/assess-distribution/index.ts +0 -223
  114. package/src/benchmark/index.ts +0 -236
  115. package/src/builder/allocator.spec.ts +0 -105
  116. package/src/builder/allocator.ts +0 -45
  117. package/src/builder/applyContextToConditions.spec.ts +0 -2677
  118. package/src/builder/applyContextToConditions.ts +0 -139
  119. package/src/builder/applyContextToDatafile.spec.ts +0 -2626
  120. package/src/builder/applyContextToDatafile.ts +0 -229
  121. package/src/builder/applyContextToSegments.spec.ts +0 -1593
  122. package/src/builder/applyContextToSegments.ts +0 -165
  123. package/src/builder/buildDatafile.spec.ts +0 -445
  124. package/src/builder/buildDatafile.ts +0 -656
  125. package/src/builder/buildProject.ts +0 -344
  126. package/src/builder/buildRuntimeDatafiles.ts +0 -57
  127. package/src/builder/getFeatureRanges.ts +0 -61
  128. package/src/builder/hashes.ts +0 -109
  129. package/src/builder/index.ts +0 -2
  130. package/src/builder/mutateVariables.spec.ts +0 -1179
  131. package/src/builder/mutateVariables.ts +0 -112
  132. package/src/builder/mutator.spec.ts +0 -413
  133. package/src/builder/mutator.ts +0 -237
  134. package/src/builder/revision.spec.ts +0 -22
  135. package/src/builder/revision.ts +0 -22
  136. package/src/builder/traffic.spec.ts +0 -946
  137. package/src/builder/traffic.ts +0 -192
  138. package/src/cli/cli.ts +0 -105
  139. package/src/cli/index.ts +0 -2
  140. package/src/cli/plugins.ts +0 -43
  141. package/src/config/index.ts +0 -1
  142. package/src/config/projectConfig.spec.ts +0 -194
  143. package/src/config/projectConfig.ts +0 -280
  144. package/src/datasource/adapter.ts +0 -56
  145. package/src/datasource/datasource.ts +0 -305
  146. package/src/datasource/filesystemAdapter.spec.ts +0 -212
  147. package/src/datasource/filesystemAdapter.ts +0 -497
  148. package/src/datasource/index.ts +0 -3
  149. package/src/dependencies.ts +0 -13
  150. package/src/evaluate/index.ts +0 -243
  151. package/src/find-duplicate-segments/findDuplicateSegments.ts +0 -76
  152. package/src/find-duplicate-segments/index.ts +0 -74
  153. package/src/find-usage/index.ts +0 -565
  154. package/src/generate-code/index.ts +0 -106
  155. package/src/generate-code/typescript.spec.ts +0 -214
  156. package/src/generate-code/typescript.ts +0 -608
  157. package/src/index.ts +0 -20
  158. package/src/info/index.ts +0 -129
  159. package/src/init/index.ts +0 -65
  160. package/src/linter/attributeSchema.spec.ts +0 -342
  161. package/src/linter/attributeSchema.ts +0 -626
  162. package/src/linter/checkCircularDependency.ts +0 -45
  163. package/src/linter/checkPercentageExceedingSlot.ts +0 -61
  164. package/src/linter/conditionSchema.spec.ts +0 -510
  165. package/src/linter/conditionSchema.ts +0 -713
  166. package/src/linter/featureSchema.spec.ts +0 -1985
  167. package/src/linter/featureSchema.ts +0 -1922
  168. package/src/linter/groupSchema.ts +0 -47
  169. package/src/linter/index.ts +0 -1
  170. package/src/linter/lintProject.spec.ts +0 -336
  171. package/src/linter/lintProject.ts +0 -873
  172. package/src/linter/mutationNotation.spec.ts +0 -704
  173. package/src/linter/mutationNotation.ts +0 -442
  174. package/src/linter/printError.ts +0 -68
  175. package/src/linter/schema.spec.ts +0 -690
  176. package/src/linter/schema.ts +0 -491
  177. package/src/linter/segmentSchema.spec.ts +0 -358
  178. package/src/linter/segmentSchema.ts +0 -18
  179. package/src/linter/targetSchema.spec.ts +0 -159
  180. package/src/linter/targetSchema.ts +0 -46
  181. package/src/linter/testSchema.spec.ts +0 -258
  182. package/src/linter/testSchema.ts +0 -148
  183. package/src/linter/zodHelpers.ts +0 -16
  184. package/src/list/index.spec.ts +0 -135
  185. package/src/list/index.ts +0 -660
  186. package/src/list/matrix.spec.ts +0 -49
  187. package/src/list/matrix.ts +0 -212
  188. package/src/promoter/index.spec.ts +0 -617
  189. package/src/promoter/index.ts +0 -1266
  190. package/src/sets.ts +0 -76
  191. package/src/targeting.spec.ts +0 -74
  192. package/src/targeting.ts +0 -106
  193. package/src/tester/cliFormat.ts +0 -16
  194. package/src/tester/helpers.spec.ts +0 -149
  195. package/src/tester/helpers.ts +0 -76
  196. package/src/tester/index.ts +0 -2
  197. package/src/tester/prettyDuration.ts +0 -34
  198. package/src/tester/printTestResult.ts +0 -84
  199. package/src/tester/testFeature.ts +0 -367
  200. package/src/tester/testProject.spec.ts +0 -50
  201. package/src/tester/testProject.ts +0 -311
  202. package/src/tester/testSegment.ts +0 -77
  203. package/src/utils/extractKeys.ts +0 -136
  204. package/src/utils/git.ts +0 -158
  205. package/src/utils/index.ts +0 -3
  206. package/src/utils/pretty.ts +0 -12
  207. package/tsconfig.cjs.json +0 -13
  208. package/tsconfig.typecheck.json +0 -9
@@ -1,656 +0,0 @@
1
- import * as fs from "fs";
2
-
3
- import type { Schema, VariableType } from "@featurevisor/types";
4
- import {
5
- Segment,
6
- Feature,
7
- DatafileContent,
8
- Variation,
9
- VariableOverride,
10
- Traffic,
11
- SegmentKey,
12
- AttributeKey,
13
- Attribute,
14
- GroupSegment,
15
- Condition,
16
- ExistingState,
17
- FeatureKey,
18
- Allocation,
19
- Expose,
20
- Rule,
21
- Force,
22
- } from "@featurevisor/types";
23
-
24
- import { ProjectConfig } from "../config";
25
- import { Datasource } from "../datasource";
26
- import { extractAttributeKeysFromConditions, extractSegmentKeysFromGroupSegments } from "../utils";
27
- import { generateHashForDatafile, generateHashForFeature, getSegmentHashes } from "./hashes";
28
-
29
- import { getTraffic } from "./traffic";
30
- import { getFeatureRanges } from "./getFeatureRanges";
31
- import {
32
- resolveMutationsForMultipleVariables,
33
- resolveMutationsForSingleVariable,
34
- } from "./mutateVariables";
35
-
36
- export interface CustomDatafileOptions {
37
- featureKey?: string;
38
- environment: string | false;
39
- projectConfig: ProjectConfig;
40
- datasource: Datasource;
41
- revision?: string;
42
- inflate?: number;
43
- tag?: string;
44
- tags?: BuildTags;
45
- includeFeatures?: "*" | FeatureKey[];
46
- excludeFeatures?: "*" | FeatureKey[];
47
- featurevisorVersion?: string;
48
- }
49
-
50
- export async function getCustomDatafile(options: CustomDatafileOptions): Promise<DatafileContent> {
51
- let featuresToInclude;
52
-
53
- if (options.featureKey) {
54
- const requiredChain = await options.datasource.getRequiredFeaturesChain(options.featureKey);
55
- featuresToInclude = Array.from(requiredChain);
56
- }
57
-
58
- const existingState = await options.datasource.readState(options.environment);
59
- const datafileContent = await buildDatafile(
60
- options.projectConfig,
61
- options.datasource,
62
- {
63
- revision: options.revision || "tester",
64
- environment: options.environment,
65
- features: featuresToInclude,
66
- inflate: options.inflate,
67
- tag: options.tag,
68
- tags: options.tags,
69
- includeFeatures: options.includeFeatures,
70
- excludeFeatures: options.excludeFeatures,
71
- featurevisorVersion: options.featurevisorVersion,
72
- },
73
- existingState,
74
- );
75
-
76
- return datafileContent;
77
- }
78
-
79
- export interface BuildOrTags {
80
- or: string[];
81
- }
82
-
83
- export interface BuildAndTags {
84
- and: string[];
85
- }
86
-
87
- export type BuildTags = BuildOrTags | BuildAndTags | string[];
88
-
89
- function matchesBuildTags(candidateTags: string[], buildTags?: BuildTags): boolean {
90
- if (!buildTags) {
91
- return true;
92
- }
93
-
94
- if (Array.isArray(buildTags)) {
95
- return buildTags.some((searchTag) => candidateTags.indexOf(searchTag) !== -1);
96
- }
97
-
98
- if ("or" in buildTags) {
99
- return buildTags.or.some((searchTag) => candidateTags.indexOf(searchTag) !== -1);
100
- }
101
-
102
- if ("and" in buildTags) {
103
- return buildTags.and.every((searchTag) => candidateTags.indexOf(searchTag) !== -1);
104
- }
105
-
106
- return true;
107
- }
108
-
109
- function matchesFeaturePatterns(featureKey: FeatureKey, patterns?: "*" | FeatureKey[]): boolean {
110
- if (!patterns) {
111
- return false;
112
- }
113
-
114
- const normalizedPatterns = patterns === "*" ? [patterns] : patterns;
115
-
116
- return normalizedPatterns.some((pattern) => {
117
- const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
118
- return new RegExp(`^${escaped}$`).test(featureKey);
119
- });
120
- }
121
-
122
- export interface BuildOptions {
123
- revision: string;
124
- featurevisorVersion?: string;
125
- revisionFromHash?: boolean;
126
- environment: string | false;
127
- tag?: string;
128
- tags?: BuildTags;
129
- includeFeatures?: "*" | FeatureKey[];
130
- excludeFeatures?: "*" | FeatureKey[];
131
- features?: FeatureKey[];
132
- inflate?: number;
133
- }
134
-
135
- export async function buildDatafile(
136
- projectConfig: ProjectConfig,
137
- datasource: Datasource,
138
- options: BuildOptions,
139
- existingState: ExistingState,
140
- ): Promise<DatafileContent> {
141
- const segmentKeysUsedByTag = new Set<SegmentKey>();
142
- const attributeKeysUsedByTag = new Set<AttributeKey>();
143
- const { featureRanges, featureIsInGroup } = await getFeatureRanges(projectConfig, datasource);
144
-
145
- // Load schemas for resolving variable schema references in features
146
- const schemaKeys = await datasource.listSchemas();
147
- const schemasByKey: Record<string, Schema> = {};
148
- for (const key of schemaKeys) {
149
- try {
150
- schemasByKey[key] = await datasource.readSchema(key);
151
- } catch {
152
- // Schema file may be invalid; skip for datafile build
153
- }
154
- }
155
-
156
- // features
157
- const features: Feature[] = [];
158
- const featuresDirectory = projectConfig.featuresDirectoryPath;
159
-
160
- if (fs.existsSync(featuresDirectory)) {
161
- const featureFiles = await datasource.listFeatures();
162
-
163
- for (const featureKey of featureFiles) {
164
- const parsedFeature = await datasource.readFeature(featureKey);
165
-
166
- if (parsedFeature.archived === true) {
167
- continue;
168
- }
169
-
170
- const featureTags = parsedFeature.tags || [];
171
-
172
- if (options.tag && featureTags.indexOf(options.tag) === -1) {
173
- continue;
174
- }
175
-
176
- if (options.tags && matchesBuildTags(featureTags, options.tags) === false) {
177
- continue;
178
- }
179
-
180
- if (
181
- options.includeFeatures &&
182
- matchesFeaturePatterns(featureKey, options.includeFeatures) === false
183
- ) {
184
- continue;
185
- }
186
-
187
- if (options.excludeFeatures && matchesFeaturePatterns(featureKey, options.excludeFeatures)) {
188
- continue;
189
- }
190
-
191
- if (options.features && options.features.indexOf(featureKey) === -1) {
192
- continue;
193
- }
194
-
195
- let expose: Expose | undefined;
196
- let rules: Rule[];
197
- let force: Force[] | undefined;
198
-
199
- if (options.environment) {
200
- expose = parsedFeature.expose?.[options.environment] as Expose | undefined;
201
- rules = parsedFeature.rules?.[options.environment] as Rule[];
202
- force = parsedFeature.force?.[options.environment] as Force[] | undefined;
203
- } else {
204
- expose = parsedFeature.expose as Expose | undefined;
205
- rules = parsedFeature.rules as Rule[];
206
- force = parsedFeature.force as Force[] | undefined;
207
- }
208
-
209
- if (expose === false) {
210
- continue;
211
- }
212
-
213
- if (Array.isArray(expose)) {
214
- const exposeTags = expose;
215
-
216
- if (options.tag && exposeTags.indexOf(options.tag) === -1) {
217
- continue;
218
- }
219
-
220
- if (options.tags && matchesBuildTags(exposeTags, options.tags) === false) {
221
- continue;
222
- }
223
- }
224
-
225
- for (const parsedRule of rules) {
226
- const extractedSegmentKeys = extractSegmentKeysFromGroupSegments(parsedRule.segments);
227
- extractedSegmentKeys.forEach((segmentKey) => segmentKeysUsedByTag.add(segmentKey));
228
-
229
- if (parsedRule.variableOverrides) {
230
- for (const variableOverrides of Object.values(parsedRule.variableOverrides)) {
231
- for (const override of variableOverrides) {
232
- if (typeof override.conditions !== "undefined") {
233
- const extractedAttributeKeys = extractAttributeKeysFromConditions(
234
- override.conditions,
235
- );
236
- extractedAttributeKeys.forEach((attributeKey) =>
237
- attributeKeysUsedByTag.add(attributeKey),
238
- );
239
- }
240
-
241
- if (typeof override.segments !== "undefined") {
242
- const extractedOverrideSegmentKeys = extractSegmentKeysFromGroupSegments(
243
- override.segments as GroupSegment | GroupSegment[],
244
- );
245
- extractedOverrideSegmentKeys.forEach((segmentKey) =>
246
- segmentKeysUsedByTag.add(segmentKey),
247
- );
248
- }
249
- }
250
- }
251
- }
252
- }
253
-
254
- const feature: Feature = {
255
- key: featureKey,
256
- deprecated: parsedFeature.deprecated === true ? true : undefined,
257
- bucketBy: parsedFeature.bucketBy || projectConfig.defaultBucketBy,
258
- required: parsedFeature.required,
259
- disabledVariationValue: parsedFeature.disabledVariationValue,
260
- variations: Array.isArray(parsedFeature.variations)
261
- ? parsedFeature.variations.map((variation: Variation) => {
262
- const mappedVariation: any = {
263
- value: variation.value,
264
- weight: variation.weight, // @NOTE: added so state files can maintain weight info, but datafiles don't need this. find a way to remove it from datafiles later
265
- variables: resolveMutationsForMultipleVariables(
266
- parsedFeature.variablesSchema,
267
- variation.variables,
268
- ),
269
- variableOverrides: variation.variableOverrides,
270
- };
271
-
272
- if (variation.variableOverrides) {
273
- const variableOverrides = variation.variableOverrides;
274
- const variableKeys = Object.keys(variableOverrides);
275
-
276
- for (const variableKey of variableKeys) {
277
- const baseValue = mappedVariation.variables
278
- ? mappedVariation.variables[variableKey]
279
- : undefined;
280
-
281
- mappedVariation.variableOverrides[variableKey] = variableOverrides[
282
- variableKey
283
- ].map((override: VariableOverride) => {
284
- if (typeof override.conditions !== "undefined") {
285
- const extractedAttributeKeys = extractAttributeKeysFromConditions(
286
- override.conditions,
287
- );
288
- extractedAttributeKeys.forEach((attributeKey) =>
289
- attributeKeysUsedByTag.add(attributeKey),
290
- );
291
-
292
- return {
293
- conditions:
294
- projectConfig.stringify && typeof override.conditions !== "string"
295
- ? JSON.stringify(override.conditions)
296
- : override.conditions,
297
- value: resolveMutationsForSingleVariable(
298
- parsedFeature.variablesSchema,
299
- variableKey,
300
- override.value,
301
- baseValue,
302
- ),
303
- };
304
- }
305
-
306
- if (typeof override.segments !== "undefined") {
307
- const extractedSegmentKeys = extractSegmentKeysFromGroupSegments(
308
- override.segments as GroupSegment | GroupSegment[],
309
- );
310
- extractedSegmentKeys.forEach((segmentKey) =>
311
- segmentKeysUsedByTag.add(segmentKey),
312
- );
313
-
314
- return {
315
- segments:
316
- projectConfig.stringify && typeof override.segments !== "string"
317
- ? JSON.stringify(override.segments)
318
- : override.segments,
319
- value: resolveMutationsForSingleVariable(
320
- parsedFeature.variablesSchema,
321
- variableKey,
322
- override.value,
323
- baseValue,
324
- ),
325
- };
326
- }
327
-
328
- return override;
329
- });
330
- }
331
- }
332
-
333
- return mappedVariation;
334
- })
335
- : undefined,
336
- traffic: getTraffic(
337
- parsedFeature.variations,
338
- rules,
339
- existingState.features[featureKey],
340
- featureRanges.get(featureKey) || [],
341
- ).map((t: Traffic) => {
342
- const resolvedVariables = resolveMutationsForMultipleVariables(
343
- parsedFeature.variablesSchema,
344
- t.variables,
345
- );
346
-
347
- let resolvedVariableOverrides = t.variableOverrides;
348
-
349
- if (t.variableOverrides) {
350
- resolvedVariableOverrides = {};
351
- const variableKeys = Object.keys(t.variableOverrides);
352
-
353
- for (const variableKey of variableKeys) {
354
- const overrides = t.variableOverrides[variableKey];
355
- const baseValue = resolvedVariables ? resolvedVariables[variableKey] : undefined;
356
-
357
- resolvedVariableOverrides[variableKey] = overrides.map(
358
- (override: VariableOverride) => {
359
- if (typeof override.conditions !== "undefined") {
360
- return {
361
- conditions:
362
- projectConfig.stringify && typeof override.conditions !== "string"
363
- ? JSON.stringify(override.conditions)
364
- : override.conditions,
365
- value: resolveMutationsForSingleVariable(
366
- parsedFeature.variablesSchema,
367
- variableKey,
368
- override.value,
369
- baseValue,
370
- ),
371
- };
372
- }
373
-
374
- if (typeof override.segments !== "undefined") {
375
- return {
376
- segments:
377
- projectConfig.stringify && typeof override.segments !== "string"
378
- ? JSON.stringify(override.segments)
379
- : override.segments,
380
- value: resolveMutationsForSingleVariable(
381
- parsedFeature.variablesSchema,
382
- variableKey,
383
- override.value,
384
- baseValue,
385
- ),
386
- };
387
- }
388
-
389
- return override;
390
- },
391
- );
392
- }
393
- }
394
-
395
- return {
396
- ...t,
397
- variables: resolvedVariables,
398
- variableOverrides: resolvedVariableOverrides,
399
- segments:
400
- typeof t.segments !== "string" && projectConfig.stringify
401
- ? JSON.stringify(t.segments)
402
- : t.segments,
403
- };
404
- }),
405
- ranges: featureRanges.get(featureKey) || undefined,
406
- };
407
-
408
- // update state in memory, so that next datafile build can use it (in case it contains the same feature)
409
- existingState.features[featureKey] = {
410
- variations: Array.isArray(feature.variations)
411
- ? feature.variations.map((v: Variation) => {
412
- return {
413
- value: v.value,
414
- weight: v.weight || 0,
415
- };
416
- })
417
- : undefined,
418
- traffic: feature.traffic.map((t: Traffic) => {
419
- return {
420
- key: t.key,
421
- percentage: t.percentage,
422
- allocation:
423
- t.allocation &&
424
- t.allocation.map((a: Allocation) => {
425
- return {
426
- variation: a.variation,
427
- range: a.range,
428
- };
429
- }),
430
- };
431
- }),
432
- };
433
-
434
- if (featureIsInGroup[featureKey] === true) {
435
- feature.ranges = featureRanges.get(featureKey);
436
- }
437
-
438
- if (parsedFeature.variablesSchema) {
439
- const variableKeys = Object.keys(parsedFeature.variablesSchema);
440
- feature.variablesSchema = {};
441
-
442
- for (const variableKey of variableKeys) {
443
- const v = parsedFeature.variablesSchema[variableKey];
444
- const resolved =
445
- "schema" in v && v.schema
446
- ? schemasByKey[v.schema]
447
- : (v as { type: string; properties?: Schema; required?: string[]; items?: Schema });
448
-
449
- feature.variablesSchema[variableKey] = {
450
- key: variableKey,
451
- type: (resolved?.type ?? "string") as VariableType,
452
- defaultValue: v.defaultValue,
453
- deprecated: v.deprecated === true ? true : undefined,
454
- useDefaultWhenDisabled: v.useDefaultWhenDisabled === true ? true : undefined,
455
- disabledValue: typeof v.disabledValue !== "undefined" ? v.disabledValue : undefined,
456
- };
457
- }
458
- }
459
-
460
- if (force) {
461
- feature.force = force.map((f) => {
462
- if (f.segments) {
463
- const extractedSegmentKeys = extractSegmentKeysFromGroupSegments(f.segments);
464
- extractedSegmentKeys.forEach((segmentKey) => segmentKeysUsedByTag.add(segmentKey));
465
-
466
- f.segments =
467
- typeof f.segments !== "string" && projectConfig.stringify
468
- ? JSON.stringify(f.segments)
469
- : f.segments;
470
- }
471
-
472
- if (f.conditions) {
473
- f.conditions =
474
- typeof f.conditions !== "string" && projectConfig.stringify
475
- ? JSON.stringify(f.conditions)
476
- : f.conditions;
477
- }
478
-
479
- if (f.variables) {
480
- f.variables = resolveMutationsForMultipleVariables(
481
- parsedFeature.variablesSchema,
482
- f.variables,
483
- );
484
- }
485
-
486
- return f;
487
- });
488
- }
489
-
490
- features.push(feature);
491
- }
492
- }
493
-
494
- // segments
495
- const segments: Segment[] = [];
496
- const segmentsDirectory = projectConfig.segmentsDirectoryPath;
497
-
498
- if (fs.existsSync(segmentsDirectory)) {
499
- const segmentFiles = await datasource.listSegments();
500
-
501
- for (const segmentKey of segmentFiles) {
502
- const parsedSegment = await datasource.readSegment(segmentKey);
503
-
504
- if (parsedSegment.archived === true) {
505
- continue;
506
- }
507
-
508
- if (segmentKeysUsedByTag.has(segmentKey) === false) {
509
- continue;
510
- }
511
-
512
- const extractedAttributeKeys = extractAttributeKeysFromConditions(
513
- parsedSegment.conditions as Condition | Condition[],
514
- );
515
- extractedAttributeKeys.forEach((attributeKey) => attributeKeysUsedByTag.add(attributeKey));
516
-
517
- const segment: Segment = {
518
- key: segmentKey,
519
- conditions:
520
- typeof parsedSegment.conditions !== "string" && projectConfig.stringify === true
521
- ? JSON.stringify(parsedSegment.conditions)
522
- : parsedSegment.conditions,
523
- };
524
-
525
- segments.push(segment);
526
- }
527
- }
528
-
529
- // attributes
530
- const attributes: Attribute[] = [];
531
- const attributesDirectory = projectConfig.attributesDirectoryPath;
532
-
533
- if (fs.existsSync(attributesDirectory)) {
534
- const attributeFiles = await datasource.listAttributes();
535
-
536
- for (const attributeKey of attributeFiles) {
537
- const parsedAttribute = await datasource.readAttribute(attributeKey);
538
-
539
- if (parsedAttribute.archived === true) {
540
- continue;
541
- }
542
-
543
- if (attributeKeysUsedByTag.has(attributeKey) === false) {
544
- continue;
545
- }
546
-
547
- const attribute: Attribute = {
548
- key: attributeKey,
549
- type: parsedAttribute.type,
550
- };
551
-
552
- attributes.push(attribute);
553
- }
554
- }
555
-
556
- // inflate
557
- if (options.inflate && options.inflate >= 2) {
558
- const allFeatureKeys = features.map((f) => f.key);
559
- const allSegmentKeys = segments.map((s) => s.key);
560
- const allAttributeKeys = attributes.map((a) => a.key);
561
-
562
- for (let i = 0; i < options.inflate - 1; i++) {
563
- // feature
564
- for (const featureKey of allFeatureKeys) {
565
- const originalFeature = features.find((f) => f.key === featureKey) as Feature;
566
-
567
- features.unshift({
568
- ...originalFeature,
569
- key: `${originalFeature.key}-${i}`,
570
- });
571
- }
572
-
573
- // segment
574
- for (const segmentKey of allSegmentKeys) {
575
- const originalSegment = segments.find((s) => s.key === segmentKey) as Segment;
576
-
577
- segments.unshift({
578
- ...originalSegment,
579
- key: `${originalSegment.key}-${i}`,
580
- });
581
- }
582
-
583
- // attribute
584
- for (const attributeKey of allAttributeKeys) {
585
- const originalAttribute = attributes.find((a) => a.key === attributeKey) as Attribute;
586
-
587
- attributes.unshift({
588
- ...originalAttribute,
589
- key: `${originalAttribute.key}-${i}`,
590
- });
591
- }
592
- }
593
- }
594
-
595
- const datafileContent: DatafileContent = {
596
- schemaVersion: "2",
597
- featurevisorVersion: options.featurevisorVersion,
598
- revision: options.revision,
599
- segments: {},
600
- features: {},
601
- };
602
-
603
- datafileContent.segments = segments.reduce((acc, segment) => {
604
- if (segment.key) {
605
- acc[segment.key] = segment;
606
- delete acc[segment.key].key; // remove key from segment, as it is not needed in v2 datafile
607
- }
608
-
609
- return acc;
610
- }, {});
611
-
612
- datafileContent.features = features.reduce((acc, feature) => {
613
- if (!feature.key) {
614
- return acc;
615
- }
616
-
617
- const featureKey = feature.key as FeatureKey;
618
- const featureV2 = feature;
619
-
620
- // remove key, as it is not needed in v2 datafile
621
- delete featureV2.key;
622
-
623
- // remove variablesSchema[key].key
624
- if (featureV2.variablesSchema) {
625
- for (const [variableKey, variable] of Object.entries(featureV2.variablesSchema)) {
626
- if (variable.key) {
627
- delete featureV2.variablesSchema[variableKey].key;
628
- }
629
- }
630
- }
631
-
632
- acc[featureKey] = featureV2;
633
-
634
- return acc;
635
- }, {});
636
-
637
- // add feature hashes for change detection
638
- const segmentHashes = getSegmentHashes(datafileContent.segments);
639
- Object.keys(datafileContent.features).forEach((featureKey) => {
640
- const hash = generateHashForFeature(featureKey, datafileContent.features, segmentHashes);
641
-
642
- datafileContent.features[featureKey].hash = hash;
643
-
644
- // check needed to support --inflate option
645
- if (existingState.features[featureKey]) {
646
- existingState.features[featureKey].hash = hash;
647
- }
648
- });
649
-
650
- if (options.revisionFromHash) {
651
- const datafileHash = generateHashForDatafile(datafileContent);
652
- datafileContent.revision = `${datafileHash}`;
653
- }
654
-
655
- return datafileContent;
656
- }