Auto-migrate legacy enterprise directory and drop legacy reads
This commit is contained in:
1 parent
08cbc541b4
commit
3d16857aeb
2 files changed
+118
-5
No files matched your search
@@ -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<string, CorpConfig>();
|
||||
@@ -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();
|
||||
|
||||
Reference in new issue
Block a user