-
Notifications
You must be signed in to change notification settings - Fork 6
/
plopfile.mjs
57 lines (55 loc) · 1.75 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
import path from "node:path";
import * as glob from "glob";
import autocompletePrompt from "inquirer-autocomplete-prompt";
const PACKAGES_NEXTJS = "./packages/nextjs";
/**
* @param {import("plop").NodePlopAPI} plop
*/
export default function (plop) {
plop.setPrompt("autocomplete", autocompletePrompt);
plop.setGenerator("stories", {
description: "Generates a Storybook stories file for a component",
prompts: [
{
name: "COMPONENT",
message: "Create a Stories file for which component? (you can search with wildcards)",
type: "autocomplete",
loop: false,
async source(answers, searchTerms) {
return glob
.sync(`components/**/*${searchTerms || ""}*`, {
cwd: PACKAGES_NEXTJS,
nodir: true,
nocase: true,
})
.sort((a, b) => a.localeCompare(b))
.map((file) => {
const COMPONENT = {
FILE: file,
DIRNAME: path.dirname(file),
BASENAME: path.basename(file, path.extname(file)),
};
return {
name: file,
value: COMPONENT,
};
});
},
validate(input, _answers) {
const COMPONENT = input.value;
if ([".stories", ".test"].some((end) => COMPONENT.BASENAME.endsWith(end))) {
return `This does not appear to be a Component file: "${input.name}"`;
}
return true;
},
},
],
actions: [
{
type: "add",
templateFile: `${PACKAGES_NEXTJS}/__plop_templates/COMPONENT.stories.tsx.hbs`,
path: `${PACKAGES_NEXTJS}/{{COMPONENT.DIRNAME}}/{{COMPONENT.BASENAME}}.stories.tsx`,
},
],
});
}