@anthonylzq/simba.js 4.3.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,466 @@
1
+ const { platform } = require('os')
2
+ const { promisify } = require('util')
3
+ const exec = promisify(require('child_process').exec)
4
+ const writeFile = require('../../utils/writeFile')
5
+
6
+ /**
7
+ * @param {Object} args
8
+ * @param {Boolean} args.express
9
+ * @param {String} args.projectName
10
+ * @param {String} args.email
11
+ * @param {String} args.projectVersion
12
+ * @param {String} args.graphql
13
+ */
14
+ module.exports = async ({
15
+ express,
16
+ projectName,
17
+ email,
18
+ projectVersion,
19
+ graphql
20
+ }) => {
21
+ if (express) {
22
+ const createFoldersCommand = `mkdir ${projectName}/src/utils`
23
+
24
+ if (platform() === 'win32')
25
+ await exec(createFoldersCommand.replaceAll('/', '\\'))
26
+ else await exec(createFoldersCommand)
27
+
28
+ const utils = {
29
+ docs: {
30
+ content: `{
31
+ "openapi": "3.0.0",
32
+ "info": {
33
+ "title": "${projectName}",
34
+ "description": "Documentation of the test",
35
+ "contact": {
36
+ "email": "${email}"
37
+ },
38
+ "license": {
39
+ "name": "MIT",
40
+ "url": "https://opensource.org/licenses/MIT"
41
+ },
42
+ "version": "${projectVersion}"
43
+ },
44
+ "servers": [
45
+ {
46
+ "url": "http://localhost:1996/api",
47
+ "description": "${projectName} local API"
48
+ }
49
+ ],
50
+ "tags": [
51
+ {
52
+ "name": "user",
53
+ "description": "Operations related to the user"
54
+ }
55
+ ]
56
+ ${
57
+ !graphql
58
+ ? `,
59
+ "paths": {
60
+ "/users": {
61
+ "post": {
62
+ "tags": [
63
+ "user"
64
+ ],
65
+ "summary": "Save a user in the database",
66
+ "operationId": "store",
67
+ "requestBody": {
68
+ "$ref": "#/components/requestBodies/UserDTO"
69
+ },
70
+ "responses": {
71
+ "201": {
72
+ "description": "User successfully stored",
73
+ "content": {
74
+ "application/json": {
75
+ "schema": {
76
+ "$ref": "#/components/schemas/User"
77
+ }
78
+ }
79
+ }
80
+ },
81
+ "422": {
82
+ "description": "Invalid request format",
83
+ "content": {
84
+ "application/json": {
85
+ "schema": {
86
+ "$ref": "#/components/schemas/DefaultError"
87
+ }
88
+ }
89
+ }
90
+ },
91
+ "500": {
92
+ "description": "Internal server error",
93
+ "content": {
94
+ "application/json": {
95
+ "schema": {
96
+ "$ref": "#/components/schemas/DefaultError"
97
+ }
98
+ }
99
+ }
100
+ }
101
+ }
102
+ },
103
+ "get": {
104
+ "tags": [
105
+ "user"
106
+ ],
107
+ "summary": "Get all the users in the database",
108
+ "operationId": "getAll",
109
+ "responses": {
110
+ "200": {
111
+ "description": "All the users in the database",
112
+ "content": {
113
+ "application/json": {
114
+ "schema": {
115
+ "type": "object",
116
+ "properties": {
117
+ "error": {
118
+ "type": "boolean",
119
+ "default": false
120
+ },
121
+ "message": {
122
+ "type": "object",
123
+ "properties": {
124
+ "result": {
125
+ "type": "array",
126
+ "items": {
127
+ "$ref": "#/components/schemas/User"
128
+ }
129
+ }
130
+ }
131
+ }
132
+ }
133
+ }
134
+ }
135
+ }
136
+ },
137
+ "500": {
138
+ "description": "Internal server error",
139
+ "content": {
140
+ "application/json": {
141
+ "schema": {
142
+ "$ref": "#/components/schemas/DefaultError"
143
+ }
144
+ }
145
+ }
146
+ }
147
+ }
148
+ },
149
+ "delete": {
150
+ "tags": [
151
+ "user"
152
+ ],
153
+ "summary": "Delete all the users in the database",
154
+ "operationId": "deleteAll",
155
+ "responses": {
156
+ "200": {
157
+ "description": "All the users in the database",
158
+ "content": {
159
+ "application/json": {
160
+ "schema": {
161
+ "$ref": "#/components/schemas/DefaultSuccess"
162
+ }
163
+ }
164
+ }
165
+ },
166
+ "500": {
167
+ "description": "Internal server error",
168
+ "content": {
169
+ "application/json": {
170
+ "schema": {
171
+ "$ref": "#/components/schemas/DefaultError"
172
+ }
173
+ }
174
+ }
175
+ }
176
+ }
177
+ }
178
+ },
179
+ "/user/{id}": {
180
+ "get": {
181
+ "tags": [
182
+ "user"
183
+ ],
184
+ "summary": "Get an specific user",
185
+ "operationId": "getOne",
186
+ "parameters": [
187
+ {
188
+ "name": "id",
189
+ "in": "path",
190
+ "description": "MongoDB user id",
191
+ "required": true,
192
+ "style": "simple",
193
+ "explode": false,
194
+ "schema": {
195
+ "type": "string"
196
+ }
197
+ }
198
+ ],
199
+ "responses": {
200
+ "200": {
201
+ "description": "User stored in the database",
202
+ "content": {
203
+ "application/json": {
204
+ "schema": {
205
+ "$ref": "#/components/schemas/User"
206
+ }
207
+ }
208
+ }
209
+ },
210
+ "404": {
211
+ "description": "User not found",
212
+ "content": {
213
+ "application/json": {
214
+ "schema": {
215
+ "$ref": "#/components/schemas/DefaultError"
216
+ }
217
+ }
218
+ }
219
+ },
220
+ "422": {
221
+ "description": "Invalid request format",
222
+ "content": {
223
+ "application/json": {
224
+ "schema": {
225
+ "$ref": "#/components/schemas/DefaultError"
226
+ }
227
+ }
228
+ }
229
+ },
230
+ "500": {
231
+ "description": "Internal server error",
232
+ "content": {
233
+ "application/json": {
234
+ "schema": {
235
+ "$ref": "#/components/schemas/DefaultError"
236
+ }
237
+ }
238
+ }
239
+ }
240
+ }
241
+ },
242
+ "patch": {
243
+ "tags": [
244
+ "user"
245
+ ],
246
+ "summary": "Update the user data",
247
+ "operationId": "update",
248
+ "parameters": [
249
+ {
250
+ "name": "id",
251
+ "in": "path",
252
+ "description": "MongoDB user id",
253
+ "required": true,
254
+ "style": "simple",
255
+ "explode": false,
256
+ "schema": {
257
+ "type": "string"
258
+ }
259
+ }
260
+ ],
261
+ "requestBody": {
262
+ "$ref": "#/components/requestBodies/UserDTO"
263
+ },
264
+ "responses": {
265
+ "200": {
266
+ "description": "User successfully updated",
267
+ "content": {
268
+ "application/json": {
269
+ "schema": {
270
+ "$ref": "#/components/schemas/User"
271
+ }
272
+ }
273
+ }
274
+ },
275
+ "404": {
276
+ "description": "User not found",
277
+ "content": {
278
+ "application/json": {
279
+ "schema": {
280
+ "$ref": "#/components/schemas/DefaultError"
281
+ }
282
+ }
283
+ }
284
+ },
285
+ "422": {
286
+ "description": "Invalid request format",
287
+ "content": {
288
+ "application/json": {
289
+ "schema": {
290
+ "$ref": "#/components/schemas/DefaultError"
291
+ }
292
+ }
293
+ }
294
+ },
295
+ "500": {
296
+ "description": "Internal server error",
297
+ "content": {
298
+ "application/json": {
299
+ "schema": {
300
+ "$ref": "#/components/schemas/DefaultError"
301
+ }
302
+ }
303
+ }
304
+ }
305
+ }
306
+ },
307
+ "delete": {
308
+ "tags": [
309
+ "user"
310
+ ],
311
+ "summary": "Delete one user from the database",
312
+ "operationId": "delete",
313
+ "parameters": [
314
+ {
315
+ "name": "id",
316
+ "in": "path",
317
+ "description": "MongoDB user id",
318
+ "required": true,
319
+ "style": "simple",
320
+ "explode": false,
321
+ "schema": {
322
+ "type": "string"
323
+ }
324
+ }
325
+ ],
326
+ "responses": {
327
+ "200": {
328
+ "description": "User successfully deleted",
329
+ "content": {
330
+ "application/json": {
331
+ "schema": {
332
+ "$ref": "#/components/schemas/DefaultSuccess"
333
+ }
334
+ }
335
+ }
336
+ },
337
+ "404": {
338
+ "description": "User not found",
339
+ "content": {
340
+ "application/json": {
341
+ "schema": {
342
+ "$ref": "#/components/schemas/DefaultError"
343
+ }
344
+ }
345
+ }
346
+ },
347
+ "422": {
348
+ "description": "Invalid request format",
349
+ "content": {
350
+ "application/json": {
351
+ "schema": {
352
+ "$ref": "#/components/schemas/DefaultError"
353
+ }
354
+ }
355
+ }
356
+ },
357
+ "500": {
358
+ "description": "Internal server error",
359
+ "content": {
360
+ "application/json": {
361
+ "schema": {
362
+ "$ref": "#/components/schemas/DefaultError"
363
+ }
364
+ }
365
+ }
366
+ }
367
+ }
368
+ }
369
+ }
370
+ },
371
+ "components": {
372
+ "schemas": {
373
+ "User": {
374
+ "type": "object",
375
+ "properties": {
376
+ "id": {
377
+ "type": "string"
378
+ },
379
+ "lastName": {
380
+ "type": "string"
381
+ },
382
+ "name": {
383
+ "type": "string"
384
+ }
385
+ }
386
+ },
387
+ "DefaultSuccess": {
388
+ "type": "object",
389
+ "properties": {
390
+ "error": {
391
+ "type": "boolean",
392
+ "default": false
393
+ },
394
+ "message": {
395
+ "type": "object",
396
+ "properties": {
397
+ "result": {
398
+ "type": "string"
399
+ }
400
+ }
401
+ }
402
+ }
403
+ },
404
+ "DefaultError": {
405
+ "type": "object",
406
+ "properties": {
407
+ "error": {
408
+ "type": "boolean",
409
+ "default": true
410
+ },
411
+ "message": {
412
+ "type": "object",
413
+ "properties": {
414
+ "result": {
415
+ "type": "string"
416
+ }
417
+ }
418
+ }
419
+ }
420
+ }
421
+ },
422
+ "requestBodies": {
423
+ "UserDTO": {
424
+ "description": "User name and last name",
425
+ "content": {
426
+ "application/json": {
427
+ "schema": {
428
+ "type": "object",
429
+ "properties": {
430
+ "args": {
431
+ "type": "object",
432
+ "properties": {
433
+ "name": {
434
+ "type": "string"
435
+ },
436
+ "lastName": {
437
+ "type": "string"
438
+ }
439
+ }
440
+ }
441
+ }
442
+ }
443
+ }
444
+ },
445
+ "required": true
446
+ }
447
+ }
448
+ }
449
+ }`
450
+ : '}'
451
+ }`,
452
+ file: `${projectName}/src/utils/docs.json`
453
+ },
454
+ index: {
455
+ content: "export { default as docs } from './docs.json'\n",
456
+ file: `${projectName}/src/utils/index.ts`
457
+ }
458
+ }
459
+
460
+ if (express)
461
+ await Promise.all([
462
+ writeFile(utils.docs.file, utils.docs.content),
463
+ writeFile(utils.index.file, utils.index.content)
464
+ ])
465
+ }
466
+ }
package/lib/src/index.js CHANGED
@@ -46,7 +46,8 @@ module.exports = async ({
46
46
  licenseYear,
47
47
  manager,
48
48
  mainFile,
49
- fastify
49
+ fastify,
50
+ graphql
50
51
  }) => {
51
52
  const process = 4
52
53
  let i = 0
@@ -57,14 +58,25 @@ module.exports = async ({
57
58
  cliProgress.Presets.shades_classic
58
59
  )
59
60
 
60
- const expressProdPackages = 'express morgan swagger-ui-express cors'
61
- const fastifyProdPackages = 'fastify fastify-swagger fastify-cors pino-pretty'
62
- const prodPackages = `${manager} http-errors mongoose @sinclair/typebox ajv@^6 ${
63
- fastify ? fastifyProdPackages : expressProdPackages
61
+ const expressProdPackages = `express swagger-ui-express cors express-pino-logger ${
62
+ graphql ? 'apollo-server-express' : ''
64
63
  }`
65
64
 
66
- const expressDevPackages = `@types/express @types/morgan @types/swagger-ui-express @types/cors`
65
+ const fastifyProdPackages = `fastify fastify-swagger fastify-cors ${
66
+ graphql ? 'apollo-server-fastify apollo-server-plugin-base' : ''
67
+ }`
68
+
69
+ const prodPackages = `${manager} @sinclair/typebox http-errors mongoose pino-pretty ${
70
+ graphql
71
+ ? '@graphql-tools/schema ajv ajv-formats apollo-server-core graphql'
72
+ : 'ajv@^6'
73
+ } ${fastify ? fastifyProdPackages : expressProdPackages}`
74
+
75
+ const expressDevPackages =
76
+ '@types/express @types/swagger-ui-express @types/cors @types/express-pino-logger'
77
+
67
78
  const fastifyDevPackages = ''
79
+
68
80
  const devPackages = `${manager} -D \
69
81
  @types/http-errors \
70
82
  @types/node \
@@ -114,7 +126,7 @@ ${fastify ? fastifyDevPackages : expressDevPackages}`
114
126
  eslint(projectName),
115
127
  webpack(projectName),
116
128
  docker(projectName),
117
- api({ projectName, version, email, fastify }),
129
+ api({ projectName, version, email, fastify, graphql }),
118
130
  exec('git init', { cwd: `./${projectName}` })
119
131
  ]
120
132
 
@@ -159,6 +171,8 @@ ${fastify ? fastifyDevPackages : expressDevPackages}`
159
171
  * @property {Boolean} npm true means that the package manager will be npm, otherwise yarn
160
172
  * @property {'yarn add'|'npm i'} manager command that will be used to install packages
161
173
  * @property {String} mainFile main file of the project
174
+ * @property {Boolean} fastify true means that the project will be using Fastify
175
+ * @property {Boolean} graphql true means that the project will be using GraphQL
162
176
  */
163
177
 
164
178
  /**
@@ -1,4 +1,4 @@
1
- const fs = require('fs')
1
+ const { writeFile } = require('fs')
2
2
 
3
3
  /**
4
4
  * @param {String} filename
@@ -7,7 +7,7 @@ const fs = require('fs')
7
7
  */
8
8
  module.exports = (filename, data) => {
9
9
  return new Promise((resolve, reject) => {
10
- fs.writeFile(filename, data, error => {
10
+ writeFile(filename, data, error => {
11
11
  if (error) reject(error.message)
12
12
  else resolve('Saved successfully')
13
13
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anthonylzq/simba.js",
3
- "version": "4.3.0",
3
+ "version": "5.0.0",
4
4
  "description": "set up a modern backend app by running one command",
5
5
  "main": "lib/index.js",
6
6
  "directories": {
@@ -9,17 +9,25 @@
9
9
  },
10
10
  "scripts": {
11
11
  "service": "node ./bin",
12
+ "help": "node ./bin --help",
12
13
  "release": "standard-version",
13
- "test": "npm run rm && npm run test-express && npm run test-fastify",
14
+ "test": "npm run rm && npm run test-express && npm run test-fastify && npm run test-express-graphql && npm run test-fastify-graphql",
14
15
  "test-express": "npm run rm-express && npm run cd-mv-example && node -r dotenv/config ./bin -N example/express -D 'This is a test using express' -a AnthonyLzq -e sluzquinosa@uni.pe -l mit -H && npm run rm-git-express",
15
16
  "test-fastify": "npm run rm-fastify && npm run cd-mv-example && node -r dotenv/config ./bin -N example/fastify -D 'This is a test using fastify' -a AnthonyLzq -e sluzquinosa@uni.pe -l mit -H -F && npm run rm-git-fastify",
17
+ "test-express-graphql": "npm run rm-express-graphql && npm run cd-mv-example && node -r dotenv/config ./bin -N example/express-graphql -D 'This is a test using express with GraphQL' -a AnthonyLzq -e sluzquinosa@uni.pe -l mit -H -g && npm run rm-git-express-graphql",
18
+ "test-fastify-graphql": "npm run rm-fastify-graphql && npm run cd-mv-example && node -r dotenv/config ./bin -N example/fastify-graphql -D 'This is a test using fastify with GraphQL' -a AnthonyLzq -e sluzquinosa@uni.pe -l mit -H -F -g && npm run rm-git-fastify-graphql",
16
19
  "lint": "eslint --ext js lib/ --fix",
17
20
  "rm": "if [ -d \"example\" ]; then rm -rf example; fi",
18
21
  "rm-express": "if [ -d \"example/express\" ]; then rm -rf example/express; fi",
19
22
  "rm-fastify": "if [ -d \"example/fastify\" ]; then rm -rf example/fastify; fi",
23
+ "rm-express-graphql": "if [ -d \"example/express-graphql\" ]; then rm -rf example/express-graphql; fi",
24
+ "rm-fastify-graphql": "if [ -d \"example/fastify-graphql\" ]; then rm -rf example/fastify-graphql; fi",
20
25
  "rm-git-express": "if [ -d \"example/express/.git\" ]; then rm -rf example/express/.git; fi",
21
26
  "rm-git-fastify": "if [ -d \"example/fastify/.git\" ]; then rm -rf example/fastify/.git; fi",
22
- "cd-mv-example": "if [ ! -d \"example\" ]; then mkdir example && cd example; fi"
27
+ "rm-git-express-graphql": "if [ -d \"example/express-graphql/.git\" ]; then rm -rf example/express-graphql/.git; fi",
28
+ "rm-git-fastify-graphql": "if [ -d \"example/fastify-graphql/.git\" ]; then rm -rf example/fastify-graphql/.git; fi",
29
+ "cd-mv-example": "if [ ! -d \"example\" ]; then mkdir example && cd example; fi",
30
+ "version": "npm run release && git add CHANGELOG.md"
23
31
  },
24
32
  "bin": {
25
33
  "simba": "./bin/index.js"
@@ -66,5 +74,12 @@
66
74
  "files": [
67
75
  "lib",
68
76
  "bin"
69
- ]
77
+ ],
78
+ "standard-version": {
79
+ "skip": {
80
+ "tag": true,
81
+ "commit": true,
82
+ "bump": true
83
+ }
84
+ }
70
85
  }