-
Notifications
You must be signed in to change notification settings - Fork 1
/
flattener.js
executable file
·50 lines (46 loc) · 1.46 KB
/
flattener.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
#!/usr/bin/env node
/* global Promise, require, __dirname */
const {spawn} = require('child_process');
const path = require('path');
const fs = require('fs');
async function flatten(contractSource) {
return new Promise((resolv, reject) => {
const stdout = [];
const stderr = [];
const flattener = path.join(__dirname, 'flattener.py')
const python = spawn('python3', [flattener, contractSource]);
python.on('exit', (code) => {
if (stderr.length > 0 || code !== 0) {
reject("error in child : " + code + stderr.join(""));
return;
}
resolv(stdout.join(""))
});
python.stdout.on('data', (data) => {
stdout.push(data);
});
python.stderr.on('data', (data) => {
stderr.push(data);
});
python.stdin.setEncoding('utf-8');
// python.stdin.write("console.log('Hello from PhantomJS')\n");
python.stdin.end();
})
}
if (module === require.main) {
if (process.argv.length !== 3) {
console.error(`Usage: ${path.parse(process.argv[1]).base} contract_name.sol`)
return 1;
}
const filePath = path.normalize(process.argv[2]);
if (!fs.existsSync(filePath)) {
console.error("Invalid file name", filePath)
return 2;
}
flatten(filePath)
.then(source => console.log(source))
.catch(err => console.error(err));
}
module.exports = {
flatten
}