miqro 4.0.1 → 5.0.2

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 (44) hide show
  1. package/build/cli.js +1 -1
  2. package/build/cmd-map.d.ts +5 -4
  3. package/build/cmd-map.js +340 -34
  4. package/build/utils/doc/json.d.ts +2 -1
  5. package/build/utils/doc/json.js +3 -2
  6. package/build/utils/doc/md.d.ts +1 -0
  7. package/build/utils/doc/md.js +1 -1
  8. package/build/utils/{index.d.ts → exec.d.ts} +1 -0
  9. package/build/utils/templates.d.ts +19 -0
  10. package/build/utils/templates.js +153 -0
  11. package/build/utils/watch.d.ts +1 -0
  12. package/build/utils/watch.js +35 -0
  13. package/package.json +7 -7
  14. package/build/cmds/config-bash.d.ts +0 -2
  15. package/build/cmds/config-bash.js +0 -14
  16. package/build/cmds/config-env.d.ts +0 -2
  17. package/build/cmds/config-env.js +0 -14
  18. package/build/cmds/config-init.d.ts +0 -2
  19. package/build/cmds/config-init.js +0 -31
  20. package/build/cmds/config.d.ts +0 -2
  21. package/build/cmds/config.js +0 -11
  22. package/build/cmds/doc-json.d.ts +0 -2
  23. package/build/cmds/doc-json.js +0 -13
  24. package/build/cmds/doc-md.d.ts +0 -2
  25. package/build/cmds/doc-md.js +0 -13
  26. package/build/cmds/handler-apiroute-new.d.ts +0 -2
  27. package/build/cmds/handler-apiroute-new.js +0 -66
  28. package/build/cmds/handler-main-new.d.ts +0 -2
  29. package/build/cmds/handler-main-new.js +0 -78
  30. package/build/cmds/help.d.ts +0 -1
  31. package/build/cmds/help.js +0 -5
  32. package/build/cmds/new-test.d.ts +0 -2
  33. package/build/cmds/new-test.js +0 -44
  34. package/build/cmds/new.d.ts +0 -2
  35. package/build/cmds/new.js +0 -125
  36. package/build/cmds/serve.d.ts +0 -2
  37. package/build/cmds/serve.js +0 -78
  38. package/build/cmds/start.d.ts +0 -2
  39. package/build/cmds/start.js +0 -11
  40. package/build/cmds/watch.d.ts +0 -2
  41. package/build/cmds/watch.js +0 -50
  42. package/build/cmds/wc-new.d.ts +0 -2
  43. package/build/cmds/wc-new.js +0 -118
  44. /package/build/utils/{index.js → exec.js} +0 -0
@@ -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;
@@ -0,0 +1,35 @@
1
+ import { execSync } from "child_process";
2
+ import { watch, watchFile } from "fs";
3
+ export function setupWatch(directory, cmd, timeout = 1000) {
4
+ watchFile(directory, () => {
5
+ queueRunCMD(cmd, timeout);
6
+ });
7
+ watch(directory, {
8
+ recursive: true
9
+ }, () => {
10
+ queueRunCMD(cmd, timeout);
11
+ });
12
+ }
13
+ let cmdTimeout = null;
14
+ let running = false;
15
+ function queueRunCMD(cmd, timeout) {
16
+ if (running) {
17
+ return;
18
+ }
19
+ if (cmdTimeout) {
20
+ clearTimeout(cmdTimeout);
21
+ }
22
+ cmdTimeout = setTimeout(() => {
23
+ running = true;
24
+ try {
25
+ execSync(cmd, {
26
+ cwd: process.cwd(),
27
+ env: process.env
28
+ });
29
+ }
30
+ catch (e) {
31
+ console.error(e);
32
+ }
33
+ running = false;
34
+ }, timeout);
35
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "miqro",
3
- "version": "4.0.1",
3
+ "version": "5.0.2",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",
@@ -13,20 +13,20 @@
13
13
  "prebuild": "rm -Rf build;",
14
14
  "build": "tsc",
15
15
  "pretest": "npm run build",
16
- "test": "node --enable-source-maps --test test/",
17
- "coverage": "node --enable-source-maps --experimental-test-coverage --test test/"
16
+ "test": "node --enable-source-maps --test test/**.test.js",
17
+ "coverage": "node --enable-source-maps --experimental-test-coverage --test test/**.test.js"
18
18
  },
19
19
  "author": "claukers",
20
20
  "license": "ISC",
21
21
  "dependencies": {
22
- "@miqro/core": "^4.0.1",
23
- "@miqro/parser": "^2.0.1",
22
+ "@miqro/core": "^5.0.9",
23
+ "@miqro/parser": "^2.0.2",
24
24
  "@miqro/runner": "^2.0.1"
25
25
  },
26
26
  "devDependencies": {
27
- "@types/cookie": "0.4.1",
27
+ "@types/cookie": "0.6.0",
28
28
  "@types/node": "^20.4.5",
29
- "typescript": "5.0.4"
29
+ "typescript": "5.3.3"
30
30
  },
31
31
  "engines": {
32
32
  "node": ">=18.0.0",
@@ -1,2 +0,0 @@
1
- export declare const usage = "usage: [NODE_ENV=development] npx miqro config:bash";
2
- export declare const main: () => void;
@@ -1,14 +0,0 @@
1
- import { loadConfig } from "@miqro/core";
2
- export const usage = "usage: [NODE_ENV=development] npx miqro config:bash";
3
- export const main = () => {
4
- const logger = console;
5
- if (process.argv.length !== 3) {
6
- throw new Error(`invalid number of args. ${usage}`);
7
- }
8
- const configOut = loadConfig();
9
- const config = configOut.combined;
10
- const keys = Object.keys(config);
11
- for (const key of keys) {
12
- logger.info(`export ${key}=${config[key]}`);
13
- }
14
- };
@@ -1,2 +0,0 @@
1
- export declare const usage = "usage: [NODE_ENV=development] npx miqro config:env";
2
- export declare const main: () => void;
@@ -1,14 +0,0 @@
1
- import { loadConfig } from "@miqro/core";
2
- export const usage = "usage: [NODE_ENV=development] npx miqro config:env";
3
- export const main = () => {
4
- const logger = console;
5
- if (process.argv.length !== 3) {
6
- throw new Error(`invalid number of args. ${usage}`);
7
- }
8
- const configOut = loadConfig();
9
- const config = configOut.combined;
10
- const keys = Object.keys(config);
11
- for (const key of keys) {
12
- logger.info(`${key}=${config[key]}`);
13
- }
14
- };
@@ -1,2 +0,0 @@
1
- export declare const usage = "usage: [NODE_ENV=development] npx miqro config:init";
2
- export declare const main: () => void;
@@ -1,31 +0,0 @@
1
- import { existsSync, mkdirSync, writeFileSync } from "fs";
2
- import { resolve } from "path";
3
- import { ConfigPathResolver, loadConfig } from "@miqro/core";
4
- import { templates } from "../utils/templates.js";
5
- export const usage = "usage: [NODE_ENV=development] npx miqro config:init";
6
- export const main = () => {
7
- const logger = console;
8
- if (process.argv.length !== 3) {
9
- throw new Error(`invalid number of args. ${usage}`);
10
- }
11
- loadConfig();
12
- const configPath = ConfigPathResolver.getConfigDirname();
13
- const initEnvFile = (path, template) => {
14
- if (!existsSync(path)) {
15
- logger.warn(`creating ${path} env file`);
16
- writeFileSync(path, template);
17
- }
18
- else {
19
- logger.warn(`${path} already exists!. init will not create it.`);
20
- }
21
- };
22
- if (!existsSync(configPath)) {
23
- logger.warn(`[${configPath}] doesnt exists!`);
24
- mkdirSync(configPath, {
25
- recursive: true
26
- });
27
- }
28
- initEnvFile(resolve(configPath, `log.env`), templates.logEnvFile);
29
- initEnvFile(resolve(configPath, `auth.env`), templates.authEnvFile);
30
- initEnvFile(resolve(configPath, `features.env`), templates.featuresEnvFile);
31
- };
@@ -1,2 +0,0 @@
1
- export declare const usage = "usage: [NODE_ENV=development] npx miqro config";
2
- export declare const main: () => void;
@@ -1,11 +0,0 @@
1
- import { loadConfig } from "@miqro/core";
2
- export const usage = "usage: [NODE_ENV=development] npx miqro config";
3
- export const main = () => {
4
- const logger = console;
5
- if (process.argv.length !== 3) {
6
- throw new Error(`invalid number of args. ${usage}`);
7
- }
8
- const configOut = loadConfig();
9
- const config = configOut.combined;
10
- logger.info(JSON.stringify(config, undefined, 2));
11
- };
@@ -1,2 +0,0 @@
1
- export declare const usage = "usage: [NODE_ENV=development] npx miqro doc <api_folder> <subPath> [apiName]";
2
- export declare const main: () => Promise<void>;
@@ -1,13 +0,0 @@
1
- import { loadConfig } from "@miqro/core";
2
- import { getDOCJSON } from "../utils/doc/json.js";
3
- export const usage = `usage: [NODE_ENV=development] npx miqro doc <api_folder> <subPath> [apiName]`;
4
- export const main = async () => {
5
- if (process.argv.length < 5 || process.argv.length > 6) {
6
- throw new Error(usage);
7
- }
8
- const dirname = process.argv[3];
9
- const subPath = process.argv[4];
10
- const apiName = process.argv[5];
11
- loadConfig();
12
- console.log(JSON.stringify(await getDOCJSON({ dirname, subPath, apiName }), undefined, 2));
13
- };
@@ -1,2 +0,0 @@
1
- export declare const usage = "usage: [NODE_ENV=development] npx miqro doc <api_folder> <subPath> [apiName]";
2
- export declare const main: () => Promise<void>;
@@ -1,13 +0,0 @@
1
- import { loadConfig } from "@miqro/core";
2
- import { getMDDoc } from "../utils/doc/md.js";
3
- export const usage = `usage: [NODE_ENV=development] npx miqro doc <api_folder> <subPath> [apiName]`;
4
- export const main = async () => {
5
- if (process.argv.length < 5 || process.argv.length > 6) {
6
- throw new Error(usage);
7
- }
8
- const dirname = process.argv[3];
9
- const subPath = process.argv[4];
10
- const apiName = process.argv[5];
11
- loadConfig();
12
- console.log(await getMDDoc({ dirname, subPath, apiName }));
13
- };
@@ -1,2 +0,0 @@
1
- export declare const usage = "usage: [NODE_ENV=development] npx miqro new:api:route <identifier ex: SRC_API_V1_HEALTH>";
2
- export declare const main: () => void;
@@ -1,66 +0,0 @@
1
- import { ConfigPathResolver, loadConfig } from "@miqro/core";
2
- import { existsSync, mkdirSync, writeFileSync } from "fs";
3
- import { resolve } from "path";
4
- const templates = {
5
- ts: (noMethod = false) => noMethod ? `import { APIRoute } from "@miqro/core";
6
-
7
- export default {
8
- method: "GET",
9
- handler: async (ctx) => {
10
- return {
11
- text: \`Hello\`
12
- }
13
- }
14
- } as APIRoute;
15
- ` : `import { APIRoute } from "@miqro/core";
16
-
17
- export default {
18
- handler: async (ctx) => {
19
- return {
20
- text: \`Hello\`
21
- }
22
- }
23
- } as APIRoute;
24
- `,
25
- js: (noMethod = false) => noMethod ? `module.exports = {
26
- method: "GET
27
- handler: async (ctx) => {
28
- return {
29
- text: \`Hello\`
30
- }
31
- }
32
- };
33
- ` : `module.exports = {
34
- handler: async (ctx) => {
35
- return {
36
- text: \`Hello\`
37
- }
38
- }
39
- };
40
- `
41
- };
42
- export const usage = `usage: [NODE_ENV=development] npx miqro new:api:route <identifier ex: SRC_API_V1_HEALTH>`;
43
- export const main = () => {
44
- if (process.argv.length !== 4 || process.argv[3].length < 1) {
45
- throw new Error(usage);
46
- }
47
- const identifier = process.argv[3].toLocaleLowerCase();
48
- const split = identifier.split("_").map(s => s.trim()).filter(s => s);
49
- const dots = split.filter(s => s.indexOf(".") !== -1);
50
- if (dots.length > 0) {
51
- throw new Error(`identifier cannot contain dots\n${usage}`);
52
- }
53
- loadConfig();
54
- const path = resolve(ConfigPathResolver.getBaseDirname(), ...split.splice(0, split.length - 1));
55
- const ext = existsSync(resolve(ConfigPathResolver.getBaseDirname(), "tsconfig.json")) ? "ts" : "js";
56
- const noMethod = ["post", "get", "put", "delete", "patch", "options"].indexOf(split[0].toLocaleLowerCase()) === -1;
57
- const filePath = resolve(path, `${split[0]}.${ext}`);
58
- if (existsSync(filePath)) {
59
- throw new Error(`file ${filePath} already exists! doing nothing`);
60
- }
61
- console.log(`creating ${filePath}`);
62
- mkdirSync(path, {
63
- recursive: true
64
- });
65
- writeFileSync(filePath, templates[ext](noMethod));
66
- };
@@ -1,2 +0,0 @@
1
- export declare const usage = "usage: [NODE_ENV=development] npx miqro new:api:main <identifier ex: NEW_APP>";
2
- export declare const main: () => void;
@@ -1,78 +0,0 @@
1
- import { ConfigPathResolver, loadConfig } from "@miqro/core";
2
- import { existsSync, mkdirSync, writeFileSync } from "fs";
3
- import { resolve } from "path";
4
- const mainTemplates = {
5
- ts: () => `import { APIRouter, Server, checkEnvVariables, getLogger } from "@miqro/core";
6
- import { resolve } from "path";
7
-
8
- /*
9
- To be start as a main file
10
- node file.js
11
- */
12
-
13
- const [PORT] = checkEnvVariables(["PORT"], ["8080"]);
14
-
15
- const logger = getLogger("server");
16
-
17
- async function main() {
18
- const server = new Server();
19
- server.use(await APIRouter({
20
- dirname: resolve("./build/api")
21
- }), "/api");
22
- await server.listen(PORT);
23
- server.logPaths(logger);
24
- logger.info("listening on " + PORT);
25
- }
26
-
27
- main().catch(e => logger.error(e));
28
- `,
29
- js: () => `const { APIRouter, App, checkEnvVariables, getLogger } = require("@miqro/core");
30
- const { resolve } = require("path");
31
-
32
- /*
33
- To be start as a main file
34
- node file.js
35
- */
36
-
37
- const [PORT] = checkEnvVariables(["PORT"], ["8080"]);
38
-
39
- const logger = getLogger("server");
40
-
41
- async function main() {
42
- const server = new App();
43
- server.use(await APIRouter({
44
- dirname: resolve(__dirname, "api")
45
- }), "/api");
46
- await server.listen(PORT);
47
- server.logPaths(logger);
48
- logger.info("listening on " + PORT);
49
- }
50
-
51
- main().catch(e => logger.error(e));
52
- `
53
- };
54
- export const usage = `usage: [NODE_ENV=development] npx miqro new:api:main <identifier ex: NEW_APP>`;
55
- export const main = () => {
56
- if (process.argv.length !== 4 || process.argv[3].length < 1) {
57
- throw new Error(usage);
58
- }
59
- const identifier = process.argv[3].toLocaleLowerCase();
60
- const split = identifier.split("_");
61
- const dots = split.filter(s => s.indexOf(".") !== -1);
62
- if (dots.length > 0) {
63
- throw new Error(`identifier cannot contain dots\narguments: <identifier ex: SRC_MAIN>`);
64
- }
65
- loadConfig();
66
- const path = resolve(ConfigPathResolver.getBaseDirname(), ...split.splice(0, split.length - 1));
67
- const ext = existsSync(resolve(ConfigPathResolver.getBaseDirname(), "tsconfig.json")) ? "ts" : "js";
68
- const filePath = resolve(path, `${split[0]}.${ext}`);
69
- if (existsSync(filePath)) {
70
- throw new Error(`file ${filePath} already exists! doing nothing`);
71
- }
72
- console.log(`creating ${filePath}`);
73
- mkdirSync(path, {
74
- recursive: true
75
- });
76
- writeFileSync(filePath, mainTemplates[ext]());
77
- console.log(`file ${filePath} created`);
78
- };
@@ -1 +0,0 @@
1
- export declare function main(): void;
@@ -1,5 +0,0 @@
1
- import { CMD_MAP, usage } from "../cmd-map.js";
2
- import { getUsage } from "../utils/index.js";
3
- export function main() {
4
- console.log(getUsage(CMD_MAP, usage));
5
- }
@@ -1,2 +0,0 @@
1
- export declare const usage = "usage: [NODE_ENV=development] npx miqro new:test <identifier ex: TEST_SOMEFILE>";
2
- export declare const main: () => void;
@@ -1,44 +0,0 @@
1
- import { ConfigPathResolver, loadConfig } from "@miqro/core";
2
- import { existsSync, mkdirSync, writeFileSync } from "fs";
3
- import { resolve } from "path";
4
- export const usage = `usage: [NODE_ENV=development] npx miqro new:test <identifier ex: TEST_SOMEFILE>`;
5
- const testTemplates = {
6
- js: (category) => `import { it } from "node:test";
7
- import { TestHelper } from "@miqro/test-http";
8
- import { Server, APIRouter } from "@miqro/core";
9
- import { resolve } from "path";
10
- import { strictEqual } from "assert";
11
-
12
- it("happy path health", async () => {
13
- const response = await TestHelper(new Server().use(await APIRouter({
14
- dirname: resolve("./build/api")
15
- })), {
16
- url: "/health"
17
- });
18
- strictEqual(response.status, 200);
19
- });
20
- `
21
- };
22
- export const main = () => {
23
- if (process.argv.length !== 4 || process.argv[3].length < 1) {
24
- throw new Error(usage);
25
- }
26
- const identifier = process.argv[3].toLocaleLowerCase();
27
- const split = identifier.split("_");
28
- const dots = split.filter(s => s.indexOf(".") !== -1);
29
- if (dots.length > 0) {
30
- throw new Error(`identifier cannot contain dots\narguments: <identifier ex: TEST_SOMETEST>`);
31
- }
32
- loadConfig();
33
- const path = resolve(ConfigPathResolver.getBaseDirname(), ...split.splice(0, split.length - 1));
34
- const filePath = resolve(path, `${split[0]}.test.js`);
35
- if (existsSync(filePath)) {
36
- throw new Error(`file ${filePath} already exists! doing nothing`);
37
- }
38
- console.log(`creating ${filePath}`);
39
- mkdirSync(path, {
40
- recursive: true
41
- });
42
- writeFileSync(filePath, testTemplates.js(split[0]));
43
- console.log(`file ${filePath} created`);
44
- };
@@ -1,2 +0,0 @@
1
- export declare const usageTS = "usage: npx miqro new:api <identifier ex: NEW_APP>";
2
- export declare const mainTS: () => void;