Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding some type check #217

Merged
merged 13 commits into from
Apr 12, 2024
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 { 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 @@
}

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 @@
* @param accountCmd an instance of NodeCommand
*/
static getCommandDefinition (accountCmd) {
if (!accountCmd | !(accountCmd instanceof AccountCommand)) {
throw new IllegalArgumentError('An instance of AccountCommand is required', accountCmd)

Check warning on line 405 in src/commands/account.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/account.mjs#L405

Added line #L405 was not covered by tests
}
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 @@
* @param clusterCmd an instance of ClusterCommand
*/
static getCommandDefinition (clusterCmd) {
if (!clusterCmd || !(clusterCmd instanceof ClusterCommand)) {
throw new IllegalArgumentError('Invalid ClusterCommand instance')

Check warning on line 224 in src/commands/cluster.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/cluster.mjs#L224

Added line #L224 was not covered by tests
}
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 * 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 @@
* @param initCmd an instance of InitCommand
*/
static getCommandDefinition (initCmd) {
if (!initCmd || !(initCmd instanceof InitCommand)) {
throw new IllegalArgumentError('Invalid InitCommand')

Check warning on line 148 in src/commands/init.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/init.mjs#L148

Added line #L148 was not covered by tests
}
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 @@
* @param mirrorNodeCmd an instance of NodeCommand
*/
static getCommandDefinition (mirrorNodeCmd) {
if (!mirrorNodeCmd || !(mirrorNodeCmd instanceof MirrorNodeCommand)) {
throw new IllegalArgumentError('Invalid MirrorNodeCommand instance', mirrorNodeCmd)

Check warning on line 284 in src/commands/mirror_node.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/mirror_node.mjs#L284

Added line #L284 was not covered by tests
}
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 @@
}

static getCommandDefinition (networkCmd) {
if (!networkCmd || !(networkCmd instanceof NetworkCommand)) {
throw new IllegalArgumentError('An instance of NetworkCommand is required', networkCmd)

Check warning on line 397 in src/commands/network.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/network.mjs#L397

Added line #L397 was not covered by tests
}
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 @@
* @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')

Check warning on line 182 in src/commands/node.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/node.mjs#L182

Added line #L182 was not covered by tests
}
const self = this
const subTasks = []

Expand Down Expand Up @@ -276,6 +279,10 @@
* @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')

Check warning on line 284 in src/commands/node.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/node.mjs#L284

Added line #L284 was not covered by tests
}
const self = this
const nodeKeyFiles = new Map()
const subTasks = []
Expand Down Expand Up @@ -1126,6 +1133,9 @@
* @param nodeCmd an instance of NodeCommand
*/
static getCommandDefinition (nodeCmd) {
if (!nodeCmd || !(nodeCmd instanceof NodeCommand)) {
throw new IllegalArgumentError('An instance of NodeCommand is required', nodeCmd)

Check warning on line 1137 in src/commands/node.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/node.mjs#L1137

Added line #L1137 was not covered by tests
}
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 @@
* @return {Promise<void>}
*/
export async function execute (task, configManager, flagList = []) {
if (!configManager || !(configManager instanceof ConfigManager)) {
throw new IllegalArgumentError('an instance of ConfigManager is required')

Check warning on line 455 in src/commands/prompts.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/prompts.mjs#L455

Added line #L455 was not covered by tests
}
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 @@
}

static getCommandDefinition (relayCmd) {
if (!relayCmd || !(relayCmd instanceof RelayCommand)) {
throw new MissingArgumentError('An instance of RelayCommand is required', relayCmd)

Check warning on line 262 in src/commands/relay.mjs

View check run for this annotation

Codecov / codecov/patch

src/commands/relay.mjs#L262

Added line #L262 was not covered by tests
}
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
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