-
Notifications
You must be signed in to change notification settings - Fork 0
/
plopfile.js
100 lines (94 loc) · 2.51 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
const promptDirectory = require('inquirer-directory');
const { appendFileSync } = require('fs');
const { join, resolve } = require('path');
const { paths } = require('./tools/config');
const indexTemplate = 'tools/plop/generators/component/index.js.hbs';
const mainTemplate = 'tools/plop/generators/component/styled.js.hbs';
const featureDirs = [
'actions',
'components',
'connectors',
'constants',
'records',
'reducers',
]
const cmpfiles = [
'index.js',
'{{properCase name}}.jsx',
'{{properCase name}}-styled.js'
]
const addEntry = (data, config, plop) => {
const { path, template } = config;
const dir = plop.getDestBasePath();
const file = resolve(dir, path);
const entry = plop.renderString(template, data);
try {
appendFileSync(file, entry, 'utf-8');
return file.replace(resolve(dir), '');
} catch(err) {
if (typeof err === 'string') {
throw err;
} else {
throw err.message || JSON.stringify(err);
}
}
}
module.exports = (plop) => {
plop.setActionType('addEntry', addEntry);
plop.setPrompt('directory', promptDirectory)
plop.setGenerator('component', {
description: 'Add styled component',
prompts: [{
type: 'input',
name: 'name',
message: 'component name please'
}],
actions: [{
type: 'add',
path: join(paths.lib, '{{dashCase name}}', '{{properCase name}}.js'),
templateFile: mainTemplate,
}, {
type: 'add',
path: join(paths.lib, '{{dashCase name}}', 'index.js'),
templateFile: indexTemplate,
}, {
type: 'addEntry',
path: join(paths.lib, 'index.js'),
template: 'export * from \'./{{dashCase name}}\'\n'
}],
});
plop.setGenerator('feature', {
description: 'Generate new feature',
prompts: [{
type: 'input',
name: 'feature',
message: 'feature name please'
},{
type: 'directory',
name: 'dir',
message: 'feature path please',
basePath: paths.app,
}],
actions: featureDirs.map(dirname => ({
type: 'add',
path: join(paths.app, `{{dashCase dir}}/{{dashCase feature}}/${dirname}/index.js`)
})),
});
plop.setGenerator('cmp', {
description: 'create component',
prompts: [{
type: 'input',
name: 'name',
message: 'component name please'
}, {
type: 'directory',
name: 'dir',
message: 'component path',
basePath: paths.app,
}],
actions: cmpfiles.map(file => ({
type: 'add',
path: join(paths.app, '{{ dir }}', '{{properCase name}}', file),
})),
});
}