-
Notifications
You must be signed in to change notification settings - Fork 0
/
new-post.mjs
46 lines (40 loc) · 1007 Bytes
/
new-post.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { mkdirSync, writeFileSync } from "fs";
import path from "path";
import inquirer from "inquirer";
import chalk from "chalk";
import { DateTime } from "luxon";
import slugify from "slugify";
// Ask the name of the new post and create it
const questions = [
{
type: "input",
name: "title",
message: "What is the title of the new post?",
},
];
inquirer.prompt(questions).then((answers) => {
const title = answers.title;
const slug = slugify(title, {
remove: /[_*+~.()'"!:@]/g,
strict: true,
lower: true,
});
const date = DateTime.local().toFormat("yyyy-LL-dd");
const year = DateTime.local().toFormat("yyyy");
const filepath = `./src/content/blog/${year}/${slug}/index.md`;
// Create the directory and file
mkdirSync(path.dirname(filepath), { recursive: true });
writeFileSync(
filepath,
`---
title: "${title}"
desc: ""
date: ${date}
tags: []
draft: true
---
This is a draft post
`,
);
console.log(chalk.green(`Created ${filepath}`));
});