Add Docker Build Support
Blog posts with 'draft: true' in the frontmatter are excluded from the production artifact --no-cache docker builds ensure fresh database build each time. Caching isn't needed do to small size anyway
This commit is contained in:
85
init-db.ts
Normal file
85
init-db.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
const { Database } = require("bun:sqlite");
|
||||
|
||||
async function waitForDatabase() {
|
||||
const serverProcess = Bun.spawn(["bun", "run", "index.tsx"], {
|
||||
env: { ...process.env, NODE_ENV: "development" },
|
||||
stdout: "pipe",
|
||||
stderr: "pipe"
|
||||
});
|
||||
|
||||
// Count expected markdown files - this gives us total potential posts
|
||||
const files = [];
|
||||
for await (const file of new Bun.Glob("**/*.md").scan("./content")) {
|
||||
files.push(file);
|
||||
}
|
||||
const totalMarkdownFiles = files.length;
|
||||
console.log(`Found ${totalMarkdownFiles} markdown files to process`);
|
||||
|
||||
const maxWait = 30000; // 30 seconds total timeout
|
||||
const checkInterval = 100; // Check every 100ms
|
||||
let waited = 0;
|
||||
let stableCount = 0;
|
||||
let previousCount = -1;
|
||||
|
||||
while (waited < maxWait) {
|
||||
if (await Bun.file("blog.sqlite").exists()) {
|
||||
try {
|
||||
const db = new Database("blog.sqlite", { readonly: true });
|
||||
|
||||
// Count only published posts (routes ending without .md)
|
||||
// This matches the logic used in the actual application queries
|
||||
const result = db.query("SELECT COUNT(*) as count FROM posts WHERE path NOT LIKE '%.md'").get();
|
||||
const publishedPosts = result.count;
|
||||
db.close();
|
||||
|
||||
if (publishedPosts === previousCount) {
|
||||
stableCount++;
|
||||
} else {
|
||||
stableCount = 0;
|
||||
previousCount = publishedPosts;
|
||||
console.log(`Importing progress: ${publishedPosts}/${totalMarkdownFiles} published posts`);
|
||||
}
|
||||
|
||||
// If the count has been stable for 10 consecutive checks (1 second), consider import complete
|
||||
// We check against available posts, not total markdown files, since some may be drafts
|
||||
if (stableCount >= 10 && publishedPosts >= 1) {
|
||||
console.log(`Import complete: ${publishedPosts} published posts ready`);
|
||||
break;
|
||||
}
|
||||
} catch (e) {
|
||||
// Database might still be writing or locked
|
||||
}
|
||||
}
|
||||
|
||||
await new Promise(resolve => setTimeout(resolve, checkInterval));
|
||||
waited += checkInterval;
|
||||
}
|
||||
|
||||
// Check if we succeeded
|
||||
if (!(await Bun.file("blog.sqlite").exists())) {
|
||||
console.error("Database file was not created");
|
||||
await serverProcess.kill();
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Final verification - check published posts count
|
||||
const db = new Database("blog.sqlite", { readonly: true });
|
||||
const result = db.query("SELECT COUNT(*) as count FROM posts WHERE path NOT LIKE '%.md'").get();
|
||||
const finalPublishedPosts = result.count;
|
||||
|
||||
// Also get total posts for informational purposes
|
||||
const totalResult = db.query("SELECT COUNT(*) as count FROM posts").get();
|
||||
const totalPosts = totalResult.count;
|
||||
db.close();
|
||||
|
||||
if (finalPublishedPosts < 1) {
|
||||
console.error(`No published posts found! Total: ${totalPosts}, Published: ${finalPublishedPosts}`);
|
||||
await serverProcess.kill();
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
await serverProcess.kill();
|
||||
console.log(`Database initialization complete: ${totalPosts} total posts (${finalPublishedPosts} published)`);
|
||||
}
|
||||
|
||||
waitForDatabase();
|
||||
Reference in New Issue
Block a user