miqro 6.1.1 → 6.1.3

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 (167) hide show
  1. package/build/esm/editor/auth.d.ts +6 -0
  2. package/build/esm/editor/auth.js +41 -0
  3. package/build/esm/editor/common/admin-interface.d.ts +36 -0
  4. package/build/esm/editor/common/admin-interface.js +44 -0
  5. package/build/esm/editor/common/constants.d.ts +4 -0
  6. package/build/esm/editor/common/constants.js +20 -0
  7. package/build/esm/editor/common/constants.server.d.ts +2 -0
  8. package/build/esm/editor/common/constants.server.js +4 -0
  9. package/build/esm/editor/common/editor-index.d.ts +2 -0
  10. package/build/esm/editor/common/editor-index.js +14 -0
  11. package/build/esm/editor/common/html-encode.d.ts +1 -0
  12. package/build/esm/editor/common/html-encode.js +14 -0
  13. package/build/esm/editor/common/log-socket.d.ts +15 -0
  14. package/build/esm/editor/common/log-socket.js +70 -0
  15. package/build/esm/editor/common/templates.d.ts +11 -0
  16. package/build/esm/editor/common/templates.js +461 -0
  17. package/build/esm/editor/components/api-preview.d.ts +11 -0
  18. package/build/esm/editor/components/api-preview.js +90 -0
  19. package/build/esm/editor/components/editor.d.ts +16 -0
  20. package/build/esm/editor/components/editor.js +357 -0
  21. package/build/esm/editor/components/file-browser.d.ts +37 -0
  22. package/build/esm/editor/components/file-browser.js +126 -0
  23. package/build/esm/editor/components/file-editor-toolbar.d.ts +22 -0
  24. package/build/esm/editor/components/file-editor-toolbar.js +93 -0
  25. package/build/esm/editor/components/file-editor.d.ts +32 -0
  26. package/build/esm/editor/components/file-editor.js +59 -0
  27. package/build/esm/editor/components/filter-query.d.ts +1 -0
  28. package/build/esm/editor/components/filter-query.js +22 -0
  29. package/build/esm/editor/components/highlight-text-area.d.ts +11 -0
  30. package/build/esm/editor/components/highlight-text-area.js +124 -0
  31. package/build/esm/editor/components/log-viewer.d.ts +6 -0
  32. package/build/esm/editor/components/log-viewer.js +69 -0
  33. package/build/esm/editor/components/new-file.d.ts +10 -0
  34. package/build/esm/editor/components/new-file.js +117 -0
  35. package/build/esm/editor/components/scroll-query.d.ts +7 -0
  36. package/build/esm/editor/components/scroll-query.js +21 -0
  37. package/build/esm/editor/components/start-page.d.ts +13 -0
  38. package/build/esm/editor/components/start-page.js +30 -0
  39. package/build/esm/editor/http/admin/editor/api/fs/delete.api.d.ts +3 -0
  40. package/build/esm/editor/http/admin/editor/api/fs/delete.api.js +29 -0
  41. package/build/esm/editor/http/admin/editor/api/fs/read.api.d.ts +5 -0
  42. package/build/esm/editor/http/admin/editor/api/fs/read.api.js +49 -0
  43. package/build/esm/editor/http/admin/editor/api/fs/rename.api.d.ts +4 -0
  44. package/build/esm/editor/http/admin/editor/api/fs/rename.api.js +39 -0
  45. package/build/esm/editor/http/admin/editor/api/fs/scan.api.d.ts +26 -0
  46. package/build/esm/editor/http/admin/editor/api/fs/scan.api.js +149 -0
  47. package/build/esm/editor/http/admin/editor/api/fs/write.api.d.ts +3 -0
  48. package/build/esm/editor/http/admin/editor/api/fs/write.api.js +38 -0
  49. package/build/esm/editor/http/admin/editor/api/server/reload.api.d.ts +10 -0
  50. package/build/esm/editor/http/admin/editor/api/server/reload.api.js +46 -0
  51. package/build/esm/editor/http/admin/editor/api/server/restart.api.d.ts +10 -0
  52. package/build/esm/editor/http/admin/editor/api/server/restart.api.js +45 -0
  53. package/build/esm/editor/http/admin/editor/editor.d.ts +1 -0
  54. package/build/esm/editor/http/admin/editor/editor.js +7 -0
  55. package/build/esm/editor/http/admin/editor/index.api.d.ts +3 -0
  56. package/build/esm/editor/http/admin/editor/index.api.js +21 -0
  57. package/build/esm/editor/server.d.ts +3 -0
  58. package/build/esm/editor/server.js +49 -0
  59. package/build/esm/editor/ws.d.ts +3 -0
  60. package/build/esm/editor/ws.js +11 -0
  61. package/build/esm/src/bin/compile.d.ts +3 -0
  62. package/build/esm/src/bin/compile.js +30 -0
  63. package/build/esm/src/bin/doc-md.d.ts +17 -0
  64. package/build/esm/src/bin/doc-md.js +186 -0
  65. package/build/esm/src/bin/generate-doc.d.ts +6 -0
  66. package/build/esm/src/bin/generate-doc.js +53 -0
  67. package/build/esm/src/bin/test.d.ts +3 -0
  68. package/build/esm/src/bin/test.js +67 -0
  69. package/build/esm/src/bin/types.d.ts +3 -0
  70. package/build/esm/src/bin/types.js +20 -0
  71. package/editor/auth.ts +51 -0
  72. package/editor/common/admin-interface.ts +84 -0
  73. package/editor/common/constants.server.ts +5 -0
  74. package/editor/common/constants.ts +21 -0
  75. package/editor/common/editor-index.tsx +17 -0
  76. package/editor/common/html-encode.ts +14 -0
  77. package/editor/common/log-socket.tsx +85 -0
  78. package/editor/common/templates.ts +466 -0
  79. package/editor/components/api-preview.tsx +116 -0
  80. package/editor/components/editor.tsx +486 -0
  81. package/editor/components/file-browser.tsx +308 -0
  82. package/editor/components/file-editor-toolbar.tsx +191 -0
  83. package/editor/components/file-editor.tsx +122 -0
  84. package/editor/components/filter-query.tsx +22 -0
  85. package/editor/components/highlight-text-area.tsx +144 -0
  86. package/editor/components/log-viewer.tsx +110 -0
  87. package/editor/components/new-file.tsx +169 -0
  88. package/editor/components/scroll-query.tsx +22 -0
  89. package/editor/components/start-page.tsx +49 -0
  90. package/editor/http/admin/editor/api/fs/delete.api.tsx +32 -0
  91. package/editor/http/admin/editor/api/fs/read.api.tsx +55 -0
  92. package/editor/http/admin/editor/api/fs/rename.api.tsx +41 -0
  93. package/editor/http/admin/editor/api/fs/scan.api.tsx +181 -0
  94. package/editor/http/admin/editor/api/fs/write.api.tsx +41 -0
  95. package/editor/http/admin/editor/api/server/reload.api.ts +53 -0
  96. package/editor/http/admin/editor/api/server/restart.api.tsx +52 -0
  97. package/editor/http/admin/editor/editor.tsx +8 -0
  98. package/editor/http/admin/editor/index.api.tsx +39 -0
  99. package/editor/server.ts +57 -0
  100. package/editor/ws.ts +15 -0
  101. package/package.json +1 -1
  102. package/src/bin/compile.ts +35 -0
  103. package/src/bin/doc-md.ts +210 -0
  104. package/src/bin/generate-doc.ts +64 -0
  105. package/src/bin/test.ts +92 -0
  106. package/src/bin/types.ts +22 -0
  107. package/src/cluster.ts +27 -0
  108. package/src/common/arguments.ts +503 -0
  109. package/src/common/assets.ts +128 -0
  110. package/src/common/checksum.ts +58 -0
  111. package/src/common/constants.ts +18 -0
  112. package/src/common/content-type.ts +84 -0
  113. package/src/common/esbuild.ts +94 -0
  114. package/src/common/exit.ts +91 -0
  115. package/src/common/fs.ts +17 -0
  116. package/src/common/help.ts +51 -0
  117. package/src/common/jsx.ts +512 -0
  118. package/src/common/paths.ts +158 -0
  119. package/src/common/watch.ts +85 -0
  120. package/src/inflate/inflate-sea.ts +206 -0
  121. package/src/inflate/inflate.ts +99 -0
  122. package/src/inflate/md.ts +25 -0
  123. package/src/inflate/setup-auth.ts +40 -0
  124. package/src/inflate/setup-cors.ts +40 -0
  125. package/src/inflate/setup-db.ts +113 -0
  126. package/src/inflate/setup-error.ts +43 -0
  127. package/src/inflate/setup-http.ts +655 -0
  128. package/src/inflate/setup-log.ts +44 -0
  129. package/src/inflate/setup-middleware.ts +46 -0
  130. package/src/inflate/setup-server-config.ts +47 -0
  131. package/src/inflate/setup-test.ts +23 -0
  132. package/src/inflate/setup-ws.ts +48 -0
  133. package/src/inflate/setup.doc.ts +68 -0
  134. package/src/inflate/utils/sea-utils.ts +14 -0
  135. package/src/lib.ts +19 -0
  136. package/src/main.ts +87 -0
  137. package/src/services/app.ts +658 -0
  138. package/src/services/editor.tsx +101 -0
  139. package/src/services/globals.ts +143 -0
  140. package/src/services/hot-reload.ts +48 -0
  141. package/src/services/migrations.ts +66 -0
  142. package/src/services/utils/admin-interface.ts +37 -0
  143. package/src/services/utils/cache.ts +88 -0
  144. package/src/services/utils/cluster-cache.ts +230 -0
  145. package/src/services/utils/cluster-ws.ts +197 -0
  146. package/src/services/utils/db-manager.ts +92 -0
  147. package/src/services/utils/get-route.ts +70 -0
  148. package/src/services/utils/log-transport.ts +75 -0
  149. package/src/services/utils/log.ts +92 -0
  150. package/src/services/utils/server-interface.ts +172 -0
  151. package/src/services/utils/websocketmanager.ts +149 -0
  152. package/src/types/@esbuild.d.ts +1 -0
  153. package/src/types/@miqro/core.d.ts +2 -0
  154. package/src/types/@miqro/jsx.d.ts +2 -0
  155. package/src/types/@miqro/parser.d.ts +2 -0
  156. package/src/types/@miqro/query.d.ts +2 -0
  157. package/src/types/@miqro/request.d.ts +2 -0
  158. package/src/types/@miqro/test.d.ts +2 -0
  159. package/src/types/@miqro.d.ts +1 -0
  160. package/src/types/@types.d.ts +1 -0
  161. package/src/types/browser.globals.d.ts +9 -0
  162. package/src/types/globals.d.ts +2 -0
  163. package/src/types/jsx.globals.d.ts +37 -0
  164. package/src/types/miqro.d.ts +129 -0
  165. package/src/types/postject.d.ts +1 -0
  166. package/src/types/server.globals.d.ts +72 -0
  167. package/src/types.ts +222 -0
@@ -0,0 +1,503 @@
1
+ import { checkEnvVariable } from "@miqro/core";
2
+ import { cp, existsSync, readFileSync, statSync } from "node:fs";
3
+ import { basename, dirname, join, relative, resolve } from "node:path";
4
+ import { cwd, env, platform, arch } from "node:process";
5
+
6
+ import { EXIT_CODES } from "./constants.js";
7
+ import { tmpdir } from "node:os";
8
+ import { randomUUID } from "node:crypto";
9
+ import { BIN_NAME, help, usage } from "./help.js";
10
+ import { getMiqroJSONPath, TEST_SOCKET } from "./paths.js";
11
+ import { isSea } from "node:sea";
12
+ import cluster from "node:cluster";
13
+ import { __package_dirname, getVersion } from "./assets.js";
14
+ import { Parser, Schema } from "@miqro/parser";
15
+
16
+ const parser = new Parser();
17
+
18
+ interface MiqroJSON {
19
+ services?: string[];
20
+ port?: string | number;
21
+ inflateDir?: string;
22
+ name?: string;
23
+ }
24
+
25
+ const MiqroJSONSchema: Schema<MiqroJSON> = {
26
+ type: "object",
27
+ properties: {
28
+ name: "string?",
29
+ services: "string[]?",
30
+ port: "number?|string?",
31
+ inflateDir: "string?"
32
+ }
33
+ }
34
+
35
+ export function importMiqroJSON(inFile: string) {
36
+ const mod = JSON.parse(readFileSync(inFile).toString());
37
+ const module = parser.parse(mod, MiqroJSONSchema, basename(inFile));
38
+ if (module !== undefined) {
39
+ return module as MiqroJSON;
40
+ } else {
41
+ throw new Error(`error loading [${inFile}] undefined`);
42
+ }
43
+ }
44
+
45
+ export function getPORT() {
46
+ return checkEnvVariable("PORT", "8080");
47
+ }
48
+
49
+ export interface Arguments {
50
+ name: string;
51
+ installTypes: boolean;
52
+ installTSConfig: boolean;
53
+ test: boolean;
54
+ port: string;
55
+ inflate: boolean;
56
+ generateDoc: boolean;
57
+ generateDocOut: string;
58
+ miqroJSONPath: string | false;
59
+ disableMiqroJSON: boolean;
60
+ generateDocAll: boolean;
61
+ generateDocType: "JSON" | "MD";
62
+ migrateUp: boolean;
63
+ migrateDown: boolean;
64
+ compile: boolean;
65
+ inflateSEA: boolean;
66
+ inflateDir: string;
67
+ services: string[];
68
+ editor: boolean;
69
+ hotreload: boolean;
70
+ }
71
+
72
+ /**
73
+ * parse process.argv arguments
74
+ */
75
+ export function parseArguments(): Arguments {
76
+ //env["LOG_FILE"] = env["LOG_FILE"] ? env["LOG_FILE"] : "./server.log";
77
+
78
+
79
+ const args = cluster.isPrimary ? process.argv.slice(2, process.argv.length) : process.argv.slice(3, process.argv.length);
80
+ const flags: {
81
+ name: string | null;
82
+ installTypes: boolean | null;
83
+ installTSConfig: boolean | null;
84
+ inflate: boolean | null;
85
+ port: string | null;
86
+ generateDoc: boolean | null;
87
+ generateDocAll: boolean | null;
88
+ miqroJSONPath: string | null;
89
+ disableMiqroJSON: boolean | null;
90
+ generateDocOut?: string | null;
91
+ generateDocType?: string | null;
92
+ test: boolean;
93
+ migrateUp: boolean | null;
94
+ migrateDown: boolean | null;
95
+ compile: boolean | null;
96
+ inflateSEA: boolean | null;
97
+ editor: boolean | null;
98
+ inflateDir?: string | null;
99
+ hotreload?: boolean | null;
100
+ } = {
101
+ name: null,
102
+ hotreload: null,
103
+ miqroJSONPath: null,
104
+ disableMiqroJSON: null,
105
+ installTypes: null,
106
+ port: null,
107
+ installTSConfig: null,
108
+ migrateUp: null,
109
+ migrateDown: null,
110
+ inflateSEA: null,
111
+ compile: null,
112
+ test: null,
113
+ inflate: null,
114
+ generateDoc: null,
115
+ generateDocAll: null,
116
+ generateDocOut: null,
117
+ generateDocType: null,
118
+ editor: null,
119
+ inflateDir: null
120
+ };
121
+ const services: string[] = [];
122
+
123
+ for (let i = 0; i < args.length; i++) {
124
+ const argument = args[i];
125
+ switch (argument) {
126
+ case "-v":
127
+ case "--version":
128
+ const { VERSION, NODE_VERSION, ESBUILD_VERSION } = getVersion();
129
+ if (isSea()) {
130
+ console.log("version [%s]", VERSION);
131
+ console.log("Node.js version [%s]", NODE_VERSION);
132
+ console.log("esbuild version [%s]", ESBUILD_VERSION);
133
+ console.log("platform [%s-%s]", platform, arch);
134
+ } else {
135
+ const packageJSON = JSON.parse(readFileSync(resolve(__package_dirname, "package.json")).toString());
136
+ const VERSION = packageJSON.version;
137
+ console.log("version [%s]", VERSION);
138
+ }
139
+ process.exit(EXIT_CODES.NORMAL_EXIT);
140
+ case "-h":
141
+ case "--help":
142
+ console.log(usage);
143
+ console.log(help);
144
+ process.exit(EXIT_CODES.NORMAL_EXIT);
145
+ case "--disable-miqrojson":
146
+ if (flags.disableMiqroJSON !== null || flags.miqroJSONPath !== null) {
147
+ console.error("bad arguments.");
148
+ console.error(usage);
149
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
150
+ }
151
+ flags.disableMiqroJSON = true;
152
+ continue;
153
+ case "--config":
154
+ if (flags.miqroJSONPath !== null || flags.disableMiqroJSON !== null) {
155
+ console.error("bad arguments.");
156
+ console.error(usage);
157
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
158
+ }
159
+ const cPath = String(args[i + 1]).toUpperCase() as any;
160
+ if (typeof cPath !== "string") {
161
+ console.error("bad arguments. --config must be a string.");
162
+ console.error(usage);
163
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
164
+ }
165
+ flags.miqroJSONPath = cPath;
166
+ i++;
167
+ continue;
168
+ case "--install-tsconfig":
169
+ if (flags.inflate !== null || flags.installTSConfig !== null) {
170
+ console.error("bad arguments.");
171
+ console.error(usage);
172
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
173
+ }
174
+ flags.installTSConfig = true;
175
+ continue;
176
+ case "--watch":
177
+ if (flags.hotreload !== null) {
178
+ console.error("bad arguments.");
179
+ console.error(usage);
180
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
181
+ }
182
+ flags.hotreload = true;
183
+ continue;
184
+ case "--port":
185
+ if (flags.port !== null) {
186
+ console.error("bad arguments.");
187
+ console.error(usage);
188
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
189
+ }
190
+ const cPort = String(args[i + 1]).toUpperCase() as any;
191
+ if (typeof cPort !== "string") {
192
+ console.error("bad arguments. --port must be a string.");
193
+ console.error(usage);
194
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
195
+ }
196
+ flags.port = cPort;
197
+ i++;
198
+ continue;
199
+ case "--name":
200
+ if (flags.name !== null) {
201
+ console.error("bad arguments.");
202
+ console.error(usage);
203
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
204
+ }
205
+ const cName = String(args[i + 1]).toUpperCase() as any;
206
+ if (typeof cName !== "string") {
207
+ console.error("bad arguments. --port must be a string.");
208
+ console.error(usage);
209
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
210
+ }
211
+ flags.name = cName;
212
+ i++;
213
+ continue;
214
+ case "--generate-doc":
215
+ if (flags.generateDoc !== null) {
216
+ console.error("bad arguments.");
217
+ console.error(usage);
218
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
219
+ }
220
+ flags.generateDoc = true;
221
+ continue;
222
+ case "--generate-doc-all":
223
+ if (flags.generateDocAll !== null) {
224
+ console.error("bad arguments.");
225
+ console.error(usage);
226
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
227
+ }
228
+ flags.generateDocAll = true;
229
+ continue;
230
+ case "--generate-doc-out":
231
+ if (flags.generateDocOut !== null) {
232
+ console.error("bad arguments. --generate-doc-out already set.");
233
+ console.error(usage);
234
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
235
+ }
236
+ flags.generateDocOut = args[i + 1];
237
+ if (typeof flags.generateDocOut !== "string") {
238
+ console.error("bad arguments. --generate-doc-out missing value.");
239
+ console.error(usage);
240
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
241
+ }
242
+ i++;
243
+ continue;
244
+ case "--generate-doc-type":
245
+ if (flags.generateDocType !== null) {
246
+ console.error("bad arguments. --generate-doc-type already set.");
247
+ console.error(usage);
248
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
249
+ }
250
+ const gDt = String(args[i + 1]).toUpperCase() as any;
251
+ if (typeof gDt !== "string" || (gDt !== "JSON" && gDt !== "MD")) {
252
+ console.error("bad arguments. --generate-doc-type can be json or md.");
253
+ console.error(usage);
254
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
255
+ }
256
+ flags.generateDocType = gDt;
257
+ i++;
258
+ continue;
259
+ case "--compile":
260
+ if (flags.compile !== null) {
261
+ if (flags.inflateDir === null) {
262
+ flags.inflateDir = resolve(tmpdir(), `${BIN_NAME}-compile`, randomUUID());
263
+ }
264
+ console.error("bad arguments. --compile already set");
265
+ console.error(usage);
266
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
267
+ }
268
+ flags.compile = true;
269
+ flags.inflateSEA = true;
270
+ flags.inflate = true;
271
+ continue;
272
+ case "--install-types":
273
+ if (flags.inflate !== null || flags.installTypes !== null) {
274
+ console.error("bad arguments.");
275
+ console.error(usage);
276
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
277
+ }
278
+ flags.installTypes = true;
279
+ continue;
280
+ case "--editor":
281
+ if (flags.editor !== null) {
282
+ console.error("bad arguments.");
283
+ console.error(usage);
284
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
285
+ }
286
+ flags.editor = true;
287
+ continue;
288
+ case "--inflate-sea":
289
+ if (flags.inflateSEA !== null) {
290
+ console.error("bad arguments. --inflate-sea already set.");
291
+ console.error(usage);
292
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
293
+ }
294
+ flags.inflateSEA = true;
295
+ continue;
296
+ case "--migrate-up":
297
+ if (flags.migrateUp !== null) {
298
+ console.error("bad arguments.");
299
+ console.error(usage);
300
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
301
+ }
302
+ flags.migrateUp = true;
303
+ continue;
304
+ case "--migrate-down":
305
+ if (flags.migrateDown !== null) {
306
+ console.error("bad arguments.");
307
+ console.error(usage);
308
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
309
+ }
310
+ flags.migrateDown = true;
311
+ continue;
312
+ case "--inflate":
313
+ if (flags.inflate !== null) {
314
+ console.error("bad arguments.");
315
+ console.error(usage);
316
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
317
+ }
318
+ flags.inflate = true;
319
+ continue;
320
+ case "--test":
321
+ if (flags.test !== null) {
322
+ console.error("bad arguments.");
323
+ console.error(usage);
324
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
325
+ }
326
+ env["BROWSER"] = env["BROWSER"] ? env["BROWSER"] : "none";
327
+ env["PORT"] = TEST_SOCKET;
328
+ flags.test = true;
329
+ continue;
330
+ case "--service":
331
+ if (args[i + 1] === undefined) {
332
+ console.error("bad arguments. service directory not provided.");
333
+ console.error(usage);
334
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
335
+ }
336
+ services.push(args[i + 1]);
337
+ i++;
338
+ continue;
339
+ case "--inflate-dir":
340
+ if (flags.inflateDir !== null && flags.compile === null) {
341
+ console.error("bad arguments. --inflate-dir already set.");
342
+ console.error(usage);
343
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
344
+ }
345
+ flags.inflateDir = args[i + 1];
346
+ if (typeof flags.inflateDir !== "string") {
347
+ console.error("bad arguments. inflate directory not provided.");
348
+ console.error(usage);
349
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
350
+ }
351
+ i++;
352
+ continue;
353
+ default:
354
+ //console.error("IGNORING bad arguments. [%s]", argument);
355
+ //continue;
356
+ console.error("bad argument. [%s]", argument);
357
+ console.error(usage);
358
+ console.log(help);
359
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
360
+ /*flags.inflate = flags.inflate ? flags.inflate : false;
361
+ flags.editor = flags.editor ? flags.editor : false;
362
+ flags.inflateDir = flags.inflateDir ? flags.inflateDir : undefined;
363
+ services.push(argument);
364
+ continue;*/
365
+ }
366
+ }
367
+
368
+ flags.inflate = flags.inflate ? flags.inflate : false;
369
+ flags.editor = flags.editor ? flags.editor : false;
370
+ flags.test = flags.test ? flags.test : false;
371
+ flags.inflateDir = flags.inflateDir ? flags.inflateDir : undefined;
372
+
373
+ const miqroJSONPath = !flags.disableMiqroJSON ? flags.miqroJSONPath ? resolve(flags.miqroJSONPath) : getMiqroJSONPath() : false;
374
+ const miqroRC = miqroJSONPath ? importMiqroJSON(miqroJSONPath) : {};
375
+
376
+ // try to load .miqrorc
377
+ if (!flags.disableMiqroJSON && miqroJSONPath) {
378
+ if (services.length === 0) {
379
+ if (miqroRC.services) {
380
+ for (const service of miqroRC.services) {
381
+ services.push(join(relative(cwd(), dirname(miqroJSONPath)), service));
382
+ }
383
+ }
384
+ }
385
+ if (!flags.port) {
386
+ if (miqroRC.port) {
387
+ flags.port = String(miqroRC.port);
388
+ }
389
+ }
390
+ if (!flags.inflateDir && flags.inflate) {
391
+ if (miqroRC.inflateDir) {
392
+ flags.inflateDir = miqroRC.inflateDir;
393
+ }
394
+ }
395
+ if (!flags.name) {
396
+ if (miqroRC.name) {
397
+ flags.name = miqroRC.name;
398
+ }
399
+ }
400
+ }
401
+
402
+ if (services.length === 0 && (!flags.installTSConfig && !flags.installTypes)) {
403
+ flags.inflateDir = flags.inflateDir ? flags.inflateDir : undefined;
404
+ console.error(`bad arguments. missing --service argument`);
405
+ console.error(usage);
406
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
407
+ }
408
+
409
+ const notDirServices = services.filter(service => existsSync(resolve(process.cwd(), service)) && !statSync(resolve(process.cwd(), service)).isDirectory());
410
+
411
+ if (notDirServices.length > 0) {
412
+ console.error(`bad arguments. [${notDirServices.join(",")}] are not directories.`);
413
+ console.error(usage);
414
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
415
+ }
416
+
417
+ if (flags.inflateDir && existsSync(resolve(cwd(), flags.inflateDir)) && !statSync(resolve(cwd(), flags.inflateDir)).isDirectory()) {
418
+ console.error("bad arguments. inflate directory not a directory.");
419
+ console.error(usage);
420
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
421
+ }
422
+
423
+ if (!flags.inflate && flags.inflateDir) {
424
+ console.error("bad arguments. to use --inflate-dir you must use --inflate.");
425
+ console.error(usage);
426
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
427
+ }
428
+
429
+ if (flags.inflate === null || flags.inflateDir === null) {
430
+ console.error("for ts");
431
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
432
+ }
433
+
434
+ if (flags.inflate && flags.editor) {
435
+ console.error("bad arguments. cannot use --inflate with --editor");
436
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
437
+ }
438
+
439
+ if (flags.inflate && (flags.installTypes || flags.installTSConfig)) {
440
+ console.error("bad arguments. cannot use --inflate with --install-types");
441
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
442
+ }
443
+
444
+ if (flags.inflateSEA && !flags.inflate) {
445
+ console.error("bad arguments. cannot use --inflate-sea without --inflate");
446
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
447
+ }
448
+
449
+ if (flags.editor && (flags.installTypes || flags.installTSConfig)) {
450
+ console.error("bad arguments. cannot use --editor with --install-types");
451
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
452
+ }
453
+
454
+ if (flags.test && (flags.hotreload || flags.editor || flags.compile || flags.inflate)) {
455
+ console.error("bad arguments. cannot use --editor with --test");
456
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
457
+ }
458
+
459
+ if (flags.migrateUp && (flags.hotreload || flags.editor || flags.compile || flags.test || flags.migrateDown || flags.inflate)) {
460
+ console.error("bad arguments. cannot use with --migrate-up");
461
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
462
+ }
463
+
464
+ if (flags.migrateDown && (flags.hotreload || flags.editor || flags.compile || flags.test || flags.migrateUp || flags.inflate)) {
465
+ console.error("bad arguments. cannot use with --migrate-down");
466
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
467
+ }
468
+
469
+ if (flags.generateDoc && (flags.hotreload || flags.editor || flags.compile || flags.test || flags.migrateUp || flags.inflate || flags.migrateDown)) {
470
+ console.error("bad arguments. cannot use with --generate-doc");
471
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
472
+ }
473
+
474
+ if ((flags.generateDocAll || flags.generateDocOut || flags.generateDocType) && !flags.generateDoc) {
475
+ console.error("bad arguments. cannot use without --generate-doc");
476
+ process.exit(EXIT_CODES.BAD_ARGUMENTS);
477
+ }
478
+
479
+ const generateDocType = flags.generateDocType ? flags.generateDocType as any : "MD";
480
+
481
+ return {
482
+ name: flags.name ? flags.name : undefined,
483
+ generateDocAll: flags.generateDocAll ? true : false,
484
+ hotreload: flags.hotreload ? true : false,
485
+ disableMiqroJSON: flags.disableMiqroJSON !== null ? flags.disableMiqroJSON : false,
486
+ miqroJSONPath: miqroJSONPath ? miqroJSONPath : false,
487
+ installTypes: flags.installTypes ? true : false,
488
+ installTSConfig: flags.installTSConfig ? true : false,
489
+ inflate: flags.inflate,
490
+ port: flags.port ? flags.port : getPORT(),
491
+ migrateUp: flags.migrateUp ? true : false,
492
+ migrateDown: flags.migrateDown ? true : false,
493
+ test: flags.test ? true : false,
494
+ compile: flags.compile,
495
+ inflateSEA: flags.inflateSEA ? true : false,
496
+ inflateDir: flags.inflateDir ? resolve(process.cwd(), flags.inflateDir) : resolve(process.cwd(), "inflated"),
497
+ generateDoc: flags.generateDoc = flags.generateDoc ? flags.generateDoc : false,
498
+ generateDocOut: flags.generateDocOut ? resolve(process.cwd(), flags.generateDocOut) : generateDocType === "MD" ? resolve(process.cwd(), "API.md") : resolve(process.cwd(), "API.json"),
499
+ generateDocType,
500
+ services,
501
+ editor: flags.editor ? true : false
502
+ }
503
+ }
@@ -0,0 +1,128 @@
1
+ //import { createRequire } from 'node:module';
2
+ import { Logger, MinimalLogger } from "@miqro/core";
3
+ import { chmodSync, constants, existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync } from "node:fs";
4
+ import { getAsset as seaGetAsset, isSea } from "node:sea";
5
+ import { dirname, resolve } from "node:path";
6
+ import { calculateChecksum, calculateChecksumFromBuffer } from "./checksum.js";
7
+ import { arch, cwd, platform } from "node:process";
8
+ import { fileURLToPath } from 'node:url';
9
+ import { initESBuild } from "./esbuild.js";
10
+ import { initJSXJS } from "./jsx.js";
11
+ //const require = createRequire(import.meta.url);
12
+
13
+ export const __package_dirname = import.meta.url ? resolve(dirname(fileURLToPath(import.meta.url)), "..", "..", "..", "..") : null;
14
+
15
+ const ASSETS_ROUTER = {
16
+ "lib.cjs": "build/lib.cjs",
17
+ "version.tag": "sea/version.tag",
18
+ "node.version.tag": "sea/node.version.tag",
19
+ "esbuild.version.tag": "sea/esbuild.version.tag",
20
+ "postject.base64.cjs": "build/postject.base64.cjs",
21
+ "sea.basic.config.json": "sea/basic-config.json",
22
+ "node.sh": "sea/node.sh",
23
+ "install-nodejs.sh": "sea/install-nodejs.sh",
24
+ "compile.base64.sh": "sea/basic-compile.base64.sh",
25
+ "sign-remove.sh": "sea/sign-remove.sh",
26
+ "sign-add.sh": "sea/sign-add.sh",
27
+ "app.sh": "sea/app.sh",
28
+ "jsx.dom.js": "build/jsx.dom.js",
29
+ "types.json": "sea/types.json",
30
+ "editor-assets/editor.bundle.js": "build/editor.bundle.js",
31
+ "editor-assets/style.css": "build/style.css",
32
+ "editor-assets/font.ttf": "build/font.ttf"
33
+ }
34
+
35
+ export function getAsset(key: string): ArrayBuffer {
36
+ if (isSea()) {
37
+ return seaGetAsset(key);
38
+ } else {
39
+ if (!ASSETS_ROUTER[key]) {
40
+ if (key === "esbuild-binary") {
41
+ if (__package_dirname === null) {
42
+ return readFileSync(resolve(`./sea/esbuild`));
43
+ } else {
44
+ //return readFileSync(resolve(__package_dirname, `./node_modules/@esbuild/${platform}-${arch}/bin/esbuild`));
45
+ const esBinaryPath = resolve(dirname(import.meta.resolve(`esbuild`).substring("file://".length)), "..", "..", "@esbuild", `${platform}-${arch}`);
46
+ return readFileSync(resolve(esBinaryPath, "bin", "esbuild"));
47
+ }
48
+ } else {
49
+ throw new Error("asset not registered!");
50
+ }
51
+ }
52
+ return readFileSync(resolve(__package_dirname, ASSETS_ROUTER[key]));
53
+ }
54
+ }
55
+
56
+ export function getVersion() {
57
+ const VERSION = !isSea() ? "" : Buffer.from(getAsset("version.tag")).toString().trim();
58
+ const NODE_VERSION = !isSea() ? "" : Buffer.from(getAsset("node.version.tag")).toString().trim();
59
+ const ESBUILD_VERSION = !isSea() ? "" : Buffer.from(getAsset("esbuild.version.tag")).toString().trim();
60
+ return {
61
+ VERSION,
62
+ NODE_VERSION,
63
+ ESBUILD_VERSION
64
+ }
65
+ }
66
+
67
+ export async function initAssets(logger: Logger) {
68
+ const { VERSION, NODE_VERSION, ESBUILD_VERSION } = getVersion();
69
+ if (isSea()) {
70
+ logger.debug("version [%s]", VERSION);
71
+ logger.debug("Node.js version [%s]", NODE_VERSION);
72
+ logger.debug("esbuild version [%s]", ESBUILD_VERSION);
73
+ }
74
+ logger.debug("platform [%s-%s]", platform, arch);
75
+ await Promise.all([initJSXJS(logger), initESBuild(logger)]);
76
+ }
77
+
78
+ export async function initTypes(logger: MinimalLogger) {
79
+ const typesJSON = JSON.parse(Buffer.from(getAsset("types.json")).toString("utf-8"));
80
+ await Promise.all(Object.keys(typesJSON).map(typeFile =>
81
+ initAsset(logger, resolve(cwd(), "." + typeFile), Buffer.from(typesJSON[typeFile], "base64"))
82
+ ));
83
+ }
84
+
85
+ export async function validateAsset(logger: MinimalLogger | undefined, path: string, internalChecksum: string): Promise<boolean> {
86
+ mkdirSync(dirname(path), {
87
+ recursive: true
88
+ });
89
+ if (existsSync(path)) {
90
+ logger?.trace("validating [%s]", path);
91
+ const checksum = await calculateChecksum(path);
92
+ //const internalChecksum = await calculateChecksumFromBuffer(buffer);
93
+ if (internalChecksum !== checksum) {
94
+ logger?.trace("invalid [%s] checksum", path);
95
+ return false;
96
+ } else {
97
+ logger?.trace("validated [%s]", path);
98
+ return true;
99
+ }
100
+ } else {
101
+ logger?.trace("invalid [%s] doesnt exists", path);
102
+ return false;
103
+ }
104
+ }
105
+
106
+ export async function initAsset(logger: MinimalLogger, path: string, buffer: Buffer, executable?: boolean, checksum?: Promise<string>, logInstallasInfo = true) {
107
+ const valid = await validateAsset(logger, path, checksum === undefined ? await calculateChecksumFromBuffer(buffer) : await checksum);
108
+ if (existsSync(path)) {
109
+ if (!valid) {
110
+ logger.info("updating [%s]", path);
111
+ unlinkSync(path);
112
+ writeFileSync(path, buffer);
113
+ if (executable) {
114
+ chmodSync(path, constants.S_IXUSR | constants.S_IRUSR | constants.S_IWUSR);
115
+ }
116
+ }
117
+ } else {
118
+ if (!logInstallasInfo) {
119
+ logger.debug("installing [%s]", path);
120
+ } else {
121
+ logger.info("installing [%s]", path);
122
+ }
123
+ writeFileSync(path, buffer);
124
+ if (executable) {
125
+ chmodSync(path, constants.S_IXUSR | constants.S_IRUSR | constants.S_IWUSR);
126
+ }
127
+ }
128
+ }
@@ -0,0 +1,58 @@
1
+ import { createHash } from "node:crypto";
2
+ import { createReadStream } from "node:fs";
3
+ import { Readable } from "node:stream";
4
+
5
+ /**
6
+ *
7
+ * @param {string} filePath
8
+ * @returns {Promise<string>} the checksum
9
+ */
10
+ export async function calculateChecksum(filePath: string): Promise<string> {
11
+ return calculateChecksumFromStream(createReadStream(filePath));
12
+ }
13
+
14
+ /**
15
+ *
16
+ * @param {string} filePath
17
+ * @returns {Promise<string>} the checksum
18
+ */
19
+ export async function calculateChecksumFromStream(input: Readable): Promise<string> {
20
+ return new Promise<string>((resolve, reject) => {
21
+ try {
22
+ const hash = createHash('sha256');
23
+ input.on('readable', () => {
24
+ // Only one element is going to be produced by the
25
+ // hash stream.
26
+ const data = input.read();
27
+ if (data)
28
+ hash.update(data);
29
+ else {
30
+ //log(`${hash.digest('hex')} ${filename}`);
31
+ resolve(hash.digest('hex').toString());
32
+ }
33
+ });
34
+ input.on("error", (e) => {
35
+ reject(e);
36
+ });
37
+ } catch (e) {
38
+ reject(e);
39
+ }
40
+ });
41
+ }
42
+
43
+ /**
44
+ *
45
+ * @param {string} filePath
46
+ * @returns {Promise<string>} the checksum
47
+ */
48
+ export async function calculateChecksumFromBuffer(buffer: Buffer): Promise<string> {
49
+ return new Promise<string>((resolve, reject) => {
50
+ try {
51
+ const hash = createHash('sha256');
52
+ hash.update(buffer);
53
+ resolve(hash.digest('hex').toString());
54
+ } catch (e) {
55
+ reject(e);
56
+ }
57
+ });
58
+ }