-
Notifications
You must be signed in to change notification settings - Fork 25
/
UpdateGraphGenerator.js
110 lines (104 loc) · 4.63 KB
/
UpdateGraphGenerator.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
var fs = require('fs-extra');
var componentGenerator = require('./ComponentGenerator');
var generatedUpdatePatch = {};
var hashMap = {};
/**
* Generates the update patch for the given release config
* @param configFilePath - releaseconfig generated after generation of React Native Bundles
* @param existingPatchPath - updateGraphs already present in production
* @param updateGraphVersion - version number for the new update graph
*/
function generateUpdatePatch(configFilePath, existingPatchPath, updateGraphVersion) {
var configJson = fs.readFileSync(configFilePath, { encoding: 'utf-8' });
var config = JSON.parse(configJson);
if (config.appConfig) {
(Object.keys(config.appConfig)).forEach(function (appVersion) {
var updateGraphConfig = config.appConfig[appVersion];
console.log('Generating update patch for appVersion: ', appVersion);
generateUpdateGraph(updateGraphConfig, existingPatchPath, appVersion, updateGraphVersion);
})
}
return {
componentMap: hashMap,
updatePatch: generatedUpdatePatch
}
}
/**
* generates update graph for a given appversion and inserts it
* in the generatedUpdatePatch corresponding to the appVersion
* @param updateGraphConfig - config for the given appVersion
* @param existingPatchPath - updateGraphs already present in production
* @param appVersion
* @param updateGraphVersion - version number for the new update graph
*/
function generateUpdateGraph(updateGraphConfig, existingPatchPath, appVersion, updateGraphVersion) {
var updateGraph = getExistingUpdateGraphForAppVersion(appVersion, existingPatchPath);
console.log('existing update graph', JSON.stringify(updateGraph));
var newUpdateGraph = {};
newUpdateGraph.currentUpdateGraph = sanitizeUpdateGraph(updateGraph.currentUpdateGraph, Object.keys(updateGraphConfig.currentUpdateGraph));
console.log('santized update graph', JSON.stringify(newUpdateGraph));
newUpdateGraph = generateUpdateGraphForAppVersion(updateGraphConfig, newUpdateGraph);
console.log('generated update graph', JSON.stringify(newUpdateGraph));
newUpdateGraph.currentUpdateGraphVersion = updateGraphVersion;
generatedUpdatePatch[appVersion] = newUpdateGraph;
}
/**
*
* @param appVersion - appVersion for which update graph is required
* @param existingPatchPath - filePath for update patches in production
*/
function getExistingUpdateGraphForAppVersion(appVersion, existingPatchPath) {
var existingPatch = getExistingUpdatePatch(existingPatchPath);
return Object.assign({}, existingPatch[appVersion]);
}
/**
* gets update patches present in production from the filePath
* @param existingPatchPath - filePath for update patches in production
* @returns {{production update patches}}
*/
function getExistingUpdatePatch(existingPatchPath) {
var existingPatch = {};
if (existingPatchPath && existingPatchPath !== '' && fs.pathExistsSync(existingPatchPath)) {
var existingPatchJSON = fs.readFileSync(existingPatchPath, { encoding: 'utf-8' });
if (existingPatchJSON !== '') {
existingPatch = JSON.stringify(existingPatchJSON);
}
}
return existingPatch;
}
/**
* Removes all bundles in production which are not to be included in the
* new update graph
* @param updateGraph - existing update graph
* @param bundleNames - bundle names to be included in the new update graph
* @returns {{}}
*/
function sanitizeUpdateGraph(updateGraph, bundleNames) {
var sanitizedUpdateGraph = {};
if (updateGraph) {
bundleNames.forEach(function (bundleName) {
if (updateGraph[bundleName]) {
sanitizedUpdateGraph[bundleName] = updateGraph[bundleName];
}
})
}
return sanitizedUpdateGraph;
}
/**
* generates update graph and components for an app version config and adds components to the componentMap
* @param updateGraphConfig - updategraph config for a particular appVersion
* @param productionUpdateGraph - updategraph already existing in production
* @returns {generated update graph}
*/
function generateUpdateGraphForAppVersion(updateGraphConfig, productionUpdateGraph) {
(Object.keys(updateGraphConfig.currentUpdateGraph)).forEach(function (bundleName) {
var screenConfig = updateGraphConfig.currentUpdateGraph[bundleName];
if (screenConfig.shouldDeploy) {
var components = componentGenerator(screenConfig.bundlePath);
productionUpdateGraph.currentUpdateGraph[bundleName] = components.componentList;
hashMap = Object.assign({}, hashMap, components.hashMap);
}
});
return productionUpdateGraph;
}
module.exports = generateUpdatePatch;