Archived
1
0
This repository has been archived on 2024-09-09. You can view files and clone it, but cannot push or open issues or pull requests.
code-server/src/node/settings.ts
futengda ecb9bb2428 refactor: write lastVisited and query at the same time
In addition, the `settings.write` method now uses shallow merge by default
2020-07-31 12:25:20 +08:00

68 lines
1.7 KiB
TypeScript

import * as fs from "fs-extra"
import * as path from "path"
import { extend, paths } from "./util"
import { logger } from "@coder/logger"
import { Route } from "./http"
export type Settings = { [key: string]: Settings | string | boolean | number }
/**
* Provides read and write access to settings.
*/
export class SettingsProvider<T> {
public constructor(private readonly settingsPath: string) {}
/**
* Read settings from the file. On a failure return last known settings and
* log a warning.
*/
public async read(): Promise<T> {
try {
const raw = (await fs.readFile(this.settingsPath, "utf8")).trim()
return raw ? JSON.parse(raw) : {}
} catch (error) {
if (error.code !== "ENOENT") {
logger.warn(error.message)
}
}
return {} as T
}
/**
* Write settings combined with current settings. On failure log a warning.
* Settings can be shallow or deep merged.
*/
public async write(settings: Partial<T>, shallow = true): Promise<void> {
try {
const oldSettings = await this.read()
const nextSettings = shallow ? Object.assign({}, oldSettings, settings) : extend(oldSettings, settings)
await fs.writeFile(this.settingsPath, JSON.stringify(nextSettings, null, 2))
} catch (error) {
logger.warn(error.message)
}
}
}
export interface UpdateSettings {
update: {
checked: number
version: string
}
}
/**
* Global code-server settings.
*/
export interface CoderSettings extends UpdateSettings {
lastVisited: {
url: string
workspace: boolean
}
query: Route["query"]
}
/**
* Global code-server settings file.
*/
export const settings = new SettingsProvider<CoderSettings>(path.join(paths.data, "coder.json"))