Skip to content

Commit 28ed53f

Browse files
committed
fix: refactor config loading and model generation to use getAdminInstance
1 parent 7b9dcd2 commit 28ed53f

2 files changed

Lines changed: 41 additions & 35 deletions

File tree

adminforth/commands/createCustomComponent/configLoader.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ import dotenv from "dotenv";
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,7 +29,17 @@ export async function loadAdminForthConfig() {
2929
const configModule = _require(configPath);
3030

3131
const adminInstance = configModule.admin || configModule.default?.admin;
32+
return adminInstance;
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+
try {
42+
const adminInstance = getAdminInstance();
3343

3444
if (!adminInstance) {
3545
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({...});'`);

adminforth/commands/generateModels.js

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import path from "path";
33
import { toPascalCase, mapToTypeScriptType, getInstance } from "./utils.js";
44
import dotenv from "dotenv";
55
import { callTsProxy } from "./callTsProxy.js";
6+
import { getAdminInstance } from "../commands/createCustomComponent/configLoader.js";
67

78
const envFileArg = process.argv.find((arg) => arg.startsWith("--env-file="));
89
const envFilePath = envFileArg ? envFileArg.split("=")[1] : ".env";
@@ -24,44 +25,39 @@ async function generateModels() {
2425
}
2526

2627
let modelContent = "// Generated model file\n\n";
27-
const files = fs.readdirSync(currentDirectory);
2828
let instanceFound = false;
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-
for (const resource of instance.config.resources) {
36-
if (resource.columns) {
37-
const typeName = toPascalCase(resource.resourceId);
3829

39-
const tsCode = `
40-
export async function exec() {
41-
const columns = ${JSON.stringify(resource.columns)};
42-
const typeName = "${typeName}";
43-
function mapToTypeScriptType(type) {
44-
const map = { "int": "number", "varchar": "string", "boolean": "boolean" };
45-
return map[type] || "any";
46-
}
30+
const instance = await getAdminInstance();
31+
if (instance) {
32+
await instance.discoverDatabases();
33+
instanceFound = true;
34+
for (const resource of instance.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+
}
4745
48-
let typeStr = \`export type \${typeName} = {\\n\`;
49-
for (const col of columns) {
50-
if (col.name && col.type) {
51-
typeStr += \` \${col.name}: \${mapToTypeScriptType(col.type)};\\n\`;
52-
}
53-
}
54-
typeStr += "}\\n\\n";
55-
return typeStr;
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\`;
5650
}
57-
`;
58-
59-
const result = await callTsProxy(tsCode);
60-
modelContent += result;
51+
}
52+
typeStr += "}\\n\\n";
53+
return typeStr;
6154
}
62-
};
55+
`;
56+
57+
const result = await callTsProxy(tsCode);
58+
modelContent += result;
6359
}
64-
}
60+
};
6561
}
6662

6763
if (!instanceFound) {

0 commit comments

Comments
 (0)