-
Notifications
You must be signed in to change notification settings - Fork 25
/
ComponentGenerator.js
74 lines (71 loc) · 2.6 KB
/
ComponentGenerator.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
var fs = require('fs');
var md5 = require('md5');
var THRESHOLD_LENGTH = 80;
/**
* returns the componentMap generated for a given React Native bundle
* and the list of components which needs to be merged to generate
* the bundle on the client end
* @param {string} filePath - filePath of the React Native bundle
*/
function generateComponents(filePath) {
var hashMap = {};
var placeHolderStructure = {};
placeHolderStructure.fileStructure = [];
var placeHolderResponse = generatePlaceHolder(filePath, placeHolderStructure, hashMap);
hashMap = placeHolderResponse.hashMap;
placeHolderStructure = placeHolderResponse.placeHolderStructure;
var componentList = [];
var placeHolderJson = JSON.stringify(placeHolderStructure);
var placeHolderKey = md5(placeHolderJson);
componentList.push(placeHolderKey);
(Object.keys(hashMap)).forEach(function (key) {
componentList.push(key);
});
hashMap[placeHolderKey] = placeHolderJson;
return {
hashMap: hashMap,
componentList: componentList
};
}
/**
* Reads the react native bundle from the filePath, splits it into components and adds them
* into the hashMap and generates the placeHolderStructure
* @param {string} filePath - filePath of the React Native bundle
* @param {{fileStructure-[]}} placeHolderStructure - placeHolderStructure which describes
* the concatenation logic of the components for the bundle
* @param {{}} hashMap - map of all the components that are generated
*/
function generatePlaceHolder(filePath, placeHolderStructure, hashMap) {
var componentList = readJSFile(filePath);
componentList.forEach(function (component) {
var componentMetaModel;
if (component.length < THRESHOLD_LENGTH) {
componentMetaModel = {
text: 'text',
value: component + "\n"
}
} else {
componentMetaModel = {
text: 'component',
value: md5(component)
};
hashMap[md5(component)] = component + "\n";
}
placeHolderStructure.fileStructure.push(componentMetaModel);
});
return {
placeHolderStructure: placeHolderStructure,
hashMap: hashMap
};
}
/**
* This function reads the react native bundle and
* returns an array of string generated from the split of the file
* by the newline character
* @param {string} filePath - file path of the react native bundle
*/
function readJSFile(filePath) {
var jsFile = fs.readFileSync(filePath, { encoding: 'utf-8' });
return jsFile.split(/[\r\n]+/g);
}
module.exports = generateComponents;