This repository has been archived by the owner on Oct 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathplopfile.mjs
91 lines (81 loc) · 2.41 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
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());
};
/**
* @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-changelog", {
description: "Generates a changelog",
prompts: [
{
type: "input",
name: "title",
message: "Enter the title of the changelog post:",
validate: (input) => (input ? true : "Title is required."),
},
{
type: "input",
name: "slug",
message: (answers) =>
`Enter the slug for the changelog 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: "version",
message: "Enter the version of the changelog post:",
validate: (input) => (input ? true : "Title is required."),
},
{
type: "input",
name: "description",
message: "Enter the description of the changelog post:",
validate: (input) => (input ? true : "Description is required."),
},
],
actions(answers) {
const actions = [];
if (!answers) return actions;
const { version, title, description, slug } = answers;
actions.push({
type: "addMany",
templateFiles: "templates/**",
destination: `./changelog`,
globOptions: { dot: true },
data: { title, description, version },
abortOnFail: true,
});
console.log(chalk.green(`Your changelog post is created!`));
console.log(chalk.green(`You can modify under /changelog/${slug}`));
console.log(
chalk.cyan(
`You can view it at: http://localhost:3000/changelog/${slug}`
)
);
return actions;
},
});
function generateSlug(title) {
return title
? title
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "")
: "";
}
}