#!/usr/bin/env node
import { mkdirSync, writeFileSync, existsSync } from 'node:fs'
import { join, resolve } from 'node:path'

const name = process.argv[2]
if (!name) {
  console.error('Usage: pnpm new-post <name>')
  process.exit(1)
}

const now = new Date()
const date = now.toISOString().slice(0, 19)

const dir = resolve('content/posts', name)
const file = join(dir, 'index.md')
const imgDir = join(dir, 'img')

if (existsSync(file)) {
  console.error(`Already exists: ${file}`)
  process.exit(1)
}

mkdirSync(dir, { recursive: true })
mkdirSync(imgDir, { recursive: true })

const content = `---
title: ${name}
published: ${date}
description:
image: /posts/${name}/img/cover.jpg
tags: []
draft: false
---

在这里写文章内容...
`

writeFileSync(file, content, 'utf8')
console.log(`Created: ${file}`)
console.log(`Image dir: ${imgDir}`)
