const fs = require("fs").promises
const fsSync = require("fs")
const path = require("path")
const matter = require("gray-matter")
const { execSync } = require("child_process")
outputDir: "astroContent",
validExtensions: [".md"],
localDestinationRoot: "", // to edit
localDestination: "", // to edit
* Ensures output directory exists
function initializeOutputDir() {
if (!fsSync.existsSync(CONFIG.outputDir)) {
fsSync.mkdirSync(CONFIG.outputDir, { recursive: true })
* Validates if a file should be processed based on its metadata
function shouldProcessFile(frontMatter) {
return frontMatter.published === true && !frontMatter.notAstro
* Processes a single markdown file
async function processMarkdownFile(fullPath, outputPath) {
const fileContent = await fs.readFile(fullPath, "utf8")
const { data } = matter(fileContent)
if (shouldProcessFile(data)) {
await fs.copyFile(fullPath, outputPath)
console.log(`✓ Copied: ${path.relative(CONFIG.inputDir, fullPath)}`)
console.error(`Error processing file ${fullPath}:`, error.message)
* Recursively processes directories and files
async function processDirectory(dir) {
const files = await fs.readdir(dir)
files.map(async (file) => {
const fullPath = path.join(dir, file)
const relativePath = path.relative(CONFIG.inputDir, fullPath)
const outputPath = path.join(CONFIG.outputDir, relativePath)
const stats = await fs.stat(fullPath)
if (stats.isDirectory()) {
if (CONFIG.ignoreList.includes(file)) return
await fs.mkdir(outputPath, { recursive: true })
await processDirectory(fullPath)
} else if (path.extname(file) === ".md" && file !== "index.md") {
await processMarkdownFile(fullPath, outputPath)
console.error(`Error processing directory ${dir}:`, error.message)
* Copies processed files to final destination and commits changes
async function copyToLocalDestination() {
// Remove existing content
if (fsSync.existsSync(CONFIG.localDestination)) {
await fs.rm(CONFIG.localDestination, { recursive: true })
// Create destination directory
await fs.mkdir(CONFIG.localDestination, { recursive: true })
const files = await fs.readdir(CONFIG.outputDir)
files.map(async (file) => {
const sourcePath = path.join(CONFIG.outputDir, file)
const destPath = path.join(CONFIG.localDestination, file)
await fs.rename(sourcePath, destPath)
console.log("Content has been successfully copied to the local destination.")
execSync(`cd ${path.dirname(CONFIG.localDestination)} && npm run sort`)
execSync(`git add ${CONFIG.localDestination} && git commit -m "update content"`)
console.error("Error copying to destination:", error.message)
* Cleans up the temporary output directory
async function cleanupOutputDir() {
if (fsSync.existsSync(CONFIG.outputDir)) {
await fs.rm(CONFIG.outputDir, { recursive: true })
console.log("Cleaned up temporary directory.")
console.error("Error cleaning up:", error.message)
const isLocalBuild = process.argv.includes("--local")
console.log(`Starting document processing... (${isLocalBuild ? "local" : "server"} build)`)
await processDirectory(CONFIG.inputDir)
await copyToLocalDestination()
console.log("Document processing completed successfully!")
console.error("Fatal error:", error.message)