-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathcommand-config.ts
More file actions
113 lines (90 loc) · 3.34 KB
/
Copy pathcommand-config.ts
File metadata and controls
113 lines (90 loc) · 3.34 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
108
109
110
111
112
113
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'fs'
import { homedir, platform } from 'os'
import { join } from 'path'
import { exit } from 'process'
import { beeApiUrl } from '../../config'
import { Identity } from '../../service/identity/types'
import { ConfigOption } from '../../utils/types/config-option'
import { CommandLog } from './command-log'
/**
* Options listed here will be read from the config file
* when they are not set explicitly or via `process.env`.
*
* `optionKey` is the kebab-case variant of the argument used in the parser, a.k.a. the key,
* `propertyKey` is the camelCase variant used in TypeScript command classes, a.k.a. the property.
*/
export const CONFIG_OPTIONS: ConfigOption[] = [{ optionKey: 'bee-api-url', propertyKey: 'beeApiUrl' }]
export interface Config {
beeApiUrl: string
identities: { [name: string]: Identity }
historyEnabled?: boolean
}
export class CommandConfig {
public config: Config
public configFilePath: string
public configFolderPath: string
public console: CommandLog
constructor(appName: string, console: CommandLog, configFile: string, configFolder?: string) {
this.console = console
this.config = {
beeApiUrl: beeApiUrl.default || '',
identities: {},
historyEnabled: true,
}
this.configFolderPath = this.getConfigFolderPath(appName, configFolder)
this.configFilePath = join(this.configFolderPath, configFile)
this.prepareConfig()
}
public saveIdentity(name: string, identity: Identity): boolean {
if (this.config.identities?.[name]) return false
this.config.identities[name] = identity
this.saveConfig()
return true
}
public getHistoryFilePath(): string {
return process.env.SWARM_CLI_HISTORY_FILE_PATH || join(this.configFolderPath, 'upload-history.json')
}
public getVersionCheckFilePath(): string {
return process.env.SWARM_CLI_VERSION_CHECK_FILE_PATH || join(this.configFolderPath, 'version-check.json')
}
public setHistoryEnabled(enabled: boolean): void {
this.config.historyEnabled = enabled
this.saveConfig()
}
public removeIdentity(name: string): void {
delete this.config.identities?.[name]
this.saveConfig()
}
/** Save configuration object to the CLI's config file */
private saveConfig() {
writeFileSync(this.configFilePath, JSON.stringify(this.config), { mode: 0o600 })
}
/** Load configuration from config path or creates config folder */
private prepareConfig() {
if (!existsSync(this.configFilePath)) {
if (!existsSync(this.configFolderPath)) mkdirSync(this.configFolderPath, { mode: 0o700, recursive: true })
//save config initialized in constructor
this.saveConfig()
} else {
//load config
const configData = readFileSync(this.configFilePath)
try {
this.config = JSON.parse(configData.toString())
} catch (err) {
this.console.error(
`There has been an error parsing JSON configuration of CLI from path: '${this.configFilePath}'`,
)
exit(1)
}
}
}
private getConfigFolderPath(appName: string, configFolder?: string): string {
if (configFolder) return configFolder
const homePath = homedir()
if (platform() === 'win32') {
return join(homePath, 'AppData', appName)
} else {
return join(homePath, `.${appName}`)
}
}
}