-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (77 loc) · 3.64 KB
/
index.js
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
import { promises as fs } from "fs";
import jsonPatch from 'fast-json-patch';
const readDataFileObjectFromDisk = async filePath => {
let buffer;
try {
buffer = await fs.readFile(filePath);
} catch (e) {
console.log(e);
return;
}
if (buffer.length < 0) {
return;
}
return JSON.parse(buffer.toString());
};
const previousDatafile = await readDataFileObjectFromDisk("./datafile-previous.json");
const newDatafile = await readDataFileObjectFromDisk("./datafile-new.json");
const patches = jsonPatch.compare(previousDatafile, newDatafile);
// console.dir(patches, {depth: null});
const allTargetPaths = patches.map(patch => patch.path.match(/(\/featureFlags\/\d)|(\/rollouts\/\d)|(\/experiments\/\d)|(\/audiences\/\d)/gm)[0]);
// console.dir(allTargetPaths, {depth: null});
const distinctTargetPaths = [...new Set(allTargetPaths)];
// console.dir(distinctTargetPaths, {depth: null});
const distinctTargetObjects = distinctTargetPaths.map(path => {
let foundObject = jsonPatch.getValueByPointer(newDatafile, path);
// if a node was removed then look at previous
if (typeof foundObject === "undefined") {
foundObject = jsonPatch.getValueByPointer(previousDatafile, path);
}
// set the type of object
if (path.startsWith("/featureFlags/")) {
foundObject.type = "flag";
} else if (path.startsWith("/rollouts/")) {
foundObject.type = "rollout";
} else if (path.startsWith("/experiments/")) {
foundObject.type = "experiment";
} else if (path.startsWith("/audiences/")) {
foundObject.type = "audience";
} else {
throw new Error("Unknown JSON Pointer type found");
}
return foundObject;
});
// console.dir(distinctTargetObjects, {depth: null});
const allFeatureFlags = [...newDatafile.featureFlags, ...previousDatafile.featureFlags];
const allRollouts = [...newDatafile.rollouts, ...previousDatafile.rollouts];
const experimentsFromRollouts = allRollouts.map(rollout => [...rollout.experiments]).reduce((accumulator, experiment) => [...accumulator, ...experiment]);
const experimentsFromExperiments = [...newDatafile.experiments, ...previousDatafile.experiments];
const allExperiments = [...experimentsFromRollouts, ...experimentsFromExperiments];
const featureFlagsSearchBy = {
flag: (featureFlag) => {
return [featureFlag];
},
rollout: (rollout) => {
const rolloutId = rollout.id.toString();
const foundFeatureFlags = allFeatureFlags.filter(featureFlag => featureFlag.rolloutId === rolloutId);
return foundFeatureFlags;
},
experiment: (experiment) => {
const experimentId = experiment.id.toString();
const foundFeatureFlags = allFeatureFlags.filter(featureFlag => featureFlag.experimentIds.includes(experimentId));
return foundFeatureFlags;
},
audience: (audience) => {
const audienceId = audience.id.toString();
const experimentsContainingAudienceId = allExperiments.filter(experiment => experiment.audienceIds.includes(audienceId));
const foundFeatureFlags = allFeatureFlags.filter(featureFlag => experimentsContainingAudienceId.find(experiment => featureFlag.experimentIds.includes(experiment.id)));
return foundFeatureFlags;
},
};
const affectedFeatureFlagKeys = distinctTargetObjects.map(target => {
const foundFeatureFlags = featureFlagsSearchBy[target.type](target);
const featureFlagKeys = foundFeatureFlags.map(featureFlag => featureFlag.key);
return featureFlagKeys;
});
const distinctAffectedFeatureFlagKeys = [...new Set(affectedFeatureFlagKeys.filter(key => key.length > 0).flat())];
console.dir(distinctAffectedFeatureFlagKeys);