miqro 4.0.1 → 5.0.1
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/cli.js +1 -1
- package/build/cmd-map.d.ts +5 -4
- package/build/cmd-map.js +338 -34
- package/build/utils/doc/md.js +1 -1
- package/build/utils/{index.d.ts → exec.d.ts} +1 -0
- package/build/utils/templates.d.ts +19 -0
- package/build/utils/templates.js +153 -0
- package/build/utils/watch.d.ts +1 -0
- package/build/utils/watch.js +35 -0
- package/package.json +8 -8
- package/build/cmds/config-bash.d.ts +0 -2
- package/build/cmds/config-bash.js +0 -14
- package/build/cmds/config-env.d.ts +0 -2
- package/build/cmds/config-env.js +0 -14
- package/build/cmds/config-init.d.ts +0 -2
- package/build/cmds/config-init.js +0 -31
- package/build/cmds/config.d.ts +0 -2
- package/build/cmds/config.js +0 -11
- package/build/cmds/doc-json.d.ts +0 -2
- package/build/cmds/doc-json.js +0 -13
- package/build/cmds/doc-md.d.ts +0 -2
- package/build/cmds/doc-md.js +0 -13
- package/build/cmds/handler-apiroute-new.d.ts +0 -2
- package/build/cmds/handler-apiroute-new.js +0 -66
- package/build/cmds/handler-main-new.d.ts +0 -2
- package/build/cmds/handler-main-new.js +0 -78
- package/build/cmds/help.d.ts +0 -1
- package/build/cmds/help.js +0 -5
- package/build/cmds/new-test.d.ts +0 -2
- package/build/cmds/new-test.js +0 -44
- package/build/cmds/new.d.ts +0 -2
- package/build/cmds/new.js +0 -125
- package/build/cmds/serve.d.ts +0 -2
- package/build/cmds/serve.js +0 -78
- package/build/cmds/start.d.ts +0 -2
- package/build/cmds/start.js +0 -11
- package/build/cmds/watch.d.ts +0 -2
- package/build/cmds/watch.js +0 -50
- package/build/cmds/wc-new.d.ts +0 -2
- package/build/cmds/wc-new.js +0 -118
- /package/build/utils/{index.js → exec.js} +0 -0
package/build/cli.js
CHANGED
package/build/cmd-map.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { main as help } from "./cmds/help.js";
|
|
2
1
|
export declare const usage = "npx miqro <command> [args]";
|
|
3
2
|
export declare const CMD_MAP: {
|
|
4
3
|
"new:api": {
|
|
@@ -53,12 +52,12 @@ export declare const CMD_MAP: {
|
|
|
53
52
|
};
|
|
54
53
|
start: {
|
|
55
54
|
tabs: number;
|
|
56
|
-
cb:
|
|
55
|
+
cb: typeof start;
|
|
57
56
|
description: string;
|
|
58
57
|
};
|
|
59
58
|
cluster: {
|
|
60
59
|
tabs: number;
|
|
61
|
-
cb:
|
|
60
|
+
cb: typeof start;
|
|
62
61
|
description: string;
|
|
63
62
|
};
|
|
64
63
|
watch: {
|
|
@@ -72,8 +71,10 @@ export declare const CMD_MAP: {
|
|
|
72
71
|
description: string;
|
|
73
72
|
};
|
|
74
73
|
help: {
|
|
75
|
-
cb:
|
|
74
|
+
cb: () => void;
|
|
76
75
|
tabs: number;
|
|
77
76
|
description: string;
|
|
78
77
|
};
|
|
79
78
|
};
|
|
79
|
+
declare function start(): void;
|
|
80
|
+
export {};
|
package/build/cmd-map.js
CHANGED
|
@@ -1,66 +1,275 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import { main as newRoute } from "./cmds/handler-apiroute-new.js";
|
|
11
|
-
import { main as newTest } from "./cmds/new-test.js";
|
|
12
|
-
import { main as serve } from "./cmds/serve.js";
|
|
13
|
-
import { main as help } from "./cmds/help.js";
|
|
14
|
-
import { main as newMain } from "./cmds/handler-main-new.js";
|
|
1
|
+
import { resolve } from "path";
|
|
2
|
+
import { existsSync, mkdirSync, statSync, writeFileSync } from "fs";
|
|
3
|
+
import { apiRouteTemplate, gitignoreTemplate, mainTemplates, packageTemplate, templates, testTemplates } from "./utils/templates.js";
|
|
4
|
+
import { App, ConfigPathResolver, LoggerHandler, Proxy, ReadBuffer, Static, loadConfig, normalizePath } from "@miqro/core";
|
|
5
|
+
import { getDOCJSON } from "./utils/doc/json.js";
|
|
6
|
+
import { getMDDoc } from "./utils/doc/md.js";
|
|
7
|
+
import { mainPath } from "@miqro/runner";
|
|
8
|
+
import { setupWatch } from "./utils/watch.js";
|
|
9
|
+
import { extractFlags, getUsage, execSync } from "./utils/exec.js";
|
|
15
10
|
export const usage = "npx miqro <command> [args]";
|
|
16
11
|
export const CMD_MAP = {
|
|
17
12
|
["new:api"]: {
|
|
18
13
|
//section: "api development",
|
|
19
|
-
cb:
|
|
14
|
+
cb: () => {
|
|
15
|
+
const usageTS = `usage: npx miqro new:api <identifier ex: NEW_APP>`;
|
|
16
|
+
if (process.argv.length !== 4 || process.argv[3].length < 1) {
|
|
17
|
+
throw new Error(usageTS);
|
|
18
|
+
}
|
|
19
|
+
const identifier = process.argv[3].toLocaleLowerCase();
|
|
20
|
+
const appFolder = resolve(process.cwd(), identifier);
|
|
21
|
+
if (existsSync(appFolder)) {
|
|
22
|
+
throw new Error(`${appFolder} already exists!`);
|
|
23
|
+
}
|
|
24
|
+
console.log(`creating ${appFolder}`);
|
|
25
|
+
mkdirSync(appFolder, {
|
|
26
|
+
recursive: true
|
|
27
|
+
});
|
|
28
|
+
writeFileSync(resolve(appFolder, "package.json"), packageTemplate["ts"](identifier));
|
|
29
|
+
execSync(`npm install miqro --save-dev`, {
|
|
30
|
+
cwd: appFolder
|
|
31
|
+
});
|
|
32
|
+
execSync(`npm install @miqro/core --save`, {
|
|
33
|
+
cwd: appFolder
|
|
34
|
+
});
|
|
35
|
+
writeFileSync(resolve(appFolder, "tsconfig.json"), `{
|
|
36
|
+
"compileOnSave": true,
|
|
37
|
+
"compilerOptions": {
|
|
38
|
+
"lib": ["es2021"],
|
|
39
|
+
"module": "Node16",
|
|
40
|
+
"moduleResolution": "Node16",
|
|
41
|
+
"target": "es2021",
|
|
42
|
+
"strict": false,
|
|
43
|
+
"outDir": "./build/",
|
|
44
|
+
"removeComments": true,
|
|
45
|
+
"noImplicitAny": false,
|
|
46
|
+
"preserveConstEnums": true,
|
|
47
|
+
"sourceMap": true,
|
|
48
|
+
"esModuleInterop": true,
|
|
49
|
+
"declaration": true
|
|
50
|
+
},
|
|
51
|
+
"exclude": [
|
|
52
|
+
"node_modules",
|
|
53
|
+
"test"
|
|
54
|
+
],
|
|
55
|
+
"include": [
|
|
56
|
+
"src"
|
|
57
|
+
]
|
|
58
|
+
}`);
|
|
59
|
+
execSync(`npm install typescript --save-dev`, {
|
|
60
|
+
cwd: appFolder
|
|
61
|
+
});
|
|
62
|
+
execSync(`npm install @types/node --save-dev`, {
|
|
63
|
+
cwd: appFolder
|
|
64
|
+
});
|
|
65
|
+
writeFileSync(resolve(appFolder, ".gitignore"), gitignoreTemplate.ts());
|
|
66
|
+
execSync(`npm install @miqro/test-http --save-dev`, {
|
|
67
|
+
cwd: appFolder
|
|
68
|
+
});
|
|
69
|
+
execSync(`npx miqro new:api:main src_main`, {
|
|
70
|
+
cwd: appFolder
|
|
71
|
+
});
|
|
72
|
+
execSync(`npx miqro new:api:route src_api_health`, {
|
|
73
|
+
cwd: appFolder
|
|
74
|
+
});
|
|
75
|
+
mkdirSync(resolve(appFolder, "test"), {
|
|
76
|
+
recursive: true
|
|
77
|
+
});
|
|
78
|
+
execSync(`npx miqro new:api:route:test test_api_health`, {
|
|
79
|
+
cwd: appFolder
|
|
80
|
+
});
|
|
81
|
+
console.log("");
|
|
82
|
+
console.log("");
|
|
83
|
+
console.log(`new project created on ${appFolder}`);
|
|
84
|
+
console.log(`cd ${identifier}`);
|
|
85
|
+
console.log(`npm run start`);
|
|
86
|
+
console.log("");
|
|
87
|
+
}, tabs: 4, description: `create a new project.`
|
|
20
88
|
},
|
|
21
|
-
//["new:typescript"]: {cb: newTS, tabs: 4, description: `create a new typescript project.`},
|
|
22
89
|
["new:api:main"]: {
|
|
23
90
|
//section: "http scaffolding",
|
|
24
|
-
cb:
|
|
91
|
+
cb: () => {
|
|
92
|
+
if (process.argv.length !== 4 || process.argv[3].length < 1) {
|
|
93
|
+
throw new Error(`usage: [NODE_ENV=development] npx miqro new:api:main <identifier ex: NEW_APP>`);
|
|
94
|
+
}
|
|
95
|
+
const identifier = process.argv[3].toLocaleLowerCase();
|
|
96
|
+
const split = identifier.split("_");
|
|
97
|
+
const dots = split.filter(s => s.indexOf(".") !== -1);
|
|
98
|
+
if (dots.length > 0) {
|
|
99
|
+
throw new Error(`identifier cannot contain dots\narguments: <identifier ex: SRC_MAIN>`);
|
|
100
|
+
}
|
|
101
|
+
loadConfig();
|
|
102
|
+
const path = resolve(ConfigPathResolver.getBaseDirname(), ...split.splice(0, split.length - 1));
|
|
103
|
+
const ext = existsSync(resolve(ConfigPathResolver.getBaseDirname(), "tsconfig.json")) ? "ts" : "js";
|
|
104
|
+
const filePath = resolve(path, `${split[0]}.${ext}`);
|
|
105
|
+
if (existsSync(filePath)) {
|
|
106
|
+
throw new Error(`file ${filePath} already exists! doing nothing`);
|
|
107
|
+
}
|
|
108
|
+
console.log(`creating ${filePath}`);
|
|
109
|
+
mkdirSync(path, {
|
|
110
|
+
recursive: true
|
|
111
|
+
});
|
|
112
|
+
writeFileSync(filePath, mainTemplates[ext]());
|
|
113
|
+
console.log(`file ${filePath} created`);
|
|
114
|
+
},
|
|
25
115
|
tabs: 3,
|
|
26
116
|
description: `creates a new main file.`
|
|
27
117
|
},
|
|
28
|
-
["new:api:route"]: {
|
|
29
|
-
|
|
118
|
+
["new:api:route"]: {
|
|
119
|
+
cb: () => {
|
|
120
|
+
const nUsage = `usage: [NODE_ENV=development] npx miqro new:api:main <identifier ex: NEW_APP>`;
|
|
121
|
+
if (process.argv.length !== 4 || process.argv[3].length < 1) {
|
|
122
|
+
throw new Error(nUsage);
|
|
123
|
+
}
|
|
124
|
+
const identifier = process.argv[3].toLocaleLowerCase();
|
|
125
|
+
const split = identifier.split("_").map(s => s.trim()).filter(s => s);
|
|
126
|
+
const dots = split.filter(s => s.indexOf(".") !== -1);
|
|
127
|
+
if (dots.length > 0) {
|
|
128
|
+
throw new Error(`identifier cannot contain dots\n${nUsage}`);
|
|
129
|
+
}
|
|
130
|
+
loadConfig();
|
|
131
|
+
const path = resolve(ConfigPathResolver.getBaseDirname(), ...split.splice(0, split.length - 1));
|
|
132
|
+
const ext = existsSync(resolve(ConfigPathResolver.getBaseDirname(), "tsconfig.json")) ? "ts" : "js";
|
|
133
|
+
const noMethod = ["post", "get", "put", "delete", "patch", "options"].indexOf(split[0].toLocaleLowerCase()) === -1;
|
|
134
|
+
const filePath = resolve(path, `${split[0]}.${ext}`);
|
|
135
|
+
if (existsSync(filePath)) {
|
|
136
|
+
throw new Error(`file ${filePath} already exists! doing nothing`);
|
|
137
|
+
}
|
|
138
|
+
console.log(`creating ${filePath}`);
|
|
139
|
+
mkdirSync(path, {
|
|
140
|
+
recursive: true
|
|
141
|
+
});
|
|
142
|
+
writeFileSync(filePath, apiRouteTemplate[ext](noMethod));
|
|
143
|
+
}, tabs: 3, description: `creates a new route.`
|
|
144
|
+
},
|
|
145
|
+
["new:api:route:test"]: {
|
|
146
|
+
cb: () => {
|
|
147
|
+
if (process.argv.length !== 4 || process.argv[3].length < 1) {
|
|
148
|
+
throw new Error(`usage: [NODE_ENV=development] npx miqro new:test <identifier ex: TEST_SOMEFILE>`);
|
|
149
|
+
}
|
|
150
|
+
const identifier = process.argv[3].toLocaleLowerCase();
|
|
151
|
+
const split = identifier.split("_");
|
|
152
|
+
const dots = split.filter(s => s.indexOf(".") !== -1);
|
|
153
|
+
if (dots.length > 0) {
|
|
154
|
+
throw new Error(`identifier cannot contain dots\narguments: <identifier ex: TEST_SOMETEST>`);
|
|
155
|
+
}
|
|
156
|
+
loadConfig();
|
|
157
|
+
const path = resolve(ConfigPathResolver.getBaseDirname(), ...split.splice(0, split.length - 1));
|
|
158
|
+
const filePath = resolve(path, `${split[0]}.test.js`);
|
|
159
|
+
if (existsSync(filePath)) {
|
|
160
|
+
throw new Error(`file ${filePath} already exists! doing nothing`);
|
|
161
|
+
}
|
|
162
|
+
console.log(`creating ${filePath}`);
|
|
163
|
+
mkdirSync(path, {
|
|
164
|
+
recursive: true
|
|
165
|
+
});
|
|
166
|
+
writeFileSync(filePath, testTemplates.js(split[0]));
|
|
167
|
+
console.log(`file ${filePath} created`);
|
|
168
|
+
}, tabs: 2, description: `create new test.js file for an APIRouter.`
|
|
169
|
+
},
|
|
30
170
|
["config"]: {
|
|
31
171
|
//section: "config management",
|
|
32
|
-
cb:
|
|
172
|
+
cb: () => {
|
|
173
|
+
const logger = console;
|
|
174
|
+
if (process.argv.length !== 3) {
|
|
175
|
+
throw new Error(`invalid number of args. ${"usage: [NODE_ENV=development] npx miqro config"}`);
|
|
176
|
+
}
|
|
177
|
+
const configOut = loadConfig();
|
|
178
|
+
const config = configOut.combined;
|
|
179
|
+
logger.info(JSON.stringify(config, undefined, 2));
|
|
180
|
+
},
|
|
33
181
|
tabs: 4,
|
|
34
182
|
description: `print config as a json.`
|
|
35
183
|
},
|
|
36
184
|
["config:bash"]: {
|
|
37
|
-
cb:
|
|
185
|
+
cb: () => {
|
|
186
|
+
const logger = console;
|
|
187
|
+
if (process.argv.length !== 3) {
|
|
188
|
+
throw new Error(`invalid number of args. usage: [NODE_ENV=development] npx miqro config:bash`);
|
|
189
|
+
}
|
|
190
|
+
const configOut = loadConfig();
|
|
191
|
+
const config = configOut.combined;
|
|
192
|
+
const keys = Object.keys(config);
|
|
193
|
+
for (const key of keys) {
|
|
194
|
+
logger.info(`export ${key}=${config[key]}`);
|
|
195
|
+
}
|
|
196
|
+
},
|
|
38
197
|
tabs: 3,
|
|
39
198
|
description: `print config as a bash script.`
|
|
40
199
|
},
|
|
41
200
|
["config:env"]: {
|
|
42
|
-
cb:
|
|
201
|
+
cb: () => {
|
|
202
|
+
const logger = console;
|
|
203
|
+
if (process.argv.length !== 3) {
|
|
204
|
+
throw new Error(`invalid number of args. usage: [NODE_ENV=development] npx miqro config:env`);
|
|
205
|
+
}
|
|
206
|
+
const configOut = loadConfig();
|
|
207
|
+
const config = configOut.combined;
|
|
208
|
+
const keys = Object.keys(config);
|
|
209
|
+
for (const key of keys) {
|
|
210
|
+
logger.info(`${key}=${config[key]}`);
|
|
211
|
+
}
|
|
212
|
+
},
|
|
43
213
|
tabs: 3,
|
|
44
214
|
description: `print config as a env file.`
|
|
45
215
|
},
|
|
46
|
-
["config:init"]: {
|
|
216
|
+
["config:init"]: {
|
|
217
|
+
cb: () => {
|
|
218
|
+
const logger = console;
|
|
219
|
+
if (process.argv.length !== 3) {
|
|
220
|
+
throw new Error(`invalid number of args. usage: [NODE_ENV=development] npx miqro config:init`);
|
|
221
|
+
}
|
|
222
|
+
loadConfig();
|
|
223
|
+
const configPath = ConfigPathResolver.getConfigDirname();
|
|
224
|
+
const initEnvFile = (path, template) => {
|
|
225
|
+
if (!existsSync(path)) {
|
|
226
|
+
logger.warn(`creating ${path} env file`);
|
|
227
|
+
writeFileSync(path, template);
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
logger.warn(`${path} already exists!. init will not create it.`);
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
if (!existsSync(configPath)) {
|
|
234
|
+
logger.warn(`[${configPath}] doesnt exists!`);
|
|
235
|
+
mkdirSync(configPath, {
|
|
236
|
+
recursive: true
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
initEnvFile(resolve(configPath, `log.env`), templates.logEnvFile);
|
|
240
|
+
initEnvFile(resolve(configPath, `auth.env`), templates.authEnvFile);
|
|
241
|
+
initEnvFile(resolve(configPath, `features.env`), templates.featuresEnvFile);
|
|
242
|
+
}, tabs: 3, description: `inits your config folder.`
|
|
243
|
+
},
|
|
47
244
|
["doc"]: {
|
|
48
245
|
//section: "api documentation",
|
|
49
246
|
tabs: 4,
|
|
50
|
-
cb:
|
|
247
|
+
cb: async () => {
|
|
248
|
+
if (process.argv.length < 5 || process.argv.length > 6) {
|
|
249
|
+
throw new Error(`usage: [NODE_ENV=development] npx miqro doc <api_folder> <subPath> [apiName]`);
|
|
250
|
+
}
|
|
251
|
+
const dirname = process.argv[3];
|
|
252
|
+
const subPath = process.argv[4];
|
|
253
|
+
const apiName = process.argv[5];
|
|
254
|
+
loadConfig();
|
|
255
|
+
console.log(JSON.stringify(await getDOCJSON({ dirname, subPath, apiName }), undefined, 2));
|
|
256
|
+
},
|
|
51
257
|
description: `api folder auto doc as a json.`
|
|
52
258
|
},
|
|
53
259
|
["doc:md"]: {
|
|
54
|
-
cb:
|
|
260
|
+
cb: async () => {
|
|
261
|
+
if (process.argv.length < 5 || process.argv.length > 6) {
|
|
262
|
+
throw new Error(`usage: [NODE_ENV=development] npx miqro doc <api_folder> <subPath> [apiName]`);
|
|
263
|
+
}
|
|
264
|
+
const dirname = process.argv[3];
|
|
265
|
+
const subPath = process.argv[4];
|
|
266
|
+
const apiName = process.argv[5];
|
|
267
|
+
loadConfig();
|
|
268
|
+
console.log(await getMDDoc({ dirname, subPath, apiName }));
|
|
269
|
+
},
|
|
55
270
|
tabs: 4,
|
|
56
271
|
description: `api folder auto doc as a markdown.`
|
|
57
272
|
},
|
|
58
|
-
/*["new:front"]: {
|
|
59
|
-
section: "front end development",
|
|
60
|
-
cb: newTSFront,
|
|
61
|
-
tabs: 3,
|
|
62
|
-
description: `create a new web-components project.`
|
|
63
|
-
},*/
|
|
64
273
|
["start"]: {
|
|
65
274
|
//section: "start helpers",
|
|
66
275
|
tabs: 4,
|
|
@@ -74,18 +283,113 @@ export const CMD_MAP = {
|
|
|
74
283
|
},
|
|
75
284
|
["watch"]: {
|
|
76
285
|
//section: "watch",
|
|
77
|
-
cb:
|
|
286
|
+
cb: () => {
|
|
287
|
+
const usageMessage = (message) => `${message ? `${message}.\n` : ""}usage: npx miqro watch <directory> <cmd>`;
|
|
288
|
+
if (process.argv.length < 5) {
|
|
289
|
+
throw new Error(usageMessage("invalid number of args"));
|
|
290
|
+
}
|
|
291
|
+
const directory = process.argv[3];
|
|
292
|
+
const cmd = process.argv.slice(4).join(" ");
|
|
293
|
+
const timeout = process.env.WATCH_TIMEOUT ? parseInt(process.env.WATCH_TIMEOUT, 10) : undefined;
|
|
294
|
+
if (!existsSync(directory) || !statSync(directory).isDirectory()) {
|
|
295
|
+
throw new Error(usageMessage("directory not found!"));
|
|
296
|
+
}
|
|
297
|
+
console.log(`setting up watch on ${directory} with cmd ${cmd}`);
|
|
298
|
+
setupWatch(directory, cmd, timeout);
|
|
299
|
+
},
|
|
78
300
|
tabs: 4,
|
|
79
301
|
description: `watch folder for changes.`
|
|
80
302
|
},
|
|
81
303
|
["serve"]: {
|
|
82
304
|
//section: "serve static files",
|
|
83
305
|
tabs: 4,
|
|
84
|
-
cb:
|
|
306
|
+
cb: async () => {
|
|
307
|
+
const nUsage = `usage: [NODE_ENV=development] npx miqro serve [directory=./] [path=/] [--index404 ./index.html] [--proxy-cert-ignore] [--port 8080] [--proxy /api=https://host/api]`;
|
|
308
|
+
const flags = extractFlags(process.argv.slice(3), {
|
|
309
|
+
flags: {
|
|
310
|
+
"help": {
|
|
311
|
+
description: "get help page", hasValue: false
|
|
312
|
+
},
|
|
313
|
+
"index404Status": {
|
|
314
|
+
description: "status to handle index404 status", hasValue: true
|
|
315
|
+
},
|
|
316
|
+
"index404": {
|
|
317
|
+
description: "file to handle 404", hasValue: true
|
|
318
|
+
}, "proxy": {
|
|
319
|
+
description: "proxy", hasValue: true
|
|
320
|
+
}, "proxy-cert-ignore": {
|
|
321
|
+
description: "proxy ignore certs", hasValue: false
|
|
322
|
+
}, "port": {
|
|
323
|
+
description: "port", hasValue: true
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
});
|
|
327
|
+
if (flags.flags.help !== undefined) {
|
|
328
|
+
console.log(nUsage);
|
|
329
|
+
process.exit(102);
|
|
330
|
+
}
|
|
331
|
+
if (flags.files.length > 2) {
|
|
332
|
+
throw new Error(`invalid arguments.\n${nUsage}`);
|
|
333
|
+
}
|
|
334
|
+
if (flags.flags.index404 instanceof Array) {
|
|
335
|
+
throw new Error(`invalid index404!.\n${nUsage}`);
|
|
336
|
+
}
|
|
337
|
+
if (flags.flags.port instanceof Array) {
|
|
338
|
+
throw new Error(`invalid port!.\n${nUsage}`);
|
|
339
|
+
}
|
|
340
|
+
let [directory, path] = flags.files;
|
|
341
|
+
path = path ? path : "/";
|
|
342
|
+
directory = directory ? directory : process.cwd();
|
|
343
|
+
loadConfig();
|
|
344
|
+
const PORT = flags.flags.port ? flags.flags.port : (process.env.PORT ? process.env.PORT : 8080);
|
|
345
|
+
if (PORT === undefined) {
|
|
346
|
+
throw new Error(`invalid port!.\n${nUsage}`);
|
|
347
|
+
}
|
|
348
|
+
if (!existsSync(directory) || !statSync(directory).isDirectory()) {
|
|
349
|
+
throw new Error(`${directory} directory not found!\n${nUsage}`);
|
|
350
|
+
}
|
|
351
|
+
const app = new App();
|
|
352
|
+
app.use(LoggerHandler());
|
|
353
|
+
const proxyList = flags.flags.proxy instanceof Array ? flags.flags.proxy : [flags.flags.proxy];
|
|
354
|
+
for (const proxy of proxyList) {
|
|
355
|
+
if (proxy) {
|
|
356
|
+
const proxySplit = proxy.split("=");
|
|
357
|
+
if (proxySplit.length !== 2) {
|
|
358
|
+
throw new Error("proxy must be in the form. /path=proxy");
|
|
359
|
+
}
|
|
360
|
+
const proxyPath = normalizePath(proxySplit[0]);
|
|
361
|
+
const proxyURL = new URL(proxySplit[1]);
|
|
362
|
+
proxyURL.pathname = normalizePath(proxyURL.pathname);
|
|
363
|
+
const proxyRouter = Proxy({
|
|
364
|
+
url: proxyURL.toString(), rejectUnauthorized: flags.flags["proxy-cert-ignore"] ? true : false
|
|
365
|
+
});
|
|
366
|
+
console.log("setting up proxy to %s on %s", proxyURL.toString(), proxyPath);
|
|
367
|
+
proxyRouter.use(ReadBuffer());
|
|
368
|
+
app.use(proxyRouter, proxyPath);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
app.use(Static({
|
|
372
|
+
directory,
|
|
373
|
+
list: true,
|
|
374
|
+
index404: flags.flags.index404 ? flags.flags.index404 : undefined,
|
|
375
|
+
index404Status: flags.flags.index404Status ? parseInt(flags.flags.index404Status, 10) : undefined
|
|
376
|
+
}), path);
|
|
377
|
+
await app.listen(PORT);
|
|
378
|
+
console.log("serving " + directory + " on http://localhost:%s%s", PORT, path);
|
|
379
|
+
}, description: `serve static files.`
|
|
85
380
|
},
|
|
86
381
|
["help"]: {
|
|
87
382
|
//section: "help",
|
|
88
|
-
cb:
|
|
383
|
+
cb: () => {
|
|
384
|
+
console.log(getUsage(CMD_MAP, usage));
|
|
385
|
+
},
|
|
89
386
|
tabs: 4, description: "prints this page"
|
|
90
387
|
}
|
|
91
388
|
};
|
|
389
|
+
function start() {
|
|
390
|
+
if (process.argv.length <= 3) {
|
|
391
|
+
throw new Error(`invalid number of args\nusage: CLUSTER_COUNT=os.cpus().length [CLUSTER_AUTO_BROADCAST=true|false] [CLUSTER_DISABLE_RESTART=true|false] npx miqro start <script> [...args]`);
|
|
392
|
+
}
|
|
393
|
+
loadConfig();
|
|
394
|
+
execSync(`${process.argv[0]} ${mainPath()} ${process.argv.slice(3).join(" ")}`);
|
|
395
|
+
}
|
package/build/utils/doc/md.js
CHANGED
|
@@ -29,7 +29,7 @@ export async function getMDDoc(args, logger) {
|
|
|
29
29
|
const requestOutMD = parserToString(apiData.request);
|
|
30
30
|
outMD += requestOutMD !== "" ? `### request\n\n${requestOutMD}` : "";
|
|
31
31
|
}
|
|
32
|
-
if (apiData.response) {
|
|
32
|
+
if (apiData.response && typeof apiData.response !== "boolean") {
|
|
33
33
|
const responseOutMD = parserToString(apiData.response);
|
|
34
34
|
outMD += responseOutMD !== "" ? `### response\n\n${responseOutMD}` : "";
|
|
35
35
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
|
+
/// <reference types="node" resolution-mode="require"/>
|
|
2
3
|
import { ExecSyncOptionsWithBufferEncoding } from "child_process";
|
|
3
4
|
export declare const execSync: (cmd: string, options?: ExecSyncOptionsWithBufferEncoding) => void;
|
|
4
5
|
export type Callback<T = any> = (...args: any[]) => T;
|
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
export declare const testTemplates: {
|
|
2
|
+
js: (category: string) => string;
|
|
3
|
+
};
|
|
4
|
+
export declare const apiRouteTemplate: {
|
|
5
|
+
ts: (noMethod?: boolean) => "import { APIRoute } from \"@miqro/core\";\n\nexport default {\n method: \"GET\",\n handler: async (req, res) => {\n return {\n text: `Hello`\n }\n }\n} as APIRoute;\n" | "import { APIRoute } from \"@miqro/core\";\n\nexport default {\n handler: async (req, res) => {\n return {\n text: `Hello`\n }\n }\n} as APIRoute;\n";
|
|
6
|
+
js: (noMethod?: boolean) => "module.exports = {\n method: \"GET\n handler: async (req, res) => {\n return {\n text: `Hello`\n }\n }\n};\n" | "module.exports = {\n handler: async (req, res) => {\n return {\n text: `Hello`\n }\n }\n};\n";
|
|
7
|
+
};
|
|
8
|
+
export declare const mainTemplates: {
|
|
9
|
+
ts: () => string;
|
|
10
|
+
js: () => string;
|
|
11
|
+
};
|
|
12
|
+
export declare const gitignoreTemplate: {
|
|
13
|
+
ts: () => string;
|
|
14
|
+
js: () => string;
|
|
15
|
+
};
|
|
16
|
+
export declare const packageTemplate: {
|
|
17
|
+
ts: (name: string) => string;
|
|
18
|
+
js: (name: string) => string;
|
|
19
|
+
};
|
|
1
20
|
export declare const logEnvFile = "####################\n## logging\nLOG_LEVEL=info\nLOG_LEVEL_Database=error\n#LOG_FILE=./logs/dev.log\n";
|
|
2
21
|
export declare const featuresEnvFile = "####################\n## features\n#MY_CUSTOM_FEATURE=false\n";
|
|
3
22
|
export declare const dbEnvFile = "####################\n## db\nDB_URI=sqlite://user:password@localhost:3306/devdb\nDB_POOL=false\nDB_DIALECT_SSL=true\nDB_POOL_MAX=5\nDB_POOL_MIN=0\nDB_POOL_ACQUIRE=30000\nDB_POOL_IDLE=10000\nDB_STORAGE=./dev.sqlite3\n";
|
package/build/utils/templates.js
CHANGED
|
@@ -1,4 +1,157 @@
|
|
|
1
1
|
// noinspection SpellCheckingInspection
|
|
2
|
+
export const testTemplates = {
|
|
3
|
+
js: (category) => `import { it } from "node:test";
|
|
4
|
+
import { TestHelper } from "@miqro/test-http";
|
|
5
|
+
import { Server, APIRouter } from "@miqro/core";
|
|
6
|
+
import { resolve } from "path";
|
|
7
|
+
import { strictEqual } from "assert";
|
|
8
|
+
|
|
9
|
+
it("happy path health", async () => {
|
|
10
|
+
const response = await TestHelper(new Server().use(await APIRouter({
|
|
11
|
+
dirname: resolve("./build/api")
|
|
12
|
+
})), {
|
|
13
|
+
url: "/health"
|
|
14
|
+
});
|
|
15
|
+
strictEqual(response.status, 200);
|
|
16
|
+
});
|
|
17
|
+
`
|
|
18
|
+
};
|
|
19
|
+
export const apiRouteTemplate = {
|
|
20
|
+
ts: (noMethod = false) => noMethod ? `import { APIRoute } from "@miqro/core";
|
|
21
|
+
|
|
22
|
+
export default {
|
|
23
|
+
method: "GET",
|
|
24
|
+
handler: async (req, res) => {
|
|
25
|
+
return {
|
|
26
|
+
text: \`Hello\`
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
} as APIRoute;
|
|
30
|
+
` : `import { APIRoute } from "@miqro/core";
|
|
31
|
+
|
|
32
|
+
export default {
|
|
33
|
+
handler: async (req, res) => {
|
|
34
|
+
return {
|
|
35
|
+
text: \`Hello\`
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
} as APIRoute;
|
|
39
|
+
`,
|
|
40
|
+
js: (noMethod = false) => noMethod ? `module.exports = {
|
|
41
|
+
method: "GET
|
|
42
|
+
handler: async (req, res) => {
|
|
43
|
+
return {
|
|
44
|
+
text: \`Hello\`
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
` : `module.exports = {
|
|
49
|
+
handler: async (req, res) => {
|
|
50
|
+
return {
|
|
51
|
+
text: \`Hello\`
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
`
|
|
56
|
+
};
|
|
57
|
+
export const mainTemplates = {
|
|
58
|
+
ts: () => `import { APIRouter, Server, checkEnvVariables, getLogger } from "@miqro/core";
|
|
59
|
+
import { resolve } from "path";
|
|
60
|
+
|
|
61
|
+
/*
|
|
62
|
+
To be start as a main file
|
|
63
|
+
node file.js
|
|
64
|
+
*/
|
|
65
|
+
|
|
66
|
+
const [PORT] = checkEnvVariables(["PORT"], ["8080"]);
|
|
67
|
+
|
|
68
|
+
const logger = getLogger("server");
|
|
69
|
+
|
|
70
|
+
async function main() {
|
|
71
|
+
const server = new Server();
|
|
72
|
+
server.use(await APIRouter({
|
|
73
|
+
dirname: resolve("./build/api")
|
|
74
|
+
}), "/api");
|
|
75
|
+
await server.listen(PORT);
|
|
76
|
+
server.logPaths(logger);
|
|
77
|
+
logger.info("listening on " + PORT);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
main().catch(e => logger.error(e));
|
|
81
|
+
`,
|
|
82
|
+
js: () => `const { APIRouter, App, checkEnvVariables, getLogger } = require("@miqro/core");
|
|
83
|
+
const { resolve } = require("path");
|
|
84
|
+
|
|
85
|
+
/*
|
|
86
|
+
To be start as a main file
|
|
87
|
+
node file.js
|
|
88
|
+
*/
|
|
89
|
+
|
|
90
|
+
const [PORT] = checkEnvVariables(["PORT"], ["8080"]);
|
|
91
|
+
|
|
92
|
+
const logger = getLogger("server");
|
|
93
|
+
|
|
94
|
+
async function main() {
|
|
95
|
+
const server = new App();
|
|
96
|
+
server.use(await APIRouter({
|
|
97
|
+
dirname: resolve(__dirname, "api")
|
|
98
|
+
}), "/api");
|
|
99
|
+
await server.listen(PORT);
|
|
100
|
+
server.logPaths(logger);
|
|
101
|
+
logger.info("listening on " + PORT);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
main().catch(e => logger.error(e));
|
|
105
|
+
`
|
|
106
|
+
};
|
|
107
|
+
export const gitignoreTemplate = {
|
|
108
|
+
ts: () => `node_modules/
|
|
109
|
+
dist/
|
|
110
|
+
`,
|
|
111
|
+
js: () => `node_modules/`
|
|
112
|
+
};
|
|
113
|
+
export const packageTemplate = {
|
|
114
|
+
ts: (name) => `{
|
|
115
|
+
"name": "${name}",
|
|
116
|
+
"type": "module",
|
|
117
|
+
"version": "1.0.0",
|
|
118
|
+
"description": "",
|
|
119
|
+
"private": true,
|
|
120
|
+
"scripts": {
|
|
121
|
+
"prebuild": "rm -Rf build/;",
|
|
122
|
+
"build": "tsc",
|
|
123
|
+
"prestart": "npm run build",
|
|
124
|
+
"start": "node --enable-source-maps build/main.js",
|
|
125
|
+
"cluster": "NODE_OPTIONS=--enable-source-maps miqro cluster build/main.js",
|
|
126
|
+
"pretest": "npm run build",
|
|
127
|
+
"test": "node --enable-source-maps --test test/**/*.test.js",
|
|
128
|
+
"coverage": "node --enable-source-maps --experimental-test-coverage --test test/**/*.test.js"
|
|
129
|
+
},
|
|
130
|
+
"devDependencies": {
|
|
131
|
+
},
|
|
132
|
+
"dependencies": {
|
|
133
|
+
},
|
|
134
|
+
"author": ""
|
|
135
|
+
}`,
|
|
136
|
+
js: (name) => `{
|
|
137
|
+
"name": "${name}",
|
|
138
|
+
"type": "module",
|
|
139
|
+
"version": "1.0.0",
|
|
140
|
+
"description": "",
|
|
141
|
+
"private": true,
|
|
142
|
+
"scripts": {
|
|
143
|
+
"start": "node src/main.js",
|
|
144
|
+
"cluster": "miqro cluster src/main.js",
|
|
145
|
+
"test": "node --test test/**/*.test.js",
|
|
146
|
+
"coverage": "node --experimental-test-coverage --test test/**/*.test.js"
|
|
147
|
+
},
|
|
148
|
+
"devDependencies": {
|
|
149
|
+
},
|
|
150
|
+
"dependencies": {
|
|
151
|
+
},
|
|
152
|
+
"author": ""
|
|
153
|
+
}`
|
|
154
|
+
};
|
|
2
155
|
export const logEnvFile = `####################
|
|
3
156
|
## logging
|
|
4
157
|
LOG_LEVEL=info
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function setupWatch(directory: string, cmd: string, timeout?: number): void;
|