pwi-plata-type 0.4.166 → 0.4.167
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/README.md +2 -2
- package/__BUILD__/libs/sql.js +4 -1
- package/__BUILD__/libs/sql.js.map +1 -1
- package/package.json +53 -53
- package/src/@types/build.d.ts +26 -26
- package/src/@types/cluster.actions.d.ts +27 -27
- package/src/@types/cluster.d.ts +55 -55
- package/src/@types/create-cli.d.ts +8 -8
- package/src/@types/exported.d.ts +31 -31
- package/src/@types/fs.d.ts +6 -6
- package/src/@types/global.d.ts +113 -113
- package/src/@types/knex.d.ts +13 -13
- package/src/@types/model.d.ts +143 -143
- package/src/@types/router.d.ts +107 -107
- package/src/@types/sql.d.ts +75 -75
- package/src/@types/swagger.d.ts +22 -22
- package/src/bin/extras/create-api-cli.ts +218 -218
- package/src/bin/extras/create-cluster-cli.ts +160 -160
- package/src/bin/extras/create-lib-cli.ts +84 -84
- package/src/bin/extras/plata-create-tools.ts +97 -97
- package/src/bin/plata-api.ts +189 -189
- package/src/bin/plata-build.ts +17 -17
- package/src/bin/plata-cluster.ts +42 -42
- package/src/bin/plata-cmd.ts +18 -18
- package/src/bin/plata-create.ts +49 -49
- package/src/bin/plata-runtime.ts +131 -131
- package/src/bin/plata-swagger.ts +99 -99
- package/src/bin/runtime/_setupRuntime.ts +48 -48
- package/src/bin/runtime/_types.d.ts +7 -7
- package/src/bin/runtime/class.ts +20 -20
- package/src/bin/runtime/dirs.ts +25 -25
- package/src/bin/runtime/effect.ts +60 -60
- package/src/bin/runtime/fs.ts +10 -10
- package/src/bin/runtime/promises.ts +18 -18
- package/src/bin/runtime/require.ts +113 -113
- package/src/bin/runtime/sql.ts +12 -12
- package/src/bin/runtime/switch.ts +60 -60
- package/src/index.ts +19 -19
- package/src/libs/axios.ts +51 -51
- package/src/libs/build.ts +162 -162
- package/src/libs/cluster/cluster-controller.ts +93 -93
- package/src/libs/cluster.ts +330 -330
- package/src/libs/custom.ts +15 -15
- package/src/libs/env.ts +10 -10
- package/src/libs/fs.ts +399 -399
- package/src/libs/hot-reload.ts +63 -63
- package/src/libs/models/index.ts +4 -4
- package/src/libs/models/model-template-combine.ts +93 -93
- package/src/libs/models/model-template-env.ts +58 -58
- package/src/libs/models/model-template-union.ts +83 -83
- package/src/libs/models/model-template.ts +107 -107
- package/src/libs/models/model-validations.ts +443 -443
- package/src/libs/models/tools.ts +194 -194
- package/src/libs/router.ts +185 -185
- package/src/libs/sql.ts +792 -788
- package/src/libs/swagger.ts +81 -81
- package/src/libs/tools.ts +240 -240
- package/src/scripts/post-install/api.ts +32 -32
- package/src/scripts/post-install/cluster.ts +28 -28
- package/src/scripts/post-install/lib.ts +25 -25
- package/src/scripts/post-install.ts +64 -64
- package/src/scripts/pre-test.ts +33 -33
- package/src/tests/assets/sql/example.sql +76 -76
- package/src/tests/config.test.ts +12 -12
- package/src/tests/effect.test.ts +62 -62
- package/src/tests/model.env.test.ts +66 -66
- package/src/tests/model.test.ts +141 -141
- package/src/tests/model.union.test.ts +160 -160
- package/src/tests/router.test.ts +44 -44
- package/src/tests/sql.test.ts +502 -502
- package/src/tests/switch.test.ts +76 -76
- package/templates/create-cli/class.ts +13 -13
- package/templates/create-cli/cluster.ts +14 -14
- package/templates/create-cli/cmd.ts +3 -3
- package/templates/create-cli/config.ts +2 -2
- package/templates/create-cli/lib.ts +3 -3
- package/templates/create-cli/model.ts +14 -14
- package/templates/create-cli/route.ts +8 -8
- package/templates/create-cli/task.ts +13 -13
- package/templates/postinstall/api/.vscode/launch.json +20 -20
- package/templates/postinstall/api/Dockerfile +24 -24
- package/templates/postinstall/api/configs/env.ts +2 -2
- package/templates/postinstall/api/envs/debug.env +15 -15
- package/templates/postinstall/api/envs/homolog.env +15 -15
- package/templates/postinstall/api/envs/prod.env +15 -15
- package/templates/postinstall/api/tsconfig.json +24 -24
- package/templates/postinstall/cluster/.vscode/launch.json +20 -20
- package/templates/postinstall/cluster/Dockerfile +24 -24
- package/templates/postinstall/cluster/configs/env.ts +2 -2
- package/templates/postinstall/cluster/envs/debug.env +10 -10
- package/templates/postinstall/cluster/envs/homolog.env +10 -10
- package/templates/postinstall/cluster/envs/prod.env +10 -10
- package/templates/postinstall/cluster/tsconfig.json +24 -24
- package/templates/postinstall/lib/postinstall-tools.mjs +111 -111
- package/templates/postinstall/lib/postinstall.js +33 -33
- package/templates/postinstall/lib/tsconfig.json +22 -22
- package/templates/swagger-cli/header.json +21 -21
- package/templates/swagger-cli/route/body.json +14 -14
- package/templates/swagger-cli/route/header.json +18 -18
- package/templates/swagger-cli/route/response.json +53 -53
- package/tsconfig.json +28 -28
package/src/libs/swagger.ts
CHANGED
|
@@ -1,82 +1,82 @@
|
|
|
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 = Object.create(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(/\\/g, '/').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] = Object.create(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
|
-
...body.extras,
|
|
47
|
-
type: body.type ?? "object",
|
|
48
|
-
}
|
|
49
|
-
} as any
|
|
50
|
-
|
|
51
|
-
if (swaggerRouteBody.schema.type === 'array') {
|
|
52
|
-
swaggerRouteBody.schema.items = {
|
|
53
|
-
example: body.example
|
|
54
|
-
}
|
|
55
|
-
} else {
|
|
56
|
-
swaggerRouteBody.schema.properties = body.example
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
obj.paths[httpPath][method].parameters.push(swaggerRouteBody)
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return obj
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
export const swagger: Router.RouterBuilder = async (router) => {
|
|
69
|
-
router.swaggerHide = true
|
|
70
|
-
|
|
71
|
-
if (Plata.config._PLATA_WORKER_NAME === '__PLATA_EXPRESS__') {
|
|
72
|
-
router.use(
|
|
73
|
-
swaggerUi.serve as any,
|
|
74
|
-
swaggerUi.setup(
|
|
75
|
-
await Internals.loadSwaggerConfig(),
|
|
76
|
-
{}
|
|
77
|
-
) as any
|
|
78
|
-
)
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return router
|
|
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 = Object.create(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(/\\/g, '/').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] = Object.create(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
|
+
...body.extras,
|
|
47
|
+
type: body.type ?? "object",
|
|
48
|
+
}
|
|
49
|
+
} as any
|
|
50
|
+
|
|
51
|
+
if (swaggerRouteBody.schema.type === 'array') {
|
|
52
|
+
swaggerRouteBody.schema.items = {
|
|
53
|
+
example: body.example
|
|
54
|
+
}
|
|
55
|
+
} else {
|
|
56
|
+
swaggerRouteBody.schema.properties = body.example
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
obj.paths[httpPath][method].parameters.push(swaggerRouteBody)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return obj
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export const swagger: Router.RouterBuilder = async (router) => {
|
|
69
|
+
router.swaggerHide = true
|
|
70
|
+
|
|
71
|
+
if (Plata.config._PLATA_WORKER_NAME === '__PLATA_EXPRESS__') {
|
|
72
|
+
router.use(
|
|
73
|
+
swaggerUi.serve as any,
|
|
74
|
+
swaggerUi.setup(
|
|
75
|
+
await Internals.loadSwaggerConfig(),
|
|
76
|
+
{}
|
|
77
|
+
) as any
|
|
78
|
+
)
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return router
|
|
82
82
|
}
|
package/src/libs/tools.ts
CHANGED
|
@@ -1,241 +1,241 @@
|
|
|
1
|
-
import path from 'node:path'
|
|
2
|
-
import fs from 'node:fs'
|
|
3
|
-
import readline from 'node:readline'
|
|
4
|
-
import * as tGlob from 'glob'
|
|
5
|
-
// TODO matar esse aquivo e usar o PlataFS
|
|
6
|
-
|
|
7
|
-
const glob = (tGlob as any).default as typeof tGlob
|
|
8
|
-
|
|
9
|
-
export interface PlataError {
|
|
10
|
-
errorID: string,
|
|
11
|
-
msg: string,
|
|
12
|
-
error?: any
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export type PlataResultado<T> = (T & { errorID?: undefined }) | PlataError
|
|
16
|
-
|
|
17
|
-
export namespace PlataDirs {
|
|
18
|
-
export function getProjectDir(): string {
|
|
19
|
-
return path.resolve('.')
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
export function getProjectDirTasks(): string {
|
|
23
|
-
return path.resolve('.', 'tasks')
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function getProjectDirSwagger(): string {
|
|
27
|
-
return path.resolve('.', 'swagger')
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export function getProjectDirSwaggerRotas(): string {
|
|
31
|
-
return path.resolve('.', 'swagger', 'rotas')
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export function getProjectDirClusters(): string {
|
|
35
|
-
return path.resolve('.', 'clusters')
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
export function getProjectDirConfig(): string {
|
|
39
|
-
return path.resolve('.', 'configs')
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
export function getProjectConfigFileDir(file: string): string {
|
|
43
|
-
return path.resolve('.', 'configs', file).replace(/\\/g, '/')
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function getProjectDirEnvs(): string {
|
|
47
|
-
return path.resolve('.', 'envs')
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function getProjectEnvFileDir(file: string): string {
|
|
51
|
-
return path.resolve('.', 'envs', file)
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function getProjectRoutesDir(): string {
|
|
55
|
-
return path.resolve('.', 'routes')
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function getPlataTempletesDir(): string {
|
|
59
|
-
return path.join(Plata.PlataDir, 'templates')
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export function getPlataTempleteDir(group:string, ...template: string[]) {
|
|
63
|
-
return path.join(getPlataTempletesDir(), group, ...template)
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function getPlataBinExtraFile(file: string): string {
|
|
67
|
-
return `${path.join(Plata.ProjectJson, 'bin', 'extras', file)}`.replace(/\\/g, '/')
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
export namespace PlataFiles {
|
|
73
|
-
export type readLineCallback = (line: string) => Promise<void>
|
|
74
|
-
|
|
75
|
-
export type readLineCallbackSync = (line: string) => string
|
|
76
|
-
|
|
77
|
-
export function readFileAsync(file: string, callback: readLineCallback): Promise<Error | null> {
|
|
78
|
-
return new Promise(resolve => {
|
|
79
|
-
try {
|
|
80
|
-
const promises: any = []
|
|
81
|
-
|
|
82
|
-
const stream = readline.createInterface({
|
|
83
|
-
input: fs.createReadStream(file),
|
|
84
|
-
crlfDelay: Infinity
|
|
85
|
-
})
|
|
86
|
-
|
|
87
|
-
stream.on('close', () => {
|
|
88
|
-
Promise.all(promises).then(
|
|
89
|
-
() => resolve(null),
|
|
90
|
-
(err) => resolve(err),
|
|
91
|
-
)
|
|
92
|
-
})
|
|
93
|
-
|
|
94
|
-
stream.on('line', (line) => {
|
|
95
|
-
promises.push(callback(line))
|
|
96
|
-
})
|
|
97
|
-
} catch (e) {
|
|
98
|
-
resolve(e)
|
|
99
|
-
}
|
|
100
|
-
})
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export function readFileSync(file: string, callback: readLineCallbackSync): Promise<string | { err: any }> {
|
|
104
|
-
return new Promise(resolve => {
|
|
105
|
-
try {
|
|
106
|
-
const content: string[] = []
|
|
107
|
-
const stream = readline.createInterface({
|
|
108
|
-
input: fs.createReadStream(file),
|
|
109
|
-
crlfDelay: Infinity
|
|
110
|
-
})
|
|
111
|
-
|
|
112
|
-
stream.on('close', () => {
|
|
113
|
-
resolve(content.join('\n'))
|
|
114
|
-
})
|
|
115
|
-
|
|
116
|
-
stream.on('line', (line) => {
|
|
117
|
-
content.push(callback(line))
|
|
118
|
-
})
|
|
119
|
-
|
|
120
|
-
} catch(e) {
|
|
121
|
-
resolve({ err: e })
|
|
122
|
-
}
|
|
123
|
-
})
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
export async function findFile(folders: string[], file: string): Promise<string | null> {
|
|
127
|
-
for (const folder of folders) {
|
|
128
|
-
const f = glob.sync(`${folder.replace(/\\/g, '/')}/**/${file.toLowerCase()}*`)[0]
|
|
129
|
-
|
|
130
|
-
if (f !== undefined) {
|
|
131
|
-
return f
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
return null
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export async function findFiles(folders: string[], file: string): Promise<string[]> {
|
|
139
|
-
const files: string[] = []
|
|
140
|
-
|
|
141
|
-
for (const folder of folders) {
|
|
142
|
-
files.push(...glob.sync(`${folder.replace(/\\/g, '/')}/**/${file}`))
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
return files
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
export async function createFileIfNotExists(filePath: string, content: string): Promise<PlataResultado<string>> {
|
|
149
|
-
const p = path.resolve(filePath)
|
|
150
|
-
|
|
151
|
-
if (!fs.existsSync(p)) {
|
|
152
|
-
return createFile(filePath, content)
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return p
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export async function createFile(filePath: string, content: string): Promise<PlataResultado<string>> {
|
|
159
|
-
try {
|
|
160
|
-
const p = path.resolve(filePath)
|
|
161
|
-
|
|
162
|
-
fs.mkdirSync(path.dirname(p), { recursive: true })
|
|
163
|
-
fs.writeFileSync(p, content)
|
|
164
|
-
|
|
165
|
-
return p
|
|
166
|
-
} catch (e) {
|
|
167
|
-
return {
|
|
168
|
-
errorID: 'PLTPF0001',
|
|
169
|
-
msg: `Erro ao criar ao criar o arquivo ${filePath}`,
|
|
170
|
-
error: e
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
export function readJsonSync(filePath: string): any {
|
|
177
|
-
return JSON.parse(fs.readFileSync(filePath).toString())
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
export namespace PlataRequire {
|
|
182
|
-
export type Required<Type> = PlataResultado<RequiredInterface<Type>>
|
|
183
|
-
export interface RequiredInterface<Type> {
|
|
184
|
-
filePath: string,
|
|
185
|
-
name: string,
|
|
186
|
-
exports: Type
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
export async function requireAsync<Type>(file: string): Promise<Required<Type>> {
|
|
190
|
-
const p = path.resolve(file)
|
|
191
|
-
|
|
192
|
-
const e = await import(p.replace(/\\/g, '/')).catch(e => { return {_plataError: e } })
|
|
193
|
-
|
|
194
|
-
if (e._plataError !== undefined) {
|
|
195
|
-
return {
|
|
196
|
-
errorID: 'PLTRF0001',
|
|
197
|
-
msg: `Erro ao importar o arquivo ${file}`,
|
|
198
|
-
error: e._plataError
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
return {
|
|
203
|
-
filePath: p,
|
|
204
|
-
name: path.basename(p).replace('.ts', '').replace('.js', ''),
|
|
205
|
-
exports: e.default as Type
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
export async function requireFolderAsync<Type>(folder: string): Promise<PlataResultado<Required<Type>[]>> {
|
|
210
|
-
const promises: Promise<Required<Type>>[] = []
|
|
211
|
-
const p = path.resolve(folder)
|
|
212
|
-
|
|
213
|
-
glob.sync(`${p.replace(/\\/g, '/')}/**/*.+(js|ts)`)
|
|
214
|
-
.filter(file => !file.endsWith('.d.ts'))
|
|
215
|
-
.forEach(file => {
|
|
216
|
-
file = file.replace(/\.ts|\.js$/, '')
|
|
217
|
-
|
|
218
|
-
promises.push(requireAsync<Type>(file))
|
|
219
|
-
})
|
|
220
|
-
|
|
221
|
-
return Promise.all(promises).catch(
|
|
222
|
-
e => {
|
|
223
|
-
return {
|
|
224
|
-
errorID: 'PLTRF0002',
|
|
225
|
-
msg: `Erro ao importar a pasta ${folder}`,
|
|
226
|
-
error: e
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
)
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
export namespace PlataExtra {
|
|
234
|
-
export function sleep(ms: number) {
|
|
235
|
-
return new Promise(resolve => setTimeout(resolve, ms))
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
export function timeout(ms: number, error: PlataError): Promise<PlataError> {
|
|
239
|
-
return new Promise((_, reject) => setTimeout(() => reject(error), ms))
|
|
240
|
-
}
|
|
1
|
+
import path from 'node:path'
|
|
2
|
+
import fs from 'node:fs'
|
|
3
|
+
import readline from 'node:readline'
|
|
4
|
+
import * as tGlob from 'glob'
|
|
5
|
+
// TODO matar esse aquivo e usar o PlataFS
|
|
6
|
+
|
|
7
|
+
const glob = (tGlob as any).default as typeof tGlob
|
|
8
|
+
|
|
9
|
+
export interface PlataError {
|
|
10
|
+
errorID: string,
|
|
11
|
+
msg: string,
|
|
12
|
+
error?: any
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type PlataResultado<T> = (T & { errorID?: undefined }) | PlataError
|
|
16
|
+
|
|
17
|
+
export namespace PlataDirs {
|
|
18
|
+
export function getProjectDir(): string {
|
|
19
|
+
return path.resolve('.')
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function getProjectDirTasks(): string {
|
|
23
|
+
return path.resolve('.', 'tasks')
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function getProjectDirSwagger(): string {
|
|
27
|
+
return path.resolve('.', 'swagger')
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function getProjectDirSwaggerRotas(): string {
|
|
31
|
+
return path.resolve('.', 'swagger', 'rotas')
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function getProjectDirClusters(): string {
|
|
35
|
+
return path.resolve('.', 'clusters')
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function getProjectDirConfig(): string {
|
|
39
|
+
return path.resolve('.', 'configs')
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function getProjectConfigFileDir(file: string): string {
|
|
43
|
+
return path.resolve('.', 'configs', file).replace(/\\/g, '/')
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function getProjectDirEnvs(): string {
|
|
47
|
+
return path.resolve('.', 'envs')
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export function getProjectEnvFileDir(file: string): string {
|
|
51
|
+
return path.resolve('.', 'envs', file)
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function getProjectRoutesDir(): string {
|
|
55
|
+
return path.resolve('.', 'routes')
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export function getPlataTempletesDir(): string {
|
|
59
|
+
return path.join(Plata.PlataDir, 'templates')
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export function getPlataTempleteDir(group:string, ...template: string[]) {
|
|
63
|
+
return path.join(getPlataTempletesDir(), group, ...template)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function getPlataBinExtraFile(file: string): string {
|
|
67
|
+
return `${path.join(Plata.ProjectJson, 'bin', 'extras', file)}`.replace(/\\/g, '/')
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export namespace PlataFiles {
|
|
73
|
+
export type readLineCallback = (line: string) => Promise<void>
|
|
74
|
+
|
|
75
|
+
export type readLineCallbackSync = (line: string) => string
|
|
76
|
+
|
|
77
|
+
export function readFileAsync(file: string, callback: readLineCallback): Promise<Error | null> {
|
|
78
|
+
return new Promise(resolve => {
|
|
79
|
+
try {
|
|
80
|
+
const promises: any = []
|
|
81
|
+
|
|
82
|
+
const stream = readline.createInterface({
|
|
83
|
+
input: fs.createReadStream(file),
|
|
84
|
+
crlfDelay: Infinity
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
stream.on('close', () => {
|
|
88
|
+
Promise.all(promises).then(
|
|
89
|
+
() => resolve(null),
|
|
90
|
+
(err) => resolve(err),
|
|
91
|
+
)
|
|
92
|
+
})
|
|
93
|
+
|
|
94
|
+
stream.on('line', (line) => {
|
|
95
|
+
promises.push(callback(line))
|
|
96
|
+
})
|
|
97
|
+
} catch (e) {
|
|
98
|
+
resolve(e)
|
|
99
|
+
}
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
export function readFileSync(file: string, callback: readLineCallbackSync): Promise<string | { err: any }> {
|
|
104
|
+
return new Promise(resolve => {
|
|
105
|
+
try {
|
|
106
|
+
const content: string[] = []
|
|
107
|
+
const stream = readline.createInterface({
|
|
108
|
+
input: fs.createReadStream(file),
|
|
109
|
+
crlfDelay: Infinity
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
stream.on('close', () => {
|
|
113
|
+
resolve(content.join('\n'))
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
stream.on('line', (line) => {
|
|
117
|
+
content.push(callback(line))
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
} catch(e) {
|
|
121
|
+
resolve({ err: e })
|
|
122
|
+
}
|
|
123
|
+
})
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export async function findFile(folders: string[], file: string): Promise<string | null> {
|
|
127
|
+
for (const folder of folders) {
|
|
128
|
+
const f = glob.sync(`${folder.replace(/\\/g, '/')}/**/${file.toLowerCase()}*`)[0]
|
|
129
|
+
|
|
130
|
+
if (f !== undefined) {
|
|
131
|
+
return f
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return null
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export async function findFiles(folders: string[], file: string): Promise<string[]> {
|
|
139
|
+
const files: string[] = []
|
|
140
|
+
|
|
141
|
+
for (const folder of folders) {
|
|
142
|
+
files.push(...glob.sync(`${folder.replace(/\\/g, '/')}/**/${file}`))
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return files
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export async function createFileIfNotExists(filePath: string, content: string): Promise<PlataResultado<string>> {
|
|
149
|
+
const p = path.resolve(filePath)
|
|
150
|
+
|
|
151
|
+
if (!fs.existsSync(p)) {
|
|
152
|
+
return createFile(filePath, content)
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return p
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export async function createFile(filePath: string, content: string): Promise<PlataResultado<string>> {
|
|
159
|
+
try {
|
|
160
|
+
const p = path.resolve(filePath)
|
|
161
|
+
|
|
162
|
+
fs.mkdirSync(path.dirname(p), { recursive: true })
|
|
163
|
+
fs.writeFileSync(p, content)
|
|
164
|
+
|
|
165
|
+
return p
|
|
166
|
+
} catch (e) {
|
|
167
|
+
return {
|
|
168
|
+
errorID: 'PLTPF0001',
|
|
169
|
+
msg: `Erro ao criar ao criar o arquivo ${filePath}`,
|
|
170
|
+
error: e
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export function readJsonSync(filePath: string): any {
|
|
177
|
+
return JSON.parse(fs.readFileSync(filePath).toString())
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
export namespace PlataRequire {
|
|
182
|
+
export type Required<Type> = PlataResultado<RequiredInterface<Type>>
|
|
183
|
+
export interface RequiredInterface<Type> {
|
|
184
|
+
filePath: string,
|
|
185
|
+
name: string,
|
|
186
|
+
exports: Type
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export async function requireAsync<Type>(file: string): Promise<Required<Type>> {
|
|
190
|
+
const p = path.resolve(file)
|
|
191
|
+
|
|
192
|
+
const e = await import(p.replace(/\\/g, '/')).catch(e => { return {_plataError: e } })
|
|
193
|
+
|
|
194
|
+
if (e._plataError !== undefined) {
|
|
195
|
+
return {
|
|
196
|
+
errorID: 'PLTRF0001',
|
|
197
|
+
msg: `Erro ao importar o arquivo ${file}`,
|
|
198
|
+
error: e._plataError
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
return {
|
|
203
|
+
filePath: p,
|
|
204
|
+
name: path.basename(p).replace('.ts', '').replace('.js', ''),
|
|
205
|
+
exports: e.default as Type
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export async function requireFolderAsync<Type>(folder: string): Promise<PlataResultado<Required<Type>[]>> {
|
|
210
|
+
const promises: Promise<Required<Type>>[] = []
|
|
211
|
+
const p = path.resolve(folder)
|
|
212
|
+
|
|
213
|
+
glob.sync(`${p.replace(/\\/g, '/')}/**/*.+(js|ts)`)
|
|
214
|
+
.filter(file => !file.endsWith('.d.ts'))
|
|
215
|
+
.forEach(file => {
|
|
216
|
+
file = file.replace(/\.ts|\.js$/, '')
|
|
217
|
+
|
|
218
|
+
promises.push(requireAsync<Type>(file))
|
|
219
|
+
})
|
|
220
|
+
|
|
221
|
+
return Promise.all(promises).catch(
|
|
222
|
+
e => {
|
|
223
|
+
return {
|
|
224
|
+
errorID: 'PLTRF0002',
|
|
225
|
+
msg: `Erro ao importar a pasta ${folder}`,
|
|
226
|
+
error: e
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
)
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export namespace PlataExtra {
|
|
234
|
+
export function sleep(ms: number) {
|
|
235
|
+
return new Promise(resolve => setTimeout(resolve, ms))
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
export function timeout(ms: number, error: PlataError): Promise<PlataError> {
|
|
239
|
+
return new Promise((_, reject) => setTimeout(() => reject(error), ms))
|
|
240
|
+
}
|
|
241
241
|
}
|