-
Notifications
You must be signed in to change notification settings - Fork 7
/
plopfile.js
152 lines (141 loc) · 4 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
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
module.exports = (plop) => {
plop.load('./.plop/actions/install-dependencies.js');
plop.setGenerator('component', {
description: 'A LaunchPad UI component',
prompts: [
{
type: 'input',
name: 'name',
message: 'What is your singular component name?',
},
{
type: 'input',
name: 'description',
message: 'Give a short description of the purpose of this component.',
},
],
actions: [
/*
* Component package
*/
{
type: 'add',
path: 'packages/{{dashCase name}}/README.md',
templateFile: '.plop/templates/component/README.md.hbs',
},
{
type: 'add',
path: 'packages/{{dashCase name}}/package.json',
templateFile: '.plop/templates/component/package.json.hbs',
},
{
type: 'add',
path: 'packages/{{dashCase name}}/tsconfig.build.json',
templateFile: '.plop/templates/component/tsconfig.build.json.hbs',
},
{
type: 'add',
path: 'packages/{{dashCase name}}/stories/{{pascalCase name}}.stories.tsx',
templateFile: '.plop/templates/component/story.tsx.hbs',
},
{
type: 'add',
path: 'packages/{{dashCase name}}/__tests__/{{pascalCase name}}.spec.tsx',
templateFile: '.plop/templates/component/spec.tsx.hbs',
},
{
type: 'add',
path: 'packages/{{dashCase name}}/src/{{pascalCase name}}.tsx',
templateFile: '.plop/templates/component/component.tsx.hbs',
},
{
type: 'add',
path: 'packages/{{dashCase name}}/src/index.ts',
templateFile: '.plop/templates/component/src-index.ts.hbs',
},
{
type: 'add',
path: 'packages/{{dashCase name}}/src/styles/{{pascalCase name}}.module.css',
templateFile: '.plop/templates/component/styles.css.hbs',
},
/*
* Monorepo config
*/
{
path: 'tsconfig.json',
pattern: /("paths": {)/g,
template:
'$1\n "@launchpad-ui/{{dashCase name}}": ["./packages/{{dashCase name}}/src"],',
type: 'modify',
transform: (file) =>
sortModification(file, {
openPatternStr: '"paths": {',
closePatternStr: ' }',
handleNonTrailingCommas: true,
}),
},
/*
* Core package integration
*/
{
path: 'packages/core/package.json',
pattern: /("dependencies": {)/g,
template: '$1\n "@launchpad-ui/{{dashCase name}}": "workspace:~",',
type: 'modify',
transform: (file) =>
sortModification(file, {
openPatternStr: '"dependencies": {',
closePatternStr: ' },',
handleNonTrailingCommas: true,
}),
},
{
path: 'packages/core/src/index.ts',
pattern: /(\/\/ plop end type exports)/g,
template:
"export type { {{pascalCase name}}Props } from '@launchpad-ui/{{dashCase name}}';\n$1",
type: 'modify',
},
{
path: 'packages/core/src/index.ts',
pattern: /(\/\/ plop end module exports)/g,
template: "export { {{pascalCase name}} } from '@launchpad-ui/{{dashCase name}}';\n$1",
type: 'modify',
},
{
type: 'installDependencies',
path: process.cwd(),
},
],
});
};
const sortModification = (file, params) => {
const { openPatternStr, closePatternStr, handleNonTrailingCommas } = params;
const lines = file.split('\n');
let listOpenIndex;
let listCloseIndex;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (typeof listOpenIndex === 'number' && typeof listCloseIndex === 'number') {
break;
}
if (typeof listOpenIndex === 'number' && line.includes(closePatternStr)) {
listCloseIndex = i;
}
if (typeof listOpenIndex !== 'number') {
if (line.includes(openPatternStr)) {
listOpenIndex = i;
}
}
}
const startLines = lines.slice(0, listOpenIndex + 1);
let pathsLines = lines.slice(listOpenIndex + 1, listCloseIndex).sort();
if (handleNonTrailingCommas) {
pathsLines = pathsLines
.map((line) => line.replace(',', ''))
.map((line, i) => (i === pathsLines.length - 1 ? `${line}` : `${line},`));
}
const endLines = lines.slice(listCloseIndex, lines.length);
const updatedFile = [...startLines, ...pathsLines, ...endLines].join('\n');
return updatedFile;
};