obsidian-plugin-config
Version:
Global CLI injection tool for Obsidian plugins
105 lines (84 loc) ⢠3.04 kB
text/typescript
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);