Skip to content

Commit

Permalink
feat: adding some type check (#217)
Browse files Browse the repository at this point in the history
Signed-off-by: Jeffrey Tang <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
JeffreyDallas and github-actions[bot] authored Apr 12, 2024
1 parent 07f5fe4 commit d055496
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/commands/account.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { flags } from './index.mjs'
import { Listr } from 'listr2'
import * as prompts from './prompts.mjs'
import { constants } from '../core/index.mjs'
import { HbarUnit, PrivateKey } from '@hashgraph/sdk'
import { AccountInfo, HbarUnit, PrivateKey } from '@hashgraph/sdk'

export class AccountCommand extends BaseCommand {
constructor (opts, systemAccounts = constants.SYSTEM_ACCOUNTS) {
Expand All @@ -39,6 +39,8 @@ export class AccountCommand extends BaseCommand {
}

async buildAccountInfo (accountInfo, namespace, shouldRetrievePrivateKey) {
if (!accountInfo || !(accountInfo instanceof AccountInfo)) throw new IllegalArgumentError('An instance of AccountInfo is required')

const newAccountInfo = {
accountId: accountInfo.accountId.toString(),
publicKey: accountInfo.key.toString(),
Expand Down Expand Up @@ -399,6 +401,9 @@ export class AccountCommand extends BaseCommand {
* @param accountCmd an instance of NodeCommand
*/
static getCommandDefinition (accountCmd) {
if (!accountCmd | !(accountCmd instanceof AccountCommand)) {
throw new IllegalArgumentError('An instance of AccountCommand is required', accountCmd)
}
return {
command: 'account',
desc: 'Manage Hedera accounts in fullstack testing network',
Expand Down
5 changes: 4 additions & 1 deletion src/commands/cluster.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
import { ListrEnquirerPromptAdapter } from '@listr2/prompt-adapter-enquirer'
import { Listr } from 'listr2'
import { FullstackTestingError } from '../core/errors.mjs'
import { FullstackTestingError, IllegalArgumentError } from '../core/errors.mjs'
import * as flags from './flags.mjs'
import { BaseCommand } from './base.mjs'
import chalk from 'chalk'
Expand Down Expand Up @@ -220,6 +220,9 @@ export class ClusterCommand extends BaseCommand {
* @param clusterCmd an instance of ClusterCommand
*/
static getCommandDefinition (clusterCmd) {
if (!clusterCmd || !(clusterCmd instanceof ClusterCommand)) {
throw new IllegalArgumentError('Invalid ClusterCommand instance')
}
return {
command: 'cluster',
desc: 'Manage fullstack testing cluster',
Expand Down
5 changes: 4 additions & 1 deletion src/commands/init.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { BaseCommand } from './base.mjs'
import * as core from '../core/index.mjs'
import { constants } from '../core/index.mjs'
import * as fs from 'fs'
import { FullstackTestingError } from '../core/errors.mjs'
import { FullstackTestingError, IllegalArgumentError } from '../core/errors.mjs'
import * as flags from './flags.mjs'
import chalk from 'chalk'

Expand Down Expand Up @@ -144,6 +144,9 @@ export class InitCommand extends BaseCommand {
* @param initCmd an instance of InitCommand
*/
static getCommandDefinition (initCmd) {
if (!initCmd || !(initCmd instanceof InitCommand)) {
throw new IllegalArgumentError('Invalid InitCommand')
}
return {
command: 'init',
desc: 'Initialize local environment and default flags',
Expand Down
3 changes: 3 additions & 0 deletions src/commands/mirror_node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ export class MirrorNodeCommand extends BaseCommand {
* @param mirrorNodeCmd an instance of NodeCommand
*/
static getCommandDefinition (mirrorNodeCmd) {
if (!mirrorNodeCmd || !(mirrorNodeCmd instanceof MirrorNodeCommand)) {
throw new IllegalArgumentError('Invalid MirrorNodeCommand instance', mirrorNodeCmd)
}
return {
command: 'mirror-node',
desc: 'Manage Hedera Mirror Node in fullstack testing network',
Expand Down
5 changes: 4 additions & 1 deletion src/commands/network.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { ListrEnquirerPromptAdapter } from '@listr2/prompt-adapter-enquirer'
import chalk from 'chalk'
import { Listr } from 'listr2'
import { FullstackTestingError, MissingArgumentError } from '../core/errors.mjs'
import { FullstackTestingError, IllegalArgumentError, MissingArgumentError } from '../core/errors.mjs'
import { BaseCommand } from './base.mjs'
import * as flags from './flags.mjs'
import { constants } from '../core/index.mjs'
Expand Down Expand Up @@ -393,6 +393,9 @@ export class NetworkCommand extends BaseCommand {
}

static getCommandDefinition (networkCmd) {
if (!networkCmd || !(networkCmd instanceof NetworkCommand)) {
throw new IllegalArgumentError('An instance of NetworkCommand is required', networkCmd)
}
return {
command: 'network',
desc: 'Manage fullstack testing network deployment',
Expand Down
10 changes: 10 additions & 0 deletions src/commands/node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ export class NodeCommand extends BaseCommand {
* @private
*/
_nodeGossipKeysTaskList (keyFormat, nodeIds, keysDir, curDate = new Date()) {
if (!Array.isArray(nodeIds) || !nodeIds.every((nodeId) => typeof nodeId === 'string')) {
throw new IllegalArgumentError('nodeIds must be an array of strings')
}
const self = this
const subTasks = []

Expand Down Expand Up @@ -276,6 +279,10 @@ export class NodeCommand extends BaseCommand {
* @private
*/
_nodeTlsKeyTaskList (nodeIds, keysDir, curDate = new Date()) {
// check if nodeIds is an array of strings
if (!Array.isArray(nodeIds) || !nodeIds.every((nodeId) => typeof nodeId === 'string')) {
throw new FullstackTestingError('nodeIds must be an array of strings')
}
const self = this
const nodeKeyFiles = new Map()
const subTasks = []
Expand Down Expand Up @@ -1126,6 +1133,9 @@ export class NodeCommand extends BaseCommand {
* @param nodeCmd an instance of NodeCommand
*/
static getCommandDefinition (nodeCmd) {
if (!nodeCmd || !(nodeCmd instanceof NodeCommand)) {
throw new IllegalArgumentError('An instance of NodeCommand is required', nodeCmd)
}
return {
command: 'node',
desc: 'Manage Hedera platform node in fullstack testing network',
Expand Down
5 changes: 4 additions & 1 deletion src/commands/prompts.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { ListrEnquirerPromptAdapter } from '@listr2/prompt-adapter-enquirer'
import fs from 'fs'
import { FullstackTestingError, IllegalArgumentError } from '../core/errors.mjs'
import { constants } from '../core/index.mjs'
import { ConfigManager, constants } from '../core/index.mjs'
import * as flags from './flags.mjs'
import * as helpers from '../core/helpers.mjs'

Expand Down Expand Up @@ -451,6 +451,9 @@ export function getPromptMap () {
* @return {Promise<void>}
*/
export async function execute (task, configManager, flagList = []) {
if (!configManager || !(configManager instanceof ConfigManager)) {
throw new IllegalArgumentError('an instance of ConfigManager is required')
}
const prompts = getPromptMap()
for (const flag of flagList) {
if (!prompts.has(flag.name)) {
Expand Down
3 changes: 3 additions & 0 deletions src/commands/relay.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,9 @@ export class RelayCommand extends BaseCommand {
}

static getCommandDefinition (relayCmd) {
if (!relayCmd || !(relayCmd instanceof RelayCommand)) {
throw new MissingArgumentError('An instance of RelayCommand is required', relayCmd)
}
return {
command: 'relay',
desc: 'Manage JSON RPC relays in fullstack testing network',
Expand Down
1 change: 1 addition & 0 deletions src/core/dependency_managers/dependency_manager.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { ShellRunner } from '../shell_runner.mjs'

export class DependencyManager extends ShellRunner {
constructor (logger, depManagerMap) {
if (!logger) throw new MissingArgumentError('an instance of core/Logger is required', logger)
super(logger)
if (!depManagerMap) throw new MissingArgumentError('A map of dependency managers are required')
this.depManagerMap = depManagerMap
Expand Down
3 changes: 2 additions & 1 deletion src/core/dependency_managers/helm_dependency_manager.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import fs from 'fs'
import os from 'os'
import path from 'path'
import * as util from 'util'
import { MissingArgumentError } from '../errors.mjs'
import { IllegalArgumentError, MissingArgumentError } from '../errors.mjs'
import * as helpers from '../helpers.mjs'
import { constants, Templates } from '../index.mjs'
import * as version from '../../../version.mjs'
Expand Down Expand Up @@ -51,6 +51,7 @@ export class HelmDependencyManager extends ShellRunner {

if (!downloader) throw new MissingArgumentError('An instance of core/PackageDownloader is required')
if (!zippy) throw new MissingArgumentError('An instance of core/Zippy is required')
if (!logger) throw new IllegalArgumentError('an instance of core/Logger is required', logger)
if (!installationDir) throw new MissingArgumentError('installation directory is required')

this.downloader = downloader
Expand Down
3 changes: 2 additions & 1 deletion src/core/dependency_managers/keytool_dependency_manager.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import os from 'os'
import path from 'path'
import * as semver from 'semver'
import * as util from 'util'
import { MissingArgumentError, FullstackTestingError } from '../errors.mjs'
import { MissingArgumentError, FullstackTestingError, IllegalArgumentError } from '../errors.mjs'
import * as helpers from '../helpers.mjs'
import { constants, Keytool, Templates } from '../index.mjs'
import * as version from '../../../version.mjs'
Expand All @@ -44,6 +44,7 @@ export class KeytoolDependencyManager extends ShellRunner {

if (!downloader) throw new MissingArgumentError('An instance of core/PackageDownloader is required')
if (!zippy) throw new MissingArgumentError('An instance of core/Zippy is required')
if (!logger) throw new IllegalArgumentError('an instance of core/Logger is required', logger)
if (!installationDir) throw new MissingArgumentError('installation directory is required')

this.downloader = downloader
Expand Down
2 changes: 2 additions & 0 deletions src/core/helm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import os from 'os'
import { constants } from './index.mjs'
import { ShellRunner } from './shell_runner.mjs'
import { Templates } from './templates.mjs'
import { IllegalArgumentError } from './errors.mjs'

export class Helm extends ShellRunner {
constructor (logger, osPlatform = os.platform()) {
if (!logger) throw new IllegalArgumentError('an instance of core/Logger is required', logger)
super(logger)
this.osPlatform = osPlatform
this.helmPath = Templates.installationPath(constants.HELM, this.osPlatform)
Expand Down
2 changes: 2 additions & 0 deletions src/core/keytool.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import os from 'os'
import { constants } from './index.mjs'
import { ShellRunner } from './shell_runner.mjs'
import { Templates } from './templates.mjs'
import { MissingArgumentError } from './errors.mjs'

export class Keytool extends ShellRunner {
constructor (logger, osPlatform = os.platform()) {
if (!logger) throw new MissingArgumentError('an instance of core/Logger is required', logger)
super(logger)
this.osPlatform = osPlatform
this.keytoolPath = Templates.installationPath(constants.KEYTOOL, this.osPlatform)
Expand Down

0 comments on commit d055496

Please sign in to comment.