in cli-functions.js [123:154]
export async function compileContract(source, type = 'path') {
let sourcePath = source;
let outputPath = '';
if (type === 'data') {
const uniqueId = crypto.randomUUID();
const sourceFilename = path.join(os.tmpdir(), `contract-${uniqueId}.sol`);
outputPath = path.join(compiledContractsDir, `contract-${uniqueId}.json`);
fs.writeFileSync(sourceFilename, source);
sourcePath = sourceFilename;
} else {
outputPath = path.join(compiledContractsDir, 'MyContract.json');
}
const command = 'rescontract';
const args = ['compile', '--sol', sourcePath, '--output', outputPath];
try {
const result = await handleExecFile(command, args);
if (type === 'data') {
return path.basename(outputPath);
}
const successMessage = result.match(/Compiled successfully to (\/[^\s]+)/)[0];
return successMessage;
} catch (error) {
throw error;
} finally {
if (type === 'data') {
if (fs.existsSync(sourcePath)) fs.unlinkSync(sourcePath);
}
}
}