UNPKG

@hashgraph/hedera-local

Version:

Developer tooling for running Local Hedera Network (Consensus + Mirror Nodes).

513 lines 17.9 kB
"use strict"; // SPDX-License-Identifier: Apache-2.0 Object.defineProperty(exports, "__esModule", { value: true }); exports.CLIService = void 0; const VerboseLevel_1 = require("../types/VerboseLevel"); const LoggerService_1 = require("./LoggerService"); const ServiceLocator_1 = require("./ServiceLocator"); const FileSystemUtils_1 = require("../utils/FileSystemUtils"); /** * Class representing the CLI service. * @implements {IService} */ class CLIService { /** * Get the verbose level. * @returns {string} The verbose level. */ get verboseLevel() { var _a; return (_a = this.currentArgv) === null || _a === void 0 ? void 0 : _a.verboseLevel; } /** * Create a CLI service. * @param {yargs.ArgumentsCamelCase<{}>} argv - The command line arguments. */ constructor(argv) { this.serviceName = CLIService.name; this.setCurrentArgv(argv); this.logger = ServiceLocator_1.ServiceLocator.Current.get(LoggerService_1.LoggerService.name); this.logger.trace('CLI Service Initialized!', this.serviceName); } /** * Loads the startup options for the CLI service. * @param {yargs.Argv<{}>} yargs - The yargs instance. */ static loadStartupOptions(yargs) { CLIService.loadCommonOptions(yargs); CLIService.loadAccountOptions(yargs, true); CLIService.hostOption(yargs); CLIService.rateLimitOption(yargs); CLIService.devModeOption(yargs); CLIService.fullModeOption(yargs); CLIService.enableBlockNode(yargs); CLIService.multiNodeOption(yargs); CLIService.userComposeOption(yargs); CLIService.userComposeDirOption(yargs); CLIService.blocklistingOption(yargs); CLIService.selectNetworkTag(yargs); CLIService.selectMirrorTag(yargs); CLIService.selectRelayTag(yargs); CLIService.selectBlockNodeTag(yargs); CLIService.createInitialResources(yargs); } /** * Loads the account options for the CLI. * @param {yargs.Argv<{}>} yargs - The yargs instance. * @param {boolean} skipCommon - Whether to skip loading common options. */ static loadAccountOptions(yargs, skipCommon = false) { if (!skipCommon) CLIService.loadCommonOptions(yargs); CLIService.accountOption(yargs); CLIService.asyncOption(yargs); CLIService.balanceOption(yargs); CLIService.hostOption(yargs); } /** * Loads the stop options for the CLI. * @param {yargs.Argv<{}>} yargs - The yargs instance. */ static loadStopOptions(yargs) { CLIService.loadCommonOptions(yargs); } /** * Loads the generate accounts options for the CLI. * @param {yargs.Argv<{}>} yargs - The yargs instance. */ static loadCommonOptions(yargs) { CLIService.verboseLevelOption(yargs); CLIService.workDirOption(yargs); } /** * Get the current command line arguments. * @returns {CLIOptions} The current command line arguments. * @public */ getCurrentArgv() { const argv = this.currentArgv; const accounts = argv.accounts; const async = argv.async; const balance = argv.balance; const host = argv.host; const limits = argv.limits; const devMode = argv.dev; const fullMode = argv.full; const blockNode = argv.enableBlockNode; const multiNode = argv.multinode; const userCompose = argv.usercompose; const userComposeDir = argv.composedir; const blocklisting = argv.blocklist; const startup = argv.startup; const verbose = CLIService.resolveVerboseLevel(argv.verbose); const networkTag = argv.networkTag; const mirrorTag = argv.mirrorTag; const relayTag = argv.relayTag; const blockNodeTag = argv.blockNodeTag; const workDir = FileSystemUtils_1.FileSystemUtils.parseWorkDir(argv.workdir); const createInitialResources = argv.createInitialResources; const persistTransactionBytes = argv.persistTransactionBytes; const currentArgv = { accounts, async, balance, host, limits, devMode, fullMode, blockNode, multiNode, userCompose, userComposeDir, blocklisting, startup, verbose, networkTag, mirrorTag, relayTag, blockNodeTag, workDir, createInitialResources, persistTransactionBytes, }; return currentArgv; } /** * Set the current command line arguments. * @param {yargs.ArgumentsCamelCase<{}>} argv - The current command line arguments. * @public */ setCurrentArgv(argv) { const state = argv._[0]; this.currentArgv = Object.assign(Object.assign({}, argv), { startup: CLIService.isStartup(state) }); } /** * Checks if the given state represents a startup operation. * @param {string} state - The state to check. * @returns {boolean} True if the state represents a startup operation, false otherwise. * @private */ static isStartup(state) { switch (state) { case 'start': return true; case 'restart': return true; case 'stop': return false; case 'generate-accounts': return false; default: return true; } ; } /** * Adds the 'accounts' option to the command line arguments. * This option specifies the number of generated accounts of each type. * It defaults to 10. * * @param {yargs.Argv<{}>} yargs - The yargs instance to which the option is added. * @private * @static */ static accountOption(yargs) { yargs.positional('accounts', { describe: 'Generated accounts of each type.', default: 10 }); } /** * Adds the 'host' option to the command line arguments. * This option is a string that specifies the host to run the local node with. * It is not required and defaults to '127.0.0.1'. * The option can also be set using the alias 'h'. * * @param {yargs.Argv<{}>} yargs - The yargs instance to which the option is added. * @private * @static */ static hostOption(yargs) { yargs.option('host', { alias: 'h', type: 'string', describe: 'Run the local node with host', demandOption: false, default: '127.0.0.1' }); } /** * Adds the 'limits' option to the command line arguments. * This option is a boolean that enables or disables the rate limits in the JSON-RPC relay. * It is not required and defaults to false. * The option can also be set using the alias 'l'. * * @param {yargs.Argv<{}>} yargs - The yargs instance to which the option is added. * @private * @static */ static rateLimitOption(yargs) { yargs.option('limits', { alias: 'l', type: 'boolean', describe: 'Enable or disable the rate limits in the JSON-RPC relay', demandOption: false, default: false }); } /** * Adds the 'dev' option to the command line arguments. * This option is a boolean that enables or disables developer mode. * It is not required and defaults to false. * * @param {yargs.Argv<{}>} yargs - The yargs instance to which the option is added. * @private * @static */ static devModeOption(yargs) { yargs.option('dev', { type: 'boolean', describe: 'Enable or disable developer mode', demandOption: false, default: false }); } /** * Adds the 'block-node' option to the command line arguments. * This option is a boolean that enables or disables block-node. * It is not required and defaults to false. * * @param {yargs.Argv<{}>} yargs - The yargs instance to which the option is added. * @private * @static */ static enableBlockNode(yargs) { yargs.option('enable-block-node', { type: 'boolean', describe: 'Enable or disable block-node', demandOption: false, default: false }); } /** * Adds the 'full' option to the command line arguments. * This option is a boolean that enables or disables full mode for a production local-node. * It is not required and defaults to false. * * @param {yargs.Argv<{}>} yargs - The yargs instance to which the option is added. * @private * @static */ static fullModeOption(yargs) { yargs.option('full', { type: 'boolean', describe: 'Enable or disable full mode. Production local-node.', demandOption: false, default: false }); } /** * Adds the 'multinode' option to the command line arguments. * This option is a boolean that enables or disables multi-node mode. * It is not required and defaults to false. * * @param {yargs.Argv<{}>} yargs - The yargs instance to which the option is added. * @private * @static */ static multiNodeOption(yargs) { yargs.option('multinode', { type: 'boolean', describe: 'Enable or disable multi-node mode.', demandOption: false, default: false }); } /** * Adds the 'balance' option to the command line arguments. * This option is a number that sets the starting balance of the created accounts in HBAR. * It is not required and defaults to 10000. * * @param {yargs.Argv<{}>} yargs - The yargs instance to which the option is added. * @private * @static */ static balanceOption(yargs) { yargs.option('balance', { type: 'number', describe: 'Set starting balance of the created accounts in HBAR', demandOption: false, default: 10000 }); } /** * Adds the 'async' option to the command line arguments. * This option is a boolean that enables or disables asynchronous creation of accounts. * It is not required and defaults to false. * The option can also be set using the alias 'a'. * * @param {yargs.Argv<{}>} yargs - The yargs instance to which the option is added. * @private * @static */ static asyncOption(yargs) { yargs.option('async', { alias: 'a', type: 'boolean', describe: 'Enable or disable asynchronous creation of accounts', demandOption: false, default: false }); } /** * Adds the 'usercompose' option to the command line arguments. * This option is a boolean that enables or disables user Compose configuration files. * It is not required and defaults to true. * * @param {yargs.Argv<{}>} yargs - The yargs instance to which the option is added. * @private * @static */ static userComposeOption(yargs) { yargs.option('usercompose', { type: 'boolean', describe: 'Enable or disable user Compose configuration files', demandOption: false, default: true }); } /** * Adds the 'composedir' option to the command line arguments. * This option is a string that specifies the path to a directory with user Compose configuration files. * It is not required and defaults to './overrides/'. * * @param {yargs.Argv<{}>} yargs - The yargs instance to which the option is added. * @private * @static */ static userComposeDirOption(yargs) { yargs.option('composedir', { type: 'string', describe: 'Path to a directory with user Compose configuration files', demandOption: false, default: './overrides/' }); } static workDirOption(yargs) { yargs.option('workdir', { type: 'string', describe: 'Path to the working directory for local node', demandOption: false, default: FileSystemUtils_1.FileSystemUtils.getPlatformSpecificAppDataPath('hedera-local') }); } /** * Adds the 'blocklist' option to the command line arguments. * This option is a boolean that enables or disables blocklisting of accounts. * It is not required and defaults to false. * The option can also be set using the alias 'b'. * * @param {yargs.Argv<{}>} yargs - The yargs instance to which the option is added. * @private * @static */ static blocklistingOption(yargs) { yargs.option('blocklist', { alias: 'b', type: 'boolean', describe: 'Enable or disable blocklisting accounts', demandOption: false, default: false }); } /** * Adds the 'verbose' option to the command line arguments. * This option is a string that sets the verbose level. * It is not required and defaults to 'info'. * The valid choices for this option are 'info' and 'trace'. * * @param {yargs.Argv<{}>} yargs - The yargs instance to which the option is added. * @public * @static */ static verboseLevelOption(yargs) { yargs.option('verbose', { type: 'string', describe: 'Set the verbose level', demandOption: false, choices: ['silent', 'error', 'warning', 'info', 'debug', 'trace'], default: 'info', }); } /** * Adds the 'network-tag' option to the command line arguments. * This option is a string that enables usage of custom network tag. * It is not required and defaults to predefined configuration. * * @param {yargs.Argv<{}>} yargs - The yargs instance to which the option is added. * @private * @static */ static selectNetworkTag(yargs) { yargs.option('network-tag', { type: 'string', describe: 'Select custom network node tag', demandOption: false, default: '' }); } /** * Adds the 'mirror-tag' option to the command line arguments. * This option is a string that enables usage of custom mirror-node tag. * It is not required and defaults to predefined configuration. * * @param {yargs.Argv<{}>} yargs - The yargs instance to which the option is added. * @private * @static */ static selectMirrorTag(yargs) { yargs.option('mirror-tag', { type: 'string', describe: 'Select custom mirror-node tag', demandOption: false, default: '' }); } /** * Adds the 'block-node-tag' option to the command line arguments. * This option is a string that enables usage of custom block-node tag. * It is not required and defaults to predefined configuration. * * @param {yargs.Argv<{}>} yargs - The yargs instance to which the option is added. * @private * @static */ static selectBlockNodeTag(yargs) { yargs.option('block-node-tag', { type: 'string', describe: 'Select custom block-node tag', demandOption: false, default: '' }); } /** * Adds the 'mirror-tag' option to the command line arguments. * This option is a string that enables usage of custom mirror-node tag. * It is not required and defaults to predefined configuration. * * @param {yargs.Argv<{}>} yargs - The yargs instance to which the option is added. * @private * @static */ static selectRelayTag(yargs) { yargs.option('relay-tag', { type: 'string', describe: 'Select custom hedera-json-rpc relay tag', demandOption: false, default: '' }); } /** * Adds the 'create-initial-resources' option to the command line arguments. * This option is a boolean that enables or disables creation of initial resources. * It is not required and defaults to false. * * @param {yargs.Argv<{}>} yargs - The yargs instance to which the option is added. * @private * @static */ static createInitialResources(yargs) { yargs.option('create-initial-resources', { type: 'boolean', describe: 'Enable or disable creation of initial resources', demandOption: false, default: false }); } /** * Resolve the verbose level from a string. * @param {string} level - The verbose level as a string. * @returns {VerboseLevel} The verbose level. * @public */ static resolveVerboseLevel(level) { switch (level) { case 'silent': return VerboseLevel_1.VerboseLevel.SILENT; case 'error': return VerboseLevel_1.VerboseLevel.ERROR; case 'warning': return VerboseLevel_1.VerboseLevel.WARNING; case 'info': return VerboseLevel_1.VerboseLevel.INFO; case 'debug': return VerboseLevel_1.VerboseLevel.DEBUG; case 'trace': return VerboseLevel_1.VerboseLevel.TRACE; default: return VerboseLevel_1.VerboseLevel.INFO; } } } exports.CLIService = CLIService; //# sourceMappingURL=CLIService.js.map