bgenc.net/scripts/new-post.js

24 lines
640 B
JavaScript

import { format, formatISO } from 'date-fns';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const title = process.argv[2] ?? 'New Blog Post';
const slug = title.toLowerCase().replaceAll(' ', '-').slice(0, 60);
const filename = `${format(new Date(), 'yyyy.MM.dd')}.${slug}.md`;
const contents = `---
title: '${title}'
date: ${formatISO(new Date())}
description: ''
---
`;
console.log('dirname', __dirname);
console.log('filename', filename);
fs.writeFileSync(path.join(__dirname, '..', 'src', 'routes', 'posts', filename), contents);