-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplopfile.js
102 lines (94 loc) · 2.39 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
const prettier = require('prettier');
/**
* @param {import("plop").NodePlopAPI} plop
*/
module.exports = function main(plop) {
plop.setGenerator('config', {
description: 'Generates a new eslint config',
prompts: [
{
type: 'input',
name: 'name',
message: 'Enter the name of the config',
},
],
actions: [
{
type: 'add',
path: 'src/configs/{{kebabCase name}}.ts',
templateFile: 'plop/config/config.hbs',
},
{
type: 'add',
path: 'tests/configs/{{kebabCase name}}/utils.ts',
templateFile: 'plop/config/test-utils.hbs',
},
{
type: 'modify',
path: 'src/configs/index.ts',
transform: async (file, { name }) => {
const camelCaseName = name.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
const kebabCaseName = name.replace(/([A-Z])/g, '-$1').toLowerCase();
const config = await prettier.resolveConfig();
const newFile = prettier.format(
[
`import ${camelCaseName} from './${kebabCaseName}';\n`, // import
file.replace(/(\n\};)/m, `\n${kebabCaseName},$1`), // extend exported object
].join(''),
config
);
return newFile;
},
},
],
});
plop.setGenerator('rule', {
description: 'Generates a new eslint rule',
prompts: [
{
type: 'input',
name: 'name',
message: 'Enter the name of the rule',
},
{
type: 'input',
name: 'description',
message: 'Enter the description of the rule',
},
],
actions: [
{
type: 'add',
path: 'src/rules/{{kebabCase name}}.ts',
templateFile: 'plop/rule/rule.hbs',
},
{
type: 'add',
path: 'docs/rules/{{kebabCase name}}.md',
templateFile: 'plop/rule/docs.hbs',
},
{
type: 'add',
path: 'tests/rules/{{kebabCase name}}.md',
templateFile: 'plop/rule/test.hbs',
},
{
type: 'modify',
path: 'src/rules/index.ts',
transform: async (file, { name }) => {
const camelCaseName = name.replace(/-([a-z])/g, (g) => g[1].toUpperCase());
const kebabCaseName = name.replace(/([A-Z])/g, '-$1').toLowerCase();
const config = await prettier.resolveConfig();
const newFile = prettier.format(
[
`import ${camelCaseName} from './${kebabCaseName}';\n`, // import
file.replace(/\{(.*)\}/m, `{$1\n${kebabCaseName}: ${camelCaseName}}`), // extend exported object
].join(''),
config
);
return newFile;
},
},
],
});
};