@biuxiu/codegen 0.3.2 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -2,7 +2,7 @@ import { ApiServiceField, DtoField, EntityField } from '../types';
2
2
  export declare class RenderNest {
3
3
  #private;
4
4
  constructor(name: string, path?: string);
5
- remove(): Promise<void>;
5
+ remove(): void;
6
6
  editAppModule(isDelete?: boolean): Promise<void>;
7
7
  genModule(): Promise<void>;
8
8
  genApiService(apiService: ApiServiceField[]): Promise<void>;
package/dist/code/nest.js CHANGED
@@ -1,14 +1,13 @@
1
1
  import xiu from "../render";
2
2
  import { join } from "path";
3
- import { writeFormatFile } from "../util";
4
- import { existsSync, mkdirSync, statSync } from "fs";
3
+ import { deleteDir, writeFormatFile } from "../util";
4
+ import { existsSync, mkdirSync } from "fs";
5
5
  import { DTOFromat } from "../format/dto";
6
6
  import { EntityFormat } from "../format/entity";
7
7
  import { ApiFormat } from "../format/api";
8
8
  import { ServiceFormat } from "../format/service";
9
9
  import { getApiDir } from "../config";
10
10
  import { Project, SyntaxKind } from "ts-morph";
11
- import { readdir, rmdir, unlink } from "fs/promises";
12
11
  const editFileQueue = [];
13
12
  let isRunning = false;
14
13
  const runEditTask = async () => {
@@ -34,21 +33,9 @@ class RenderNest {
34
33
  this.#name = name;
35
34
  this.#moduleDir = join(apiDir, this.#path);
36
35
  }
37
- async #deleteDir(path) {
38
- const files = await readdir(path);
39
- for (let i = 0; i < files.length; i++) {
40
- const filePath = join(path, files[i]);
41
- if (statSync(filePath).isDirectory()) {
42
- await this.#deleteDir(filePath);
43
- } else {
44
- await unlink(filePath);
45
- }
46
- }
47
- await rmdir(path);
48
- }
49
- async remove() {
36
+ remove() {
50
37
  if (existsSync(this.#moduleDir)) {
51
- await this.#deleteDir(this.#moduleDir);
38
+ deleteDir(this.#moduleDir);
52
39
  }
53
40
  }
54
41
  async editAppModule(isDelete = false) {
@@ -2,8 +2,7 @@ import { ApiServiceField, FieldSchema, ModuleConfig, ModuleRoute } from '../type
2
2
  export declare class RenderVue {
3
3
  #private;
4
4
  constructor(name: string, path?: string);
5
- editDir(isDelete?: boolean): Promise<void>;
6
- setDatabase(route: ModuleRoute, isDelete?: boolean): Promise<undefined>;
5
+ editDir(isDelete?: boolean): void;
7
6
  genRoute(route: ModuleRoute): Promise<void>;
8
7
  genVueTable({ name, comment }: ModuleConfig, fields: FieldSchema[]): Promise<void>;
9
8
  genVueForm(config: ModuleConfig, fields: FieldSchema[]): Promise<void>;
package/dist/code/vue.js CHANGED
@@ -1,10 +1,8 @@
1
- import { getSqliteFile, getWebDir } from "../config";
1
+ import { getWebDir } from "../config";
2
2
  import xiu from "../render";
3
- import { writeFormatFile } from "../util";
4
- import { existsSync, mkdirSync, statSync } from "fs";
5
- import { readdir, rmdir, unlink } from "fs/promises";
3
+ import { deleteDir, writeFormatFile } from "../util";
4
+ import { existsSync, mkdirSync } from "fs";
6
5
  import { join } from "path";
7
- import Database from "better-sqlite3";
8
6
  class RenderVue {
9
7
  #moduleDir;
10
8
  #name;
@@ -18,22 +16,10 @@ class RenderVue {
18
16
  this.#name = name;
19
17
  this.#moduleDir = join(webDir, this.#path);
20
18
  }
21
- async #deleteDir(path) {
22
- const files = await readdir(path);
23
- for (let i = 0; i < files.length; i++) {
24
- const filePath = join(path, files[i]);
25
- if (statSync(filePath).isDirectory()) {
26
- await this.#deleteDir(filePath);
27
- } else {
28
- await unlink(filePath);
29
- }
30
- }
31
- await rmdir(path);
32
- }
33
- async editDir(isDelete = false) {
19
+ editDir(isDelete = false) {
34
20
  if (isDelete) {
35
21
  if (existsSync(this.#moduleDir)) {
36
- await this.#deleteDir(this.#moduleDir);
22
+ deleteDir(this.#moduleDir);
37
23
  }
38
24
  } else {
39
25
  let basePath = "";
@@ -59,77 +45,6 @@ class RenderVue {
59
45
  }
60
46
  }
61
47
  }
62
- #findIdByKey(key, select) {
63
- const result = select.get(key);
64
- return result ? result.id : void 0;
65
- }
66
- async setDatabase(route, isDelete = false) {
67
- const sqliteFile = getSqliteFile();
68
- if (!existsSync(sqliteFile)) {
69
- return;
70
- }
71
- const db = new Database(sqliteFile);
72
- const selectStmt = db.prepare(
73
- `SELECT id FROM w_permission WHERE key = ?`
74
- );
75
- const insertStmt = db.prepare(
76
- `INSERT INTO w_permission (title, key, description, pid) VALUES (@title, @key, @description, @pid)`
77
- );
78
- const deleteStmt = db.prepare(`DELETE FROM w_permission WHERE key = ?`);
79
- const hasPremissionTbl = db.prepare(
80
- `SELECT name FROM sqlite_master WHERE type='table' AND name=?`
81
- ).get("w_permission");
82
- if (!hasPremissionTbl) {
83
- return Promise.reject("\u6743\u9650\u8868\u4E0D\u5B58\u5728");
84
- }
85
- const pathList = route.path.split("/").filter((p) => p);
86
- const prefixKey = pathList.length > 1 ? "#" + pathList[0].replaceAll("-", "_") : "";
87
- let pid = null;
88
- if (prefixKey) {
89
- const id = this.#findIdByKey(prefixKey, selectStmt);
90
- if (!id) return Promise.reject(`\u7236\u7EA7\u6743\u9650${prefixKey}\u4E0D\u5B58\u5728`);
91
- pid = id;
92
- }
93
- const key = "#" + pathList.join("_").replaceAll("-", "_");
94
- let fn;
95
- if (isDelete) {
96
- const list = ["@add", "@update", "@delete"].map((k) => key + k);
97
- fn = db.transaction(() => {
98
- for (const k of list) {
99
- deleteStmt.run(k);
100
- }
101
- deleteStmt.run(key);
102
- });
103
- } else {
104
- const keyId = this.#findIdByKey(key, selectStmt);
105
- if (keyId) return Promise.reject(`\u5F53\u524D\u6743\u9650${key}\u5DF2\u5B58\u5728`);
106
- const list = [
107
- { k: "@add", title: "\u6DFB\u52A0" },
108
- { k: "@update", title: "\u4FEE\u6539" },
109
- { k: "@delete", title: "\u5220\u9664" }
110
- ].map(({ k, title }) => ({
111
- title: title + route.title,
112
- key: key + k,
113
- description: title + route.title + "\u4FE1\u606F"
114
- }));
115
- fn = db.transaction(() => {
116
- const { lastInsertRowid } = insertStmt.run({
117
- key,
118
- title: route.title,
119
- description: `${route.title}\u9875\u9762\u67E5\u770B`,
120
- pid
121
- });
122
- for (const item of list) {
123
- insertStmt.run({ ...item, pid: lastInsertRowid });
124
- }
125
- });
126
- }
127
- try {
128
- fn();
129
- } catch (error) {
130
- return Promise.reject(error);
131
- }
132
- }
133
48
  async genRoute(route) {
134
49
  const filePath = join(this.#moduleDir, "meta.json");
135
50
  const source = await xiu.render("route-mata", route);
package/dist/config.d.ts CHANGED
@@ -1,7 +1,6 @@
1
1
  declare const CodegenConfig: {
2
2
  webDir: string;
3
3
  apiDir: string;
4
- sqliteFile: string;
5
4
  reverse: boolean;
6
5
  };
7
6
  export type ConfigType = Omit<Partial<typeof CodegenConfig>, 'reverse'>;
@@ -9,6 +8,5 @@ export declare function setConfig(config: ConfigType): void;
9
8
  export declare const setReverse: (reverse: boolean) => boolean;
10
9
  export declare const getWebDir: () => string;
11
10
  export declare const getApiDir: () => string;
12
- export declare const getSqliteFile: () => string;
13
11
  export declare const isReverse: () => boolean;
14
12
  export {};
package/dist/config.js CHANGED
@@ -2,7 +2,6 @@ import { resolve } from "path";
2
2
  const CodegenConfig = {
3
3
  webDir: resolve(__dirname, ".."),
4
4
  apiDir: resolve(__dirname, ".."),
5
- sqliteFile: resolve(__dirname, "..", "admin.db"),
6
5
  reverse: false
7
6
  };
8
7
  function setConfig(config) {
@@ -12,18 +11,13 @@ function setConfig(config) {
12
11
  if (config.apiDir) {
13
12
  CodegenConfig.apiDir = config.apiDir;
14
13
  }
15
- if (config.sqliteFile) {
16
- CodegenConfig.sqliteFile = config.sqliteFile;
17
- }
18
14
  }
19
15
  const setReverse = (reverse) => CodegenConfig.reverse = reverse;
20
16
  const getWebDir = () => CodegenConfig.webDir;
21
17
  const getApiDir = () => CodegenConfig.apiDir;
22
- const getSqliteFile = () => CodegenConfig.sqliteFile;
23
18
  const isReverse = () => CodegenConfig.reverse;
24
19
  export {
25
20
  getApiDir,
26
- getSqliteFile,
27
21
  getWebDir,
28
22
  isReverse,
29
23
  setConfig,
package/dist/index.d.ts CHANGED
@@ -1,7 +1,8 @@
1
1
  import { ConfigType } from './config';
2
+ import { ModuleRoute } from './types';
2
3
  declare const _default: {
3
- startGen: (filePath: string, config?: ConfigType | undefined) => Promise<undefined>;
4
- startRemove: (filePath: string, config?: ConfigType | undefined) => Promise<undefined>;
4
+ startGen: (filePath: string, config?: ConfigType | undefined) => Promise<ModuleRoute[]>;
5
+ startRemove: (filePath: string, config?: ConfigType | undefined) => Promise<ModuleRoute[]>;
5
6
  genLuaTypes: (path: string) => Promise<void>;
6
7
  };
7
8
  export default _default;
package/dist/index.js CHANGED
@@ -12,7 +12,7 @@ function startRun(isDelete) {
12
12
  const data = await compileToByte(filePath);
13
13
  const { run } = await import("./lua_codegen");
14
14
  run(new Uint8Array(data), filePath);
15
- await runTask();
15
+ return (await runTask()).filter((c) => !!c);
16
16
  } catch (error) {
17
17
  return Promise.reject(error);
18
18
  }
package/dist/libs.js CHANGED
@@ -6,7 +6,8 @@ function renderNestCode(config, entity, dto, apiService) {
6
6
  setTask(async () => {
7
7
  const nest = new RenderNest(config.name, config.path);
8
8
  if (isReverse()) {
9
- await Promise.all([nest.remove(), nest.editAppModule(true)]);
9
+ nest.remove();
10
+ await nest.editAppModule(true);
10
11
  } else {
11
12
  await nest.editAppModule();
12
13
  await Promise.all([
@@ -28,17 +29,16 @@ function renderVueCode(config, route, fieldList, api) {
28
29
  const vue = new RenderVue(config.name, config.path);
29
30
  if (isReverse()) {
30
31
  vue.editDir(true);
31
- vue.setDatabase(route, true);
32
32
  } else {
33
- await vue.editDir();
33
+ vue.editDir();
34
34
  await Promise.all([
35
- vue.setDatabase(route),
36
35
  vue.genTsFile(fields, api),
37
36
  vue.genRoute(route),
38
37
  vue.genVueForm(config, fields),
39
38
  vue.genVueTable(config, fields)
40
39
  ]);
41
40
  }
41
+ return route;
42
42
  });
43
43
  }
44
44
  function printToNode(info) {
package/dist/task.d.ts CHANGED
@@ -1,6 +1,7 @@
1
+ import { ModuleRoute } from './types';
1
2
  type TaskFn = {
2
- (): Promise<void>;
3
+ (): Promise<void | ModuleRoute>;
3
4
  };
4
5
  export declare function setTask(fn: TaskFn): void;
5
- export declare function runTask(): Promise<void[]>;
6
+ export declare function runTask(): Promise<(void | ModuleRoute)[]>;
6
7
  export {};
package/dist/task.js CHANGED
@@ -1,13 +1,14 @@
1
- const TaskQueue = [];
1
+ let taskQueue = [];
2
2
  function setTask(fn) {
3
- TaskQueue.push(fn);
3
+ taskQueue.push(fn);
4
4
  }
5
5
  function runTask() {
6
6
  const currentTask = [];
7
- for (let i = 0; i < TaskQueue.length; i++) {
8
- const fn = TaskQueue[i];
7
+ for (let i = 0; i < taskQueue.length; i++) {
8
+ const fn = taskQueue[i];
9
9
  currentTask.push(fn);
10
10
  }
11
+ taskQueue = [];
11
12
  return Promise.all(currentTask.map((f) => f()));
12
13
  }
13
14
  export {
package/dist/util.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  import { BuiltInParserName, LiteralUnion } from 'prettier';
2
2
  export declare const writeFormatFile: (filePath: string, content: string, parser?: LiteralUnion<BuiltInParserName>) => Promise<undefined>;
3
+ export declare function deleteDir(path: string): void;
package/dist/util.js CHANGED
@@ -1,4 +1,6 @@
1
+ import { readdirSync, rmdirSync, statSync, unlinkSync } from "fs";
1
2
  import { writeFile } from "fs/promises";
3
+ import { join } from "path";
2
4
  import {
3
5
  format,
4
6
  resolveConfig,
@@ -17,6 +19,22 @@ const writeFormatFile = async (filePath, content, parser = "typescript") => {
17
19
  return Promise.reject(error);
18
20
  }
19
21
  };
22
+ function deleteDir(path) {
23
+ if (!statSync(path).isDirectory()) {
24
+ return;
25
+ }
26
+ const files = readdirSync(path);
27
+ for (let i = 0; i < files.length; i++) {
28
+ const filePath = join(path, files[i]);
29
+ if (statSync(filePath).isDirectory()) {
30
+ deleteDir(filePath);
31
+ } else {
32
+ unlinkSync(filePath);
33
+ }
34
+ }
35
+ rmdirSync(path);
36
+ }
20
37
  export {
38
+ deleteDir,
21
39
  writeFormatFile
22
40
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@biuxiu/codegen",
3
- "version": "0.3.2",
3
+ "version": "0.4.1",
4
4
  "description": "代码生成工具",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -19,7 +19,6 @@
19
19
  "license": "ISC",
20
20
  "devDependencies": {
21
21
  "@biuxiu/publish": "^1.2.11",
22
- "@types/better-sqlite3": "^7.6.11",
23
22
  "@types/node": "^20.14.2",
24
23
  "@types/shelljs": "^0.8.15",
25
24
  "esbuild": "^0.21.5",
@@ -28,9 +27,6 @@
28
27
  "tsx": "^4.15.1",
29
28
  "typescript": "^5.4.5"
30
29
  },
31
- "peerDependencies": {
32
- "better-sqlite3": "^11.1.2"
33
- },
34
30
  "dependencies": {
35
31
  "@biuxiu/template": "^1.5.2",
36
32
  "prettier": "^3.3.2",