miqro 1.5.5 → 1.6.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/README.md +42 -2
- package/dist/cli.js +8 -9
- package/dist/cli.js.map +1 -0
- package/dist/cmds/config-bash.js +1 -0
- package/dist/cmds/config-bash.js.map +1 -0
- package/dist/cmds/config-env.js +1 -0
- package/dist/cmds/config-env.js.map +1 -0
- package/dist/cmds/config-init.js +2 -0
- package/dist/cmds/config-init.js.map +1 -0
- package/dist/cmds/config.js +1 -0
- package/dist/cmds/config.js.map +1 -0
- package/dist/cmds/db-console.js +2 -0
- package/dist/cmds/db-console.js.map +1 -0
- package/dist/cmds/db-createmodel.js +4 -1
- package/dist/cmds/db-createmodel.js.map +1 -0
- package/dist/cmds/db-dump-data.js +2 -0
- package/dist/cmds/db-dump-data.js.map +1 -0
- package/dist/cmds/db-init.js +2 -0
- package/dist/cmds/db-init.js.map +1 -0
- package/dist/cmds/db-makemigrations.js +3 -0
- package/dist/cmds/db-makemigrations.js.map +1 -0
- package/dist/cmds/db-push-data.js +30 -4
- package/dist/cmds/db-push-data.js.map +1 -0
- package/dist/cmds/db-sync-makemigrations.js +3 -0
- package/dist/cmds/db-sync-makemigrations.js.map +1 -0
- package/dist/cmds/doc-json.js +1 -0
- package/dist/cmds/doc-json.js.map +1 -0
- package/dist/cmds/doc-md.js +53 -16
- package/dist/cmds/doc-md.js.map +1 -0
- package/dist/cmds/handler-apiroute-new.js +2 -36
- package/dist/cmds/handler-apiroute-new.js.map +1 -0
- package/dist/cmds/handler-main-new.d.ts +1 -2
- package/dist/cmds/handler-main-new.js +15 -22
- package/dist/cmds/handler-main-new.js.map +1 -0
- package/dist/cmds/new.d.ts +1 -3
- package/dist/cmds/new.js +8 -18
- package/dist/cmds/new.js.map +1 -0
- package/dist/cmds/start.js +4 -0
- package/dist/cmds/start.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/utils/db/automigrations/deep-diff/index.d.ts +24 -0
- package/dist/utils/db/automigrations/deep-diff/index.js +240 -0
- package/dist/utils/db/automigrations/deep-diff/index.js.map +1 -0
- package/dist/utils/db/automigrations/index.js +17 -0
- package/dist/utils/db/automigrations/index.js.map +1 -0
- package/dist/utils/db/automigrations/migrate.js +136 -17
- package/dist/utils/db/automigrations/migrate.js.map +1 -0
- package/dist/utils/db/index.d.ts +3 -3
- package/dist/utils/db/index.js +11 -1
- package/dist/utils/db/index.js.map +1 -0
- package/dist/utils/doc/index.d.ts +10 -8
- package/dist/utils/doc/index.js +4 -1
- package/dist/utils/doc/index.js.map +1 -0
- package/dist/utils/index.d.ts +2 -1
- package/dist/utils/index.js +2 -1
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/templates.d.ts +2 -2
- package/dist/utils/templates.js +6 -0
- package/dist/utils/templates.js.map +1 -0
- package/package.json +9 -15
- package/src/cli.ts +62 -0
- package/src/cmds/config-bash.ts +18 -0
- package/src/cmds/config-env.ts +18 -0
- package/src/cmds/config-init.ts +35 -0
- package/src/cmds/config.ts +16 -0
- package/src/cmds/db-console.ts +35 -0
- package/src/cmds/db-createmodel.ts +44 -0
- package/src/cmds/db-dump-data.ts +44 -0
- package/src/cmds/db-init.ts +42 -0
- package/src/cmds/db-makemigrations.ts +12 -0
- package/src/cmds/db-push-data.ts +77 -0
- package/src/cmds/db-sync-makemigrations.ts +12 -0
- package/src/cmds/doc-json.ts +16 -0
- package/src/cmds/doc-md.ts +233 -0
- package/src/cmds/handler-apiroute-new.ts +67 -0
- package/src/cmds/handler-main-new.ts +85 -0
- package/src/cmds/new.ts +128 -0
- package/src/cmds/start.ts +16 -0
- package/src/index.ts +2 -0
- package/src/utils/db/automigrations/deep-diff/index.ts +264 -0
- package/src/utils/db/automigrations/index.ts +151 -0
- package/src/utils/db/automigrations/migrate.ts +888 -0
- package/src/utils/db/index.ts +128 -0
- package/src/utils/doc/index.ts +31 -0
- package/src/utils/index.ts +9 -0
- package/src/utils/templates.ts +189 -0
- package/tsconfig.json +32 -0
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
import { ConfigPathResolver, getLogger, GroupPolicy, loadConfig, Method, ParseOption, ParseOptionMap } from "@miqro/core";
|
|
2
|
+
import { resolve } from "path";
|
|
3
|
+
import { writeFileSync } from "fs";
|
|
4
|
+
|
|
5
|
+
import { getDOCJSON } from "../utils/doc";
|
|
6
|
+
|
|
7
|
+
export const main = (): void => {
|
|
8
|
+
|
|
9
|
+
if (process.argv.length !== 6) {
|
|
10
|
+
throw new Error(`arguments: <api_folder> <subPath> <out.md>`);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const dirname = process.argv[3];
|
|
14
|
+
const subPath = process.argv[4];
|
|
15
|
+
const outPath = process.argv[5];
|
|
16
|
+
|
|
17
|
+
loadConfig();
|
|
18
|
+
|
|
19
|
+
const parseOptionMap2ParseOptionList = (map: ParseOptionMap): ParseOption[] => {
|
|
20
|
+
return Object.keys(map).map(name => {
|
|
21
|
+
const val = map[name];
|
|
22
|
+
return typeof val !== "object" ? {
|
|
23
|
+
name,
|
|
24
|
+
required: true,
|
|
25
|
+
type: val
|
|
26
|
+
} : val.required === undefined ? {
|
|
27
|
+
...val,
|
|
28
|
+
required: true,
|
|
29
|
+
name
|
|
30
|
+
} : {
|
|
31
|
+
...val,
|
|
32
|
+
name
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const docJSON = getDOCJSON({ dirname, subPath }, getLogger("miqro"));
|
|
38
|
+
|
|
39
|
+
const policyTable = (policy: GroupPolicy | undefined | false): string => {
|
|
40
|
+
if (policy) {
|
|
41
|
+
return `|policy|groups|\n|----|----|\n|${policy.groupPolicy}|${policy.groups.join(", ")}|`;
|
|
42
|
+
} else {
|
|
43
|
+
return "```no policy provided!!```";
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const methodUrlTable = (options?: { path?: string | string[]; method?: Method | Method[] }): string => {
|
|
49
|
+
if (options && options.path) {
|
|
50
|
+
const rows: string[] = [];
|
|
51
|
+
const paths = (options.path as any) instanceof Array ? options.path : [options.path];
|
|
52
|
+
for (const p of paths) {
|
|
53
|
+
if (options.method instanceof Array) {
|
|
54
|
+
for (const m of options.method) {
|
|
55
|
+
rows.push(`|${m}|${p}|`);
|
|
56
|
+
}
|
|
57
|
+
} else {
|
|
58
|
+
rows.push(`|${options.method}|${p}|`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return `|method|path|\n|----|----|\n${rows.join("\n")}`;
|
|
62
|
+
} else {
|
|
63
|
+
return "";
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const getRange = (o: ParseOption, t: string): string => {
|
|
68
|
+
let range = " ";
|
|
69
|
+
switch (t) {
|
|
70
|
+
case "string":
|
|
71
|
+
if (o.stringMinLength !== undefined || o.stringMaxLength !== undefined) {
|
|
72
|
+
range = "";
|
|
73
|
+
}
|
|
74
|
+
if (o.stringMinLength !== undefined) {
|
|
75
|
+
range = `${o.stringMinLength}:`;
|
|
76
|
+
}
|
|
77
|
+
if (o.stringMaxLength !== undefined) {
|
|
78
|
+
range += `:${o.stringMaxLength}`;
|
|
79
|
+
}
|
|
80
|
+
break;
|
|
81
|
+
case "number":
|
|
82
|
+
if (o.numberMin !== undefined || o.numberMax !== undefined) {
|
|
83
|
+
range = "";
|
|
84
|
+
}
|
|
85
|
+
if (o.numberMin !== undefined) {
|
|
86
|
+
range = `${o.numberMin}:`;
|
|
87
|
+
}
|
|
88
|
+
if (o.numberMax !== undefined) {
|
|
89
|
+
range += `:${o.numberMax}`;
|
|
90
|
+
}
|
|
91
|
+
if (o.numberMinDecimals !== undefined) {
|
|
92
|
+
range = `${range ? " " : ""}decimal ${o.numberMinDecimals}:`;
|
|
93
|
+
}
|
|
94
|
+
if (o.numberMaxDecimals !== undefined) {
|
|
95
|
+
range += `${range ? " " : ""}${o.numberMinDecimals !== undefined ? "" : "decimal "}:${o.numberMaxDecimals}`;
|
|
96
|
+
}
|
|
97
|
+
break;
|
|
98
|
+
case "array":
|
|
99
|
+
if (o.arrayMinLength !== undefined || o.arrayMaxLength !== undefined) {
|
|
100
|
+
range = "";
|
|
101
|
+
}
|
|
102
|
+
if (o.arrayMinLength !== undefined) {
|
|
103
|
+
range = `${o.arrayMinLength}:`;
|
|
104
|
+
}
|
|
105
|
+
if (o.arrayMaxLength !== undefined) {
|
|
106
|
+
range += `:${o.arrayMaxLength}`;
|
|
107
|
+
}
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
return range;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const parseOptionTable = (options: { options: ParseOption[] | ParseOptionMap; } | false | undefined, subName = "", tableHeaders = 0): string => {
|
|
114
|
+
if (options) {
|
|
115
|
+
let padding = "";
|
|
116
|
+
for (let i = 0; i < tableHeaders; i++) {
|
|
117
|
+
padding += "| ";
|
|
118
|
+
}
|
|
119
|
+
const hl = "|----|----|----|----|----|----|----|----|----|----|";
|
|
120
|
+
const list: ParseOption[] = options.options instanceof Array ? options.options : parseOptionMap2ParseOptionList(options.options);
|
|
121
|
+
const headers = `|**name**|**description**|**type**|**arrayRange**|**arrayType**|**range**|**values**|**defaultValue**|**required**|**allowNull**|`;
|
|
122
|
+
return `${tableHeaders >= 0 ? `${padding}${headers}\n${tableHeaders === 0 ? `${padding}${hl}\n` : ""}` : ""}${list.map(o => {
|
|
123
|
+
const arrayRange = o.type === "array" ? getRange(o, o.type) : " ";
|
|
124
|
+
const range = o.type === "array" ? (o.arrayType ? getRange(o, o.arrayType) : " ") : getRange(o, o.type);
|
|
125
|
+
let out = `${padding}|${subName}${o.name}|${o.description ? o.description : " "}|${o.type}|${arrayRange}|${o.arrayType ? o.arrayType : " "}|` +
|
|
126
|
+
`${range}|` +
|
|
127
|
+
`${o.enumValues ? o.enumValues.join(", ") : " "}|${o.defaultValue !== undefined ? o.defaultValue : " "}|${o.required === undefined ? true : o.required}|${o.allowNull ? "true" : "false"}|`;
|
|
128
|
+
if (o.type === "multiple" || o.arrayType === "multiple") {
|
|
129
|
+
out += `\n${(o.multipleOptions ? o.multipleOptions : []).map(oM => {
|
|
130
|
+
return parseOptionTable({
|
|
131
|
+
options: [{
|
|
132
|
+
name: o.name,
|
|
133
|
+
...oM
|
|
134
|
+
}]
|
|
135
|
+
}, "", tableHeaders ? tableHeaders + 1 : 1)
|
|
136
|
+
})}`;
|
|
137
|
+
return out;
|
|
138
|
+
} else if (o.type === "nested" || o.arrayType === "nested") {
|
|
139
|
+
out += `\n${parseOptionTable({ options: o.nestedOptions ? o.nestedOptions.options : [] }, `${subName}${o.name}${o.type === "array" ? "[..]" : ""}.`, tableHeaders ? tableHeaders + 1 : 1)}`;
|
|
140
|
+
return out;
|
|
141
|
+
} else {
|
|
142
|
+
return out;
|
|
143
|
+
}
|
|
144
|
+
}).join("\n")}`;
|
|
145
|
+
} else if (options === false) {
|
|
146
|
+
return "";
|
|
147
|
+
} else {
|
|
148
|
+
return "not defined";
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const FAKE_TAB = " ";
|
|
153
|
+
const FAKE_DOUBLE_TAB = `${FAKE_TAB}${FAKE_TAB}`;
|
|
154
|
+
|
|
155
|
+
const featureIndex = `## Features\n\n${docJSON.map(doc => {
|
|
156
|
+
return `- [${doc.featureName}](#${doc.featureName.toLowerCase()})\n\n${doc.description ? `${FAKE_TAB}${doc.description}\n\n` : ""}${FAKE_DOUBLE_TAB}${doc.path}`;
|
|
157
|
+
}).join("\n\n")}`;
|
|
158
|
+
|
|
159
|
+
writeFileSync(resolve(ConfigPathResolver.getBaseDirname(), outPath), `${featureIndex}\n\n` + docJSON.map(doc => {
|
|
160
|
+
const param = doc.params instanceof Array ? doc.params : [doc.params];
|
|
161
|
+
const paramsTable = [];
|
|
162
|
+
for (const q of param) {
|
|
163
|
+
let paramTable = parseOptionTable(q);
|
|
164
|
+
// let paramsTable = parseOptionTable(doc.params);
|
|
165
|
+
if (paramTable.split("\n").length > 1) {
|
|
166
|
+
paramTable = `#### params${q && q.description ? ` (${q.description})` : ""}\n\n${paramTable}`;
|
|
167
|
+
} else {
|
|
168
|
+
paramTable = paramTable === "" ? "" : `#### params${q && q.description ? ` (${q.description})` : ""}: ${paramTable}`;
|
|
169
|
+
}
|
|
170
|
+
paramsTable.push(paramTable);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const query = doc.query instanceof Array ? doc.query : [doc.query];
|
|
174
|
+
const queryTables = [];
|
|
175
|
+
for (const q of query) {
|
|
176
|
+
let queryTable = parseOptionTable(q);
|
|
177
|
+
// let paramsTable = parseOptionTable(doc.params);
|
|
178
|
+
if (queryTable.split("\n").length > 1) {
|
|
179
|
+
queryTable = `#### query${q && q.description ? ` (${q.description})` : ""}\n\n${queryTable}`;
|
|
180
|
+
} else {
|
|
181
|
+
queryTable = queryTable === "" ? "" : `#### query${q && q.description ? ` (${q.description})` : ""}: ${queryTable}`;
|
|
182
|
+
}
|
|
183
|
+
queryTables.push(queryTable);
|
|
184
|
+
}
|
|
185
|
+
/*if (paramsTable.split("\n").length > 1) {
|
|
186
|
+
paramsTable = `- params\n\n${paramsTable}`;
|
|
187
|
+
} else {
|
|
188
|
+
paramsTable = paramsTable === "" ? "" : `- params: ${paramsTable}`;
|
|
189
|
+
}*/
|
|
190
|
+
|
|
191
|
+
const body = doc.body instanceof Array ? doc.body : [doc.body];
|
|
192
|
+
const bodyTables = [];
|
|
193
|
+
for (const b of body) {
|
|
194
|
+
let bodyTable = parseOptionTable(b);
|
|
195
|
+
|
|
196
|
+
if (bodyTable.split("\n").length > 1) {
|
|
197
|
+
bodyTable = `#### body${b && b.description ? ` (${b.description})` : ""}\n\n${bodyTable}`;
|
|
198
|
+
} else {
|
|
199
|
+
bodyTable = bodyTable === "" ? "" : `#### body${b && b.description ? ` (${b.description})` : ""}: ${bodyTable}`;
|
|
200
|
+
}
|
|
201
|
+
bodyTables.push(bodyTable);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
const results = doc.result instanceof Array ? doc.result : [doc.result];
|
|
205
|
+
const resultTables = [];
|
|
206
|
+
for (const r of results) {
|
|
207
|
+
if (!r) {
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
let resultsTable = doc.result ? parseOptionTable(r) : "";
|
|
211
|
+
if (resultsTable.split("\n").length > 1) {
|
|
212
|
+
resultsTable = `#### response.data${r.description ? ` (${r.description})` : ""}\n\n${resultsTable}`;
|
|
213
|
+
} else {
|
|
214
|
+
resultsTable = resultsTable === "" ? "" : `#### response.data${r.description ? ` (${r.description})` : ""}: ${resultsTable}`;
|
|
215
|
+
}
|
|
216
|
+
resultTables.push(resultsTable);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
const pTable = policyTable(doc.policy);
|
|
220
|
+
|
|
221
|
+
return `### ${doc.featureName}\n\n` +
|
|
222
|
+
`${doc.description ? `${doc.description}\n\n` : ""}` +
|
|
223
|
+
`${pTable ? `${pTable}\n\n` : ""}` +
|
|
224
|
+
`#### endpoint\n\n` +
|
|
225
|
+
`${methodUrlTable(doc)}\n\n` +
|
|
226
|
+
// `${paramsTable ? `${paramsTable}\n\n` : ""}` +
|
|
227
|
+
`${paramsTable.length > 0 ? `${paramsTable.join("\n\n")}\n\n` : ""}` +
|
|
228
|
+
`${queryTables.length > 0 ? `${queryTables.join("\n\n")}\n\n` : ""}` +
|
|
229
|
+
`${bodyTables.length > 0 ? `${bodyTables.join("\n\n")}\n\n` : ""}` +
|
|
230
|
+
`${resultTables.length > 0 ? `${resultTables.join("\n\n")}\n\n` : ""}`;
|
|
231
|
+
}).join("\n\n"));
|
|
232
|
+
|
|
233
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { ConfigPathResolver, loadConfig } from "@miqro/core";
|
|
2
|
+
import { mkdirSync, writeFileSync, existsSync } from "fs";
|
|
3
|
+
import { resolve } from "path";
|
|
4
|
+
|
|
5
|
+
const templates = {
|
|
6
|
+
ts: (path: string) =>
|
|
7
|
+
`import { APIRoute, Context } from "@miqro/core";
|
|
8
|
+
|
|
9
|
+
const route: APIRoute = {
|
|
10
|
+
path: "${path}",
|
|
11
|
+
method: "POST",
|
|
12
|
+
handler: async (ctx: Context) => {
|
|
13
|
+
return {
|
|
14
|
+
text: \`Hello \${ctx.body.name}!\`
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export default route;
|
|
20
|
+
`,
|
|
21
|
+
js: (path: string) =>
|
|
22
|
+
`module.exports = {
|
|
23
|
+
path: "${path}",
|
|
24
|
+
method: "POST",
|
|
25
|
+
handler: async (ctx) => {
|
|
26
|
+
return {
|
|
27
|
+
text: \`Hello \${ctx.body.name}!\`
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
`
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const main = (): void => {
|
|
35
|
+
|
|
36
|
+
if (process.argv.length !== 4 || process.argv[3].length < 1) {
|
|
37
|
+
throw new Error(`arguments: <identifier ex: SRC_API_V1_HEALTH>`);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const identifier = process.argv[3].toLocaleLowerCase();
|
|
41
|
+
|
|
42
|
+
const split = identifier.split("_");
|
|
43
|
+
|
|
44
|
+
const dots = split.filter(s => s.indexOf(".") !== -1);
|
|
45
|
+
if (dots.length > 0) {
|
|
46
|
+
throw new Error(`identifier cannot contain dots\narguments: <identifier ex: SRC_API_V1_HEALTH>`);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
loadConfig();
|
|
50
|
+
|
|
51
|
+
const path = resolve(ConfigPathResolver.getBaseDirname(), ...split.splice(0, split.length - 1));
|
|
52
|
+
|
|
53
|
+
const ext = existsSync(resolve(ConfigPathResolver.getBaseDirname(), "tsconfig.json")) ? "ts" : "js";
|
|
54
|
+
|
|
55
|
+
const filePath = resolve(path, `${split[0]}.${ext}`);
|
|
56
|
+
if (existsSync(filePath)) {
|
|
57
|
+
throw new Error(`file ${filePath} already exists! doing nothing`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
console.log(`creating ${filePath}`);
|
|
61
|
+
|
|
62
|
+
mkdirSync(path, {
|
|
63
|
+
recursive: true
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
writeFileSync(filePath, templates[ext](`/${split[0]}`));
|
|
67
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { ConfigPathResolver, loadConfig } from "@miqro/core";
|
|
2
|
+
import { mkdirSync, writeFileSync, existsSync } from "fs";
|
|
3
|
+
import { resolve } from "path";
|
|
4
|
+
|
|
5
|
+
const mainTemplates = {
|
|
6
|
+
ts: () =>
|
|
7
|
+
`import { APIRouter, App, checkEnvVariables, getLogger, middleware } from "@miqro/core";
|
|
8
|
+
import { resolve } from "path";
|
|
9
|
+
|
|
10
|
+
/*
|
|
11
|
+
To be start as a main file
|
|
12
|
+
node file.js
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const [PORT] = checkEnvVariables(["PORT"], ["8080"]);
|
|
16
|
+
|
|
17
|
+
const logger = getLogger("server");
|
|
18
|
+
|
|
19
|
+
const app = new App();
|
|
20
|
+
app.use(middleware());
|
|
21
|
+
app.use(APIRouter({
|
|
22
|
+
dirname: resolve(__dirname, "api")
|
|
23
|
+
}, logger));
|
|
24
|
+
app.listen(PORT, () => {
|
|
25
|
+
logger.info("listening on " + PORT);
|
|
26
|
+
});
|
|
27
|
+
`,
|
|
28
|
+
js: () =>
|
|
29
|
+
`const { APIRouter, App, checkEnvVariables, getLogger, middleware } = 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
|
+
const app = new App();
|
|
42
|
+
app.use(middleware());
|
|
43
|
+
app.use(APIRouter({
|
|
44
|
+
dirname: resolve(__dirname, "api")
|
|
45
|
+
}, logger));
|
|
46
|
+
app.listen(PORT, () => {
|
|
47
|
+
logger.info("listening on " + PORT);
|
|
48
|
+
});
|
|
49
|
+
`
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const main = (): void => {
|
|
53
|
+
|
|
54
|
+
if (process.argv.length !== 4 || process.argv[3].length < 1) {
|
|
55
|
+
throw new Error(`arguments: <identifier ex: SRC_MAIN>`);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const identifier = process.argv[3].toLocaleLowerCase();
|
|
59
|
+
|
|
60
|
+
const split = identifier.split("_");
|
|
61
|
+
|
|
62
|
+
const dots = split.filter(s => s.indexOf(".") !== -1);
|
|
63
|
+
if (dots.length > 0) {
|
|
64
|
+
throw new Error(`identifier cannot contain dots\narguments: <identifier ex: SRC_MAIN>`);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
loadConfig();
|
|
68
|
+
|
|
69
|
+
const path = resolve(ConfigPathResolver.getBaseDirname(), ...split.splice(0, split.length - 1));
|
|
70
|
+
|
|
71
|
+
const ext = existsSync(resolve(ConfigPathResolver.getBaseDirname(), "tsconfig.json")) ? "ts" : "js";
|
|
72
|
+
|
|
73
|
+
const filePath = resolve(path, `${split[0]}.${ext}`);
|
|
74
|
+
if (existsSync(filePath)) {
|
|
75
|
+
throw new Error(`file ${filePath} already exists! doing nothing`);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
console.log(`creating ${filePath}`);
|
|
79
|
+
|
|
80
|
+
mkdirSync(path, {
|
|
81
|
+
recursive: true
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
writeFileSync(filePath, mainTemplates[ext]());
|
|
85
|
+
}
|
package/src/cmds/new.ts
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { mkdirSync, writeFileSync, existsSync } from "fs";
|
|
2
|
+
import { resolve } from "path";
|
|
3
|
+
import { execSync } from "../utils";
|
|
4
|
+
|
|
5
|
+
const packageTemplate = {
|
|
6
|
+
ts: (name: string) =>
|
|
7
|
+
`{
|
|
8
|
+
"name": "${name}",
|
|
9
|
+
"version": "1.0.0",
|
|
10
|
+
"description": "",
|
|
11
|
+
"private": true,
|
|
12
|
+
"main": "dist/main.js",
|
|
13
|
+
"scripts": {
|
|
14
|
+
"build": "tsc",
|
|
15
|
+
"start": "node dist/main.js"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
},
|
|
21
|
+
"author": "",
|
|
22
|
+
"license": "ISC"
|
|
23
|
+
}`,
|
|
24
|
+
js: (name: string) =>
|
|
25
|
+
`{
|
|
26
|
+
"name": "${name}",
|
|
27
|
+
"version": "1.0.0",
|
|
28
|
+
"description": "",
|
|
29
|
+
"private": true,
|
|
30
|
+
"main": "src/main.js",
|
|
31
|
+
"scripts": {
|
|
32
|
+
"start": "node src/main.js"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
},
|
|
38
|
+
"author": "",
|
|
39
|
+
"license": "ISC"
|
|
40
|
+
}`
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export const mainJS = (typescript = false): void => {
|
|
44
|
+
if (process.argv.length !== 4 || process.argv[3].length < 1) {
|
|
45
|
+
throw new Error(`arguments: <name ex: NEW_APP>`);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const identifier = process.argv[3].toLocaleLowerCase();
|
|
49
|
+
|
|
50
|
+
const appFolder = resolve(process.cwd(), identifier);
|
|
51
|
+
|
|
52
|
+
if (existsSync(appFolder)) {
|
|
53
|
+
throw new Error(`${appFolder} already exists!`);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
console.log(`creating ${appFolder}`);
|
|
57
|
+
|
|
58
|
+
mkdirSync(appFolder, {
|
|
59
|
+
recursive: true
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
writeFileSync(resolve(appFolder, "package.json"), packageTemplate[typescript ? "ts" : "js"](identifier));
|
|
63
|
+
|
|
64
|
+
execSync(
|
|
65
|
+
`npm install miqro --save-dev`,
|
|
66
|
+
{
|
|
67
|
+
cwd: appFolder
|
|
68
|
+
}
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
execSync(
|
|
72
|
+
`npm install @miqro/core --save`,
|
|
73
|
+
{
|
|
74
|
+
cwd: appFolder
|
|
75
|
+
}
|
|
76
|
+
);
|
|
77
|
+
|
|
78
|
+
if (typescript) {
|
|
79
|
+
writeFileSync(resolve(appFolder, "tsconfig.json"), `{
|
|
80
|
+
"compileOnSave": true,
|
|
81
|
+
"compilerOptions": {
|
|
82
|
+
"strict": false,
|
|
83
|
+
"outDir": "./dist/",
|
|
84
|
+
"removeComments": true,
|
|
85
|
+
"noImplicitAny": false,
|
|
86
|
+
"preserveConstEnums": true,
|
|
87
|
+
"sourceMap": true,
|
|
88
|
+
"esModuleInterop": true,
|
|
89
|
+
"declaration": true,
|
|
90
|
+
"moduleResolution": "node",
|
|
91
|
+
"module": "commonjs",
|
|
92
|
+
"target": "es2017",
|
|
93
|
+
"lib": [
|
|
94
|
+
"es2017"
|
|
95
|
+
]
|
|
96
|
+
},
|
|
97
|
+
"exclude": [
|
|
98
|
+
"node_modules",
|
|
99
|
+
"test"
|
|
100
|
+
],
|
|
101
|
+
"include": [
|
|
102
|
+
"src"
|
|
103
|
+
]
|
|
104
|
+
}`);
|
|
105
|
+
execSync(`npm install typescript --save-dev`, {
|
|
106
|
+
cwd: appFolder
|
|
107
|
+
});
|
|
108
|
+
execSync(`npm install @types/node --save-dev`, {
|
|
109
|
+
cwd: appFolder
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
execSync(
|
|
114
|
+
`npx miqro new:main src_main`,
|
|
115
|
+
{
|
|
116
|
+
cwd: appFolder
|
|
117
|
+
}
|
|
118
|
+
);
|
|
119
|
+
|
|
120
|
+
execSync(
|
|
121
|
+
`npx miqro new:route src_api_health`,
|
|
122
|
+
{
|
|
123
|
+
cwd: appFolder
|
|
124
|
+
}
|
|
125
|
+
);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export const mainTS = (): void => mainJS(true);
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { execSync } from "../utils";
|
|
2
|
+
import { mainPath } from "@miqro/runner";
|
|
3
|
+
import { loadConfig } from "@miqro/core";
|
|
4
|
+
|
|
5
|
+
export const main = (): void => {
|
|
6
|
+
|
|
7
|
+
if (process.argv.length <= 3) {
|
|
8
|
+
throw new Error(`invalid number of args\nusage: CLUSTER_COUNT=1 [DISABLE_RESTART=true|false] npx miqro start <script> [...args]`);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
loadConfig();
|
|
12
|
+
|
|
13
|
+
//execSync(`npx @miqro/runner ${process.argv.slice(3).join(" ")}`);
|
|
14
|
+
execSync(`${process.argv[0]} ${mainPath()} ${process.argv.slice(3).join(" ")}`)
|
|
15
|
+
}
|
|
16
|
+
|
package/src/index.ts
ADDED