GitHub 0
Skip to content

Config files

Edit JSON, JSONC and TOML files that agents and people also write, one key at a time, and keep every other byte.

Agents keep their settings in files people edit by hand: ~/.claude.json, opencode.jsonc, ~/.codex/config.toml. skillrc changes one key in such a file and leaves the rest as it was: comments, key order, layout, a byte order mark, line endings and the file's mode.

import { editConfigFile } from "skillrc/io";

const result = editConfigFile(places, "/home/me/.claude.json", () => [
  { path: ["mcpServers", "github"], value: { command: "npx", args: ["-y", "github-mcp"] } },
]);
// { changed: true }
ts

editConfigFile(places, file, plan, options?) reads the file and calls plan with the object it holds, or undefined when the file is missing. plan returns the edits. skillrc places each one in the text and writes the file once.

import { editConfigFile, valueAt } from "skillrc/io";

editConfigFile(places, "/home/me/.claude.json", (root) =>
  valueAt(root, ["mcpServers", "old"]) === undefined
    ? []
    : [{ path: ["mcpServers", "old"], value: undefined }],
);
ts

An empty list writes nothing. Decide inside plan, not before the call: it gets the object read under the lock, so no other write slips in between.

A .jsonc file is read as JSONC, which allows comments and trailing commas. Any other file is read as strict JSON. Pass format to choose.

A KeyEdit names a key path from the root and the value to put there.

EditWhat it does
{ path, value }Sets the value. Missing objects on the way are created.
{ path, value: undefined }Removes the key, with its lines and any comment that trails it
{ path, item, value }Replaces the item at item in the list at path. An index equal to the list's length adds one.
{ path, item, value: undefined }Removes the item
{ path, item, insert: true, value }Puts the value before the item at item

A new member copies the layout of its neighbours. In a one-line object it goes on the same line; in a multi-line object it gets its own lines at the same indent.

{
  "theme": "dark", // mine
  "mcpServers": {}
}
before
{
  "theme": "dark", // mine
  "mcpServers": {
    "github": {
      "command": "npx"
    }
  }
}
after

After the edits, skillrc parses the result and checks that it holds exactly the edited value. If not, nothing is written and error says the edit could not be placed.

FieldWhen it is set
changedThe file changed, or would in a dry run
skippedNot done this time, and worth trying again: the lock was busy, the file changed meanwhile, or the file is missing and create is off
errorNot done: the file doesn't parse, it repeats a key on the path, or the edit can't be placed

editConfigFile doesn't throw for a file it can't edit, so a sync can report it and go on.

OptionDefaultWhat it does
formatBy extension"json" or "jsonc"
createfalseWrite a missing file, starting from {}
mode0o600The mode of a file this creates. An existing file keeps its own.
dryRunfalseReturn whether the file would change, and write nothing
locktrueHold <file>.lock while editing. Never taken in a dry run.
staleMs10_000How old a lock may be before it is taken as left by a dead process

Claude Code writes its files through proper-lockfile, which holds a <file>.lock folder. editConfigFile takes the same folder, so the two never write at once. A lock older than 10 seconds is stale and broken.

The new text goes to a temporary file, then renames over the file only if the file still holds what was read. A program that writes without the lock between that check and the rename still loses its write.

A file that is a link is edited at its target. A link to nothing gets an error and is left alone.

lockFile(file, staleMs?) takes the lock on its own. It returns the release, or undefined while another holder has it. The TOML example below uses it.

readConfigFile(file, format?) returns { root }, { error } for a file that doesn't parse, or {} for a missing file. valueAt(root, path) walks a key path and returns undefined at the first missing key.

import { readConfigFile, valueAt } from "skillrc/io";

const { root } = readConfigFile("/home/me/.claude.json");
const servers = valueAt(root, ["mcpServers"]);
ts

Codex and Grok keep MCP servers in TOML, one [mcp_servers.<name>] table each. setTomlTable(places, text, table, name, value) returns the text with that table and its subtables set, or removed when value is undefined.

import { readTomlTable, setTomlTable } from "skillrc/io";

const edited = setTomlTable(places, text, "mcp_servers", "github", {
  command: "npx",
  args: ["-y", "github-mcp"],
});
if ("refused" in edited) throw new Error(edited.refused);
ts
[mcp_servers.github]
command = "npx"
args = [ "-y", "github-mcp" ]
toml

It writes header tables only. It refuses, with the reason, text that doesn't parse, an entry written inline or with dotted keys, and a result that would change another table. A parse error says where, not what: the text could hold a secret.

readTomlTable(text, table, name) reads one entry back, and throws when the text doesn't parse.

setTomlTable works on text. To edit a file, hold the lock and write only if the file is unchanged, as editConfigFile does:

import { lockFile, readText, replaceIfUnchanged, setTomlTable } from "skillrc/io";

const file = "/home/me/.codex/config.toml";
const release = lockFile(file);
if (release) {
  try {
    const before = readText(file);
    const edited = setTomlTable(places, before ?? "", "mcp_servers", "github", server);
    if ("text" in edited) replaceIfUnchanged(places, file, before, edited.text, 0o600);
  } finally {
    release();
  }
}
ts

replaceIfUnchanged returns false when the file changed after readText.

editJsonText(text, before, after) edits JSON text that parses to before so it parses to after, changing only the parts that differ. A one-line array stays on one line. Use it for a file your program owns and people edit, such as its own manifest.

import { editJsonText } from "skillrc/io";

const out = editJsonText(text, before, { ...before, agents: [...before.agents, "codex"] });
ts

It returns undefined for text it can't follow, such as comments. Write the whole file then.