-
Notifications
You must be signed in to change notification settings - Fork 0
/
new-blog.ts
135 lines (120 loc) · 2.99 KB
/
new-blog.ts
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import fs from 'fs-extra'
import path from 'path'
import inquirer from 'inquirer'
async function go() {
console.log("\nLet's create a new blog 💿\n")
const blogsPath = path.resolve(process.cwd(), 'src', 'content', 'blog')
if (!fs.existsSync(blogsPath)) {
fs.mkdirSync(blogsPath)
}
const blogs = fs
.readdirSync(blogsPath)
.map(blog => blog.replace(/(\.md?)?$/, ''))
const slug = (
await inquirer.prompt<{ name: string }>([
{
type: 'input',
name: 'name',
message: 'What is the slug of this post?',
validate: input => {
if (!input) {
return 'Enter a valid name for the blog.'
}
if (blogs.includes(input)) {
return `Blog named ${input} alread exist, enter another blog name.`
}
return true
},
},
])
).name
const title = (
await inquirer.prompt<{ name: string }>([
{
type: 'input',
name: 'name',
message: 'What is the title of the blog?',
validate: input => {
if (!input) {
return 'Enter a valid name for the blog.'
}
return true
},
},
])
).name
const { tags, description, draft, featured } = await inquirer.prompt<{
description: string
draft: boolean
featured: string
tags: string
}>([
{
type: 'input',
name: 'tags',
message: 'Enter the blog keywords (comma separated)',
filter: (input: string) => input.trim(),
validate: (input: string) => {
if (input.trim().length === 0) {
return 'Enter a keyword'
}
return true
},
},
{
type: 'input',
name: 'description',
message: 'Enter the blog description',
filter: (input: string) => input.trim(),
validate: (input: string) => {
if (input.trim().length === 0) {
return 'Enter a description for the blog'
}
return true
},
},
{
type: 'list',
name: 'draft',
message: 'Is the blog ready to be published?',
choices: [
{ name: 'Publish', value: false },
{ name: 'Draft', value: true },
],
},
{
type: 'list',
name: 'featured',
message: 'Will this blog be featured?',
choices: [
{ name: 'No', value: false },
{ name: 'Yes', value: true },
],
},
])
const data = `---
title: ${title}
pubDatetime: ${new Date().toISOString()}
postSlug: ${slug}
featured: ${featured}
draft: ${draft}
tags:
${tags
.split(/, ?/)
.map(tag => ` - ${tag}`)
.join('\n')}
description: ${description}
---
`
let relativePath = ''
const filePath = path.resolve(blogsPath, `${slug}.md`)
relativePath = path.relative(process.cwd(), filePath)
fs.writeFileSync(filePath, data)
console.log(
`\nBlog created 🚀\n\`cd\` into ${relativePath}\nOpen it in you favorite text editor, and get started!\n`,
)
}
go().catch(err => {
console.error(err)
process.exit(1)
})