Skip to content

Commit 023425d

Browse files
committed
Merge branch 'next' of github.com:devforth/adminforth into next
2 parents dad5e4d + 09a40da commit 023425d

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

adminforth/commands/createCustomComponent/configLoader.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@ import fs from 'fs/promises';
22
import path from 'path';
33
import chalk from 'chalk';
44
import jiti from 'jiti';
5-
import dotenv from "dotenv";
5+
import dotenv, { config } from "dotenv";
66

77
dotenv.config({ path: '.env.local', override: true });
88
dotenv.config({ path: '.env', override: true });
99

10-
export async function loadAdminForthConfig() {
10+
export async function getAdminInstance() {
1111
const configFileName = 'index.ts';
1212
const configPath = path.resolve(process.cwd(), configFileName);
13-
13+
console.log('Loading config from', configPath);
1414
try {
1515
await fs.access(configPath);
1616
} catch (error) {
1717
console.error(chalk.red(`\nError: Configuration file not found at ${configPath}`));
1818
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;
2020
}
2121

2222
try {
@@ -29,8 +29,19 @@ export async function loadAdminForthConfig() {
2929
const configModule = _require(configPath);
3030

3131
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+
}
3239

40+
export async function loadAdminForthConfig() {
41+
42+
const { adminInstance, configPath, configFileName } = await getAdminInstance();
3343

44+
try {
3445
if (!adminInstance) {
3546
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({...});'`);
3647
}
@@ -53,7 +64,7 @@ export async function loadAdminForthConfig() {
5364
return config;
5465

5566
} 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}`));
5768
console.error(error);
5869
process.exit(1);
5970
}

adminforth/commands/generateModels.js

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import fs from "fs";
22
import path from "path";
33
import { toPascalCase, mapToTypeScriptType, getInstance } from "./utils.js";
44
import dotenv from "dotenv";
5+
import { callTsProxy } from "./callTsProxy.js";
6+
import { getAdminInstance } from "../commands/createCustomComponent/configLoader.js";
57

68
const envFileArg = process.argv.find((arg) => arg.startsWith("--env-file="));
79
const envFilePath = envFileArg ? envFileArg.split("=")[1] : ".env";
@@ -23,39 +25,45 @@ async function generateModels() {
2325
}
2426

2527
let modelContent = "// Generated model file\n\n";
26-
const files = fs.readdirSync(currentDirectory);
2728
let instanceFound = false;
2829

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\`;
4550
}
46-
});
47-
modelContent += `}\n\n`;
51+
}
52+
typeStr += "}\\n\\n";
53+
return typeStr;
4854
}
49-
});
55+
`;
56+
57+
const result = await callTsProxy(tsCode);
58+
modelContent += result;
5059
}
51-
}
60+
};
5261
}
5362

5463
if (!instanceFound) {
5564
console.error("Error: No valid instance found to generate models.");
5665
return;
5766
}
58-
5967
fs.writeFileSync(modelFilePath, modelContent, "utf-8");
6068
console.log(`Generated TypeScript model file: ${modelFilePath}`);
6169
return true;

0 commit comments

Comments
 (0)