@ethersphere/swarm-cli
Version:
CLI tool for Bee
94 lines (69 loc) • 2.48 kB
text/typescript
import { Bee, BeeDev, BeeOptions, Reference } from '@ethersphere/bee-js'
import { Optional } from 'cafe-utility'
import { ExternalOption, Sourcemap, Utils } from 'furious-commander'
import { printCurlCommand } from '../../curl'
import { parseHeaders } from '../../utils'
import { ConfigOption } from '../../utils/types/config-option'
import { CONFIG_OPTIONS, CommandConfig } from './command-config'
import { CommandLog, VerbosityLevel } from './command-log'
export class RootCommand {
public beeApiUrl!: string
public configFolder!: string
public configFile!: string
public verbosity!: VerbosityLevel
public verbose!: boolean
public quiet!: boolean
public curl!: boolean
public header!: string[]
public yes!: boolean
public dev!: boolean
public bee!: Bee
public console!: CommandLog
public readonly appName = 'swarm-cli'
public commandConfig!: CommandConfig
private sourcemap!: Sourcemap
/**
* Resulting reference of the command for reflection (e.g. in tests)
*/
public result: Optional<Reference> = Optional.empty()
protected init(): void {
this.commandConfig = new CommandConfig(this.appName, this.console, this.configFile, this.configFolder)
this.sourcemap = Utils.getSourcemap()
CONFIG_OPTIONS.forEach((option: ConfigOption) => {
this.maybeSetFromConfig(option)
})
const beeOptions: BeeOptions = {}
if (this.curl) {
beeOptions.onRequest = printCurlCommand
}
if (this.header.length) {
beeOptions.headers = parseHeaders(this.header)
}
this.bee = this.dev ? new BeeDev(this.beeApiUrl, beeOptions) : new Bee(this.beeApiUrl, beeOptions)
this.verbosity = VerbosityLevel.Normal
if (this.quiet) {
this.verbosity = VerbosityLevel.Quiet
} else if (this.verbose) {
this.verbosity = VerbosityLevel.Verbose
}
this.console = new CommandLog(this.verbosity)
}
private maybeSetFromConfig(option: ConfigOption): void {
if (this.sourcemap[option.optionKey] === 'default') {
const value = this.commandConfig.config[option.propertyKey]
if (value !== undefined) {
this[option.propertyKey] = value
}
}
}
}