-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathplopfile.js
178 lines (170 loc) · 4.9 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
const path = require("path");
const promptDirectory = require("inquirer-directory");
const databaseConfigLocation = path.join(__dirname, "shared/config/db.ts");
const isNotEmptyFor = (name) => {
return (value) => {
if (!value.trim()) return name + " is required";
return true;
};
};
const textPrompt = (name) => ({
type: "input",
name: "name",
message: `What is your ${name} name?`,
validate: isNotEmptyFor("name"),
});
const createModel = {
type: "add",
path: `shared/models/{{kebabCase name}}.model.ts`,
templateFile: "plop-templates/model.ts",
};
const workflowResourceTemplate = `
{{pascalCase name}}WorkflowStateMachine:
Description: {{sentenceCase name}} state machine Arn
Value:
Ref: {{pascalCase name}}Workflow\${self:service}\${opt:stage, 'dev'}
$1
`;
const workFlowStepTemplate = ` {{camelCase name}}Step:
Type: Task
Resource: !GetAtt {{kebabCase name}}-lambda.Arn
TimeoutSeconds: 28
End: true
$1
`;
const updateTypeORMModels = [
{
type: "modify",
path: databaseConfigLocation,
pattern: /(\/\/ MODELS_IMPORT)/,
template: 'import { {{pascalCase name}}Model } from "../models/{{kebabCase name}}.model";\n$1',
},
{
type: "modify",
path: databaseConfigLocation,
pattern: /(\/\/ MODELS_SETUP)/,
template: "{{pascalCase name}}Model,\n $1",
},
];
module.exports = function (plop) {
plop.setPrompt("directory", promptDirectory);
plop.setGenerator("model", {
description: "Create model",
prompts: [textPrompt("model")],
actions: [createModel, ...updateTypeORMModels],
});
plop.setGenerator("lambda", {
description: "Create lambda",
prompts: [
{
type: "input",
name: "name",
message: "Name",
},
],
actions: [
{
type: "add",
path: "functions/{{kebabCase name}}/handler.ts",
templateFile: "plop-templates/function/handler.ts",
},
{
type: "add",
path: "functions/{{kebabCase name}}/event.schema.ts",
templateFile: "plop-templates/function/event.schema.ts",
},
{
type: "add",
path: "functions/{{kebabCase name}}/config/index.ts",
templateFile: "plop-templates/function/config/index.ts",
},
{
type: "add",
path: "functions/{{kebabCase name}}/function.yml",
templateFile: "plop-templates/function/function.yml",
},
{
type: "modify",
path: "serverless.yml",
pattern: / +(\# PLOP_ADD_LAMBDA)/,
template: " - ${file(functions/{{kebabCase name}}/function.yml)}\n $1",
},
],
});
plop.setGenerator("workflow", {
description: "Create workflow",
prompts: [
{
type: "input",
name: "name",
message: "Workflow name",
},
],
actions: [
{
type: "add",
path: "workflows/{{kebabCase name}}/workflow.yml",
templateFile: "plop-templates/workflow/workflow.yml",
},
{
type: "add",
path: "workflows/{{kebabCase name}}/workflow.asl.yml",
templateFile: "plop-templates/workflow/workflow.asl.yml",
},
{
type: "modify",
path: "serverless.yml",
pattern: / +(\# PLOP_ADD_WORKFLOW_STATE_MACHINE)/,
template: " {{pascalCase name}}Workflow: ${file(workflows/{{kebabCase name}}/workflow.yml)}\n $1",
},
{
type: "modify",
path: "serverless.yml",
pattern: / +(\# PLOP_ADD_WORKFLOW_RESOURCE)/,
template: workflowResourceTemplate,
},
],
});
plop.setGenerator("workflow-step", {
description: "Create workflow step",
prompts: [
{ type: "directory", name: "workflow", message: "Select workflow", basePath: "./workflows" },
{
type: "input",
name: "name",
message: "Step name",
},
],
actions: [
{
type: "add",
path: "workflows/{{workflow}}/{{kebabCase name}}/function.yml",
templateFile: "plop-templates/workflow/step/function.yml",
},
{
type: "add",
path: "workflows/{{workflow}}/{{kebabCase name}}/handler.ts",
templateFile: "plop-templates/workflow/step/handler.ts",
},
{
type: "modify",
path: "serverless.yml",
pattern: / +(\# PLOP_ADD_LAMBDA)/,
template: " - ${file(workflows/{{workflow}}/{{kebabCase name}}/function.yml)}\n $1",
},
{
type: "modify",
path: "workflows/{{workflow}}/workflow.asl.yml",
pattern: / +(\# PLOP_ADD_WORKFLOW_STEP)/,
template: workFlowStepTemplate,
},
{
type: "modify",
path: "serverless.yml",
pattern: / +(\# PLOP_ADD_WORKFLOW_STEP_LOCAL_STEP)/,
template:
" {{camelCase name}}Step: arn:aws:lambda:us-east-1:101010101010:function:${env:APP_NAME, 'tshExampleApp'}-${opt:stage, 'dev'}-{{kebabCase name}}-lambda\n $1",
},
],
});
};