Repository Public
1import Foundation23/// Writes the per-app hook settings file passed to `claude --settings <file>`. The file only ever4/// contains NEXTERM's own HTTP hook entries, so the user's `~/.claude/settings.json` is never touched5/// and removing the file (or launching without `--settings`) fully reverts the integration.6public enum HookSettingsWriter {7 public static func settingsJSON(url: String, token: String, events: [HookEventName] = HookEventName.subscribed, timeoutSeconds: Int = 5) -> [String: Any] {8 var hooks: [String: Any] = [:]9 for event in events {10 let handler: [String: Any] = [11 "type": "http",12 "url": url,13 "headers": ["Authorization": "Bearer \(token)", "X-NEXTERM-Event": event.rawValue],14 "timeout": timeoutSeconds,15 ]16 hooks[event.rawValue] = [["hooks": [handler]]]17 }18 return ["hooks": hooks]19 }2021 /// Writes the settings file with 0600 permissions and returns its path.22 @discardableResult23 public static func write(to directory: URL, url: String, token: String, events: [HookEventName] = HookEventName.subscribed) throws -> String {24 try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true, attributes: [.posixPermissions: 0o700])25 let file = directory.appendingPathComponent("hooks-settings.json")26 let data = try JSONSerialization.data(withJSONObject: settingsJSON(url: url, token: token, events: events), options: [.prettyPrinted, .sortedKeys])27 try data.write(to: file, options: .atomic)28 try FileManager.default.setAttributes([.posixPermissions: 0o600], ofItemAtPath: file.path)29 return file.path30 }3132 public static func remove(from directory: URL) {33 try? FileManager.default.removeItem(at: directory.appendingPathComponent("hooks-settings.json"))34 }3536 /// 32 random bytes, hex encoded. Lives only in memory and the 0600 settings file.37 public static func makeToken() -> String {38 var bytes = [UInt8](repeating: 0, count: 32)39 _ = SecRandomCopyBytes(kSecRandomDefault, bytes.count, &bytes)40 return bytes.map { String(format: "%02x", $0) }.joined()41 }42}43