2018-11-01 19:35:18 +01:00
|
|
|
import fs from 'fs-extra'
|
2018-09-24 01:09:50 +02:00
|
|
|
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)
|
2018-11-01 19:35:18 +01:00
|
|
|
.split('TITLE_SLUG')
|
|
|
|
.join(titleSlug)
|
2018-09-24 01:09:50 +02:00
|
|
|
.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)
|
2018-11-01 19:35:18 +01:00
|
|
|
const file = `${postsPath}/${datePath}-${titleSlug}/index.md`
|
2018-09-29 23:05:14 +02:00
|
|
|
|
2018-11-01 19:35:18 +01:00
|
|
|
fs.outputFile(file, newContents)
|
|
|
|
.then(() => fs.readFile(file, 'utf8'))
|
|
|
|
.then(() => {
|
|
|
|
spinner.succeed(`New post '${title}' created.`)
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
spinner.fail(`Error creating post: ${err}`)
|
|
|
|
})
|
|
|
|
|
|
|
|
// fs.appendFile(`${postsPath}/${datePath}-${titleSlug}.md`, newContents, err => {
|
|
|
|
// if (err) spinner.fail(`Error creating post: ${err}`)
|
|
|
|
// spinner.succeed(`New post '${title}' created.`)
|
|
|
|
// })
|