@haechi-labs/henesis-cli
Version:
🚀 Command Line Interface tool to Utilize henesis
103 lines • 3.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const fs = tslib_1.__importStar(require("fs"));
const path = tslib_1.__importStar(require("path"));
const findNodeModules = require('find-node-modules');
const CompilerSupplier = require('./compilerSupplier');
class CompileResult {
constructor(contracts, sources) {
this.contracts = contracts;
this.sources = sources;
}
getAbi(contractName) {
if (typeof this.contracts[contractName] === 'undefined') {
return undefined;
}
return this.contracts[contractName].abi;
}
}
exports.CompileResult = CompileResult;
function findImportPath(filePath) {
// find recursively to find .sol file
if (fs.existsSync(filePath)) {
return filePath;
}
else {
// find .sol file from node_modules
const nodeModules = findNodeModules();
for (let nodeModule of nodeModules) {
const modulePath = path.join(nodeModule, filePath);
if (fs.existsSync(modulePath))
return modulePath;
}
throw new Error(`Module path, ${filePath} is not found`);
}
}
function findImports(filePath) {
return {
contents: fs.readFileSync(findImportPath(filePath), 'utf8'),
};
}
async function getSolc(version) {
const supplier = new CompilerSupplier({
version: version,
});
return supplier.load();
}
function getLatestEvmVersion(compilerVersion) {
let evmVersion = '';
const rawVersions = compilerVersion.split('.');
const versions = [
parseInt(rawVersions[0]),
parseInt(rawVersions[1]),
parseInt(rawVersions[2].split('-')[0]),
];
// <= 0.5.4: byzantium
if ((versions[1] === 5 && versions[2] <= 4) || versions[1] < 5) {
evmVersion = 'byzantium';
}
else {
evmVersion = 'petersburg';
}
return evmVersion;
}
exports.getLatestEvmVersion = getLatestEvmVersion;
const compile = async (source, option) => {
const solc = await getSolc(option.solcVersion);
return JSON.parse(solc.compile(JSON.stringify({
language: 'Solidity',
sources: source,
settings: {
evmVersion: option.evmVersion,
optimizer: option.optimizer,
outputSelection: {
'*': {
'*': ['*'],
},
},
},
}), findImports));
};
exports.compileSol = async (file, option) => {
if (!file.endsWith('.sol')) {
throw new Error('it is not a solidity file.');
}
const output = await compile(Object.assign({}, {
[file]: { content: fs.readFileSync(file, 'utf8') },
}), option);
if (output.errors) {
let errMsg = '';
for (let i = 0; i < output.errors.length; i++) {
if (output.errors[i].severity === 'warning') {
continue;
}
errMsg = errMsg + (output.errors[i].formattedMessage + '\n');
}
if (errMsg !== '') {
throw new Error(`Compilation Error! ${errMsg}`);
}
}
return new CompileResult(output.contracts[file], output.sources[file]);
};
//# sourceMappingURL=solc.js.map