2026-03-26 02:36:04 +00:00
|
|
|
import fs from "fs/promises";
|
2026-03-28 02:29:31 +00:00
|
|
|
import path from "path";
|
2026-03-26 02:36:04 +00:00
|
|
|
import { fileURLToPath } from "url";
|
2026-03-28 02:29:31 +00:00
|
|
|
import { build } from "vite";
|
2025-07-18 11:17:31 +00:00
|
|
|
|
|
|
|
|
async function dev() {
|
2026-03-26 03:05:06 +00:00
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
|
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
|
const rootPath = path.resolve(__dirname, "../");
|
|
|
|
|
const manifestPath = path.join(rootPath, "manifest.json");
|
|
|
|
|
const outputPath = path.join(rootPath, "dist");
|
2025-07-18 11:17:31 +00:00
|
|
|
|
2026-03-26 03:05:06 +00:00
|
|
|
const manifestContents = await fs.readFile(manifestPath, "utf-8");
|
|
|
|
|
const manifest = JSON.parse(manifestContents);
|
2025-07-18 11:17:31 +00:00
|
|
|
|
2026-03-26 03:05:06 +00:00
|
|
|
const vaultPath = path.join(rootPath, "test-vault");
|
|
|
|
|
const pluginPath = path.join(vaultPath, ".obsidian", "plugins", manifest.id);
|
2025-07-18 11:17:31 +00:00
|
|
|
|
2026-03-26 03:05:06 +00:00
|
|
|
await ensureDir(pluginPath);
|
|
|
|
|
await ensureDir(outputPath);
|
2025-07-18 11:17:31 +00:00
|
|
|
|
2026-03-26 03:05:06 +00:00
|
|
|
const inputFiles = [
|
|
|
|
|
path.join(outputPath, "main.js"),
|
|
|
|
|
path.join(outputPath, "styles.css"),
|
|
|
|
|
manifestPath,
|
|
|
|
|
];
|
2025-07-18 11:17:31 +00:00
|
|
|
|
2026-03-26 03:08:22 +00:00
|
|
|
void build({
|
2026-03-26 03:05:06 +00:00
|
|
|
mode: "development",
|
|
|
|
|
configFile: path.resolve(rootPath, "vite.config.js"),
|
|
|
|
|
build: {
|
|
|
|
|
watch: {},
|
|
|
|
|
},
|
|
|
|
|
plugins: [
|
|
|
|
|
{
|
|
|
|
|
name: "build-finish-copy",
|
|
|
|
|
closeBundle() {
|
|
|
|
|
console.info("Build finished copying files");
|
2026-03-26 03:08:22 +00:00
|
|
|
void copyFiles();
|
2026-03-26 03:05:06 +00:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
],
|
|
|
|
|
});
|
2025-07-18 11:17:31 +00:00
|
|
|
|
2026-03-26 03:05:06 +00:00
|
|
|
const copyFiles = async () => {
|
|
|
|
|
for (const filePath of inputFiles) {
|
|
|
|
|
const destPath = path.join(pluginPath, path.basename(filePath));
|
|
|
|
|
try {
|
|
|
|
|
await fs.copyFile(filePath, destPath);
|
|
|
|
|
console.info(`✓ Updated ${path.basename(filePath)}`);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(`Failed to copy ${path.basename(filePath)}:`, e.message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-07-18 11:17:31 +00:00
|
|
|
|
2026-03-26 03:05:06 +00:00
|
|
|
console.info("🚀 Dev server started. Watching for changes...");
|
2025-07-18 11:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 02:36:04 +00:00
|
|
|
async function ensureDir(dir) {
|
2026-03-26 03:05:06 +00:00
|
|
|
try {
|
|
|
|
|
await fs.mkdir(dir, { recursive: true });
|
|
|
|
|
} catch {}
|
2025-07-18 11:17:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dev().catch((err) => {
|
2026-03-26 03:05:06 +00:00
|
|
|
console.error(err);
|
|
|
|
|
process.exit(1);
|
2026-03-26 02:36:04 +00:00
|
|
|
});
|