-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplopfile.js
More file actions
96 lines (87 loc) · 2.63 KB
/
plopfile.js
File metadata and controls
96 lines (87 loc) · 2.63 KB
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
const { readdirSync, existsSync, mkdirSync } = require("fs")
const path = require("path")
const requireField = (fieldName) => {
return (value) => {
if (String(value).length === 0) {
return fieldName + " is required"
}
return true
}
}
const getDirectories = (source, base = "") =>
readdirSync(source, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.reduce((acc, dirent) => {
const subdir = path.join(base, dirent.name) // Chemin relatif sans le préfixe
const children = getDirectories(path.join(source, dirent.name), subdir)
return acc.concat({ name: subdir, value: subdir }, children)
}, [])
const ensureDirectoryExistence = (filePath) => {
const dirname = path.dirname(filePath)
if (existsSync(dirname)) {
return true
}
ensureDirectoryExistence(dirname)
mkdirSync(dirname)
}
// Function to format the Storybook title to include the directory
const formatStoryTitle = (directory, componentName) => {
if (directory) {
return `${directory.toUpperCase()}/${componentName}`
}
return componentName
}
module.exports = (plop) => {
const componentDirectories = [
{ name: "Root", value: "" },
...getDirectories("./components"),
]
plop.setGenerator("component", {
description: "Create a reusable component",
prompts: [
{
type: "input",
name: "name",
message: "What is your component name?",
validate: requireField("name"),
},
{
type: "list",
name: "directory",
message: "Choose directory or Root for main directory:",
choices: componentDirectories,
},
{
type: "confirm",
name: "includeStories",
message: "Do you want to include a Storybook file?",
default: true,
},
],
actions: (data) => {
const componentName = plop.getHelper("pascalCase")(data.name)
const basePath = data.directory
? `./components/${data.directory}`
: "./components"
const formattedTitle = formatStoryTitle(data.directory, componentName)
const actions = [
{
type: "add",
path: `${basePath}/{{pascalCase name}}.tsx`,
templateFile: "plop-templates/component.tsx.hbs",
beforeAdd: ensureDirectoryExistence,
},
]
if (data.includeStories) {
actions.push({
type: "add",
path: `${basePath}/{{pascalCase name}}.stories.tsx`,
templateFile: "plop-templates/component.stories.tsx.hbs",
data: { storyTitle: formattedTitle },
beforeAdd: ensureDirectoryExistence,
})
}
return actions
},
})
}