Skip to content

Commit d055496

Browse files
feat: adding some type check (#217)
Signed-off-by: Jeffrey Tang <[email protected]> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 07f5fe4 commit d055496

13 files changed

+47
-7
lines changed

src/commands/account.mjs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { flags } from './index.mjs'
2121
import { Listr } from 'listr2'
2222
import * as prompts from './prompts.mjs'
2323
import { constants } from '../core/index.mjs'
24-
import { HbarUnit, PrivateKey } from '@hashgraph/sdk'
24+
import { AccountInfo, HbarUnit, PrivateKey } from '@hashgraph/sdk'
2525

2626
export class AccountCommand extends BaseCommand {
2727
constructor (opts, systemAccounts = constants.SYSTEM_ACCOUNTS) {
@@ -39,6 +39,8 @@ export class AccountCommand extends BaseCommand {
3939
}
4040

4141
async buildAccountInfo (accountInfo, namespace, shouldRetrievePrivateKey) {
42+
if (!accountInfo || !(accountInfo instanceof AccountInfo)) throw new IllegalArgumentError('An instance of AccountInfo is required')
43+
4244
const newAccountInfo = {
4345
accountId: accountInfo.accountId.toString(),
4446
publicKey: accountInfo.key.toString(),
@@ -399,6 +401,9 @@ export class AccountCommand extends BaseCommand {
399401
* @param accountCmd an instance of NodeCommand
400402
*/
401403
static getCommandDefinition (accountCmd) {
404+
if (!accountCmd | !(accountCmd instanceof AccountCommand)) {
405+
throw new IllegalArgumentError('An instance of AccountCommand is required', accountCmd)
406+
}
402407
return {
403408
command: 'account',
404409
desc: 'Manage Hedera accounts in fullstack testing network',

src/commands/cluster.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
import { ListrEnquirerPromptAdapter } from '@listr2/prompt-adapter-enquirer'
1818
import { Listr } from 'listr2'
19-
import { FullstackTestingError } from '../core/errors.mjs'
19+
import { FullstackTestingError, IllegalArgumentError } from '../core/errors.mjs'
2020
import * as flags from './flags.mjs'
2121
import { BaseCommand } from './base.mjs'
2222
import chalk from 'chalk'
@@ -220,6 +220,9 @@ export class ClusterCommand extends BaseCommand {
220220
* @param clusterCmd an instance of ClusterCommand
221221
*/
222222
static getCommandDefinition (clusterCmd) {
223+
if (!clusterCmd || !(clusterCmd instanceof ClusterCommand)) {
224+
throw new IllegalArgumentError('Invalid ClusterCommand instance')
225+
}
223226
return {
224227
command: 'cluster',
225228
desc: 'Manage fullstack testing cluster',

src/commands/init.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { BaseCommand } from './base.mjs'
2020
import * as core from '../core/index.mjs'
2121
import { constants } from '../core/index.mjs'
2222
import * as fs from 'fs'
23-
import { FullstackTestingError } from '../core/errors.mjs'
23+
import { FullstackTestingError, IllegalArgumentError } from '../core/errors.mjs'
2424
import * as flags from './flags.mjs'
2525
import chalk from 'chalk'
2626

@@ -144,6 +144,9 @@ export class InitCommand extends BaseCommand {
144144
* @param initCmd an instance of InitCommand
145145
*/
146146
static getCommandDefinition (initCmd) {
147+
if (!initCmd || !(initCmd instanceof InitCommand)) {
148+
throw new IllegalArgumentError('Invalid InitCommand')
149+
}
147150
return {
148151
command: 'init',
149152
desc: 'Initialize local environment and default flags',

src/commands/mirror_node.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ export class MirrorNodeCommand extends BaseCommand {
280280
* @param mirrorNodeCmd an instance of NodeCommand
281281
*/
282282
static getCommandDefinition (mirrorNodeCmd) {
283+
if (!mirrorNodeCmd || !(mirrorNodeCmd instanceof MirrorNodeCommand)) {
284+
throw new IllegalArgumentError('Invalid MirrorNodeCommand instance', mirrorNodeCmd)
285+
}
283286
return {
284287
command: 'mirror-node',
285288
desc: 'Manage Hedera Mirror Node in fullstack testing network',

src/commands/network.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import { ListrEnquirerPromptAdapter } from '@listr2/prompt-adapter-enquirer'
1818
import chalk from 'chalk'
1919
import { Listr } from 'listr2'
20-
import { FullstackTestingError, MissingArgumentError } from '../core/errors.mjs'
20+
import { FullstackTestingError, IllegalArgumentError, MissingArgumentError } from '../core/errors.mjs'
2121
import { BaseCommand } from './base.mjs'
2222
import * as flags from './flags.mjs'
2323
import { constants } from '../core/index.mjs'
@@ -393,6 +393,9 @@ export class NetworkCommand extends BaseCommand {
393393
}
394394

395395
static getCommandDefinition (networkCmd) {
396+
if (!networkCmd || !(networkCmd instanceof NetworkCommand)) {
397+
throw new IllegalArgumentError('An instance of NetworkCommand is required', networkCmd)
398+
}
396399
return {
397400
command: 'network',
398401
desc: 'Manage fullstack testing network deployment',

src/commands/node.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ export class NodeCommand extends BaseCommand {
178178
* @private
179179
*/
180180
_nodeGossipKeysTaskList (keyFormat, nodeIds, keysDir, curDate = new Date()) {
181+
if (!Array.isArray(nodeIds) || !nodeIds.every((nodeId) => typeof nodeId === 'string')) {
182+
throw new IllegalArgumentError('nodeIds must be an array of strings')
183+
}
181184
const self = this
182185
const subTasks = []
183186

@@ -276,6 +279,10 @@ export class NodeCommand extends BaseCommand {
276279
* @private
277280
*/
278281
_nodeTlsKeyTaskList (nodeIds, keysDir, curDate = new Date()) {
282+
// check if nodeIds is an array of strings
283+
if (!Array.isArray(nodeIds) || !nodeIds.every((nodeId) => typeof nodeId === 'string')) {
284+
throw new FullstackTestingError('nodeIds must be an array of strings')
285+
}
279286
const self = this
280287
const nodeKeyFiles = new Map()
281288
const subTasks = []
@@ -1126,6 +1133,9 @@ export class NodeCommand extends BaseCommand {
11261133
* @param nodeCmd an instance of NodeCommand
11271134
*/
11281135
static getCommandDefinition (nodeCmd) {
1136+
if (!nodeCmd || !(nodeCmd instanceof NodeCommand)) {
1137+
throw new IllegalArgumentError('An instance of NodeCommand is required', nodeCmd)
1138+
}
11291139
return {
11301140
command: 'node',
11311141
desc: 'Manage Hedera platform node in fullstack testing network',

src/commands/prompts.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import { ListrEnquirerPromptAdapter } from '@listr2/prompt-adapter-enquirer'
1818
import fs from 'fs'
1919
import { FullstackTestingError, IllegalArgumentError } from '../core/errors.mjs'
20-
import { constants } from '../core/index.mjs'
20+
import { ConfigManager, constants } from '../core/index.mjs'
2121
import * as flags from './flags.mjs'
2222
import * as helpers from '../core/helpers.mjs'
2323

@@ -451,6 +451,9 @@ export function getPromptMap () {
451451
* @return {Promise<void>}
452452
*/
453453
export async function execute (task, configManager, flagList = []) {
454+
if (!configManager || !(configManager instanceof ConfigManager)) {
455+
throw new IllegalArgumentError('an instance of ConfigManager is required')
456+
}
454457
const prompts = getPromptMap()
455458
for (const flag of flagList) {
456459
if (!prompts.has(flag.name)) {

src/commands/relay.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ export class RelayCommand extends BaseCommand {
258258
}
259259

260260
static getCommandDefinition (relayCmd) {
261+
if (!relayCmd || !(relayCmd instanceof RelayCommand)) {
262+
throw new MissingArgumentError('An instance of RelayCommand is required', relayCmd)
263+
}
261264
return {
262265
command: 'relay',
263266
desc: 'Manage JSON RPC relays in fullstack testing network',

src/core/dependency_managers/dependency_manager.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { ShellRunner } from '../shell_runner.mjs'
2020

2121
export class DependencyManager extends ShellRunner {
2222
constructor (logger, depManagerMap) {
23+
if (!logger) throw new MissingArgumentError('an instance of core/Logger is required', logger)
2324
super(logger)
2425
if (!depManagerMap) throw new MissingArgumentError('A map of dependency managers are required')
2526
this.depManagerMap = depManagerMap

src/core/dependency_managers/helm_dependency_manager.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import fs from 'fs'
1818
import os from 'os'
1919
import path from 'path'
2020
import * as util from 'util'
21-
import { MissingArgumentError } from '../errors.mjs'
21+
import { IllegalArgumentError, MissingArgumentError } from '../errors.mjs'
2222
import * as helpers from '../helpers.mjs'
2323
import { constants, Templates } from '../index.mjs'
2424
import * as version from '../../../version.mjs'
@@ -51,6 +51,7 @@ export class HelmDependencyManager extends ShellRunner {
5151

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

5657
this.downloader = downloader

src/core/dependency_managers/keytool_dependency_manager.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import os from 'os'
1919
import path from 'path'
2020
import * as semver from 'semver'
2121
import * as util from 'util'
22-
import { MissingArgumentError, FullstackTestingError } from '../errors.mjs'
22+
import { MissingArgumentError, FullstackTestingError, IllegalArgumentError } from '../errors.mjs'
2323
import * as helpers from '../helpers.mjs'
2424
import { constants, Keytool, Templates } from '../index.mjs'
2525
import * as version from '../../../version.mjs'
@@ -44,6 +44,7 @@ export class KeytoolDependencyManager extends ShellRunner {
4444

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

4950
this.downloader = downloader

src/core/helm.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import os from 'os'
1818
import { constants } from './index.mjs'
1919
import { ShellRunner } from './shell_runner.mjs'
2020
import { Templates } from './templates.mjs'
21+
import { IllegalArgumentError } from './errors.mjs'
2122

2223
export class Helm extends ShellRunner {
2324
constructor (logger, osPlatform = os.platform()) {
25+
if (!logger) throw new IllegalArgumentError('an instance of core/Logger is required', logger)
2426
super(logger)
2527
this.osPlatform = osPlatform
2628
this.helmPath = Templates.installationPath(constants.HELM, this.osPlatform)

src/core/keytool.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ import os from 'os'
1818
import { constants } from './index.mjs'
1919
import { ShellRunner } from './shell_runner.mjs'
2020
import { Templates } from './templates.mjs'
21+
import { MissingArgumentError } from './errors.mjs'
2122

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

0 commit comments

Comments
 (0)