(update: PermissionUpdate)
| 220 | * @param update The permission update to persist |
| 221 | */ |
| 222 | export function persistPermissionUpdate(update: PermissionUpdate): void { |
| 223 | if (!supportsPersistence(update.destination)) return |
| 224 | |
| 225 | logForDebugging( |
| 226 | `Persisting permission update: ${update.type} to source '${update.destination}'`, |
| 227 | ) |
| 228 | |
| 229 | switch (update.type) { |
| 230 | case 'addRules': { |
| 231 | logForDebugging( |
| 232 | `Persisting ${update.rules.length} ${update.behavior} rule(s) to ${update.destination}`, |
| 233 | ) |
| 234 | addPermissionRulesToSettings( |
| 235 | { |
| 236 | ruleValues: update.rules, |
| 237 | ruleBehavior: update.behavior, |
| 238 | }, |
| 239 | update.destination, |
| 240 | ) |
| 241 | break |
| 242 | } |
| 243 | |
| 244 | case 'addDirectories': { |
| 245 | logForDebugging( |
| 246 | `Persisting ${update.directories.length} director${update.directories.length === 1 ? 'y' : 'ies'} to ${update.destination}`, |
| 247 | ) |
| 248 | const existingSettings = getSettingsForSource(update.destination) |
| 249 | const existingDirs = |
| 250 | existingSettings?.permissions?.additionalDirectories || [] |
| 251 | |
| 252 | // Add new directories, avoiding duplicates |
| 253 | const dirsToAdd = update.directories.filter( |
| 254 | dir => !existingDirs.includes(dir), |
| 255 | ) |
| 256 | |
| 257 | if (dirsToAdd.length > 0) { |
| 258 | const updatedDirs = [...existingDirs, ...dirsToAdd] |
| 259 | updateSettingsForSource(update.destination, { |
| 260 | permissions: { |
| 261 | additionalDirectories: updatedDirs, |
| 262 | }, |
| 263 | }) |
| 264 | } |
| 265 | break |
| 266 | } |
| 267 | |
| 268 | case 'removeRules': { |
| 269 | // Handle rule removal |
| 270 | logForDebugging( |
| 271 | `Removing ${update.rules.length} ${update.behavior} rule(s) from ${update.destination}`, |
| 272 | ) |
| 273 | const existingSettings = getSettingsForSource(update.destination) |
| 274 | const existingPermissions = existingSettings?.permissions || {} |
| 275 | const existingRules = existingPermissions[update.behavior] || [] |
| 276 | |
| 277 | // Convert rules to normalized strings for comparison |
| 278 | // Normalize via parse→serialize roundtrip so "Bash(*)" and "Bash" match |
| 279 | const rulesToRemove = new Set( |
no test coverage detected