(input: {
systemDir: string
currentDir: string
targetDir: string | null
defaultDir?: string
migrate?: boolean
envDataDir?: string
})
| 303 | } |
| 304 | |
| 305 | export function createNodeDataDirSwitch(input: { |
| 306 | systemDir: string |
| 307 | currentDir: string |
| 308 | targetDir: string | null |
| 309 | defaultDir?: string |
| 310 | migrate?: boolean |
| 311 | envDataDir?: string |
| 312 | }): DataDirSwitchResult { |
| 313 | if (input.envDataDir) { |
| 314 | return { success: false, error: 'CHATLAB_DATA_DIR is set, data directory cannot be changed from Web UI' } |
| 315 | } |
| 316 | |
| 317 | const newDir = (input.targetDir?.trim() || input.defaultDir || '').trim() |
| 318 | if (!newDir) return { success: false, error: 'Data directory is required' } |
| 319 | if (!path.isAbsolute(newDir)) return { success: false, error: 'Data directory must be an absolute path' } |
| 320 | if (!isPathSafe(newDir)) return { success: false, error: 'System directories cannot be used as data directory' } |
| 321 | |
| 322 | const migrate = input.migrate !== false |
| 323 | if (migrate && path.resolve(input.currentDir) !== path.resolve(newDir) && isSubPath(input.currentDir, newDir)) { |
| 324 | return { success: false, error: 'Target directory cannot be inside current data directory' } |
| 325 | } |
| 326 | |
| 327 | if (!isUserDataDirSafeToUse(newDir)) { |
| 328 | return { success: false, error: 'Target directory is not empty and is not a ChatLab data directory' } |
| 329 | } |
| 330 | |
| 331 | if (path.resolve(input.currentDir) === path.resolve(newDir)) { |
| 332 | clearPendingNodeDataDirMigration(input.systemDir) |
| 333 | return { success: true, from: input.currentDir, to: newDir, requiresRelaunch: false } |
| 334 | } |
| 335 | |
| 336 | const pending = createPendingDataDirMigration({ |
| 337 | from: input.currentDir, |
| 338 | to: newDir, |
| 339 | migrate, |
| 340 | targetWasEmpty: isDirectoryEmptyOrMissing(newDir), |
| 341 | }) |
| 342 | writePendingNodeDataDirMigration(input.systemDir, pending) |
| 343 | |
| 344 | return { success: true, from: input.currentDir, to: newDir, requiresRelaunch: true } |
| 345 | } |
| 346 | |
| 347 | export function applyPendingNodeDataDirMigration( |
| 348 | systemDir: string, |
no test coverage detected