diff --git a/src/core/config/loader.test.ts b/src/core/config/loader.test.ts index e1aebf7..74b8712 100644 --- a/src/core/config/loader.test.ts +++ b/src/core/config/loader.test.ts @@ -1,5 +1,5 @@ import { afterEach, describe, expect, test } from "bun:test"; -import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; +import { existsSync, mkdirSync, mkdtempSync, rmSync, utimesSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { @@ -9,6 +9,7 @@ import { resolveEnterprisesDir, resolveGlobalConfigFile, saveGlobalConfig, + syncLegacyEnterprisesDir, updateCorpConfig } from "./loader.js"; @@ -187,6 +188,39 @@ describe("loadCorpConfigs", () => { }); }); +describe("syncLegacyEnterprisesDir", () => { + test("migrates files from legacy to primary and deletes legacy when > 7 days old", () => { + const primary = makeTempDir(); + const legacy = makeTempDir(); + + writeCorpConfig(join(legacy, "old-corp.json"), "old-corp", "Old Corp"); + + const tenDaysAgo = (Date.now() - 10 * 24 * 60 * 60 * 1000) / 1000; + utimesSync(join(legacy, "old-corp.json"), tenDaysAgo, tenDaysAgo); + utimesSync(legacy, tenDaysAgo, tenDaysAgo); + + const result = syncLegacyEnterprisesDir(primary, legacy); + expect(result.copied).toEqual(["old-corp.json"]); + expect(result.deletedLegacyDir).toBe(true); + + expect(existsSync(join(primary, "old-corp.json"))).toBe(true); + expect(existsSync(legacy)).toBe(false); + }); + + test("preserves legacy directory when modified within 7 days", () => { + const primary = makeTempDir(); + const legacy = makeTempDir(); + + writeCorpConfig(join(legacy, "recent-corp.json"), "recent-corp", "Recent Corp"); + const result = syncLegacyEnterprisesDir(primary, legacy); + expect(result.copied).toEqual(["recent-corp.json"]); + expect(result.deletedLegacyDir).toBe(false); + + expect(existsSync(join(primary, "recent-corp.json"))).toBe(true); + expect(existsSync(legacy)).toBe(true); + }); +}); + function makeTempDir(): string { const dir = mkdtempSync(join(tmpdir(), "seal-home-loader-")); mkdirSync(dir, { recursive: true }); diff --git a/src/core/config/loader.ts b/src/core/config/loader.ts index a8030bf..d2b6cf8 100644 --- a/src/core/config/loader.ts +++ b/src/core/config/loader.ts @@ -1,4 +1,4 @@ -import { existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs"; +import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs"; import { homedir } from "node:os"; import { dirname, join } from "node:path"; import { CorpConfig, CorpConfigSchema, GlobalConfig, GlobalConfigInput, GlobalConfigSchema } from "./types.js"; @@ -16,6 +16,75 @@ export const LEGACY_USER_ENTERPRISES_DIR = join(homedir(), ".config", "seal-home const PRIMARY_GLOBAL_CONFIG_FILE = join(homedir(), ".config", "seal-use", "config.json"); const LEGACY_GLOBAL_CONFIG_FILE = join(homedir(), ".config", "seal-home", "config.json"); +export function syncLegacyEnterprisesDir( + primaryDir: string = PRIMARY_USER_ENTERPRISES_DIR, + legacyDir: string = LEGACY_USER_ENTERPRISES_DIR +): { copied: string[]; deletedLegacyDir: boolean } { + if (!existsSync(legacyDir)) { + return { copied: [], deletedLegacyDir: false }; + } + + mkdirSync(primaryDir, { recursive: true, mode: 0o700 }); + const copied: string[] = []; + + let legacyFiles: string[] = []; + try { + legacyFiles = readdirSync(legacyDir).filter((file) => file.endsWith(".json")); + } catch { + return { copied: [], deletedLegacyDir: false }; + } + + for (const file of legacyFiles) { + const legacyPath = join(legacyDir, file); + const primaryPath = join(primaryDir, file); + + if (!existsSync(primaryPath)) { + copyFileSync(legacyPath, primaryPath); + copied.push(file); + } else { + const legacyMtime = statSync(legacyPath).mtimeMs; + const primaryMtime = statSync(primaryPath).mtimeMs; + if (legacyMtime > primaryMtime) { + copyFileSync(legacyPath, primaryPath); + copied.push(file); + } + } + } + + const SEVEN_DAYS_MS = 7 * 24 * 60 * 60 * 1000; + const now = Date.now(); + + let hasNewerFileInLegacy = false; + let hasRecentlyModifiedFile = false; + + for (const file of readdirSync(legacyDir)) { + const legacyPath = join(legacyDir, file); + const primaryPath = join(primaryDir, file); + const stat = statSync(legacyPath); + + if (now - stat.mtimeMs < SEVEN_DAYS_MS) { + hasRecentlyModifiedFile = true; + } + + if (file.endsWith(".json") && existsSync(primaryPath)) { + if (stat.mtimeMs > statSync(primaryPath).mtimeMs) { + hasNewerFileInLegacy = true; + } + } + } + + const dirStat = statSync(legacyDir); + const dirRecentlyModified = now - dirStat.mtimeMs < SEVEN_DAYS_MS; + + let deletedLegacyDir = false; + if (!hasNewerFileInLegacy && !hasRecentlyModifiedFile && !dirRecentlyModified) { + rmSync(legacyDir, { recursive: true, force: true }); + deletedLegacyDir = true; + } + + return { copied, deletedLegacyDir }; +} + export function loadCorpConfigs(): CorpConfig[] { const dirs = resolveEnterprisesDirs(); const byId = new Map(); @@ -42,9 +111,19 @@ function resolveEnterprisesDirs(): string[] { if (configured) return existsSync(configured) ? [configured] : []; const configuredUserDir = (process.env[USER_DIR_ENV] ?? process.env[LEGACY_USER_DIR_ENV])?.trim(); - const userDirs = configuredUserDir - ? (existsSync(configuredUserDir) ? [configuredUserDir] : []) - : [PRIMARY_USER_ENTERPRISES_DIR, LEGACY_USER_ENTERPRISES_DIR]; + let userDirs: string[]; + + if (configuredUserDir) { + userDirs = existsSync(configuredUserDir) ? [configuredUserDir] : []; + } else { + try { + syncLegacyEnterprisesDir(); + } catch {} + + userDirs = existsSync(PRIMARY_USER_ENTERPRISES_DIR) + ? [PRIMARY_USER_ENTERPRISES_DIR] + : (existsSync(LEGACY_USER_ENTERPRISES_DIR) ? [LEGACY_USER_ENTERPRISES_DIR] : [PRIMARY_USER_ENTERPRISES_DIR]); + } const globalConfig = loadGlobalConfig(); const testDir = globalConfig.testEnterprisesDir?.trim();