Skip to content

API reference / @jsprismarine/prismarine / ConfigBuilder

Class: ConfigBuilder ​

Defined in: prismarine/src/config/ConfigBuilder.ts:51

A JSON config file on disk, read and written one key at a time.

Every write goes through a ConfigDocument, so comments and blank lines in the file survive, and a key written for the first time can carry a comment that explains it. The JSON may hold // and /* */ comments and trailing commas.

Constructors ​

Constructor ​

new ConfigBuilder(filePath, keyOrder?): ConfigBuilder

Defined in: prismarine/src/config/ConfigBuilder.ts:63

Opens the file, creating it and its directory when they do not exist yet.

Parameters ​

filePath ​

string

The .json file to read and write.

keyOrder? ​

readonly string[]

Where a new top-level key is inserted, so the file keeps one order no matter which part of the server writes a key first.

Returns ​

ConfigBuilder

Throws ​

Error when the file extension is not .json.

Methods ​

del() ​

del(key): boolean

Defined in: prismarine/src/config/ConfigBuilder.ts:198

Delete a config value, and the comment above it.

Parameters ​

key ​

string

the config key

Returns ​

boolean

true if the key was there to delete


get() ​

Shared body of the two get overloads above; only they are callable.

Call Signature ​

get<T>(key, defaults, comment?): T

Defined in: prismarine/src/config/ConfigBuilder.ts:119

Get a config value from a key, writing the default to the file when the key is absent.

A stored value of a different kind than the default (a string where a number is expected, say) is ignored in favor of the default, and the file is left alone.

Type Parameters ​
T ​

T

Parameters ​
key ​

string

the config key

defaults ​

T

the value to use, and persist, when the key is missing

comment? ​

string

written above the key in the file when the default is persisted

Returns ​

T

Call Signature ​

get(key): unknown

Defined in: prismarine/src/config/ConfigBuilder.ts:125

Get a config value from a key, or undefined when the key is absent.

Parameters ​
key ​

string

the config key

Returns ​

unknown


getPath() ​

getPath(): string

Defined in: prismarine/src/config/ConfigBuilder.ts:85

Get path to the config file on the filesystem.

Returns ​

string

the path to the config file


has() ​

has(key): boolean

Defined in: prismarine/src/config/ConfigBuilder.ts:172

Check if config value exists.

Parameters ​

key ​

string

the config key

Returns ​

boolean

true if the config contains that key


rename() ​

rename(key, to): boolean

Defined in: prismarine/src/config/ConfigBuilder.ts:185

Renames a top-level key in the file, keeping its value, position and comments.

Parameters ​

key ​

string

the key the file has

to ​

string

its new name

Returns ​

boolean

true if the key was there to rename


set() ​

set(key, value, comment?): boolean

Defined in: prismarine/src/config/ConfigBuilder.ts:152

Sets a key - value pair in config.

Parameters ​

key ​

string

the config key

value ​

unknown

the value to store

comment? ​

string

written above the key when the key is new; an existing key keeps its own

Returns ​

boolean

true if the value was set successfully