-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathplopfile.ts
134 lines (128 loc) · 3.8 KB
/
plopfile.ts
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
132
133
134
// this plopfile is copied over from other project, there may be some unnecessary questions
import type { NodePlopAPI } from "plop";
/**
* Converts a string to camel case.
* @param {string} str - The input string to convert.
* @returns {string} The camel case version of the input string.
* @example
* // returns 'myVariableName'
* toCamelCase('my variable name');
*/
const toCamelCase = (str: string): string => {
return str
.split(" ")
.map((word, index) =>
index === 0
? word.toLowerCase()
: word.charAt(0).toUpperCase() + word.slice(1).toLowerCase(),
)
.join("");
};
/**
* Converts a string to pascal case.
* @param {string} str - The input string to convert.
* @returns {string} The pascal case version of the input string.
* @example
* // returns 'MyVariableName'
* toPascalCase('my variable name');
*/
const toPascalCase = (str: string): string => {
return str
.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join("");
};
/**
* Converts a string to kebab case.
* @param {string} str - The input string to convert.
* @returns {string} The kebab case version of the input string.
* @example
* // returns 'my-variable-name'
* toKebabCase('my variable name');
*/
const toKebabCase = (str: string): string => {
return str.toLowerCase().replace(/ /g, "-");
};
export default function (plop: NodePlopAPI) {
// Register all the case helpers
plop.setHelper("toCamelCase", (text) => toCamelCase(text));
plop.setHelper("toPascalCase", (text) => toPascalCase(text));
plop.setHelper("toKebabCase", (text) => toKebabCase(text));
plop.setGenerator("datatable", {
description: "generate datatable components",
prompts: [
{
type: "input",
name: "module",
message:
"module name (lowercase, singular, eg: institution, category): ",
validate: (value) => {
// Allow lowercase letters and spaces
if (!/^[a-z ]+$/.test(value)) {
return "module name must be lowercase and can contain spaces";
}
// Ensure it doesn't start or end with space
if (value.startsWith(" ") || value.endsWith(" ")) {
return "module name cannot start or end with spaces";
}
// Ensure no consecutive spaces
if (value.includes(" ")) {
return "module name cannot contain consecutive spaces";
}
return true;
},
},
{
type: "list",
name: "departmentType",
message: "choose department type:",
choices: ["select from predefined list", "enter custom route prefix"],
},
{
type: "list",
name: "department",
message: "choose a prefix:",
choices: [
{ name: "Institutions", value: "institutions" },
{ name: "Categories", value: "categories" },
{ name: "Users", value: "users" },
],
when: (answers) =>
answers.departmentType === "select from predefined list",
},
{
type: "input",
name: "department",
message: "enter route prefix (lowercase, no spaces):",
validate: (value) =>
/^[a-z]+$/.test(value)
? true
: "route prefix must be lowercase and without spaces.",
when: (answers) =>
answers.departmentType === "enter custom route prefix",
},
],
actions: [
{
type: "addMany",
destination:
"app/(dashboard)/dashboard/{{toKebabCase module}}s/_components",
templateFiles: "utils/datatable/templates/components/*.hbs",
base: "utils/datatable/templates/components",
},
{
type: "addMany",
destination:
"app/(dashboard)/dashboard/{{toKebabCase module}}s/_lib",
templateFiles: "utils/datatable/templates/lib/*.hbs",
base: "utils/datatable/templates/lib",
},
{
type: "addMany",
destination: "app/(dashboard)/dashboard/{{toKebabCase module}}s",
templateFiles: "utils/datatable/templates/pages/*.hbs",
base: "utils/datatable/templates/pages",
},
],
});
}