@haechi-labs/henesis-cli
Version:
🚀 Command Line Interface tool to Utilize henesis
128 lines • 5.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const command_1 = require("@oclif/command");
const yaml = tslib_1.__importStar(require("js-yaml"));
const fs = tslib_1.__importStar(require("fs"));
const types_1 = require("../../types");
const integration_1 = tslib_1.__importDefault(require("../../rpc/integration"));
const compiler_1 = require("../../compiler");
const types_2 = require("../../types");
const utils_1 = require("../../utils");
const base_1 = tslib_1.__importDefault(require("../../common/base"));
const defaultSpecFile = './henesis.yaml';
async function detectErrors(error, path, compilerVersion, contractName) {
if (error.message &&
error.message.includes('requires different compiler version')) {
const rawError = error.message
.split('less than the released version')[1]
.trimEnd();
return `Compile Error: The henesis is currently using solc '${compilerVersion}',
but your '${contractName}.sol' contract uses different version.
${rawError}`;
}
else if (error.message &&
error.message.includes('ENOENT: no such file or directory')) {
const errorFile = error.message.split("'")[1];
return `No such file or directory: '${errorFile}`;
}
else {
return `compile error: failed to compile '${path}' file.
make sure you are at the correct compiler version or solidity file`;
}
}
async function getAbi(path, compilerVersion, contractName) {
let result;
try {
result = await compiler_1.compileSol(path, {
solcVersion: compilerVersion,
evmVersion: compiler_1.getLatestEvmVersion(compilerVersion),
});
}
catch (error) {
const message = await detectErrors(error, path, compilerVersion, contractName);
throw new Error(message);
}
return result.getAbi(contractName);
}
async function toContractFile(contractFileSpec) {
const abi = await getAbi(contractFileSpec.path, contractFileSpec.compilerVersion, contractFileSpec.contractName);
if (abi === undefined) {
throw new Error(`Your filtered contract does not exist in '${contractFileSpec.path}' file`);
}
return abi;
}
async function toContract(contractSpec) {
utils_1.startWait('Deploying');
if (contractSpec.files === undefined) {
throw new Error(`You need to update henesis.yaml schema. Please refer to https://docs.henesis.io/ or run 'henesis init' again for the new schema`);
}
const abi = utils_1.concatAndDeDuplicate(...(await Promise.all(contractSpec.files.map(async (c) => await toContractFile(c)))));
return new types_2.Contract(contractSpec.name, contractSpec.address, abi);
}
exports.toContract = toContract;
async function toContracts(contractSpecs) {
let contracts = [];
for (let i = 0; i < contractSpecs.length; i++) {
contracts.push(await toContract(contractSpecs[i]));
}
return contracts;
}
async function toCreateIntegrationRequest(spec) {
const contracts = await toContracts(spec.filters.contracts);
if ('timeout' in spec.provider) {
throw new Error(`The timeout value is deprecated. Please refer to https://docs.henesis.io/`);
}
return new types_2.CreateIntegrationRequest(spec.name, spec.version, spec.apiVersion, new types_2.Blockchain(spec.blockchain.platform, spec.blockchain.network, spec.blockchain.threshold), new types_2.Filter(contracts), new types_2.Provider(spec.provider.type, spec.provider.url, spec.provider.method, spec.provider.headers));
}
async function toUpdateIntegrationRequest(spec) {
const contracts = await toContracts(spec.filters.contracts);
if ('timeout' in spec.provider) {
throw new Error(`The timeout value is deprecated. Please refer to https://docs.henesis.io/`);
}
return new types_1.UpdateIntegrationRequest(spec.version, spec.apiVersion, new types_2.Blockchain(spec.blockchain.platform, spec.blockchain.network, spec.blockchain.threshold), new types_2.Filter(contracts), new types_2.Provider(spec.provider.type, spec.provider.url, spec.provider.method, spec.provider.headers));
}
class Deploy extends base_1.default {
async run() {
const { flags } = this.parse(Deploy);
try {
const integrationSpec = yaml.safeLoad(fs.readFileSync(flags.path || defaultSpecFile, 'utf8'));
if (flags.force) {
const integration = await integration_1.default.getIntegrationByName(integrationSpec.name);
await integration_1.default.updateIntegration(integration.integrationId, await toUpdateIntegrationRequest(integrationSpec));
this.log(`${integration.integrationId} has been deployed with force`);
utils_1.endWait();
}
else {
const integration = await integration_1.default.createIntegration(await toCreateIntegrationRequest(integrationSpec));
this.log(`${integration.integrationId} has been deployed`);
utils_1.endWait();
return;
}
}
catch (err) {
//TODO: suggestion to remove > by replacing this.error with console.error
this.error(err.message);
}
}
}
exports.default = Deploy;
Deploy.description = 'deploy a integration';
Deploy.examples = [
`$ henesis integration:deploy my-integration-id-xqxz`,
];
Deploy.flags = {
help: command_1.flags.help({ char: 'h' }),
path: command_1.flags.string({
char: 'p',
description: 'Set where henesis.yaml is located.',
default: defaultSpecFile,
}),
force: command_1.flags.boolean({
char: 'f',
description: 'Erase existing deployed content and deploy current configuration.',
default: false,
}),
};
Deploy.args = [];
//# sourceMappingURL=deploy.js.map