adminforth 2.4.0-next.216 → 2.4.0-next.217

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.
@@ -2,21 +2,21 @@ import fs from 'fs/promises';
2
2
  import path from 'path';
3
3
  import chalk from 'chalk';
4
4
  import jiti from 'jiti';
5
- import dotenv from "dotenv";
5
+ import dotenv, { config } from "dotenv";
6
6
 
7
7
  dotenv.config({ path: '.env.local', override: true });
8
8
  dotenv.config({ path: '.env', override: true });
9
9
 
10
- export async function loadAdminForthConfig() {
10
+ export async function getAdminInstance() {
11
11
  const configFileName = 'index.ts';
12
12
  const configPath = path.resolve(process.cwd(), configFileName);
13
-
13
+ console.log('Loading config from', configPath);
14
14
  try {
15
15
  await fs.access(configPath);
16
16
  } catch (error) {
17
17
  console.error(chalk.red(`\nError: Configuration file not found at ${configPath}`));
18
18
  console.error(chalk.yellow(`Please ensure you are running this command from your project's root directory and the '${configFileName}' file exists.`));
19
- process.exit(1);
19
+ return null;
20
20
  }
21
21
 
22
22
  try {
@@ -29,8 +29,19 @@ export async function loadAdminForthConfig() {
29
29
  const configModule = _require(configPath);
30
30
 
31
31
  const adminInstance = configModule.admin || configModule.default?.admin;
32
+ return { adminInstance, configPath, configFileName };
33
+ } catch (error) {
34
+ console.error(chalk.red(`\nError loading or parsing configuration file: ${configPath}`));
35
+ console.error(error);
36
+ return null;
37
+ }
38
+ }
32
39
 
40
+ export async function loadAdminForthConfig() {
41
+
42
+ const { adminInstance, configPath, configFileName } = await getAdminInstance();
33
43
 
44
+ try {
34
45
  if (!adminInstance) {
35
46
  throw new Error(`Could not find 'admin' export in ${configFileName}. Please ensure your config file exports the AdminForth instance like: 'export const admin = new AdminForth({...});'`);
36
47
  }
@@ -53,7 +64,7 @@ export async function loadAdminForthConfig() {
53
64
  return config;
54
65
 
55
66
  } catch (error) {
56
- console.error(chalk.red(`\nError loading or parsing configuration file: ${configPath}`));
67
+ console.error(chalk.red(`\nError loading or parsing configuration file: ${configPath}, error: ${error}`));
57
68
  console.error(error);
58
69
  process.exit(1);
59
70
  }
@@ -2,6 +2,8 @@ import fs from "fs";
2
2
  import path from "path";
3
3
  import { toPascalCase, mapToTypeScriptType, getInstance } from "./utils.js";
4
4
  import dotenv from "dotenv";
5
+ import { callTsProxy } from "./callTsProxy.js";
6
+ import { getAdminInstance } from "../commands/createCustomComponent/configLoader.js";
5
7
 
6
8
  const envFileArg = process.argv.find((arg) => arg.startsWith("--env-file="));
7
9
  const envFilePath = envFileArg ? envFileArg.split("=")[1] : ".env";
@@ -23,39 +25,45 @@ async function generateModels() {
23
25
  }
24
26
 
25
27
  let modelContent = "// Generated model file\n\n";
26
- const files = fs.readdirSync(currentDirectory);
27
28
  let instanceFound = false;
28
29
 
29
- for (const file of files) {
30
- if (file.endsWith(".js") || file.endsWith(".ts")) {
31
- const instance = await getInstance(file, currentDirectory);
32
- if (instance) {
33
- await instance.discoverDatabases();
34
- instanceFound = true;
35
- instance.config.resources.forEach((resource) => {
36
- if (resource.columns) {
37
- modelContent += `export type ${toPascalCase(
38
- resource.resourceId
39
- )} = {\n`;
40
- resource.columns.forEach((column) => {
41
- if (column.name && column.type) {
42
- modelContent += ` ${column.name}: ${mapToTypeScriptType(
43
- column.type
44
- )};\n`;
30
+ const { adminInstance, configPath, configFileName } = await getAdminInstance();
31
+ if (adminInstance) {
32
+ await adminInstance.discoverDatabases();
33
+ instanceFound = true;
34
+ for (const resource of adminInstance.config.resources) {
35
+ if (resource.columns) {
36
+ const typeName = toPascalCase(resource.resourceId);
37
+ const tsCode = `
38
+ export async function exec() {
39
+ const columns = ${JSON.stringify(resource.columns)};
40
+ const typeName = "${typeName}";
41
+ function mapToTypeScriptType(type) {
42
+ const map = { "integer": "number", "varchar": "string", "boolean": "boolean", "date": "string", "datetime": "string", "decimal": "number", "float": "number", "json": "Record<string, any>", "text": "string", "string": "string", "time": "string" };
43
+ return map[type] || "any";
44
+ }
45
+
46
+ let typeStr = \`export type \${typeName} = {\\n\`;
47
+ for (const col of columns) {
48
+ if (col.name && col.type) {
49
+ typeStr += \` \${col.name}: \${mapToTypeScriptType(col.type)};\\n\`;
45
50
  }
46
- });
47
- modelContent += `}\n\n`;
51
+ }
52
+ typeStr += "}\\n\\n";
53
+ return typeStr;
48
54
  }
49
- });
55
+ `;
56
+
57
+ const result = await callTsProxy(tsCode);
58
+ modelContent += result;
50
59
  }
51
- }
60
+ };
52
61
  }
53
62
 
54
63
  if (!instanceFound) {
55
64
  console.error("Error: No valid instance found to generate models.");
56
65
  return;
57
66
  }
58
-
59
67
  fs.writeFileSync(modelFilePath, modelContent, "utf-8");
60
68
  console.log(`Generated TypeScript model file: ${modelFilePath}`);
61
69
  return true;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adminforth",
3
- "version": "2.4.0-next.216",
3
+ "version": "2.4.0-next.217",
4
4
  "description": "OpenSource Vue3 powered forth-generation admin panel",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",