forked from OpenST/openst.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webPreBuild.js
91 lines (78 loc) · 2.61 KB
/
webPreBuild.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
#!/usr/bin/env node
const fs = require('fs'),
rootPrefix = '.',
paths = ['/contracts/abi', '/contracts/bin'],
readableFile = rootPrefix + '/utils/AbiBinProvider.js',
writableFile = rootPrefix + '/tmp/AbiBinProvider.js',
protoToAddData = 'AbiBinProvider.prototype',
addAbiFuncName = 'addABI',
addBinFuncName = 'addBIN';
let fileInsertContents = [];
function prepareData(option) {
var fileContent = null,
dirFilePath = null,
readFilePath = null,
option = option || 'utf8',
insertContent = null;
paths.forEach(function(path) {
dirFilePath = rootPrefix + path;
fs.readdir(dirFilePath, function(err, items) {
items.forEach(function(item) {
readFilePath = rootPrefix + path + '/' + item;
fileContent = fs.readFileSync(readFilePath, option);
try {
JSON.parse(fileContent);
} catch (e) {
fileContent = '"' + fileContent + '"';
}
createAddFun(item, fileContent);
});
});
});
}
function createAddFun(item, fileContent) {
const separator = '.',
splitted = item.split(separator),
name = "'" + splitted[0] + "'",
fileType = splitted[1],
openBracket = '(',
closeBracket = ')',
paramsSeparator = ' , ',
funcName = fileType == 'abi' ? addAbiFuncName : addBinFuncName,
content = protoToAddData + separator + funcName + openBracket + name + paramsSeparator + fileContent + closeBracket;
fileInsertContents.push(content);
}
function writeToFile() {
fs.readFile(readableFile, 'utf8', function(err, data) {
const startDelimiter = '//__WEB_SAFE_SPACE_BEGINS__',
endDelimiter = '//__WEB_SAFE_SPACE_ENDS__',
newline = '\r\n',
regexPattern = startDelimiter + '.*?' + endDelimiter,
semicolon = ';';
let replaceRegex = new RegExp(regexPattern, 's');
if (!replaceRegex.test(data)) {
throw ' -- Error in prescript content replace, someone changed the comments ' +
startDelimiter +
' ' +
endDelimiter;
}
let fileContent =
startDelimiter + newline + fileInsertContents.join(semicolon + newline) + semicolon + newline + endDelimiter,
result = data.replace(replaceRegex, fileContent);
fs.writeFileSync(writableFile, result);
});
}
function appendToFile() {
fs.readFile(readableFile, 'utf8', function(err, data) {
const newline = '\r\n';
let semicolon = ';',
extraContent = newline + fileInsertContents.join(semicolon + newline) + semicolon,
result = data + newline + extraContent;
fs.writeFileSync(writableFile, result);
});
}
function modifyFile() {
prepareData();
appendToFile();
}
modifyFile();