-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewpost.js
46 lines (39 loc) · 1.31 KB
/
newpost.js
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
#!/usr/bin/env node
/* eslint-disable no-console */
/*!
* gatsby-new-post-cli v1.0.0
*
* https://github.com/equk/gatsby-new-post-cli
*
* Copyright (c) 2019 B.Walden. All rights reserved.
*
* Licensed under the MIT License
*
* (LICENSE file should be included with script)
*/
const fs = require('fs')
const readline = require('readline')
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
})
rl.question('Post Title:', (title) => {
const author = 'equilibriumuk'
const dateNow = new Date()
const year = dateNow.getFullYear()
const month = `${dateNow.getMonth() + 1 < 10 ? '0' : ''}${dateNow.getMonth() + 1}`
const day = `${dateNow.getDate() < 10 ? '0' : ''}${dateNow.getDate()}`
const blogPostFolder = './content/posts'
const blogPostDate = `${blogPostFolder}/${year}-${month}-${day}`
if (!fs.existsSync(blogPostFolder)) {
console.log(`ERROR: posts folder not found: ${blogPostFolder}`)
rl.close()
process.exit(1)
}
const output = `---\nslug:\ntitle: "${title}"\ndate: ${dateNow.toISOString()}\ndraft: true\nauthor: ${author}\ntags:\nimage:\n---\n\n`
fs.writeFileSync(`${blogPostDate}-${title}.md`, output)
console.log(`Post ${title} was created successfully`)
console.log(`Location: ${blogPostDate}-${title}.md`)
rl.close()
process.exit(0)
})