|
| 1 | +import fs from "node:fs"; |
| 2 | + |
| 3 | +const filePath = process.argv[2]; |
| 4 | + |
| 5 | +if (!filePath) { |
| 6 | + throw new Error("Usage: node configure-synapse.mjs <homeserver.yaml>"); |
| 7 | +} |
| 8 | + |
| 9 | +const upsertScalar = (content, key, value) => { |
| 10 | + const pattern = new RegExp(`^${key}:.*$`, "m"); |
| 11 | + const line = `${key}: ${value}`; |
| 12 | + return pattern.test(content) ? content.replace(pattern, line) : `${content.trimEnd()}\n${line}\n`; |
| 13 | +}; |
| 14 | + |
| 15 | +const removeScalar = (content, key) => content.replace(new RegExp(`^${key}:.*\n`, "m"), ""); |
| 16 | + |
| 17 | +const replaceTrustedKeyServers = content => { |
| 18 | + const pattern = /^trusted_key_servers:\n(?:[ \t].*\n)*/m; |
| 19 | + const block = "trusted_key_servers: []\n"; |
| 20 | + return pattern.test(content) ? content.replace(pattern, block) : `${content.trimEnd()}\n${block}`; |
| 21 | +}; |
| 22 | + |
| 23 | +let content = fs.readFileSync(filePath, "utf8"); |
| 24 | + |
| 25 | +content = removeScalar(content, "registration_shared_secret"); |
| 26 | +content = upsertScalar(content, "enable_registration", "false"); |
| 27 | +content = upsertScalar(content, "registration_shared_secret_path", '"/data/registration_shared_secret"'); |
| 28 | +content = upsertScalar(content, "suppress_key_server_warning", "true"); |
| 29 | +content = replaceTrustedKeyServers(content); |
| 30 | + |
| 31 | +fs.writeFileSync(filePath, content); |
0 commit comments