jacobtread_obsidian-timekeep/scripts/dev.js

70 lines
1.7 KiB
JavaScript
Raw Normal View History

import fs from "fs/promises";
2026-03-28 02:29:31 +00:00
import path from "path";
import { fileURLToPath } from "url";
2026-03-28 02:29:31 +00:00
import { build } from "vite";
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");
2026-03-26 03:05:06 +00:00
const manifestContents = await fs.readFile(manifestPath, "utf-8");
const manifest = JSON.parse(manifestContents);
2026-03-26 03:05:06 +00:00
const vaultPath = path.join(rootPath, "test-vault");
const pluginPath = path.join(vaultPath, ".obsidian", "plugins", manifest.id);
2026-03-26 03:05:06 +00:00
await ensureDir(pluginPath);
await ensureDir(outputPath);
2026-03-26 03:05:06 +00:00
const inputFiles = [
path.join(outputPath, "main.js"),
path.join(outputPath, "styles.css"),
manifestPath,
];
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");
void copyFiles();
2026-03-26 03:05:06 +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);
}
}
};
2026-03-26 03:05:06 +00:00
console.info("🚀 Dev server started. Watching for changes...");
}
async function ensureDir(dir) {
2026-03-26 03:05:06 +00:00
try {
await fs.mkdir(dir, { recursive: true });
} catch {}
}
dev().catch((err) => {
2026-03-26 03:05:06 +00:00
console.error(err);
process.exit(1);
});