pwi-plata-type 0.3.18 → 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.
Files changed (117) hide show
  1. package/__BUILD__/bin/extras/create-api-cli.js +96 -0
  2. package/__BUILD__/bin/extras/create-api-cli.js.map +1 -0
  3. package/__BUILD__/bin/extras/create-lib-cli.js +62 -0
  4. package/__BUILD__/bin/extras/create-lib-cli.js.map +1 -0
  5. package/__BUILD__/bin/extras/plata-create-tools.js +94 -0
  6. package/__BUILD__/bin/extras/plata-create-tools.js.map +1 -0
  7. package/__BUILD__/bin/plata-api.js +138 -0
  8. package/__BUILD__/bin/plata-api.js.map +1 -0
  9. package/__BUILD__/bin/plata-create.js +24 -0
  10. package/__BUILD__/bin/plata-create.js.map +1 -0
  11. package/__BUILD__/bin/plata-runtime.js +156 -0
  12. package/__BUILD__/bin/plata-runtime.js.map +1 -0
  13. package/__BUILD__/bin/plata-swagger.js +82 -0
  14. package/__BUILD__/bin/plata-swagger.js.map +1 -0
  15. package/__BUILD__/bin/runtime/_setupRuntime.js +43 -0
  16. package/__BUILD__/bin/runtime/_setupRuntime.js.map +1 -0
  17. package/__BUILD__/bin/runtime/class.js +18 -0
  18. package/__BUILD__/bin/runtime/class.js.map +1 -0
  19. package/__BUILD__/bin/runtime/dirs.js +16 -0
  20. package/__BUILD__/bin/runtime/dirs.js.map +1 -0
  21. package/__BUILD__/bin/runtime/promises.js +15 -0
  22. package/__BUILD__/bin/runtime/promises.js.map +1 -0
  23. package/__BUILD__/bin/runtime/require.js +88 -0
  24. package/__BUILD__/bin/runtime/require.js.map +1 -0
  25. package/__BUILD__/index.js +42 -0
  26. package/__BUILD__/index.js.map +1 -0
  27. package/__BUILD__/libs/axios.js +42 -0
  28. package/__BUILD__/libs/axios.js.map +1 -0
  29. package/__BUILD__/libs/cluster.js +160 -0
  30. package/__BUILD__/libs/cluster.js.map +1 -0
  31. package/__BUILD__/libs/fs.js +206 -0
  32. package/__BUILD__/libs/fs.js.map +1 -0
  33. package/__BUILD__/libs/model-validations.js +356 -0
  34. package/__BUILD__/libs/model-validations.js.map +1 -0
  35. package/__BUILD__/libs/model.js +185 -0
  36. package/__BUILD__/libs/model.js.map +1 -0
  37. package/__BUILD__/libs/router.js +127 -0
  38. package/__BUILD__/libs/router.js.map +1 -0
  39. package/__BUILD__/libs/swagger.js +64 -0
  40. package/__BUILD__/libs/swagger.js.map +1 -0
  41. package/__BUILD__/libs/tasks.js +134 -0
  42. package/__BUILD__/libs/tasks.js.map +1 -0
  43. package/__BUILD__/libs/tools.js +231 -0
  44. package/__BUILD__/libs/tools.js.map +1 -0
  45. package/__BUILD__/scripts/post-install/api.js +25 -0
  46. package/__BUILD__/scripts/post-install/api.js.map +1 -0
  47. package/__BUILD__/scripts/post-install/lib.js +25 -0
  48. package/__BUILD__/scripts/post-install/lib.js.map +1 -0
  49. package/__BUILD__/scripts/post-install.js +65 -0
  50. package/__BUILD__/scripts/post-install.js.map +1 -0
  51. package/__BUILD__/scripts/pre-test.js +25 -0
  52. package/__BUILD__/scripts/pre-test.js.map +1 -0
  53. package/package.json +18 -16
  54. package/src/@types/cluster.actions.d.ts +7 -0
  55. package/src/@types/cluster.d.ts +33 -0
  56. package/src/@types/create-cli.d.ts +9 -0
  57. package/src/@types/exported.d.ts +24 -0
  58. package/src/@types/global.d.ts +69 -0
  59. package/src/@types/model.d.ts +69 -0
  60. package/src/@types/router.d.ts +75 -0
  61. package/src/@types/swagger.d.ts +23 -0
  62. package/{bin/extras/plata-create-api.ts → src/bin/extras/create-api-cli.ts} +5 -6
  63. package/{bin/extras/plata-create-lib.ts → src/bin/extras/create-lib-cli.ts} +3 -4
  64. package/{bin → src/bin}/extras/plata-create-tools.ts +4 -4
  65. package/src/bin/plata-api.ts +172 -0
  66. package/src/bin/plata-create.ts +21 -0
  67. package/src/bin/plata-runtime.ts +188 -0
  68. package/src/bin/plata-swagger.ts +100 -0
  69. package/src/bin/runtime/_setupRuntime.ts +49 -0
  70. package/src/bin/runtime/_types.d.ts +4 -0
  71. package/src/bin/runtime/class.ts +21 -0
  72. package/src/bin/runtime/dirs.ts +26 -0
  73. package/src/bin/runtime/promises.ts +15 -0
  74. package/src/bin/runtime/require.ts +104 -0
  75. package/{index.ts → src/index.ts} +2 -4
  76. package/{libs → src/libs}/axios.ts +0 -0
  77. package/src/libs/cluster.ts +189 -0
  78. package/src/libs/fs.ts +382 -0
  79. package/{libs/model.ts → src/libs/model-validations.ts} +28 -262
  80. package/src/libs/model.ts +233 -0
  81. package/src/libs/router.ts +147 -0
  82. package/src/libs/swagger.ts +81 -0
  83. package/{libs → src/libs}/tasks.ts +0 -0
  84. package/{libs → src/libs}/tools.ts +3 -18
  85. package/src/scripts/post-install/api.ts +32 -0
  86. package/src/scripts/post-install/lib.ts +30 -0
  87. package/src/scripts/post-install.ts +65 -0
  88. package/src/scripts/pre-test.ts +34 -0
  89. package/src/tests/assets/config/test.txt +1 -0
  90. package/src/tests/config.test.ts +13 -0
  91. package/src/tests/model.test.ts +115 -0
  92. package/templates/create-cli/cluster.ts +7 -9
  93. package/templates/create-cli/config.ts +2 -8
  94. package/templates/create-cli/lib.ts +7 -17
  95. package/templates/create-cli/model.ts +10 -21
  96. package/templates/create-cli/route.ts +1 -3
  97. package/templates/postinstall/api/configs/env.ts +2 -8
  98. package/templates/postinstall/api/tsconfig.json +20 -37
  99. package/templates/postinstall/lib/postinstall-tools.mjs +1 -1
  100. package/templates/postinstall/lib/postinstall.js +1 -0
  101. package/templates/postinstall/lib/tsconfig.json +21 -38
  102. package/tsconfig.json +25 -37
  103. package/Dockerfile +0 -13
  104. package/bin/extras/plata-build-tool.ts +0 -151
  105. package/bin/plata-build.ts +0 -144
  106. package/bin/plata-create.ts +0 -28
  107. package/bin/plata-reload.ts +0 -13
  108. package/bin/plata-swagger-gen.ts +0 -183
  109. package/bin/plata.ts +0 -122
  110. package/libs/cluster.ts +0 -219
  111. package/libs/env.ts +0 -28
  112. package/libs/routes.ts +0 -275
  113. package/libs/swagger.ts +0 -79
  114. package/postinstall-tools.mjs +0 -115
  115. package/postinstall.mjs +0 -42
  116. package/templates/postinstall/api/_install.mjs +0 -34
  117. package/templates/postinstall/lib/_install.mjs +0 -30
package/src/libs/fs.ts ADDED
@@ -0,0 +1,382 @@
1
+ import nodeFS from 'node:fs/promises'
2
+ import path from 'node:path'
3
+ import nodeFSCallBack, { PathLike } from 'node:fs'
4
+ import readline from 'node:readline'
5
+ import glob from 'glob'
6
+
7
+ export namespace PlataFs {
8
+ export const constants = nodeFS.constants
9
+
10
+ export const fs = nodeFS
11
+
12
+ /**
13
+ * Tests a user's permissions for the file or directory specified by `path`.
14
+ * The `mode` argument is an optional integer that specifies the accessibility
15
+ * checks to be performed. `mode` should be either the value `fs.constants.F_OK`or a mask consisting of the bitwise OR of any of `fs.constants.R_OK`,`fs.constants.W_OK`, and `fs.constants.X_OK`
16
+ * (e.g.`fs.constants.W_OK | fs.constants.R_OK`). Check `File access constants` for
17
+ * possible values of `mode`.
18
+ *
19
+ * If the accessibility check is successful, the promise is resolved with no
20
+ * value. If any of the accessibility checks fail, the promise is rejected
21
+ * with an [Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) object. The following example checks if the file`/etc/passwd` can be read and
22
+ * written by the current process.
23
+ *
24
+ * ```js
25
+ * import { access } from 'fs/promises';
26
+ * import { constants } from 'fs';
27
+ *
28
+ * try {
29
+ * await access('/etc/passwd', constants.R_OK | constants.W_OK);
30
+ * console.log('can access');
31
+ * } catch {
32
+ * console.error('cannot access');
33
+ * }
34
+ * ```
35
+ *
36
+ * Using `fsPromises.access()` to check for the accessibility of a file before
37
+ * calling `fsPromises.open()` is not recommended. Doing so introduces a race
38
+ * condition, since other processes may change the file's state between the two
39
+ * calls. Instead, user code should open/read/write the file directly and handle
40
+ * the error raised if the file is not accessible.
41
+ * @since v10.0.0
42
+ * @param [mode=fs.constants.F_OK]
43
+ * @return Fulfills with `undefined` upon success.
44
+ */
45
+ export function access(path: PathLike, mode?: number): Promise<boolean> {
46
+ return fs.access(path, mode).then(
47
+ () => true,
48
+ () => false
49
+ )
50
+ }
51
+
52
+ /**
53
+ * `options` may also include a `start` option to allow writing data at some
54
+ * position past the beginning of the file, allowed values are in the
55
+ * \[0, [`Number.MAX_SAFE_INTEGER`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)\] range. Modifying a file rather than
56
+ * replacing it may require the `flags` option to be set to `r+` rather than the
57
+ * default `w`. The `encoding` can be any one of those accepted by `Buffer`.
58
+ *
59
+ * If `autoClose` is set to true (default behavior) on `'error'` or `'finish'`the file descriptor will be closed automatically. If `autoClose` is false,
60
+ * then the file descriptor won't be closed, even if there's an error.
61
+ * It is the application's responsibility to close it and make sure there's no
62
+ * file descriptor leak.
63
+ *
64
+ * By default, the stream will emit a `'close'` event after it has been
65
+ * destroyed. Set the `emitClose` option to `false` to change this behavior.
66
+ *
67
+ * By providing the `fs` option it is possible to override the corresponding `fs`implementations for `open`, `write`, `writev` and `close`. Overriding `write()`without `writev()` can reduce
68
+ * performance as some optimizations (`_writev()`)
69
+ * will be disabled. When providing the `fs` option, overrides for at least one of`write` and `writev` are required. If no `fd` option is supplied, an override
70
+ * for `open` is also required. If `autoClose` is `true`, an override for `close`is also required.
71
+ *
72
+ * Like `fs.ReadStream`, if `fd` is specified, `fs.WriteStream` will ignore the`path` argument and will use the specified file descriptor. This means that no`'open'` event will be
73
+ * emitted. `fd` should be blocking; non-blocking `fd`s
74
+ * should be passed to `net.Socket`.
75
+ *
76
+ * If `options` is a string, then it specifies the encoding.
77
+ * @since v0.1.31
78
+ */
79
+ export const createWriteStream: typeof nodeFSCallBack.createWriteStream = nodeFSCallBack.createWriteStream
80
+ /**
81
+ * Unlike the 16 kb default `highWaterMark` for a `stream.Readable`, the stream
82
+ * returned by this method has a default `highWaterMark` of 64 kb.
83
+ *
84
+ * `options` can include `start` and `end` values to read a range of bytes from
85
+ * the file instead of the entire file. Both `start` and `end` are inclusive and
86
+ * start counting at 0, allowed values are in the
87
+ * \[0, [`Number.MAX_SAFE_INTEGER`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER)\] range. If `fd` is specified and `start` is
88
+ * omitted or `undefined`, `fs.createReadStream()` reads sequentially from the
89
+ * current file position. The `encoding` can be any one of those accepted by `Buffer`.
90
+ *
91
+ * If `fd` is specified, `ReadStream` will ignore the `path` argument and will use
92
+ * the specified file descriptor. This means that no `'open'` event will be
93
+ * emitted. `fd` should be blocking; non-blocking `fd`s should be passed to `net.Socket`.
94
+ *
95
+ * If `fd` points to a character device that only supports blocking reads
96
+ * (such as keyboard or sound card), read operations do not finish until data is
97
+ * available. This can prevent the process from exiting and the stream from
98
+ * closing naturally.
99
+ *
100
+ * By default, the stream will emit a `'close'` event after it has been
101
+ * destroyed. Set the `emitClose` option to `false` to change this behavior.
102
+ *
103
+ * By providing the `fs` option, it is possible to override the corresponding `fs`implementations for `open`, `read`, and `close`. When providing the `fs` option,
104
+ * an override for `read` is required. If no `fd` is provided, an override for`open` is also required. If `autoClose` is `true`, an override for `close` is
105
+ * also required.
106
+ *
107
+ * ```js
108
+ * import { createReadStream } from 'fs';
109
+ *
110
+ * // Create a stream from some character device.
111
+ * const stream = createReadStream('/dev/input/event0');
112
+ * setTimeout(() => {
113
+ * stream.close(); // This may not close the stream.
114
+ * // Artificially marking end-of-stream, as if the underlying resource had
115
+ * // indicated end-of-file by itself, allows the stream to close.
116
+ * // This does not cancel pending read operations, and if there is such an
117
+ * // operation, the process may still not be able to exit successfully
118
+ * // until it finishes.
119
+ * stream.push(null);
120
+ * stream.read(0);
121
+ * }, 100);
122
+ * ```
123
+ *
124
+ * If `autoClose` is false, then the file descriptor won't be closed, even if
125
+ * there's an error. It is the application's responsibility to close it and make
126
+ * sure there's no file descriptor leak. If `autoClose` is set to true (default
127
+ * behavior), on `'error'` or `'end'` the file descriptor will be closed
128
+ * automatically.
129
+ *
130
+ * `mode` sets the file mode (permission and sticky bits), but only if the
131
+ * file was created.
132
+ *
133
+ * An example to read the last 10 bytes of a file which is 100 bytes long:
134
+ *
135
+ * ```js
136
+ * import { createReadStream } from 'fs';
137
+ *
138
+ * createReadStream('sample.txt', { start: 90, end: 99 });
139
+ * ```
140
+ *
141
+ * If `options` is a string, then it specifies the encoding.
142
+ * @since v0.1.31
143
+ */
144
+ export const createReadStream: typeof nodeFSCallBack.createReadStream = nodeFSCallBack.createReadStream
145
+
146
+ export async function ReadEnvFileToPromise(p: string): PlataPromise<Dictionary<string | undefined>> {
147
+ const envFile = new Object(null)
148
+
149
+ const result = await ReadFileAsync(p, async line => {
150
+ if (line[0] === '#') return
151
+
152
+ if (line !== '') {
153
+ const env = line.split('=')
154
+
155
+ if (env.length >= 2) {
156
+ const [ key, ...v ] = env
157
+ let value = v.join('=')
158
+
159
+ if (value[0] === '{' && value.slice(-1) === '}')
160
+ value = process.env[value.slice(1, -1)] ?? ''
161
+ ;
162
+
163
+ if (value !== '')
164
+ envFile[key] = value
165
+ ;
166
+ }
167
+ }
168
+ })
169
+
170
+ if (result !== null) {
171
+ return result
172
+ }
173
+
174
+ return envFile as any
175
+ }
176
+
177
+ export const cp: PlataFunctionWrapperReturn<typeof nodeFS.cp, Promise<MaybePlataError>> = (source, destination, opts) => {
178
+ return nodeFS.cp(source, destination, opts).then(
179
+ () => null,
180
+ err => {
181
+ return {
182
+ errorID: 'PLFS001',
183
+ msg: err.message,
184
+ error: err
185
+ }
186
+ }
187
+ )
188
+ }
189
+
190
+ export function ReadFileSync(file: string, callback: (line: string) => string): PlataPromise<string> {
191
+ return new Promise(resolve => {
192
+ try {
193
+ const content: string[] = []
194
+ const stream = readline.createInterface({
195
+ input: createReadStream(file),
196
+ crlfDelay: Infinity
197
+ })
198
+
199
+ stream.on('close', () => {
200
+ resolve(content.join('\n'))
201
+ })
202
+
203
+ stream.on('line', (line) => {
204
+ content.push(callback(line))
205
+ })
206
+
207
+ } catch(e) {
208
+ resolve({
209
+ errorID: 'PLTRF0004',
210
+ msg: `Error while reading file: ${file}`,
211
+ error: e
212
+ })
213
+ }
214
+ })
215
+ }
216
+
217
+ export function ReadFileAsync(file: string, callback: (line: string) => Promise<void>): Promise<MaybePlataError> {
218
+ return new Promise(resolve => {
219
+ try {
220
+ const promises: any = []
221
+
222
+ const stream = readline.createInterface({
223
+ input: createReadStream(file),
224
+ crlfDelay: Infinity
225
+ })
226
+
227
+ stream.on('close', () => {
228
+ Promise.all(promises).then(
229
+ () => resolve(null),
230
+ e => resolve({
231
+ errorID: 'PPFRFA0002',
232
+ msg: `Unexpected error while reading file: ${file}`,
233
+ error: e
234
+ }),
235
+ )
236
+ })
237
+
238
+ stream.on('line', line => {
239
+ promises.push(Plata.FastPromise(() => callback(line)))
240
+ })
241
+
242
+ } catch (e) {
243
+ return resolve({
244
+ errorID: 'PPFRFA0001',
245
+ msg: `Unexpected error while reading file: ${file}`,
246
+ error: e
247
+ })
248
+ }
249
+ })
250
+ }
251
+
252
+ export async function FindFiles(folders: string[], file: string): Promise<string[]> {
253
+ const files: string[] = []
254
+
255
+ for (const folder of folders) {
256
+ files.push(...glob.sync(`${folder.replace(/\\/g, '/')}/**/${file}`))
257
+ }
258
+
259
+ return files
260
+ }
261
+
262
+ export class CliTools {
263
+ public readonly sourcePath: PathLike
264
+ public readonly destPath: PathLike
265
+
266
+ constructor(sourcePath: PathLike, destPath: PathLike) {
267
+ this.destPath = destPath
268
+ this.sourcePath = sourcePath
269
+ }
270
+
271
+ public async mkdirIfNotExists(dir: PathLike) {
272
+ try {
273
+ const p = path.resolve(dir.toString())
274
+
275
+ if (!(await access(p))) {
276
+ await nodeFS.mkdir(p, { recursive: true })
277
+ }
278
+ } catch (err) {
279
+ console.error(err)
280
+ process.exit(1)
281
+ }
282
+ }
283
+
284
+ public async createFolderToDest(folder: string) {
285
+ const destinationPath = path.join(this.destPath.toString(), folder)
286
+
287
+ await this.mkdirIfNotExists(destinationPath)
288
+ }
289
+
290
+ public async forEachFileInFolder(folder: PathLike, callback: (file: string, filePath: PathLike) => Promise<void>) {
291
+ try {
292
+ const folderPath = path.resolve(folder.toString())
293
+ const files = await nodeFS.readdir(folder)
294
+
295
+ const promises: Promise<void>[] = []
296
+
297
+ files.forEach(file => {
298
+ const filePath = path.join(folderPath, file)
299
+
300
+ promises.push(
301
+ Promise.resolve().then(() => callback(file, filePath))
302
+ )
303
+ })
304
+
305
+ await Promise.all(promises)
306
+ } catch (err) {
307
+ console.error(err)
308
+ process.exit(1)
309
+ }
310
+ }
311
+
312
+ public async copyFile(sourcePath: PathLike, destinationPath: PathLike) {
313
+ try {
314
+ const s = path.resolve(sourcePath.toString())
315
+ const d = path.resolve(destinationPath.toString())
316
+
317
+ if (!(await access(d))) {
318
+ await nodeFS.copyFile(s, d)
319
+ }
320
+ } catch (err) {
321
+ console.error(err)
322
+ process.exit(1)
323
+ }
324
+ }
325
+
326
+ public async copyFileIfNotExists(sourcePath: PathLike, destinationPath: PathLike) {
327
+ try {
328
+ const s = path.resolve(sourcePath.toString())
329
+ const d = path.resolve(destinationPath.toString())
330
+
331
+ if (!(await access(d))) {
332
+ await this.copyFile(s, d)
333
+ }
334
+ } catch (err) {
335
+ console.error(err)
336
+ process.exit(1)
337
+ }
338
+ }
339
+
340
+ public async copyFolderToDest(folder: string) {
341
+ const sourcePath = path.join(this.sourcePath.toString(), folder)
342
+ const destinationPath = path.join(this.destPath.toString(), folder)
343
+
344
+ await this.mkdirIfNotExists(destinationPath)
345
+
346
+ await this.forEachFileInFolder(sourcePath, (file, filePath) =>
347
+ this.copyFileIfNotExists(
348
+ filePath,
349
+ path.join(destinationPath, file)
350
+ )
351
+ )
352
+ }
353
+
354
+ public async syncFolder(folder: string) {
355
+ const sourcePath = path.join(this.sourcePath.toString(), folder)
356
+ const destinationPath = path.join(this.destPath.toString(), folder)
357
+
358
+ await this.mkdirIfNotExists(destinationPath)
359
+
360
+ await this.forEachFileInFolder(sourcePath, (file, filePath) =>
361
+ this.copyFile(
362
+ filePath,
363
+ path.join(destinationPath, file)
364
+ )
365
+ )
366
+ }
367
+
368
+ public async copyFileToDest(file: string) {
369
+ const sourcePath = path.join(this.sourcePath.toString(), file)
370
+ const destinationPath = path.join(this.destPath.toString(), file)
371
+
372
+ await this.copyFileIfNotExists(sourcePath, destinationPath)
373
+ }
374
+
375
+ public async syncFileToDest(file: string) {
376
+ const sourcePath = path.join(this.sourcePath.toString(), file)
377
+ const destinationPath = path.join(this.destPath.toString(), file)
378
+
379
+ await this.copyFile(sourcePath, destinationPath)
380
+ }
381
+ }
382
+ }