-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplopfile.mjs
119 lines (108 loc) · 3.02 KB
/
plopfile.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
import chalk from "chalk";
const capitalize = (str) => {
return str.charAt(0).toUpperCase() + str.slice(1);
};
const camelCase = (str) => {
return str.replace(/[-_](\w)/g, (_, c) => c.toUpperCase());
};
const authors = [
"Daniel Onggunhao",
"Nam Nguyen",
"Ashley Tran",
"Hien To",
"Van Pham",
"Louis Le",
"Rex Ha",
"Alan Dao",
"Emre",
"Henry Ho",
"Nicole Zhu",
"Bach Vu",
];
/**
* @param {import("plop").NodePlopAPI} plop
*/
export default async function (plop) {
plop.setHelper("capitalize", (text) => {
return capitalize(camelCase(text));
});
plop.load("plop-helper-date");
plop.setGenerator("create-blog", {
description: "Generates a blog post",
prompts: [
{
type: "input",
name: "title",
message: "Enter the title of the blog post:",
validate: (input) => (input ? true : "Title is required."),
},
{
type: "input",
name: "slug",
message: (answers) =>
`Enter the slug for the blog post (suggested: ${generateSlug(
answers.title
)})`,
default: (answers) => generateSlug(answers.title),
validate: (input) =>
input && /^[a-z0-9]+(?:-[a-z0-9]+)*$/.test(input)
? true
: "Please enter a valid slug (lowercase letters, numbers, and hyphens only).",
},
{
type: "input",
name: "description",
message: "Enter the description of the blog post:",
validate: (input) => (input ? true : "Description is required."),
},
{
type: "checkbox",
name: "authors",
message: "Select the author(s) of the blog post:",
choices: authors,
validate: (input) =>
input.length ? true : "Please select at least one author.",
},
{
type: "input",
name: "tags",
message: "Enter tags for the blog post (separate tags with commas):",
filter: (input) =>
input
.split(",")
.map((tag) => tag.trim())
.filter(Boolean)
.join(", "),
validate: (input) =>
input.length ? true : "Please enter at least one tag.",
},
],
actions(answers) {
const actions = [];
if (!answers) return actions;
const { authors, title, description, tags, slug } = answers;
actions.push({
type: "addMany",
templateFiles: "templates/**",
destination: `./src/pages/blog`,
globOptions: { dot: true },
data: { title, description, authors, tags },
abortOnFail: true,
});
console.log(chalk.green(`Your blog post is created!`));
console.log(chalk.green(`You can modify under src/pages/blog/${slug}`));
console.log(
chalk.cyan(`You can view it at: http://localhost:3000/blog/${slug}`)
);
return actions;
},
});
function generateSlug(title) {
return title
? title
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "")
: "";
}
}