@blockfrost/blockfrost-cardano-cli
Version:
Drop-in(ish) replacement for cardano-cli powered by Blockfrost
107 lines (106 loc) • 4.46 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseCommand = void 0;
const util_1 = require("util");
const core_1 = require("@oclif/core");
const blockfrost_js_1 = require("@blockfrost/blockfrost-js");
const errors_1 = require("../constants/errors");
const blockfrost_1 = require("../services/blockfrost");
const file_1 = require("../utils/file");
const format_1 = require("../utils/format");
const constants_1 = require("../constants");
class BaseCommand extends core_1.Command {
constructor(argv, config) {
// errors thrown from constructor won't be caught by catch method
super(argv, config);
this.prettyPrint = (data) => {
this.log((0, format_1.stringify)(data));
};
this.run = async () => {
const { flags } = await this.parseBaseCommand();
const result = await this.doWork();
if (flags.json) {
this.log((0, format_1.stringify)(result));
}
else {
this.prettyPrint(result);
}
if (flags['out-file']) {
await this.toFile(result);
}
return this;
};
this.client = null;
}
async handleTestnetMagic() {
const { flags } = await this.parse(BaseCommand);
// set blockfrostNetwork based on testnet-magic flag if necessary
const testnetMagic = flags['testnet-magic'];
if (flags.testnet && !testnetMagic) {
// legacy --testnet used, update --testnet-magic
this.blockfrostNetwork = 'testnet';
const testnetFlagIndex = this.argv.indexOf('--testnet');
this.argv.splice(testnetFlagIndex, 1);
this.argv.push(`--testnet-magic`, `${constants_1.NETWORK_MAGIC.testnet}`);
}
else if (testnetMagic) {
for (const [network, magic] of Object.entries(constants_1.NETWORK_MAGIC)) {
if (magic === testnetMagic) {
// @ts-expect-error NETWORK_MAGIC is indexed by literals
this.blockfrostNetwork = network;
}
}
if (!this.blockfrostNetwork) {
throw new Error(errors_1.ERROR.FLAG_UNSUPPORTED_TESTNET_MAGIC);
}
}
}
async parseBaseCommand() {
await this.handleTestnetMagic();
return this.parse(BaseCommand);
}
async getClient() {
if (!this.client) {
// const { flags } = await this.parseBaseCommand();
this.client = (0, blockfrost_1.createBlockfrostClient)(this.blockfrostNetwork);
}
return this.client;
}
async toFile(data) {
const { flags } = await this.parseBaseCommand();
if (!flags['out-file']) {
throw new Error(errors_1.ERROR.FLAG_MISSING_OUT_FILE);
}
(0, file_1.writeToFile)(flags['out-file'], (0, format_1.stringify)(data));
}
async catch(err) {
if (err instanceof blockfrost_js_1.BlockfrostServerError && err.message.includes('Network token mismatch')) {
const { flags } = await this.parseBaseCommand();
const envVarName = flags.testnet ? constants_1.ENV_VAR_PROJECT_ID.testnet : constants_1.ENV_VAR_PROJECT_ID.mainnet;
this.error((0, util_1.format)('Network token mismatch.\nUse --tesnet for testnet network or check if environment variable %s is set correctly.', envVarName));
}
return super.catch(err);
}
}
exports.BaseCommand = BaseCommand;
// Each command extends this class and can define additional flags
// without strict mode disabled calling 'parseBaseCommand' from within this class would fail because of unknown flags/arguments
BaseCommand.strict = false;
BaseCommand.flags = {
// common flags
help: core_1.Flags.help({ char: 'h' }),
mainnet: core_1.Flags.boolean({
description: 'Cardano Mainnet (default)',
hidden: true,
exclusive: ['testnet', 'testnet-magic'],
}),
testnet: core_1.Flags.boolean({ char: 't', hidden: true, description: 'Cardano Testnet' }),
'testnet-magic': core_1.Flags.integer({
hidden: false,
exclusive: ['testnet'],
}),
json: core_1.Flags.boolean({ description: 'Always outputs json instead of pretty printed table' }),
'out-file': core_1.Flags.string({
description: 'Optional output file. Default is to write to stdout.',
}),
};