This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
42 lines (35 loc) · 1.43 KB
/
index.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
const inquirer = require('inquirer');
const fs = require('fs-extra');
const slugify = require('limax');
const path = require('path');
const { isNonEmpty, isAuthorName, formatDate } = require('./util');
module.exports = async function run({ base = path.join(process.cwd(), 'content/blog') } = {}) {
const now = new Date();
const answers = await inquirer.prompt([
{ type: 'input', name: 'title', message: 'Blog post title', validate: isNonEmpty },
{ type: 'input', name: 'description', message: 'Short description of the blog post (<= 200 characters)', validate: isNonEmpty },
{ type: 'input', name: 'author', message: 'Author name (First Last)', validate: isAuthorName },
{ type: 'list', name: 'category', message: 'Category', choices: ['JavaScript', 'Devops', 'JVM', 'Mobile', 'Misc']},
{ type: 'input', name: 'date', message: 'Date', default: formatDate(now) },
{ type: 'editor', name: 'post', message: 'Post contents (feel free to just open and save)' }
]);
const template = `
---
title: "${answers.title.replace(/"/g, '\"')}"
author: ${answers.author}
date: ${now.toJSON()}
category: ${answers.category}
meta:
description: ${answers.description}
keywords:
- ${answers.category}
---
${answers.post}
`.trim() + '\n';
try {
await fs.ensureDir(base);
await fs.writeFile(path.join(base, `${formatDate(answers.date)}-${slugify(answers.title)}.md`), template, 'utf8');
} catch (e) {
console.error(e);
}
};