-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplopfile.mjs
99 lines (90 loc) · 2.49 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
import * as fs from 'fs/promises';
import * as path from 'path';
export default function (
/** @type {import('plop').NodePlopAPI} */
plop,
) {
const kebabCase = plop.getHelper('kebabCase');
plop.setGenerator('package', {
description: 'Create a new package',
prompts: [
{
type: 'input',
name: 'name',
message: 'package name please',
},
],
actions: [
{
type: 'addMany',
destination: 'packages/{{name}}',
templateFiles: '.plop/package/**/*',
base: '.plop/package',
},
],
});
plop.setGenerator('hook', {
description: 'Create everything related to a hook',
prompts: [
{
type: 'input',
name: 'name',
message: 'hook name please (format: use-{hooks-name})',
filter: (str) => `use-${kebabCase(str.replace(/^use-/, ''))}`,
},
],
actions: [
{
type: 'addMany',
destination: 'packages/hooks/src/{{kebabCase name}}',
templateFiles: '.plop/hook/**/*',
base: '.plop/hook',
},
{
type: 'append-sorted',
path: 'packages/hooks/src/index.ts',
template: "export * from './{{ kebabCase name }}';\n",
},
],
});
plop.setGenerator('component', {
description: 'Create everything related to a component',
prompts: [
{
type: 'input',
name: 'name',
message: 'component name please',
},
],
actions: [
{
type: 'addMany',
destination: 'packages/components/src/{{pascalCase name}}',
templateFiles: '.plop/component/**/*',
base: '.plop/component',
},
{
type: 'append-sorted',
path: 'packages/components/src/index.ts',
template: "export * from './{{ pascalCase name }}';\n",
},
],
});
plop.setActionType('append-sorted', async (data, userConfig = {}) => {
const config = { separator: '\n', ...userConfig };
const base = plop.getDestBasePath();
const dest = path.join(base, config.path);
const original = await fs.readFile(dest, 'utf-8');
const newData = plop.renderString(config.template, data);
const content = original.trim() + config.separator + newData;
const sortedContent =
content
.trim()
.split('\n')
.filter((item, idx, arr) => arr.indexOf(item, idx + 1) < 0)
.sort()
.join('\n') + '\n';
await fs.writeFile(dest, sortedContent);
return `Appended and sorted ${config.path}`;
});
}