starter-blog/scripts/compose.js

123 lines
3.2 KiB
JavaScript
Raw Normal View History

2021-01-17 04:16:25 +01:00
const fs = require('fs')
const path = require('path')
2021-07-04 10:12:36 +02:00
const inquirer = require('inquirer')
const dedent = require('dedent')
2021-01-17 04:16:25 +01:00
const root = process.cwd()
const getAuthors = () => {
const authorPath = path.join(root, 'data', 'authors')
const authorList = fs.readdirSync(authorPath).map((filename) => path.parse(filename).name)
return authorList
}
const getLayouts = () => {
const layoutPath = path.join(root, 'layouts')
const layoutList = fs
.readdirSync(layoutPath)
.map((filename) => path.parse(filename).name)
.filter((file) => file.toLowerCase().includes('post'))
return layoutList
}
2021-07-04 10:12:36 +02:00
const genFrontMatter = (answers) => {
let d = new Date()
const date = [
d.getFullYear(),
('0' + (d.getMonth() + 1)).slice(-2),
('0' + d.getDate()).slice(-2),
].join('-')
const tagArray = answers.tags.split(',')
2021-07-04 10:12:36 +02:00
tagArray.forEach((tag, index) => (tagArray[index] = tag.trim()))
const tags = "'" + tagArray.join("','") + "'"
const authorArray = answers.authors.length > 0 ? "'" + answers.authors.join("','") + "'" : ''
let frontMatter = dedent`---
2021-07-04 10:12:36 +02:00
title: ${answers.title ? answers.title : 'Untitled'}
date: '${date}'
tags: [${answers.tags ? tags : ''}]
draft: ${answers.draft === 'yes' ? true : false}
summary: ${answers.summary ? answers.summary : ' '}
images: []
layout: ${answers.layout}
2021-07-04 10:12:36 +02:00
`
if (answers.authors.length > 0) {
frontMatter = frontMatter + '\n' + `authors: [${authorArray}]`
}
frontMatter = frontMatter + '\n---'
2021-07-04 10:12:36 +02:00
return frontMatter
}
2021-01-17 04:16:25 +01:00
2021-07-04 10:12:36 +02:00
inquirer
.prompt([
{
name: 'title',
message: 'Enter post title:',
type: 'input',
},
{
2021-07-04 18:23:33 +02:00
name: 'extension',
2021-07-04 10:12:36 +02:00
message: 'Choose post extension:',
type: 'list',
choices: ['mdx', 'md'],
},
{
name: 'authors',
message: 'Choose authors:',
type: 'checkbox',
choices: getAuthors,
},
2021-07-04 10:12:36 +02:00
{
name: 'summary',
message: 'Enter post summary:',
type: 'input',
},
{
name: 'draft',
message: 'Set post as draft?',
type: 'list',
choices: ['yes', 'no'],
},
{
name: 'tags',
message: 'Any Tags? Separate them with , or leave empty if no tags.',
type: 'input',
},
{
name: 'layout',
message: 'Select layout',
type: 'list',
choices: getLayouts,
},
2021-07-04 10:12:36 +02:00
])
.then((answers) => {
// Remove special characters and replace space with -
const fileName = answers.title
.toLowerCase()
.replace(/[^a-zA-Z0-9 ]/g, '')
.replace(/ /g, '-')
.replace(/-+/g, '-')
const frontMatter = genFrontMatter(answers)
2022-01-24 08:36:34 +01:00
if (!fs.existsSync('data/blog')) fs.mkdirSync('data/blog', { recursive: true })
2021-07-04 10:12:36 +02:00
const filePath = `data/blog/${fileName ? fileName : 'untitled'}.${
2021-07-04 18:23:33 +02:00
answers.extension ? answers.extension : 'md'
2021-07-04 10:12:36 +02:00
}`
fs.writeFile(filePath, frontMatter, { flag: 'wx' }, (err) => {
if (err) {
throw err
} else {
console.log(`Blog post generated successfully at ${filePath}`)
}
})
})
.catch((error) => {
if (error.isTtyError) {
console.log("Prompt couldn't be rendered in the current environment")
} else {
console.log('Something went wrong, sorry!')
}
})