🎉 Initial Commit of next version RoadSign CLI
This commit is contained in:
4
cli/src/utils/auth.ts
Normal file
4
cli/src/utils/auth.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export function createAuthHeader(username: string, password: string) {
|
||||
const credentials = Buffer.from(`${username}:${password}`).toString("base64")
|
||||
return `Basic ${credentials}`
|
||||
}
|
51
cli/src/utils/config.ts
Normal file
51
cli/src/utils/config.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import * as os from "node:os"
|
||||
import * as path from "node:path"
|
||||
import * as fs from "node:fs/promises"
|
||||
|
||||
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")
|
||||
if (!await fs.exists(filepath)) {
|
||||
await fs.writeFile(filepath, JSON.stringify(this.config))
|
||||
}
|
||||
|
||||
const data = await fs.readFile(filepath, "utf8")
|
||||
this.config = JSON.parse(data)
|
||||
}
|
||||
|
||||
public async writeConfig() {
|
||||
const basepath = os.homedir()
|
||||
const filepath = path.join(basepath, ".roadsignrc")
|
||||
await fs.writeFile(filepath, JSON.stringify(this.config))
|
||||
}
|
||||
}
|
||||
|
||||
export { RsConfig, type RsConfigData, type RsConfigServerData }
|
Reference in New Issue
Block a user