forked from abaltuta/monorepo-article
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plopfile.mjs
165 lines (152 loc) · 4.66 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
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
import { readdirSync, existsSync } from "fs";
import path from "path";
import autocompletePrompt from "inquirer-autocomplete-prompt";
import util from "util";
import * as childProcess from "child_process";
const exec = util.promisify(childProcess.exec);
const { INSTALL_DIR, PWD } = process.env;
const pathPostfix = INSTALL_DIR || "packages";
const basePath = PWD || "/";
const compPath = path.resolve(basePath, pathPostfix);
const toKebabCase = (string) =>
string
.replace(/([a-z])([A-Z])/g, "$1-$2")
.replace(/[\s_]+/g, "-")
.toLowerCase();
const isFileExists = (filePath) => {
try {
return existsSync(filePath);
} catch (err) {
console.error("isFileExists error:", err);
}
return false;
};
const getDirList = (path) => {
return readdirSync(path, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory())
.map((dirent) => dirent.name);
};
const groupsList = getDirList(compPath);
const getComponentsList = (input) => {
if (!input || input === "") return groupsList;
return groupsList.filter((group) => group.includes(input));
};
const validateInput = (input) => {
if (input && input !== "") {
return /^[a-zA-Z.-]+$/.test(input);
}
return false;
};
const checkDuplicateComponent = (name) => {
let message = "";
const status = groupsList.some((group) => {
const dirList = getDirList(`${compPath}/${group}`);
if (dirList.includes(name)) {
message = `duplicate component name ${name} at ${group}`;
return true;
}
});
return { status, message };
};
export default function (plop) {
plop.setDefaultInclude({ helpers: true });
plop.setPrompt("getGroup", autocompletePrompt);
plop.setActionType("validateFields", function (answers, config, plop) {
const { group, name } = answers || {};
const nameValid = validateInput(name);
const groupValid = validateInput(group);
if (!nameValid || !groupValid) {
throw new Error(
`[${!nameValid && "component name"}${
!groupValid && ", group name"
}}] invalid` // can refer to a conventions url here once ready
);
}
const { status, message } = checkDuplicateComponent(
name
.replace(/([a-z])([A-Z])/g, "$1-$2")
.replace(/[\s_]+/g, "-")
.toLowerCase()
);
if (status) {
throw new Error(message);
}
return "fields valid";
});
plop.setActionType("installDependencies", function (answers, config, plop) {
const { name } = answers;
console.log("installing dependencies");
const foundationDev = `lerna add typescript --scope=@monorepo/${toKebabCase(
name
)} --dev`;
return exec(`${foundationDev}`)
.then(() => "dependencies installed successfully")
.catch((err) => `error installing dependencies: ${err}`);
});
plop.setGenerator("component", {
description: "Creating new react components",
prompts: [
{
type: "getGroup",
name: "group",
message: "Choose component group",
source: function (answersSoFar, input) {
return getComponentsList(input);
},
},
{
type: "input",
message: "Enter component name",
name: "name",
},
],
actions: [
{
type: "validateFields",
},
{
type: "add",
templateFile: "templates/package.json.hbs",
path: `${compPath}/{{kebabCase group}}/{{kebabCase name}}/package.json`,
},
{
type: "add",
templateFile: "templates/tsconfig.json.hbs",
path: `${compPath}/{{kebabCase group}}/{{kebabCase name}}/tsconfig.json`,
},
{
type: "add",
templateFile: "templates/types.ts.hbs",
path: `${compPath}/{{kebabCase group}}/{{kebabCase name}}/src/types.ts`,
},
{
type: "add",
templateFile: "templates/styles.ts.hbs",
path: `${compPath}/{{kebabCase group}}/{{kebabCase name}}/src/styles.ts`,
},
{
type: "add",
templateFile: "templates/jest.config.js.hbs",
path: `${compPath}/{{kebabCase group}}/{{kebabCase name}}/jest.config.js`,
},
{
type: "add",
templateFile: "templates/index.ts.hbs",
path: `${compPath}/{{kebabCase group}}/{{kebabCase name}}/src/index.ts`,
},
{
type: "add",
templateFile: "templates/main-component.tsx.hbs",
path: `${compPath}/{{kebabCase group}}/{{kebabCase name}}/src/{{kebabCase name}}.tsx`,
},
{
type: "add",
templateFile: "templates/stories.mdx.hbs",
path: `${compPath}/{{kebabCase group}}/{{kebabCase name}}/src/stories/{{pascalCase name}}.stories.mdx`,
},
{
type: "installDependencies",
},
],
});
}