Files
platform/src/servers/sync-tools.ts
T

38 lines
1.4 KiB
TypeScript

import { readdirSync, readFileSync, existsSync, mkdirSync, cpSync, rmSync } from 'node:fs';
import { join } from 'node:path';
import { SEED_PATH, DATA_PATH } from './data-path';
import { parseSeedVersion } from './sync-version';
const SEED_TOOLS_DIR = join(SEED_PATH, 'tools');
const GLOBAL_TOOLS_DIR = join(DATA_PATH, 'tools');
export function syncSeedTools(): void {
if (!existsSync(SEED_TOOLS_DIR)) return;
mkdirSync(GLOBAL_TOOLS_DIR, { recursive: true });
const seedEntries = readdirSync(SEED_TOOLS_DIR, { withFileTypes: true });
for (const entry of seedEntries) {
if (!entry.isDirectory()) continue;
const seedToolDir = join(SEED_TOOLS_DIR, entry.name);
const seedFile = join(seedToolDir, 'TOOL.md');
if (!existsSync(seedFile)) continue;
const targetDir = join(GLOBAL_TOOLS_DIR, entry.name);
const targetFile = join(targetDir, 'TOOL.md');
if (existsSync(targetDir)) {
const seedVersion = parseSeedVersion(readFileSync(seedFile, 'utf-8'));
const targetVersion = existsSync(targetFile) ? parseSeedVersion(readFileSync(targetFile, 'utf-8')) : 0;
if (seedVersion <= targetVersion) continue;
rmSync(targetDir, { recursive: true });
console.log(`[tools] Updating seed tool: ${entry.name} (v${targetVersion} → v${seedVersion})`);
}
cpSync(seedToolDir, targetDir, { recursive: true });
console.log(`[tools] Synced seed tool: ${entry.name}`);
}
}