Files
manyplug/src/commands/sync.js
synt-xerror 649c7bb05a feat: add sync, update, and remove commands
- sync: scans plugins and updates registry.json
- update: checks and updates plugins to match registry
- remove: removes installed plugin with --yes and --remove-deps options
- registry.json stored at project root with plugin metadata and _sourcePath
2026-04-09 01:19:33 -03:00

76 lines
2.4 KiB
JavaScript

import fs from 'fs-extra';
import path from 'path';
import chalk from 'chalk';
const PLUGINS_DIR = path.resolve('src/plugins');
const REGISTRY_PATH = path.resolve('registry.json');
/**
* Sync registry.json with installed plugins.
* Scans all plugins in src/plugins/ and updates registry.json
*/
export async function syncCommand() {
console.log(chalk.blue('Syncing registry...'));
// Load existing registry or create new one
let registry = { lastUpdated: new Date().toISOString(), plugins: {} };
if (await fs.pathExists(REGISTRY_PATH)) {
try {
registry = await fs.readJson(REGISTRY_PATH);
} catch (err) {
console.warn(chalk.yellow('⚠️ Could not read existing registry, creating new one'));
}
}
// Scan plugins directory
if (!await fs.pathExists(PLUGINS_DIR)) {
console.warn(chalk.yellow('⚠️ No plugins directory found'));
await fs.writeJson(REGISTRY_PATH, registry, { spaces: 2 });
console.log(chalk.green(`✅ Registry updated at ${path.relative(process.cwd(), REGISTRY_PATH)}`));
return;
}
const entries = await fs.readdir(PLUGINS_DIR, { withFileTypes: true });
let updated = 0;
let added = 0;
for (const entry of entries) {
if (!entry.isDirectory()) continue;
const manifestPath = path.join(PLUGINS_DIR, entry.name, 'manyplug.json');
if (!await fs.pathExists(manifestPath)) continue;
try {
const manifest = await fs.readJson(manifestPath);
const pluginName = manifest.name || entry.name;
// Check if plugin exists in registry and version changed
const existing = registry.plugins[pluginName];
if (!existing) {
added++;
} else if (existing.version !== manifest.version) {
updated++;
}
registry.plugins[pluginName] = {
...manifest,
_syncedAt: new Date().toISOString(),
_sourcePath: path.join('src/plugins', entry.name)
};
} catch (err) {
console.warn(chalk.yellow(`⚠️ Failed to read ${entry.name}: ${err.message}`));
}
}
// Update timestamp
registry.lastUpdated = new Date().toISOString();
await fs.writeJson(REGISTRY_PATH, registry, { spaces: 2 });
console.log(chalk.green(`✅ Registry synced`));
console.log(chalk.gray(` Added: ${added}`));
console.log(chalk.gray(` Updated: ${updated}`));
console.log(chalk.gray(` Total plugins: ${Object.keys(registry.plugins).length}`));
}