1
0
mirror of https://github.com/kremalicious/blog.git synced 2024-12-02 14:16:11 +01:00
blog/scripts/new.js

33 lines
875 B
JavaScript
Raw Normal View History

2018-09-24 01:09:50 +02:00
import fs from 'fs'
import path from 'path'
import slugify from 'slugify'
import ora from 'ora'
const templatePath = path.join(__dirname, 'new.md')
const template = fs.readFileSync(templatePath).toString()
const spinner = ora('Adding new project').start()
if (!process.argv[2]) {
2018-09-29 23:05:14 +02:00
spinner.fail('Use the format `npm run new "Title of post"`')
2018-09-24 01:09:50 +02:00
}
const title = process.argv[2]
spinner.text = `Adding '${title}'.`
const titleSlug = slugify(title, { lower: true })
2018-09-29 23:05:14 +02:00
const postsPath = path.join('.', 'content', 'posts')
const date = new Date().toISOString()
2018-09-24 01:09:50 +02:00
const newContents = template
.split('TITLE')
.join(title)
.split('DATE')
2018-09-29 23:05:14 +02:00
.join(date)
2018-09-24 01:09:50 +02:00
2018-09-29 23:05:14 +02:00
const datePath = date.slice(0, 10)
fs.appendFile(`${postsPath}/${datePath}-${titleSlug}.md`, newContents, err => {
if (err) spinner.fail(`Error creating post: ${err}`)
spinner.succeed(`New post '${title}' created.`)
})