forked from ayanworks/polygon-did-registry-contract
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.js
112 lines (95 loc) · 3.04 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
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
111
112
const path = require('path');
const fs = require('fs-extra');
const solc = require('solc');
const ethers = require('ethers');
const sourceFolderPath = path.resolve(__dirname, 'contracts');
const buildFolderPath = path.resolve(__dirname, 'build');
const lastSourceHashFilePath = path.resolve(__dirname, 'sst-config.json');
const getContractSource = contractFileName => {
const contractPath = path.resolve(__dirname, 'contracts', contractFileName);
const source = fs.readFileSync(contractPath, 'utf8');
return source;
};
let sources = {};
fs.readdirSync(sourceFolderPath).forEach(contractFileName => {
sources = {
...sources,
[contractFileName]: {
content: getContractSource(contractFileName)
}
}
});
// console.log(sources);
function convertToHex(inputString) {
var hex = '';
for(var i=0;i<inputString.length;i++) {
hex += ''+inputString.charCodeAt(i).toString(16);
}
return hex;
}
const sourceHash = ethers.utils.sha256('0x'+convertToHex(JSON.stringify(sources)));
console.log('\n'.repeat(process.stdout.rows));
if(fs.existsSync(buildFolderPath)
&& fs.existsSync(lastSourceHashFilePath)
&& (JSON.parse(fs.readFileSync(lastSourceHashFilePath, 'utf8'))).sourceHash === sourceHash) {
console.log('No changes in .sol files detected... \nSkiping compile script...\n');
} else {
// write the source hash there at the end of
// console.log(lastSourceHash,sourceHash);
const input = {
language: 'Solidity',
sources,
settings: {
outputSelection: {
'*': {
'*': [ '*' ]
}
}
}
}
console.log('Compiling contracts...');
const output = JSON.parse(solc.compile(JSON.stringify(input)));
console.log('Done');
let shouldBuild = true;
if (output.errors) {
// console.error(output.errors);
for(error of output.errors) {
console.log('-'.repeat(process.stdout.columns));
console.group(error.severity.toUpperCase());
console.log(error.formattedMessage);
console.groupEnd();
}
if(Object.values(output.errors).length) console.log('-'.repeat(process.stdout.columns));
// throw '\nError in compilation please check the contract\n';
for(error of output.errors) {
if(error.severity === 'error') {
shouldBuild = false;
throw 'Error found\n';
break;
}
}
}
if(shouldBuild) {
console.log('\nBuilding please wait...');
fs.removeSync(buildFolderPath);
fs.ensureDirSync(buildFolderPath);
let i = 0;
for (let contractFile in output.contracts) {
for(let key in output.contracts[contractFile]) {
//console.log(key, Object.keys(output.contracts[contractFile][key]));
fs.outputJsonSync(
path.resolve(buildFolderPath, `${contractFile.split('.')[0]}_${key}.json`),
output.contracts[contractFile][key]
);
}
i++;
}
console.log('Build finished successfully!\n');
} else {
console.log('\nBuild failed\n');
}
fs.outputJsonSync(
path.resolve(lastSourceHashFilePath),
{ sourceHash }
);
}