milkio 0.2.8 → 0.2.9

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/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "milkio",
3
3
  "type": "module",
4
4
  "module": "index.ts",
5
- "version": "0.2.8",
5
+ "version": "0.2.9",
6
6
  "peerDependencies": {
7
7
  "typescript": "^5.4.2"
8
8
  },
@@ -1,8 +1,8 @@
1
1
  import ejs from "ejs";
2
2
  import { join, dirname } from "node:path";
3
- import { existsSync, mkdirSync } from "node:fs";
3
+ import { mkdirSync } from "node:fs";
4
4
  import { cwd, exit } from "node:process";
5
- import { unlink, writeFile } from "node:fs/promises";
5
+ import { unlink, writeFile, readFile, exists } from "node:fs/promises";
6
6
  import { camel, hyphen } from "@poech/camel-hump-under";
7
7
  import { $, Glob } from "bun";
8
8
  import type { MilkioConfig } from "..";
@@ -14,19 +14,19 @@ export default async () => {
14
14
  } catch (error) {} // Maybe the file does not exist
15
15
 
16
16
  // Make sure that the existing directories are all present
17
- existsSync(join("generated")) || mkdirSync(join("generated"));
18
- existsSync(join("generated", "raw")) || mkdirSync(join("generated", "raw"));
19
- existsSync(join("generated", "raw", "apps")) || mkdirSync(join("generated", "raw", "apps"));
17
+ (await exists(join("generated"))) || mkdirSync(join("generated"));
18
+ (await exists(join("generated", "raw"))) || mkdirSync(join("generated", "raw"));
19
+ (await exists(join("generated", "raw", "apps"))) || mkdirSync(join("generated", "raw", "apps"));
20
20
 
21
- if (!existsSync(join(cwd(), "milkio.toml"))) return;
21
+ if (!(await exists(join(cwd(), "milkio.toml")))) return;
22
22
  const milkioConfig = (await import(join(cwd(), "milkio.toml"))).default as MilkioConfig;
23
23
 
24
24
  for (const key in milkioConfig.exists) {
25
25
  const file = milkioConfig.exists[key];
26
- if (!existsSync(join(cwd(), file.path))) await writeFile(join(cwd(), file.path), file.content ?? "");
26
+ if (!(await exists(join(cwd(), file.path)))) await writeFile(join(cwd(), file.path), file.content ?? "");
27
27
  }
28
28
 
29
- if (!existsSync(join("generated", "README.md"))) {
29
+ if (!(await exists(join("generated", "README.md")))) {
30
30
  await writeFile(join("generated", "README.md"), "⚠️ All files in this directory are generated by milkio. Please do not modify the content, otherwise your modifications will be overwritten in the next generation.");
31
31
  }
32
32
 
@@ -57,43 +57,47 @@ export default async () => {
57
57
 
58
58
  console.time(`File Stage`);
59
59
 
60
+ const results: Array<Promise<void>> = [];
60
61
  for (const pathRaw of appFiles) {
61
62
  if (!pathRaw.endsWith(".ts")) continue;
62
- const path = pathRaw.replaceAll("\\", "/");
63
- const module = await import(/* @vite-ignore */ join(cwd(), "src", "apps", path));
64
-
65
- if (module?.api?.isApi === true) {
66
- // Exclude disallowed characters
67
- if (path.includes("_")) {
68
- console.error(`\n\nPath: "${path.slice(0, -3)}"`);
69
- console.error(`Do not use "_" in the path. If you want to add a separator between words, please use "-".\n`);
70
- exit(1);
71
- }
72
- if (!/^[a-z0-9/$/-]+$/.test(path.slice(0, -3))) {
73
- console.error(`\n\nPath: "${path.slice(0, -3)}"`);
74
- console.error(`The path can only contain lowercase letters, numbers, and "-".\n`);
75
- exit(1);
76
- }
77
-
78
- templateVars.apiPaths.push(path);
79
-
80
- if (module?.test?.isApiTest === true) {
81
- templateVars.apiTestPaths.push(path);
82
- }
83
-
84
- // typia
85
- const filePath = join(cwd(), "generated", "raw", "apps", path);
86
- const dirPath = join(cwd(), "generated", "raw", "apps", path).split("/").slice(0, -1).join("/");
87
- if (!existsSync(dirPath)) {
88
- mkdirSync(dirPath, { recursive: true });
89
- }
90
- let importPath = "../../../";
91
-
92
- for (let i = 0; i < path.split("/").length - 1; i++) {
93
- importPath = `${importPath}../`;
94
- }
95
- importPath = `${importPath}src/apps`;
96
- const template = `
63
+ results.push(
64
+ (async () => {
65
+ const path = pathRaw.replaceAll("\\", "/");
66
+ // const module = await import(/* @vite-ignore */ join(cwd(), "src", "apps", path));
67
+ const moduleCode = await readFile(pathRaw, "utf-8");
68
+
69
+ if (/\nexport const api( )*\=( )*defineApi\(\{/.test(moduleCode)) {
70
+ // Exclude disallowed characters
71
+ if (path.includes("_")) {
72
+ console.error(`\n\nPath: "${path.slice(0, -3)}"`);
73
+ console.error(`Do not use "_" in the path. If you want to add a separator between words, please use "-".\n`);
74
+ exit(1);
75
+ }
76
+ if (!/^[a-z0-9/$/-]+$/.test(path.slice(0, -3))) {
77
+ console.error(`\n\nPath: "${path.slice(0, -3)}"`);
78
+ console.error(`The path can only contain lowercase letters, numbers, and "-".\n`);
79
+ exit(1);
80
+ }
81
+
82
+ templateVars.apiPaths.push(path);
83
+
84
+ if (/\nexport const test( )*=( )*defineApiTest\(api\,/.test(moduleCode)) {
85
+ templateVars.apiTestPaths.push(path);
86
+ }
87
+
88
+ // typia
89
+ const filePath = join(cwd(), "generated", "raw", "apps", path);
90
+ const dirPath = join(cwd(), "generated", "raw", "apps", path).split("/").slice(0, -1).join("/");
91
+ if (!(await exists(dirPath))) {
92
+ mkdirSync(dirPath, { recursive: true });
93
+ }
94
+ let importPath = "../../../";
95
+
96
+ for (let i = 0; i < path.split("/").length - 1; i++) {
97
+ importPath = `${importPath}../`;
98
+ }
99
+ importPath = `${importPath}src/apps`;
100
+ const template = `
97
101
  import typia from "typia";
98
102
  import { _validate, type ExecuteResultFail, type ExecuteResultSuccess } from "milkio";
99
103
  import { type TSONEncode } from "@southern-aurora/tson";
@@ -105,12 +109,15 @@ type ResultsT = Awaited<ReturnType<typeof <%= utils.camel(path.replaceAll('/', '
105
109
  export const validateResults = async (results: any) => { _validate(typia.validate<TSONEncode<ExecuteResultSuccess<ResultsT> | ExecuteResultFail>>(results)); return typia.json.stringify<TSONEncode<ExecuteResultSuccess<ResultsT> | ExecuteResultFail>>(results); };
106
110
  export const randParams = async () => typia.random<ParamsT>();
107
111
  `.trim();
108
- // export const paramsSchema = typia.json.application<[{ data: ParamsT }], "swagger">();
112
+ // export const paramsSchema = typia.json.application<[{ data: ParamsT }], "swagger">();
109
113
 
110
- if (!existsSync(dirname(filePath))) mkdirSync(dirname(filePath), { recursive: true });
111
- await writeFile(filePath, ejs.render(template, { ...templateVars, path }));
112
- }
114
+ if (!(await exists(dirname(filePath)))) mkdirSync(dirname(filePath), { recursive: true });
115
+ await writeFile(filePath, ejs.render(template, { ...templateVars, path }));
116
+ }
117
+ })(),
118
+ );
113
119
  }
120
+ await Promise.all(results);
114
121
 
115
122
  await writeFile(
116
123
  join(cwd(), "generated", "api-schema.ts"),