mirror of
https://github.com/coder/code-server.git
synced 2026-02-09 00:50:49 -06:00
* chore: bump typescript from 5.6.2 to 5.8.3 Bumps [typescript](https://github.com/microsoft/TypeScript) from 5.6.2 to 5.8.3. - [Release notes](https://github.com/microsoft/TypeScript/releases) - [Changelog](https://github.com/microsoft/TypeScript/blob/main/azure-pipelines.release-publish.yml) - [Commits](https://github.com/microsoft/TypeScript/compare/v5.6.2...v5.8.3) --- updated-dependencies: - dependency-name: typescript dependency-version: 5.8.3 dependency-type: direct:development update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <support@github.com> * as T * Update JSON error --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Asher <ash@coder.com>
57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
import { logger } from "@coder/logger"
|
|
import { Query } from "express-serve-static-core"
|
|
import { promises as fs } from "fs"
|
|
|
|
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) : ({} as T)
|
|
} catch (error: any) {
|
|
if (error.code !== "ENOENT") {
|
|
logger.warn(error.message)
|
|
}
|
|
}
|
|
return {} as T
|
|
}
|
|
|
|
/**
|
|
* Write settings combined with current settings. On failure log a warning.
|
|
* Settings will be merged shallowly.
|
|
*/
|
|
public async write(settings: Partial<T>): Promise<void> {
|
|
try {
|
|
const oldSettings = await this.read()
|
|
const nextSettings = { ...oldSettings, ...settings }
|
|
await fs.writeFile(this.settingsPath, JSON.stringify(nextSettings, null, 2))
|
|
} catch (error: any) {
|
|
logger.warn(error.message)
|
|
}
|
|
}
|
|
}
|
|
|
|
export interface UpdateSettings {
|
|
update: {
|
|
checked: number
|
|
version: string
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Global code-server settings.
|
|
*/
|
|
export interface CoderSettings extends UpdateSettings {
|
|
query?: Query
|
|
}
|