pwi-plata-type 0.3.17 → 0.4.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.
- package/__BUILD__/bin/extras/create-api-cli.js +96 -0
- package/__BUILD__/bin/extras/create-api-cli.js.map +1 -0
- package/__BUILD__/bin/extras/create-lib-cli.js +62 -0
- package/__BUILD__/bin/extras/create-lib-cli.js.map +1 -0
- package/__BUILD__/bin/extras/plata-create-tools.js +94 -0
- package/__BUILD__/bin/extras/plata-create-tools.js.map +1 -0
- package/__BUILD__/bin/plata-api.js +138 -0
- package/__BUILD__/bin/plata-api.js.map +1 -0
- package/__BUILD__/bin/plata-create.js +24 -0
- package/__BUILD__/bin/plata-create.js.map +1 -0
- package/__BUILD__/bin/plata-runtime.js +156 -0
- package/__BUILD__/bin/plata-runtime.js.map +1 -0
- package/__BUILD__/bin/plata-swagger.js +82 -0
- package/__BUILD__/bin/plata-swagger.js.map +1 -0
- package/__BUILD__/bin/runtime/_setupRuntime.js +43 -0
- package/__BUILD__/bin/runtime/_setupRuntime.js.map +1 -0
- package/__BUILD__/bin/runtime/class.js +18 -0
- package/__BUILD__/bin/runtime/class.js.map +1 -0
- package/__BUILD__/bin/runtime/dirs.js +16 -0
- package/__BUILD__/bin/runtime/dirs.js.map +1 -0
- package/__BUILD__/bin/runtime/promises.js +15 -0
- package/__BUILD__/bin/runtime/promises.js.map +1 -0
- package/__BUILD__/bin/runtime/require.js +88 -0
- package/__BUILD__/bin/runtime/require.js.map +1 -0
- package/__BUILD__/index.js +42 -0
- package/__BUILD__/index.js.map +1 -0
- package/__BUILD__/libs/axios.js +42 -0
- package/__BUILD__/libs/axios.js.map +1 -0
- package/__BUILD__/libs/cluster.js +160 -0
- package/__BUILD__/libs/cluster.js.map +1 -0
- package/__BUILD__/libs/fs.js +206 -0
- package/__BUILD__/libs/fs.js.map +1 -0
- package/__BUILD__/libs/model-validations.js +356 -0
- package/__BUILD__/libs/model-validations.js.map +1 -0
- package/__BUILD__/libs/model.js +185 -0
- package/__BUILD__/libs/model.js.map +1 -0
- package/__BUILD__/libs/router.js +127 -0
- package/__BUILD__/libs/router.js.map +1 -0
- package/__BUILD__/libs/swagger.js +64 -0
- package/__BUILD__/libs/swagger.js.map +1 -0
- package/__BUILD__/libs/tasks.js +134 -0
- package/__BUILD__/libs/tasks.js.map +1 -0
- package/__BUILD__/libs/tools.js +231 -0
- package/__BUILD__/libs/tools.js.map +1 -0
- package/__BUILD__/scripts/post-install/api.js +25 -0
- package/__BUILD__/scripts/post-install/api.js.map +1 -0
- package/__BUILD__/scripts/post-install/lib.js +25 -0
- package/__BUILD__/scripts/post-install/lib.js.map +1 -0
- package/__BUILD__/scripts/post-install.js +65 -0
- package/__BUILD__/scripts/post-install.js.map +1 -0
- package/__BUILD__/scripts/pre-test.js +25 -0
- package/__BUILD__/scripts/pre-test.js.map +1 -0
- package/package.json +18 -16
- package/src/@types/cluster.actions.d.ts +7 -0
- package/src/@types/cluster.d.ts +33 -0
- package/src/@types/create-cli.d.ts +9 -0
- package/src/@types/exported.d.ts +24 -0
- package/src/@types/global.d.ts +69 -0
- package/src/@types/model.d.ts +69 -0
- package/src/@types/router.d.ts +75 -0
- package/src/@types/swagger.d.ts +23 -0
- package/{bin/extras/plata-create-api.ts → src/bin/extras/create-api-cli.ts} +5 -6
- package/{bin/extras/plata-create-lib.ts → src/bin/extras/create-lib-cli.ts} +3 -4
- package/{bin → src/bin}/extras/plata-create-tools.ts +4 -4
- package/src/bin/plata-api.ts +172 -0
- package/src/bin/plata-create.ts +21 -0
- package/src/bin/plata-runtime.ts +188 -0
- package/src/bin/plata-swagger.ts +100 -0
- package/src/bin/runtime/_setupRuntime.ts +49 -0
- package/src/bin/runtime/_types.d.ts +4 -0
- package/src/bin/runtime/class.ts +21 -0
- package/src/bin/runtime/dirs.ts +26 -0
- package/src/bin/runtime/promises.ts +15 -0
- package/src/bin/runtime/require.ts +104 -0
- package/{index.ts → src/index.ts} +2 -4
- package/{libs → src/libs}/axios.ts +0 -0
- package/src/libs/cluster.ts +189 -0
- package/src/libs/fs.ts +382 -0
- package/{libs/model.ts → src/libs/model-validations.ts} +28 -262
- package/src/libs/model.ts +233 -0
- package/src/libs/router.ts +147 -0
- package/src/libs/swagger.ts +81 -0
- package/{libs → src/libs}/tasks.ts +0 -0
- package/{libs → src/libs}/tools.ts +3 -18
- package/src/scripts/post-install/api.ts +32 -0
- package/src/scripts/post-install/lib.ts +30 -0
- package/src/scripts/post-install.ts +65 -0
- package/src/scripts/pre-test.ts +34 -0
- package/src/tests/assets/config/test.txt +1 -0
- package/src/tests/config.test.ts +13 -0
- package/src/tests/model.test.ts +115 -0
- package/templates/create-cli/cluster.ts +7 -9
- package/templates/create-cli/config.ts +2 -8
- package/templates/create-cli/lib.ts +7 -17
- package/templates/create-cli/model.ts +10 -21
- package/templates/create-cli/route.ts +1 -3
- package/templates/postinstall/api/configs/env.ts +2 -8
- package/templates/postinstall/api/tsconfig.json +20 -37
- package/templates/postinstall/lib/postinstall-tools.mjs +1 -1
- package/templates/postinstall/lib/postinstall.js +1 -0
- package/templates/postinstall/lib/tsconfig.json +21 -38
- package/tsconfig.json +25 -37
- package/Dockerfile +0 -13
- package/bin/extras/plata-build-tool.ts +0 -151
- package/bin/plata-build.ts +0 -144
- package/bin/plata-create.ts +0 -28
- package/bin/plata-reload.ts +0 -13
- package/bin/plata-swagger-gen.ts +0 -183
- package/bin/plata.ts +0 -122
- package/libs/cluster.ts +0 -219
- package/libs/env.ts +0 -28
- package/libs/routes.ts +0 -275
- package/libs/swagger.ts +0 -79
- package/postinstall-tools.mjs +0 -115
- package/postinstall.mjs +0 -42
- package/templates/postinstall/api/_install.mjs +0 -34
- package/templates/postinstall/lib/_install.mjs +0 -30
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import express, { Router } from 'express'
|
|
3
|
+
|
|
4
|
+
export namespace Internals {
|
|
5
|
+
export function buildRouterObject(): Router.Router {
|
|
6
|
+
const router: Router.RouterConfig = {
|
|
7
|
+
routes: [],
|
|
8
|
+
swaggerHide: false
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return new Proxy(router, {
|
|
12
|
+
get: (targetRouter, prop) => {
|
|
13
|
+
if (targetRouter[prop] !== undefined) {
|
|
14
|
+
return targetRouter[prop]
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (prop === 'then') {
|
|
18
|
+
return targetRouter
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
return (httpPathOrHandler: string | Router.RequestHandler, ...handlers: Router.RequestHandler[]) => {
|
|
22
|
+
let httpPath: string | undefined = undefined
|
|
23
|
+
|
|
24
|
+
if (typeof httpPathOrHandler === 'string') {
|
|
25
|
+
httpPath = httpPathOrHandler
|
|
26
|
+
} else {
|
|
27
|
+
handlers = [ httpPathOrHandler, ...handlers ]
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
targetRouter.routes.push({
|
|
31
|
+
method: prop.toString(),
|
|
32
|
+
handlers,
|
|
33
|
+
path: httpPath
|
|
34
|
+
})
|
|
35
|
+
}
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
set: (targetRouter, prop, value) => {
|
|
39
|
+
if (targetRouter[prop] !== undefined) {
|
|
40
|
+
targetRouter[prop] = value
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return true
|
|
44
|
+
}
|
|
45
|
+
}) as Router.Router
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function buildRequestHandler(r: Router.RequestHandler): express.RequestHandler {
|
|
49
|
+
return async (req, res, next) => {
|
|
50
|
+
const aReq = req as Router.Request
|
|
51
|
+
const aRes = res as Router.Response
|
|
52
|
+
|
|
53
|
+
if (aReq.user === undefined) {
|
|
54
|
+
aReq.user = new Object(null)
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (aReq.extras === undefined) {
|
|
58
|
+
aReq.extras = new Object(null)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (aRes.oldJson === undefined) {
|
|
62
|
+
aRes.oldJson = aRes.json
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
aRes.error = error => {
|
|
66
|
+
return aRes.oldJson(error)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return r(aReq, aRes, next)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export async function loadRoutesFromProject(): PlataPromise<Router.RequiredRoute[]> {
|
|
75
|
+
const routes: RequiredFolder<{default: Router.RouterBuilder}> = await require('@@/routes/*.*')
|
|
76
|
+
|
|
77
|
+
if (routes.errorID !== undefined) {
|
|
78
|
+
return routes
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
let rs = routes.map(route => {
|
|
82
|
+
const r: Router.RequiredRoute = {
|
|
83
|
+
...route,
|
|
84
|
+
exports: route.exports.default,
|
|
85
|
+
httpRoute: path.normalize(route.filePath
|
|
86
|
+
.replace('__BUILD__', '')
|
|
87
|
+
.replace(Plata.ProjectDir, '')
|
|
88
|
+
.replace('routes', '')
|
|
89
|
+
.replace(/(\.ts|\.js)$/, '')
|
|
90
|
+
.replace(/index/, '')
|
|
91
|
+
.replace(/\{(.*?)\}/g, ':$1')
|
|
92
|
+
.replace(/\[(.*?)\]/g, ':$1?')
|
|
93
|
+
).replace(/\\/g, '/'),
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return r
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
rs = rs.sort((x, y) => {
|
|
100
|
+
if (x.name === 'index') {
|
|
101
|
+
if (y.name === 'index') {
|
|
102
|
+
return +(x.httpRoute.length > y.httpRoute.length)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return -1
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
return +(x.name > y.name)
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
return rs
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export async function buildExpressRouter(requiredRoutes: Router.RequiredRoute[]): PlataPromise<Router.Router> {
|
|
115
|
+
const router = Router() as any as Router.Router
|
|
116
|
+
|
|
117
|
+
router.routes = []
|
|
118
|
+
router.swaggerHide = false
|
|
119
|
+
|
|
120
|
+
for (const r of requiredRoutes) {
|
|
121
|
+
const v = await r.exports(Internals.buildRouterObject())
|
|
122
|
+
|
|
123
|
+
const routes = v.routes
|
|
124
|
+
const swaggerHide = v.swaggerHide
|
|
125
|
+
|
|
126
|
+
for (let i = 0; i < routes.length; i++) {
|
|
127
|
+
const route = routes[i]
|
|
128
|
+
|
|
129
|
+
let httpPath = r.httpRoute
|
|
130
|
+
|
|
131
|
+
if (route.path !== undefined) {
|
|
132
|
+
httpPath = path.normalize(`${httpPath}/${route.path}`).replace(/\\/g, '/')
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
router[route.method](httpPath, ...route.handlers.map(Internals.buildRequestHandler))
|
|
136
|
+
|
|
137
|
+
router.routes.push({
|
|
138
|
+
path: httpPath,
|
|
139
|
+
method: route.method,
|
|
140
|
+
handlers: [],
|
|
141
|
+
swaggerHide: swaggerHide
|
|
142
|
+
})
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return router
|
|
147
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import swaggerUi from 'swagger-ui-express'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
|
|
4
|
+
export namespace Internals {
|
|
5
|
+
export async function loadSwaggerConfig(): Promise<any> {
|
|
6
|
+
const obj = require('@@/swagger/header.json')
|
|
7
|
+
|
|
8
|
+
obj.paths = new Object(null)
|
|
9
|
+
|
|
10
|
+
const swaggerFolder = path.join(
|
|
11
|
+
Plata.ProjectDir, '__BUILD__', 'swagger', 'routes'
|
|
12
|
+
).replace(/\\/g, '/')
|
|
13
|
+
|
|
14
|
+
// build routes
|
|
15
|
+
{
|
|
16
|
+
const routes = await Plata.files.FindFiles([ swaggerFolder ], 'header.json')
|
|
17
|
+
|
|
18
|
+
for (const route of routes) {
|
|
19
|
+
const docPath = path.normalize(path.dirname(route))
|
|
20
|
+
const method = path.normalize(path.basename(docPath))
|
|
21
|
+
const httpPath = path.normalize(
|
|
22
|
+
path.dirname(docPath.replace(swaggerFolder, ''))
|
|
23
|
+
).replace(/\\/g, '/')
|
|
24
|
+
|
|
25
|
+
const header = require(path.join(docPath, 'header.json'))
|
|
26
|
+
const body = require(path.join(docPath, 'body.json'))
|
|
27
|
+
const response = require(path.join(docPath, 'response.json'))
|
|
28
|
+
|
|
29
|
+
if (obj.paths[httpPath] === undefined) {
|
|
30
|
+
obj.paths[httpPath] = new Object(null)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
obj.paths[httpPath][method] = { ...header, responses: response }
|
|
34
|
+
|
|
35
|
+
//https://bobbyhadz.com/blog/javascript-check-if-object-is-empty
|
|
36
|
+
if (Object.keys(body).length !== 0) {
|
|
37
|
+
if (!Array.isArray(obj.paths[httpPath][method].parameters)) {
|
|
38
|
+
obj.paths[httpPath][method].parameters = []
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const swaggerRouteBody = {
|
|
42
|
+
name: "body",
|
|
43
|
+
description: body.description,
|
|
44
|
+
in: "body",
|
|
45
|
+
schema: {
|
|
46
|
+
type: body.type ?? "object",
|
|
47
|
+
}
|
|
48
|
+
} as any
|
|
49
|
+
|
|
50
|
+
if (swaggerRouteBody.schema.type === 'array') {
|
|
51
|
+
swaggerRouteBody.schema.items = {
|
|
52
|
+
example: body.example
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
swaggerRouteBody.schema.properties = body.example
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
obj.paths[httpPath][method].parameters.push(swaggerRouteBody)
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return obj
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export const swagger: Router.RouterBuilder = async (router) => {
|
|
68
|
+
router.swaggerHide = true
|
|
69
|
+
|
|
70
|
+
if (Plata.config._PLATA_WORKER_NAME === '__PLATA_EXPRESS__') {
|
|
71
|
+
router.use(
|
|
72
|
+
swaggerUi.serve as any,
|
|
73
|
+
swaggerUi.setup(
|
|
74
|
+
await Internals.loadSwaggerConfig(),
|
|
75
|
+
{}
|
|
76
|
+
) as any
|
|
77
|
+
)
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
return router
|
|
81
|
+
}
|
|
File without changes
|
|
@@ -2,6 +2,7 @@ import path from 'node:path'
|
|
|
2
2
|
import fs from 'node:fs'
|
|
3
3
|
import readline from 'node:readline'
|
|
4
4
|
import * as tGlob from 'glob'
|
|
5
|
+
// TODO matar esse aquivo e usar o PlataFS
|
|
5
6
|
|
|
6
7
|
const glob = (tGlob as any).default as typeof tGlob
|
|
7
8
|
|
|
@@ -54,24 +55,8 @@ export namespace PlataDirs {
|
|
|
54
55
|
return path.resolve('.', 'routes')
|
|
55
56
|
}
|
|
56
57
|
|
|
57
|
-
export const ProjectJson: any = JSON.parse(
|
|
58
|
-
fs.readFileSync(
|
|
59
|
-
path.join(PlataDirs.getProjectDir(), 'package.json')
|
|
60
|
-
).toString()
|
|
61
|
-
)
|
|
62
|
-
|
|
63
|
-
export function getPlataDir(): string {
|
|
64
|
-
return path.resolve('.', 'node_modules', ProjectJson.plata_name)
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export const PlataJson: any = JSON.parse(
|
|
68
|
-
fs.readFileSync(
|
|
69
|
-
path.join(PlataDirs.getPlataDir(), 'package.json')
|
|
70
|
-
).toString()
|
|
71
|
-
)
|
|
72
|
-
|
|
73
58
|
export function getPlataTempletesDir(): string {
|
|
74
|
-
return path.join(
|
|
59
|
+
return path.join(Plata.PlataDir, 'templates')
|
|
75
60
|
}
|
|
76
61
|
|
|
77
62
|
export function getPlataTempleteDir(group:string, ...template: string[]) {
|
|
@@ -79,7 +64,7 @@ export namespace PlataDirs {
|
|
|
79
64
|
}
|
|
80
65
|
|
|
81
66
|
export function getPlataBinExtraFile(file: string): string {
|
|
82
|
-
return `${path.join(
|
|
67
|
+
return `${path.join(Plata.ProjectJson, 'bin', 'extras', file)}`.replace(/\\/g, '/')
|
|
83
68
|
}
|
|
84
69
|
|
|
85
70
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { PlataFs } from "++/libs/fs"
|
|
2
|
+
|
|
3
|
+
export async function install(tools: PlataFs.CliTools, projectPackageJson: any) {
|
|
4
|
+
const promises: Promise<void>[] = []
|
|
5
|
+
|
|
6
|
+
promises.push(tools.createFolderToDest('clusters'))
|
|
7
|
+
promises.push(tools.createFolderToDest('routes'))
|
|
8
|
+
promises.push(tools.createFolderToDest('libs'))
|
|
9
|
+
promises.push(tools.createFolderToDest('models'))
|
|
10
|
+
promises.push(tools.createFolderToDest('tasks'))
|
|
11
|
+
promises.push(tools.createFolderToDest('@types'))
|
|
12
|
+
|
|
13
|
+
promises.push(tools.copyFolderToDest('configs'))
|
|
14
|
+
promises.push(tools.copyFolderToDest('envs'))
|
|
15
|
+
promises.push(tools.syncFolder('.vscode'))
|
|
16
|
+
|
|
17
|
+
promises.push(tools.copyFileToDest('Dockerfile'))
|
|
18
|
+
promises.push(tools.copyFileToDest('tsconfig.json'))
|
|
19
|
+
|
|
20
|
+
projectPackageJson.scripts.start = 'plata-runtime + plata-api'
|
|
21
|
+
projectPackageJson.scripts.create = 'plata-runtime + plata-create'
|
|
22
|
+
//projectPackageJson.scripts.dev = "npx nodemon -e '.js,.mjs,.json,.ts' --exec npm run debug"
|
|
23
|
+
//projectPackageJson.scripts.debug = 'tsc --sourceMap=true -p . && plata'
|
|
24
|
+
projectPackageJson.scripts['gen:swagger'] = 'plata-runtime + plata-swagger-gen'
|
|
25
|
+
//projectPackageJson.scripts['plata:reload'] = `ts-node -P ./tsconfig.json ./node_modules/${projectPackageJson.plata_name}/bin/plata-reload.ts`
|
|
26
|
+
projectPackageJson.main = undefined
|
|
27
|
+
|
|
28
|
+
await Promise.all(promises)
|
|
29
|
+
|
|
30
|
+
return projectPackageJson
|
|
31
|
+
|
|
32
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { PlataFs } from "++/libs/fs"
|
|
2
|
+
|
|
3
|
+
export async function install(tools: PlataFs.CliTools, projectPackageJson: any) {
|
|
4
|
+
const promises: Promise<void>[] = []
|
|
5
|
+
|
|
6
|
+
promises.push(tools.createFolderToDest('clusters'))
|
|
7
|
+
promises.push(tools.createFolderToDest('libs'))
|
|
8
|
+
promises.push(tools.createFolderToDest('tasks'))
|
|
9
|
+
promises.push(tools.createFolderToDest('@types'))
|
|
10
|
+
promises.push(tools.createFolderToDest('bin'))
|
|
11
|
+
|
|
12
|
+
promises.push(tools.copyFolderToDest('configs'))
|
|
13
|
+
promises.push(tools.copyFolderToDest('envs'))
|
|
14
|
+
promises.push(tools.syncFolder('.vscode'))
|
|
15
|
+
|
|
16
|
+
promises.push(tools.syncFileToDest('postinstall.js')) // TODO :: compilar esse cara ou colocar como um bin na plata mesmo
|
|
17
|
+
promises.push(tools.syncFileToDest('postinstall-tools.mjs'))
|
|
18
|
+
promises.push(tools.syncFileToDest('tsconfig.json'))
|
|
19
|
+
|
|
20
|
+
projectPackageJson.type = "module"
|
|
21
|
+
projectPackageJson.scripts.create = 'plata-runtime + plata-create'
|
|
22
|
+
projectPackageJson.scripts.postinstall = "node postinstall.js"
|
|
23
|
+
|
|
24
|
+
projectPackageJson.main = undefined
|
|
25
|
+
|
|
26
|
+
await Promise.all(promises)
|
|
27
|
+
|
|
28
|
+
return projectPackageJson
|
|
29
|
+
|
|
30
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { PlataFs } from '../libs/fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
|
|
4
|
+
const main = async () => {
|
|
5
|
+
const plataDir = path.resolve('.')
|
|
6
|
+
|
|
7
|
+
if (path.basename(path.dirname(plataDir)) !== 'node_modules') return
|
|
8
|
+
|
|
9
|
+
const templatesDir = path.resolve(plataDir, 'templates', 'postinstall')
|
|
10
|
+
const projectDir = path.resolve(plataDir, '..', '..')
|
|
11
|
+
|
|
12
|
+
// read Plata and project package.json
|
|
13
|
+
let projectPackageJson: Dictionary<string | number | null | boolean | Object | undefined> = {}
|
|
14
|
+
let plataPackageJson: Dictionary<string | number | null | boolean | Object | undefined> = {}
|
|
15
|
+
|
|
16
|
+
{
|
|
17
|
+
const promises: Promise<void>[] = []
|
|
18
|
+
|
|
19
|
+
promises.push(Promise.resolve().then( () =>
|
|
20
|
+
PlataFs.fs.readFile(path.join(projectDir, 'package.json')).then(
|
|
21
|
+
b => projectPackageJson = JSON.parse(b.toString()),
|
|
22
|
+
err => {
|
|
23
|
+
console.error(err)
|
|
24
|
+
process.exit(1)
|
|
25
|
+
}
|
|
26
|
+
)
|
|
27
|
+
))
|
|
28
|
+
|
|
29
|
+
promises.push(Promise.resolve().then(() =>
|
|
30
|
+
PlataFs.fs.readFile(path.join(plataDir, 'package.json')).then(
|
|
31
|
+
b => plataPackageJson = JSON.parse(b.toString()),
|
|
32
|
+
err => {
|
|
33
|
+
console.error(err)
|
|
34
|
+
process.exit(1)
|
|
35
|
+
}
|
|
36
|
+
)
|
|
37
|
+
))
|
|
38
|
+
|
|
39
|
+
await Promise.all(promises)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (projectPackageJson.plata_no_setup !== undefined) {
|
|
43
|
+
return
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
projectPackageJson.plata_type = projectPackageJson.plata_type ?? 'api'
|
|
47
|
+
projectPackageJson.plata_name = plataPackageJson.name
|
|
48
|
+
projectPackageJson['my-scripts'] = projectPackageJson['my-scripts'] ?? new Object(null)
|
|
49
|
+
|
|
50
|
+
// https://developer.mozilla.org/en-US/docs/Glossary/Deep_copy
|
|
51
|
+
projectPackageJson.scripts = JSON.parse(JSON.stringify(projectPackageJson['my-scripts']))
|
|
52
|
+
|
|
53
|
+
const templateDir = path.join(templatesDir, `${projectPackageJson.plata_type}`)
|
|
54
|
+
|
|
55
|
+
const { install } = await import(`./post-install/${projectPackageJson.plata_type}`.replace(/\\/g, '/'))
|
|
56
|
+
|
|
57
|
+
projectPackageJson = await install(
|
|
58
|
+
new PlataFs.CliTools(templateDir, projectDir),
|
|
59
|
+
projectPackageJson
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
await PlataFs.fs.writeFile(path.join(projectDir, 'package.json'), JSON.stringify(projectPackageJson, null, 4))
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
main()
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { PlataFs } from '../libs/fs'
|
|
2
|
+
import path from 'node:path'
|
|
3
|
+
|
|
4
|
+
const main = async () => {
|
|
5
|
+
const plataDir = path.resolve('.')
|
|
6
|
+
|
|
7
|
+
// Copy assets folder
|
|
8
|
+
{
|
|
9
|
+
const srcAssetsFolder = path.join(plataDir, 'src', 'tests', 'assets')
|
|
10
|
+
const buildAssetsFolder = path.join(plataDir, '__BUILD__', 'tests', 'assets')
|
|
11
|
+
|
|
12
|
+
if (await PlataFs.access(buildAssetsFolder)) {
|
|
13
|
+
await PlataFs.fs.rm(
|
|
14
|
+
buildAssetsFolder,
|
|
15
|
+
{ force: true, recursive: true, maxRetries: 1, retryDelay: 1 }
|
|
16
|
+
)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
await PlataFs.fs.mkdir(buildAssetsFolder, { recursive: true })
|
|
20
|
+
|
|
21
|
+
const err = await PlataFs.cp(
|
|
22
|
+
srcAssetsFolder,
|
|
23
|
+
buildAssetsFolder,
|
|
24
|
+
{ force: true, recursive: true }
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
if (err !== null) {
|
|
28
|
+
console.log(err)
|
|
29
|
+
process.exit(1)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
main()
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
1
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { describe, it } from "node:test"
|
|
2
|
+
import assert from "node:assert"
|
|
3
|
+
import fs from "node:fs/promises"
|
|
4
|
+
import path from "node:path"
|
|
5
|
+
|
|
6
|
+
describe('Test status', () => {
|
|
7
|
+
it('Assert working', () => assert.strictEqual(1,1))
|
|
8
|
+
|
|
9
|
+
it('Assets working', async () => {
|
|
10
|
+
const t = await fs.readFile(path.join(__dirname, 'assets', 'config', 'test.txt'))
|
|
11
|
+
assert.equal(t.toString(), '1')
|
|
12
|
+
})
|
|
13
|
+
})
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { describe, it } from "node:test"
|
|
2
|
+
import assert from "node:assert"
|
|
3
|
+
import { PlataModels } from ".."
|
|
4
|
+
import { loadRuntimeLibs } from "../bin/runtime/_setupRuntime"
|
|
5
|
+
|
|
6
|
+
describe('PlataModels', () => {
|
|
7
|
+
const plataPromise = (async () => {
|
|
8
|
+
const g = global as any
|
|
9
|
+
|
|
10
|
+
g.Plata = await loadRuntimeLibs()
|
|
11
|
+
})()
|
|
12
|
+
|
|
13
|
+
const modelStruct = new PlataModels.ModelTemplate({
|
|
14
|
+
param: [ PlataModels.Required(), PlataModels.Int() ],
|
|
15
|
+
object: {
|
|
16
|
+
param: [ PlataModels.Required(), PlataModels.DateTime() ],
|
|
17
|
+
},
|
|
18
|
+
array: [ [ PlataModels.VarChar(30) ] ],
|
|
19
|
+
array_objects: [{
|
|
20
|
+
param: [ PlataModels.Optional(), PlataModels.Decimal(10,2) ]
|
|
21
|
+
}]
|
|
22
|
+
} as const, {
|
|
23
|
+
toValidationsOkay: (x): typeof modelValidations.type => {
|
|
24
|
+
return {
|
|
25
|
+
enum: 0,
|
|
26
|
+
stringDateTime: '2022-12-04T02:06:22',
|
|
27
|
+
dateTime: new Date(),
|
|
28
|
+
decimal: 99.99,
|
|
29
|
+
intMin: 5,
|
|
30
|
+
intMax: 5,
|
|
31
|
+
intRequired: 1,
|
|
32
|
+
smallDateTime: '2022-12-04',
|
|
33
|
+
telefone: '99-999999999',
|
|
34
|
+
telefoneDDI: '+055 99-999999999',
|
|
35
|
+
varCharMin: '1',
|
|
36
|
+
varChar: '1',
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
toValidationsError: (x): typeof modelValidations.type => {
|
|
40
|
+
return {
|
|
41
|
+
enum: 2,
|
|
42
|
+
stringDateTime: '',
|
|
43
|
+
dateTime: '',
|
|
44
|
+
decimal: 5.666666666666,
|
|
45
|
+
intMin: 1,
|
|
46
|
+
intMax: 6,
|
|
47
|
+
intRequired: null,
|
|
48
|
+
smallDateTime: '',
|
|
49
|
+
telefone: '',
|
|
50
|
+
telefoneDDI: '',
|
|
51
|
+
varCharMin: '',
|
|
52
|
+
varChar: '11',
|
|
53
|
+
} as any // Forced just for test
|
|
54
|
+
}
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
const modelValidations = new PlataModels.ModelTemplate({
|
|
58
|
+
enum: [ PlataModels.Enum([ 0,1 ] as const) ],
|
|
59
|
+
stringDateTime: [ PlataModels.StringDateTime() ],
|
|
60
|
+
dateTime: [ PlataModels.DateTime() ],
|
|
61
|
+
decimal: [ PlataModels.Decimal(2,2) ],
|
|
62
|
+
intMin: [ PlataModels.Int(5) ],
|
|
63
|
+
intMax: [ PlataModels.Int(undefined, 5) ],
|
|
64
|
+
intRequired: [ PlataModels.Required(), PlataModels.Int() ],
|
|
65
|
+
smallDateTime: [ PlataModels.SmallDateTime() ],
|
|
66
|
+
telefone: [ PlataModels.Telefone() ],
|
|
67
|
+
telefoneDDI: [ PlataModels.TelefoneDDI() ],
|
|
68
|
+
varCharMin: [ PlataModels.VarChar(1, 1) ],
|
|
69
|
+
varChar: [ PlataModels.VarChar(1) ],
|
|
70
|
+
} as const, {})
|
|
71
|
+
|
|
72
|
+
it('Struct Filter', () => {
|
|
73
|
+
const InitObj = {
|
|
74
|
+
array_objects: [{ param: '123' }]
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const FilteredObj = modelStruct.filter(InitObj)
|
|
78
|
+
|
|
79
|
+
assert.deepStrictEqual(FilteredObj, {
|
|
80
|
+
param: null,
|
|
81
|
+
object: {
|
|
82
|
+
param: null,
|
|
83
|
+
},
|
|
84
|
+
array: [],
|
|
85
|
+
array_objects: [{
|
|
86
|
+
param: '123'
|
|
87
|
+
}]
|
|
88
|
+
})
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
it('Validations (Okay)', async () => {
|
|
92
|
+
await plataPromise
|
|
93
|
+
const okayObj = modelStruct.convert({} as any, 'toValidationsOkay')
|
|
94
|
+
|
|
95
|
+
const { errors, value } = await modelValidations.validate(okayObj)
|
|
96
|
+
|
|
97
|
+
assert.strictEqual(errors, undefined)
|
|
98
|
+
assert.deepStrictEqual(value, okayObj)
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('Validations (Not Okay)', async () => {
|
|
102
|
+
await plataPromise
|
|
103
|
+
const notOkayObj = modelStruct.convert({} as any, 'toValidationsError')
|
|
104
|
+
|
|
105
|
+
const { errors, value } = await modelValidations.validate(notOkayObj)
|
|
106
|
+
|
|
107
|
+
assert.notStrictEqual(errors, undefined)
|
|
108
|
+
assert.strictEqual(
|
|
109
|
+
errors?.length,
|
|
110
|
+
Object.keys(modelValidations.template).length,
|
|
111
|
+
errors?.map(e => `${e.errorID} - ${e.msg}`).join('\n')
|
|
112
|
+
)
|
|
113
|
+
assert.deepStrictEqual(value, notOkayObj)
|
|
114
|
+
})
|
|
115
|
+
})
|
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
//@ts-nocheck
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const onStart = async () => {
|
|
2
|
+
export async function main() {
|
|
5
3
|
|
|
6
4
|
}
|
|
7
5
|
|
|
8
|
-
|
|
6
|
+
export function exit(worker: Cluster.Worker, code: number, signal: string) {
|
|
9
7
|
|
|
10
8
|
}
|
|
11
9
|
|
|
12
10
|
export default {
|
|
13
|
-
name:
|
|
14
|
-
env:
|
|
15
|
-
onStart:
|
|
16
|
-
onExit:
|
|
17
|
-
}
|
|
11
|
+
name: '$__name__$',
|
|
12
|
+
env: Plata.config.ENV ?? 'debug',
|
|
13
|
+
onStart: main,
|
|
14
|
+
onExit: exit,
|
|
15
|
+
} satisfies Cluster.WorkerConfig
|
|
@@ -1,24 +1,14 @@
|
|
|
1
1
|
//@ts-nocheck
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
private readonly config: typeof ç__Name__çConfig.default
|
|
6
|
-
|
|
7
|
-
constructor(config: typeof ç__Name__çConfig.default) {
|
|
8
|
-
this.config = config
|
|
2
|
+
export class $__Name__$ {
|
|
3
|
+
constructor() {
|
|
4
|
+
|
|
9
5
|
}
|
|
10
6
|
}
|
|
11
7
|
|
|
12
|
-
export function
|
|
13
|
-
return new
|
|
8
|
+
export function new$__Name__$(): $__Name__$ {
|
|
9
|
+
return new $__Name__$()
|
|
14
10
|
}
|
|
15
11
|
|
|
16
|
-
export function
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
if (p._plata._libç__Name__ç === undefined) {
|
|
20
|
-
p._plata._libç__Name__ç = newç__Name__ç(ç__Name__çConfig.default)
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return p._plata._libç__Name__ç
|
|
12
|
+
export function get$__Name__$(): $__Name__$ {
|
|
13
|
+
return Plata.CacheClass(new$__Name__$)
|
|
24
14
|
}
|
|
@@ -1,26 +1,15 @@
|
|
|
1
1
|
//@ts-nocheck
|
|
2
|
-
import { PlataModels } from
|
|
2
|
+
import { PlataModels } from '$__PlataName__$'
|
|
3
3
|
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
const $__Name__$ = new PlataModels.ModelTemplate({
|
|
5
|
+
param: [ PlataModels.Required(), PlataModels.Int() ],
|
|
6
|
+
object: {
|
|
7
|
+
param: [ PlataModels.Required(), PlataModels.DateTime() ],
|
|
8
8
|
},
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
array: [ [ PlataModels.VarChar(30) ] ],
|
|
10
|
+
array_objects: [{
|
|
11
|
+
param: [ PlataModels.Optional(), PlataModels.Decimal(10,2) ]
|
|
11
12
|
}]
|
|
12
|
-
})
|
|
13
|
+
} as const, {})
|
|
13
14
|
|
|
14
|
-
|
|
15
|
-
if (objeto.campo > 30) {
|
|
16
|
-
return {
|
|
17
|
-
errorID: 'BM0001',
|
|
18
|
-
msg: 'O campo não pode ser maior que 30',
|
|
19
|
-
error: `valor do campo: ${objeto.campo}`
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return null
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
export const ç__Name__çModel = ç__Name__ç
|
|
15
|
+
export const $__Name__$Model = $__Name__$
|