-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathindex.ts
More file actions
107 lines (82 loc) · 3.01 KB
/
Copy pathindex.ts
File metadata and controls
107 lines (82 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { Bee, BeeOptions, Reference } from '@ethersphere/bee-js'
import { Optional } from 'cafe-utility'
import { ExternalOption, Sourcemap, Utils } from 'furious-commander'
import PackageJson from '../../../package.json'
import { printCurlCommand } from '../../curl'
import { checkForUpdates, getLatestVersionCheck } from '../../service/version_checker'
import { parseHeaders } from '../../utils'
import { warningText } from '../../utils/text'
import { ConfigOption } from '../../utils/types/config-option'
import { CONFIG_OPTIONS, CommandConfig } from './command-config'
import { CommandLog, VerbosityLevel } from './command-log'
export class RootCommand {
@ExternalOption('bee-api-url')
public beeApiUrl!: string
@ExternalOption('config-folder')
public configFolder!: string
@ExternalOption('config-file')
public configFile!: string
@ExternalOption('verbosity')
public verbosity!: VerbosityLevel
@ExternalOption('verbose')
public verbose!: boolean
@ExternalOption('quiet')
public quiet!: boolean
@ExternalOption('curl')
public curl!: boolean
@ExternalOption('header')
public header!: string[]
@ExternalOption('yes')
public yes!: 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 = 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)
if (!this.quiet) {
const latestVersionCheck = getLatestVersionCheck(this.commandConfig)
if (latestVersionCheck === null) {
checkForUpdates(this.commandConfig)
} else if (latestVersionCheck.latestVersion !== PackageJson.version) {
this.console.log(
warningText(
`A new version of swarm-cli is available: ${latestVersionCheck.latestVersion}. You are using version ${PackageJson.version}. Please update to get the latest features and fixes.`,
),
)
}
}
}
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
}
}
}
}