-
Notifications
You must be signed in to change notification settings - Fork 0
/
plopfile.ts
194 lines (176 loc) · 5.75 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import {
AddActionConfig,
ModifyActionConfig,
NodePlopAPI,
} from "@crutchcorn/plop";
import { designSystemA } from "./design-systems/a/design-system";
import { designSystemB } from "./design-systems/b/design-system";
import { designSystemIkea } from "./design-systems/ikea/design-system";
import { designSystemZweitag } from "./design-systems/zweitag/design-system";
import { designSystemHsd } from "./design-systems/hsd/design-system";
import {
appendImports,
singularize,
applyColor,
types,
properties,
dataAttributes,
storybookControls,
storybookArgs,
storybookVariantControls,
testAttributes,
testDefaults,
testProps,
variantTests,
} from "./plop-helper";
const getComponents = (designSystem: DesignSystemsType) => {
switch (designSystem) {
case "A":
return Object.keys(designSystemA.components);
case "B":
return Object.keys(designSystemB.components);
case "IKEA":
return Object.keys(designSystemIkea.components);
case "ZWEITAG":
return Object.keys(designSystemZweitag.components);
case "HSD":
return Object.keys(designSystemHsd.components);
default:
throw new Error("Design system not found");
}
};
const getDesignSystem = (designSystem: DesignSystemsType) => {
switch (designSystem) {
case "A":
return designSystemA;
case "B":
return designSystemB;
case "IKEA":
return designSystemIkea;
case "ZWEITAG":
return designSystemZweitag;
case "HSD":
return designSystemHsd;
default:
throw new Error("Design system not found");
}
};
const DESIGN_SYSTEMS = ["A", "B", "IKEA", "ZWEITAG", "HSD"];
type DesignSystemsType = (typeof DESIGN_SYSTEMS)[number];
const COMPONENTS_A = Object.keys(designSystemA.components);
const COMPONENTS_B = Object.keys(designSystemB.components);
const COMPONENTS_IKEA = Object.keys(designSystemIkea.components);
const COMPONENTS_ZWEITAG = Object.keys(designSystemZweitag.components);
const COMPONENTS_HSD = Object.keys(designSystemHsd.components);
interface Data {
component: string;
designSystem: string;
}
// Generator
export default function (plop: NodePlopAPI) {
// Plop does not allow callback function for partials.
/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
plop.setPartial("dataAttributes", dataAttributes);
// @ts-ignore
plop.setPartial("properties", properties);
// @ts-ignore
plop.setPartial("types", types);
// @ts-ignore
plop.setPartial("storybookControls", storybookControls);
// @ts-ignore
plop.setPartial("storybookVariantControls", storybookVariantControls);
// @ts-ignore
plop.setPartial("storybookArgs", storybookArgs);
// @ts-ignore
plop.setPartial("testProps", testProps);
// @ts-ignore
plop.setPartial("testDefaults", testDefaults);
// @ts-ignore
plop.setPartial("testAttributes", testAttributes);
// @ts-ignore
plop.setPartial("variantTests", variantTests);
/* eslint-enable @typescript-eslint/ban-ts-comment */
plop.setHelper("singularize", singularize);
plop.setHelper("applyColor", applyColor);
plop.setGenerator("component", {
description:
"Create a base component based on your design system configuration",
prompts: [
{
type: "list",
choices: DESIGN_SYSTEMS,
message: "Choose a design system",
name: "designSystem",
},
{
type: "list",
choices: [
"all",
...COMPONENTS_A,
...COMPONENTS_B,
...COMPONENTS_IKEA,
...COMPONENTS_ZWEITAG,
...COMPONENTS_HSD,
],
name: "component",
message: "Please select the component you want to create",
},
],
// eslint-disable-next-line
// @ts-ignore
actions: (data: Data) => {
const COMPONENTS = getComponents(data.designSystem);
const DESIGN_SYSTEM = getDesignSystem(data.designSystem);
const addActions: AddActionConfig[] = [];
const modifyActions: ModifyActionConfig[] = [];
let components;
if (data.component === "all") components = [...COMPONENTS];
else components = [data?.component];
components.forEach((name: string) => {
const { variants, properties } = DESIGN_SYSTEM.components[name];
const meta = DESIGN_SYSTEM.meta;
addActions.push({
force: true,
type: "add",
path: `src/components/${data.designSystem}/${name}/component.styles.ts`,
data: { variants, name, meta },
templateFile: `plop-templates/${name}/component.styles.ts`,
});
addActions.push({
force: true,
type: "add",
path: `src/components/${data.designSystem}/${name}/component.ts`,
data: { variants, properties, name, meta },
templateFile: `plop-templates/${name}/component.ts`,
});
addActions.push({
force: true,
type: "add",
path: `src/components/${data.designSystem}/${name}/component.stories.ts`,
data: { variants, properties, name, meta },
templateFile: `plop-templates/component.stories.ts`,
});
addActions.push({
force: true,
type: "add",
path: `src/components/${data.designSystem}/${name}/component.test.ts`,
data: { variants, name, meta },
templateFile: `plop-templates/${name}/component.test.ts`,
});
});
components.forEach((component: string) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
modifyActions.push({
force: true,
type: "modify",
path: "src/main.ts",
data: { name: component, designSystem: data.designSystem },
transform: appendImports,
});
});
return [...addActions, ...modifyActions];
},
});
}