-
Notifications
You must be signed in to change notification settings - Fork 27
/
generateSiteSearchIndex.ts
203 lines (173 loc) · 6.04 KB
/
generateSiteSearchIndex.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import {
HistoryEntry,
SearchIndex,
AttributeKey,
FeatureKey,
SegmentKey,
Condition,
} from "@featurevisor/types";
import { extractAttributeKeysFromConditions, extractSegmentKeysFromGroupSegments } from "../utils";
import { getRelativePaths } from "./getRelativePaths";
import { getLastModifiedFromHistory } from "./getLastModifiedFromHistory";
import { RepoDetails } from "./getRepoDetails";
import { Dependencies } from "../dependencies";
export async function generateSiteSearchIndex(
deps: Dependencies,
fullHistory: HistoryEntry[],
repoDetails: RepoDetails | undefined,
): Promise<SearchIndex> {
const { rootDirectoryPath, projectConfig, datasource } = deps;
const result: SearchIndex = {
links: undefined,
entities: {
attributes: [],
segments: [],
features: [],
},
};
/**
* Links
*/
if (repoDetails) {
const { relativeAttributesPath, relativeSegmentsPath, relativeFeaturesPath } = getRelativePaths(
rootDirectoryPath,
projectConfig,
);
let prefix = "";
if (repoDetails.topLevelPath !== rootDirectoryPath) {
prefix = rootDirectoryPath.replace(repoDetails.topLevelPath + "/", "") + "/";
}
result.links = {
attribute: repoDetails.blobUrl.replace(
"{{blobPath}}",
prefix + relativeAttributesPath + "/{{key}}." + datasource.getExtension(),
),
segment: repoDetails.blobUrl.replace(
"{{blobPath}}",
prefix + relativeSegmentsPath + "/{{key}}." + datasource.getExtension(),
),
feature: repoDetails.blobUrl.replace(
"{{blobPath}}",
prefix + relativeFeaturesPath + "/{{key}}." + datasource.getExtension(),
),
commit: repoDetails.commitUrl,
};
}
/**
* Entities
*/
// usage
const attributesUsedInFeatures: {
[key: AttributeKey]: Set<FeatureKey>;
} = {};
const attributesUsedInSegments: {
[key: AttributeKey]: Set<SegmentKey>;
} = {};
const segmentsUsedInFeatures: {
[key: SegmentKey]: Set<FeatureKey>;
} = {};
// features
const featureFiles = await datasource.listFeatures();
for (const entityName of featureFiles) {
const parsed = await datasource.readFeature(entityName);
if (Array.isArray(parsed.variations)) {
parsed.variations.forEach((variation) => {
if (!variation.variables) {
return;
}
variation.variables.forEach((v) => {
if (v.overrides) {
v.overrides.forEach((o) => {
if (o.conditions) {
extractAttributeKeysFromConditions(o.conditions).forEach((attributeKey) => {
if (!attributesUsedInFeatures[attributeKey]) {
attributesUsedInFeatures[attributeKey] = new Set();
}
attributesUsedInFeatures[attributeKey].add(entityName);
});
}
if (o.segments && o.segments !== "*") {
extractSegmentKeysFromGroupSegments(o.segments).forEach((segmentKey) => {
if (!segmentsUsedInFeatures[segmentKey]) {
segmentsUsedInFeatures[segmentKey] = new Set();
}
segmentsUsedInFeatures[segmentKey].add(entityName);
});
}
});
}
});
});
}
Object.keys(parsed.environments).forEach((environmentKey) => {
const env = parsed.environments[environmentKey];
env.rules.forEach((rule) => {
if (rule.segments && rule.segments !== "*") {
extractSegmentKeysFromGroupSegments(rule.segments).forEach((segmentKey) => {
if (!segmentsUsedInFeatures[segmentKey]) {
segmentsUsedInFeatures[segmentKey] = new Set();
}
segmentsUsedInFeatures[segmentKey].add(entityName);
});
}
});
if (env.force) {
env.force.forEach((force) => {
if (force.segments && force.segments !== "*") {
extractSegmentKeysFromGroupSegments(force.segments).forEach((segmentKey) => {
if (!segmentsUsedInFeatures[segmentKey]) {
segmentsUsedInFeatures[segmentKey] = new Set();
}
segmentsUsedInFeatures[segmentKey].add(entityName);
});
}
if (force.conditions) {
extractAttributeKeysFromConditions(force.conditions).forEach((attributeKey) => {
if (!attributesUsedInFeatures[attributeKey]) {
attributesUsedInFeatures[attributeKey] = new Set();
}
attributesUsedInFeatures[attributeKey].add(entityName);
});
}
});
}
});
result.entities.features.push({
...parsed,
key: entityName,
lastModified: getLastModifiedFromHistory(fullHistory, "feature", entityName),
});
}
// segments
const segmentFiles = await datasource.listSegments();
for (const entityName of segmentFiles) {
const parsed = await datasource.readSegment(entityName);
extractAttributeKeysFromConditions(parsed.conditions as Condition | Condition[]).forEach(
(attributeKey) => {
if (!attributesUsedInSegments[attributeKey]) {
attributesUsedInSegments[attributeKey] = new Set();
}
attributesUsedInSegments[attributeKey].add(entityName);
},
);
result.entities.segments.push({
...parsed,
key: entityName,
lastModified: getLastModifiedFromHistory(fullHistory, "segment", entityName),
usedInFeatures: Array.from(segmentsUsedInFeatures[entityName] || []),
});
}
// attributes
const attributeFiles = await datasource.listAttributes();
for (const entityName of attributeFiles) {
const parsed = await datasource.readAttribute(entityName);
result.entities.attributes.push({
...parsed,
key: entityName,
lastModified: getLastModifiedFromHistory(fullHistory, "attribute", entityName),
usedInFeatures: Array.from(attributesUsedInFeatures[entityName] || []),
usedInSegments: Array.from(attributesUsedInSegments[entityName] || []),
});
}
return result;
}