-
Notifications
You must be signed in to change notification settings - Fork 26
/
plopfile.js
150 lines (127 loc) · 3.41 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
const get = require('lodash.get');
const pkg = require('./package.json');
const finder = require('find-package-json');
const pjson = finder().next().value;
const nodePath = require('path');
const pkgDir = require('pkg-dir');
function validate(name, value) {
if ((/.+/).test(value)) {
return true;
}
return name + ' is required';
}
const defaults = function(name) {
const p = [{
type: 'input',
name: 'name',
message: name + ' for?',
validate: validate.bind(this, name + ' name')
}];
return {
description: 'New ' + name,
prompts: p
}
}
const actions = [
{
type: 'add',
path: '{{ pkg "ngrxGen.actions" "actions" }}/{{dashCase name}}.actions.ts',
templateFile: './templates/_actions.ts'
}
];
function actionGenerator(plop) {
plop.setGenerator('Action',
Object.assign({}, defaults('Action'), {
actions: actions
})
);
}
const reducer = [
{
type: 'add',
path: '{{ pkg "ngrxGen.reducers" "reducers" }}/{{dashCase name}}.reducer.ts',
templateFile: './templates/_reducer.ts'
},
{
type: 'add',
path: '{{ pkg "ngrxGen.reducers" "reducers"}}/{{dashCase name}}.reducer.spec.ts',
templateFile: './templates/_reducer.spec.ts'
}
]
function reducerGenerator(plop) {
plop.setGenerator('Reducer',
Object.assign({}, defaults('Reducer'), {
actions: reducer
})
);
}
const effect = [
{
type: 'add',
path: '{{ pkg "ngrxGen.effects" "effects" }}/{{dashCase name}}.effects.ts',
templateFile: './templates/_effect.ts'
}, {
type: 'add',
path: '{{ pkg "plop.effects" "effects" }}/{{dashCase name}}.effects.spec.ts',
templateFile: './templates/_effects.spec.ts'
}
];
function effectGenerator(plop) {
plop.setGenerator('Effect',
Object.assign({}, defaults('Effect'), {
actions: effect
})
);
}
const service = [
{
type: 'add',
path: '{{ pkg "ngrxGen.services" "services" }}/{{dashCase name}}.service.ts',
templateFile: './templates/_service.ts'
}, {
type: 'add',
path: '{{ pkg "plop.services" "services" }}/{{dashCase name}}.service.spec.ts',
templateFile: './templates/_service.spec.ts'
}
];
function serviceGenerator(plop) {
plop.setGenerator('Service',
Object.assign({}, defaults('Service'), {
actions: service
})
);
}
function wholeGenerator(plop) {
plop.setGenerator('The whole shebang',
Object.assign({}, defaults('Whole'), {
description: 'Actions, Reducer, Service and Effect',
actions: [].concat(actions, reducer, effect, service)
})
);
}
module.exports = function (plop) {
plop.addHelper('pkg', (packageJSONPath, name) => {
if(get(pjson, 'ngrxGen.basePath')) {
const basePath = nodePath.resolve(get(pjson, 'ngrxGen.basePath'));
return nodePath.resolve(pkgDir.sync(process.cwd()), get(pjson, 'ngrxGen.basePath'), name);
}
if(get(pjson, 'ngrxGen.seperateDirectory')) {
return nodePath.resolve(process.cwd(), name);
}
if (get(pjson, packageJSONPath)) {
return nodePath.resolve(process.cwd(), get(pjson, packageJSONPath))
}
return nodePath.resolve(process.cwd(), '.');
});
plop.addHelper('position', function (name) {
if(get(pjson, 'ngrxGen.seperateDirectory')) {
return '../' + name;
}
return '.';
});
wholeGenerator(plop);
actionGenerator(plop);
reducerGenerator(plop);
effectGenerator(plop);
serviceGenerator(plop);
};