-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerateNewBlog.mjs
131 lines (117 loc) · 3.5 KB
/
generateNewBlog.mjs
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import fs from 'fs/promises';
import path from 'path';
import url from 'url';
import inquirer from 'inquirer';
// Function to validate non-empty input
const validateInput = (input) => {
return input.trim() !== '' ? true : 'This field is required.';
};
// Function to prompt user for inputs
async function getUserInputs() {
return await inquirer.prompt([
{
type: 'input',
name: 'title',
message: 'Enter the title:',
validate: validateInput,
},
{
type: 'input',
name: 'description',
message: 'Enter the description:',
validate: validateInput,
},
{
type: 'confirm',
name: 'includeCode',
message: 'Does the markdown file include code?',
default: false,
},
{
type: 'input',
name: 'coverImagePath',
message: 'Enter the path to the cover image:',
validate: validateInput,
},
]);
}
// Function to create necessary folders
async function createFolders(blogFolderPath, assetsFolderPath) {
try {
await fs.mkdir(blogFolderPath, { recursive: true });
await fs.mkdir(assetsFolderPath, { recursive: true });
} catch (error) {
console.error('Error creating folders:', error);
throw error;
}
}
// Function to copy cover image
async function copyCoverImage(coverImagePath, newCoverImagePath) {
try {
await fs.copyFile(coverImagePath, newCoverImagePath);
} catch (error) {
console.error('Error copying cover image:', error);
throw error;
}
}
// Function to write markdown file
async function writeMarkdownFile(blogFilePath, frontmatter) {
try {
await fs.writeFile(blogFilePath, frontmatter);
} catch (error) {
console.error('Error writing markdown file:', error);
throw error;
}
}
// Main function to generate files
async function generateFiles() {
const currentFileUrl = import.meta.url;
const currentDir = path.dirname(url.fileURLToPath(currentFileUrl));
const currentDate = new Date().toISOString().split('T')[0];
try {
const answers = await getUserInputs();
const { title, description, includeCode, coverImagePath } = answers;
const folderTitle = title.replace(/\s+/g, '-');
const blogFolderPath = path.resolve(currentDir, 'src', 'blog', `${currentDate}-${folderTitle}`);
const assetsFolderPath = path.resolve(
currentDir,
'src',
'assets',
'images',
`${currentDate}-${folderTitle}`,
);
const coverImageFileName = path.basename(coverImagePath);
const newCoverImagePath = path.join(assetsFolderPath, coverImageFileName);
const blogFilePath = path.join(blogFolderPath, `${currentDate}-${folderTitle}.md`);
await createFolders(blogFolderPath, assetsFolderPath);
await copyCoverImage(coverImagePath, newCoverImagePath);
const frontmatter = `---
layout: blog.njk
title: ${title}
author: David Moll
date: ${currentDate}
tags:
- posts
description: ${description}
folderName: ${currentDate}-${folderTitle}
socialMediaPreviewImage: https://blog.davidmoll.net/assets/images/${currentDate}-${folderTitle}/${coverImageFileName}
socialMediaPreviewImageAlt:
hasCode: ${includeCode}
---
![{{ socialMediaPreviewImageAlt }}]({{ socialMediaPreviewImage }})
`;
await writeMarkdownFile(blogFilePath, frontmatter);
console.log(
`Blog folder "${currentDate}-${folderTitle}" and Markdown file created successfully.`,
);
} catch (error) {
if (error.isTtyError) {
console.error("Prompt couldn't be rendered in the current environment.");
} else if (error.message && error.message.includes('User force closed the prompt')) {
console.log('Prompt was cancelled.');
} else {
console.error('An error occurred:', error);
}
}
}
generateFiles();