2024-10-01 14:19:27 +00:00
|
|
|
import * as os from "node:os"
|
|
|
|
import * as path from "node:path"
|
2024-10-03 07:04:23 +00:00
|
|
|
import * as fs from "node:fs"
|
2024-10-01 14:19:27 +00:00
|
|
|
|
|
|
|
interface RsConfigData {
|
|
|
|
servers: RsConfigServerData[]
|
|
|
|
}
|
|
|
|
|
|
|
|
interface RsConfigServerData {
|
|
|
|
label: string
|
|
|
|
url: string
|
|
|
|
credential: string
|
|
|
|
}
|
|
|
|
|
|
|
|
class RsConfig {
|
|
|
|
private static instance: RsConfig
|
|
|
|
|
|
|
|
public config: RsConfigData = {
|
|
|
|
servers: []
|
|
|
|
}
|
|
|
|
|
|
|
|
private constructor() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async getInstance(): Promise<RsConfig> {
|
|
|
|
if (!RsConfig.instance) {
|
|
|
|
RsConfig.instance = new RsConfig()
|
|
|
|
await RsConfig.instance.readConfig()
|
|
|
|
}
|
|
|
|
return RsConfig.instance
|
|
|
|
}
|
|
|
|
|
|
|
|
public async readConfig() {
|
|
|
|
const basepath = os.homedir()
|
|
|
|
const filepath = path.join(basepath, ".roadsignrc")
|
2024-10-03 07:04:23 +00:00
|
|
|
if (!fs.existsSync(filepath)) {
|
|
|
|
fs.writeFileSync(filepath, JSON.stringify(this.config))
|
2024-10-01 14:19:27 +00:00
|
|
|
}
|
|
|
|
|
2024-10-03 07:04:23 +00:00
|
|
|
const data = fs.readFileSync(filepath, "utf8")
|
2024-10-01 14:19:27 +00:00
|
|
|
this.config = JSON.parse(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
public async writeConfig() {
|
|
|
|
const basepath = os.homedir()
|
|
|
|
const filepath = path.join(basepath, ".roadsignrc")
|
2024-10-03 07:04:23 +00:00
|
|
|
fs.writeFileSync(filepath, JSON.stringify(this.config))
|
2024-10-01 14:19:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export { RsConfig, type RsConfigData, type RsConfigServerData }
|