/
compile.js
62 lines (45 loc) · 1.66 KB
/
compile.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
const dirTree = require("directory-tree");
const fs = require('fs');
const { execSync } = require('child_process');
function flatDirTree(tree) {
return tree.children.reduce((acc, current) => {
if (current.children === undefined) {
return [
...acc,
current,
];
}
const flatChild = flatDirTree(current);
return [...acc, ...flatChild];
}, []);
}
// Compile all the sources
// - Get all .sol sources
const contractsNestedTree = dirTree(
"contracts",
{ extensions: /\.sol/ }
);
const contractsTree = flatDirTree(contractsNestedTree);
console.log(`Compiling ${contractsTree.length} sources`);
// - Create build if not exists
if (!fs.existsSync('build')){
fs.mkdirSync('build');
}
try {
// - Prepare each source
contractsTree.map(({ path }) => {
console.debug(`Compile ${path}`);
const [,contractFileName] = path.match(new RegExp('contracts/(.*).sol'));
const output = execSync(`cd build && solc-ton ./../${path}`);
if (output.toString() === '') {
// No code was compiled, probably interface compilation
return;
}
const contractNameNoFolderStructure = contractFileName.split('/')[contractFileName.split('/').length - 1];
const tvmLinkerLog = execSync(`cd build && tvm_linker compile "${contractNameNoFolderStructure}.code" -a "${contractNameNoFolderStructure}.abi.json"`);
const [,tvcFile] = tvmLinkerLog.toString().match(new RegExp('Saved contract to file (.*)'));
execSync(`cd build && base64 < ${tvcFile} > ${contractNameNoFolderStructure}.base64`);
execSync(`cd build && mv ${tvcFile} ${contractNameNoFolderStructure}.tvc`);
});
} catch (e) {
}