-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
plopfile.js
112 lines (95 loc) · 2.68 KB
/
plopfile.js
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
/**
* Part of this code is taken from @chakra-ui/react package ❤️
*/
const capitalize = (str) => {
return str.charAt(0).toUpperCase() + str.slice(1);
};
const camelCase = (str) => {
return str.replace(/[-_](\w)/g, (_, c) => c.toUpperCase());
};
const workspaces = ["components", "core", "hooks", "utilities"];
const generators = ["component", "package", "hook"];
const defaultOutDirs = {
component: "components",
hook: "hooks",
package: "utilities",
};
/**
* @param {import("plop").NodePlopAPI} plop
*/
module.exports = function main(plop) {
plop.setHelper("capitalize", (text) => {
return capitalize(camelCase(text));
});
plop.setHelper("camelCase", (text) => {
return camelCase(text);
});
generators.forEach((gen) => {
plop.setGenerator(gen, {
description: `Generates a ${gen}`,
prompts: [
{
type: "input",
name: `${gen}Name`,
message: `Enter ${gen} name:`,
validate: (value) => {
if (!value) {
return `${gen} name is required`;
}
// check is has a valid hook name "use-something"
if (gen === "hook" && !value.startsWith("use-")) {
return "Hook name must start with 'use-'";
}
// check is case is correct
if (value !== value.toLowerCase()) {
return `${gen} name must be in lowercase`;
}
// cannot have spaces
if (value.includes(" ")) {
return `${gen} name cannot have spaces`;
}
return true;
},
},
{
type: "input",
name: "description",
message: `The description of this ${gen}:`,
},
{
type: "list",
name: "outDir",
message: `where should this ${gen} live?`,
default: defaultOutDirs[gen],
choices: workspaces,
validate: (value) => {
if (!value) {
return `outDir is required`;
}
return true;
},
},
],
actions(answers) {
const actions = [];
if (!answers) return actions;
const {description, outDir} = answers;
const generatorName = answers[`${gen}Name`] ?? "";
const data = {
[`${gen}Name`]: generatorName,
description,
outDir,
};
actions.push({
type: "addMany",
templateFiles: `plop/${gen}/**`,
destination: `./packages/{{outDir}}/{{dashCase ${gen}Name}}`,
base: `plop/${gen}`,
data,
abortOnFail: true,
});
return actions;
},
});
});
};