UNPKG

obsidian-plugin-config

Version:

Global CLI injection tool for Obsidian plugins

105 lines (84 loc) • 3.04 kB
#!/usr/bin/env tsx import { readFile } from 'fs/promises'; import path from 'path'; import { analyzePlugin, ensurePluginConfigClean, performInjection, showInjectionPlan } from './inject-core.js'; import { askConfirmation, askQuestion, createReadlineInterface, isValidPath } from './utils.js'; const rl = createReadlineInterface(); async function promptForTargetPath(): Promise<string> { console.log(`\nšŸ“ Select target plugin directory:`); console.log(` Common paths (copy-paste ready):`); console.log(` - ../test-sample-plugin`); console.log(` šŸ’” Tip: You can paste paths with or without quotes`); while (true) { const rawInput = await askQuestion(`\nEnter plugin directory path: `, rl); if (!rawInput.trim()) { console.log(`āŒ Please enter a valid path`); continue; } const cleanPath = rawInput.trim().replace(/^["']|["']$/g, ''); const resolvedPath = path.resolve(cleanPath); if (!(await isValidPath(resolvedPath))) { console.log(`āŒ Directory not found: ${resolvedPath}`); const retry = await askConfirmation(`Try again?`, rl); if (!retry) throw new Error('User cancelled'); continue; } console.log(`šŸ“ Target directory: ${resolvedPath}`); return resolvedPath; } } async function main(): Promise<void> { try { console.log(`šŸŽÆ Obsidian Plugin Config - Interactive Injection Tool`); console.log(`šŸ“„ Inject autonomous configuration with prompts\n`); const args = process.argv.slice(2); let targetPath: string; const pathArg = args.find((arg) => !arg.startsWith('-')); if (pathArg) { const cleanPath = pathArg.trim().replace(/^["']|["']$/g, ''); targetPath = path.resolve(cleanPath); if (!(await isValidPath(targetPath))) { console.error(`āŒ Directory not found: ${targetPath}`); process.exit(1); } console.log(`šŸ“ Using provided path: ${targetPath}`); } else { targetPath = await promptForTargetPath(); } // Prevent injecting into obsidian-plugin-config itself const selfPkg = path.join(targetPath, 'package.json'); if (await isValidPath(selfPkg)) { const pkg = JSON.parse(await readFile(selfPkg, 'utf8')); if (pkg.name === 'obsidian-plugin-config') { console.error(`āŒ Cannot inject into obsidian-plugin-config itself.`); process.exit(1); } } console.log(`\nšŸ” Checking plugin-config repository status...`); await ensurePluginConfigClean(); console.log(`\nšŸ” Analyzing plugin...`); const plan = await analyzePlugin(targetPath); const confirmed = await showInjectionPlan(plan, false); if (!confirmed) { console.log(`āŒ Injection cancelled by user`); process.exit(0); } await performInjection(targetPath, false); } catch (error) { console.error(`šŸ’„ Error: ${error instanceof Error ? error.message : String(error)}`); process.exit(1); } finally { rl.close(); } } main().catch(console.error);