obsidian-plugin-confidential/scripts/symlink-to-vault.ts
Felipe M. 553e088ffd
chore: scaffold Confidential plugin with bun-based toolchain
Boilerplate derived from obsidianmd/obsidian-sample-plugin, adapted to
mirror obsidian-plugin-auto-task-status: bun scripts, esbuild with
git-tag versioning, prettier/eslint/jest, husky pre-commit, Forgejo
CI/release workflows, and vault install/symlink helpers.
2026-04-18 11:55:02 +02:00

51 lines
1.6 KiB
TypeScript

import { existsSync, lstatSync, symlinkSync, unlinkSync } from 'fs';
import { resolve } from 'path';
const manifest = await Bun.file('manifest.json').json();
const pluginId: string = manifest.id;
const vaultPath = process.argv[2] || process.env.OBSIDIAN_VAULT;
if (!vaultPath) {
console.error(
'Usage: bun run scripts/symlink-to-vault.ts <vault-path>\n' + ' or set OBSIDIAN_VAULT environment variable',
);
process.exit(1);
}
const resolvedVault = resolve(vaultPath);
const pluginsDir = resolve(resolvedVault, '.obsidian', 'plugins');
const targetDir = resolve(pluginsDir, pluginId);
const distDir = resolve(import.meta.dir, '..', 'dist');
if (!existsSync(pluginsDir)) {
console.error(`Invalid vault path: ${resolvedVault}`);
console.error(`.obsidian/plugins directory not found at ${pluginsDir}`);
process.exit(1);
}
if (!existsSync(distDir)) {
console.error(`Build not found: run 'bun run build' first`);
process.exit(1);
}
if (existsSync(targetDir)) {
if (lstatSync(targetDir).isSymbolicLink()) {
const link = lstatSync(targetDir);
const linkTarget = resolve(pluginsDir, link.path);
if (linkTarget !== distDir) {
unlinkSync(targetDir);
symlinkSync(distDir, targetDir);
console.log(`Updated symlink: ${targetDir} -> ${distDir}`);
} else {
console.log(`Symlink already correct: ${targetDir} -> ${distDir}`);
}
} else {
console.error(`${targetDir} exists and is not a symlink. Refusing to remove for safety.`);
process.exit(1);
}
} else {
symlinkSync(distDir, targetDir);
console.log(`Created symlink: ${targetDir} -> ${distDir}`);
}