-
Notifications
You must be signed in to change notification settings - Fork 1
/
hardhat.js
80 lines (68 loc) · 2.71 KB
/
hardhat.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
const { exec } = require('child_process');
const { v4: uuidv4 } = require('uuid');
const path = require('path');
const fs = require('fs');
const { hasYarn, hasPnpm } = require('./utils');
async function hardhat_build_json(project_dir) {
let promise = new Promise((resolve, reject) => {
try {
console.log('Running hardhat compile');
let process;
exec(`cd ${project_dir}`, (error, stdout) => {
if (error) {
console.error(`exec error: ${error}`);
return;
}
const currentDir = stdout.trim();
if (hasYarn(currentDir)) {
process = exec(`yarn && yarn hardhat compile`);
console.log('This project uses yarn');
} else if (hasPnpm(currentDir)) {
process = exec(`pnpm i && pnpm hardhat compile`);
console.log('This project uses pnpm');
} else {
process = exec(`npm i && npx hardhat compile`);
console.log('This project uses npm');
}
process.stdout.on('data', (data) => {
console.info(data.toString());
});
process.stderr.on('data', (data) => {
console.log(data.toString());
});
process.on('exit', (code) => {
console.log('Hardhat compile finished');
let build_info_dir = path.join(project_dir, 'artifacts/build-info');
if (!fs.existsSync(build_info_dir)) {
fs.mkdirSync(build_info_dir);
}
let files = fs.readdirSync(build_info_dir);
let contents = [];
for (let file of files) {
if (!file.endsWith('.json')) {
continue;
}
let fp = path.join(build_info_dir, file);
let content = fs.readFileSync(fp, 'utf8');
content = JSON.parse(content);
// remove key "output"
delete content['output'];
contents.push(content);
}
resolve({
success: true,
contents
});
});
});
} catch (err) {
console.log(err);
resolve({
success: false,
err: err.stderr.toString()
});
}
});
return await promise;
}
module.exports = { hardhat_build_json };