tailwind-nextjs-blog/scripts/compose.js

133 lines
3.4 KiB
JavaScript
Raw Normal View History

2022-10-17 23:37:01 +08:00
const fs = require('fs');
const path = require('path');
const inquirer = require('inquirer');
const dedent = require('dedent');
2022-07-17 21:40:41 +08:00
2022-10-17 23:37:01 +08:00
const root = process.cwd();
2022-07-17 21:40:41 +08:00
const getAuthors = () => {
2022-10-17 23:37:01 +08:00
const authorPath = path.join(root, 'data', 'authors');
const authorList = fs
.readdirSync(authorPath)
.map((filename) => path.parse(filename).name);
return authorList;
};
2022-07-17 21:40:41 +08:00
const getLayouts = () => {
2022-10-17 23:37:01 +08:00
const layoutPath = path.join(root, 'layouts');
2022-07-17 21:40:41 +08:00
const layoutList = fs
.readdirSync(layoutPath)
.map((filename) => path.parse(filename).name)
2022-10-17 23:37:01 +08:00
.filter((file) => file.toLowerCase().includes('post'));
return layoutList;
};
2022-07-17 21:40:41 +08:00
const genFrontMatter = (answers) => {
2022-10-17 23:37:01 +08:00
let d = new Date();
2022-07-17 21:40:41 +08:00
const date = [
d.getFullYear(),
('0' + (d.getMonth() + 1)).slice(-2),
('0' + d.getDate()).slice(-2),
2022-10-17 23:37:01 +08:00
].join('-');
const tagArray = answers.tags.split(',');
tagArray.forEach((tag, index) => (tagArray[index] = tag.trim()));
const tags = "'" + tagArray.join("','") + "'";
const authorArray =
answers.authors.length > 0 ? "'" + answers.authors.join("','") + "'" : '';
2022-07-17 21:40:41 +08:00
let frontMatter = dedent`---
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}
canonicalUrl: ${answers.canonicalUrl}
2022-10-17 23:37:01 +08:00
`;
2022-07-17 21:40:41 +08:00
if (answers.authors.length > 0) {
2022-10-17 23:37:01 +08:00
frontMatter = frontMatter + '\n' + `authors: [${authorArray}]`;
2022-07-17 21:40:41 +08:00
}
2022-10-17 23:37:01 +08:00
frontMatter = frontMatter + '\n---';
2022-07-17 21:40:41 +08:00
2022-10-17 23:37:01 +08:00
return frontMatter;
};
2022-07-17 21:40:41 +08:00
inquirer
.prompt([
{
name: 'title',
message: 'Enter post title:',
type: 'input',
},
{
name: 'extension',
message: 'Choose post extension:',
type: 'list',
choices: ['mdx', 'md'],
},
{
name: 'authors',
message: 'Choose authors:',
type: 'checkbox',
choices: getAuthors,
},
{
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,
},
{
name: 'canonicalUrl',
message: 'Enter canonical url:',
type: 'input',
},
])
.then((answers) => {
// Remove special characters and replace space with -
const fileName = answers.title
.toLowerCase()
.replace(/[^a-zA-Z0-9 ]/g, '')
.replace(/ /g, '-')
2022-10-17 23:37:01 +08:00
.replace(/-+/g, '-');
const frontMatter = genFrontMatter(answers);
if (!fs.existsSync('data/blog'))
fs.mkdirSync('data/blog', { recursive: true });
2022-07-17 21:40:41 +08:00
const filePath = `data/blog/${fileName ? fileName : 'untitled'}.${
answers.extension ? answers.extension : 'md'
2022-10-17 23:37:01 +08:00
}`;
2022-07-17 21:40:41 +08:00
fs.writeFile(filePath, frontMatter, { flag: 'wx' }, (err) => {
if (err) {
2022-10-17 23:37:01 +08:00
throw err;
2022-07-17 21:40:41 +08:00
} else {
2022-10-17 23:37:01 +08:00
console.log(`Blog post generated successfully at ${filePath}`);
2022-07-17 21:40:41 +08:00
}
2022-10-17 23:37:01 +08:00
});
2022-07-17 21:40:41 +08:00
})
.catch((error) => {
if (error.isTtyError) {
2022-10-17 23:37:01 +08:00
console.log("Prompt couldn't be rendered in the current environment");
2022-07-17 21:40:41 +08:00
} else {
2022-10-17 23:37:01 +08:00
console.log('Something went wrong, sorry!');
2022-07-17 21:40:41 +08:00
}
2022-10-17 23:37:01 +08:00
});