This commit is contained in:
Benjamin Toby
2025-08-13 11:16:28 +01:00
parent a1e56bb1b0
commit 700a704abd
24 changed files with 400 additions and 260 deletions
+73 -95
View File
@@ -1,121 +1,94 @@
#! /usr/bin/env node
import fs from "fs";
import fs, { readFileSync } from "fs";
import datasquirel from "..";
import { parseArgs } from "util";
import { DSQL_DatabaseSchemaType } from "../package-shared/types";
import dbSchemaToType from "../package-shared/functions/dsql/db-schema-to-type";
import path from "path";
import debugLog from "../package-shared/utils/logging/debug-log";
import parseEnv from "../package-shared/utils/parse-env";
import { AppNames } from "../package-shared/dict/app-names";
import { APIConnectionOptions } from "../package-shared/types";
const args = parseArgs({
args: process.argv,
options: {
apiKey: {
type: "string",
default: process.env.DSQL_FULL_ACCESS_API_KEY,
short: "k",
},
database: {
type: "string",
default: process.env.DSQL_DB_NAME,
short: "d",
},
userid: {
type: "string",
default: process.env.DSQL_API_USER_ID,
short: "i",
},
outfile: {
type: "string",
short: "o",
},
envfile: {
type: "string",
short: "e",
},
debug: {
type: "boolean",
short: "u",
},
},
strict: false,
});
type Config = {
database?: string;
outDir?: string;
outFile?: string;
apiConnectionConfig?: APIConnectionOptions;
debug?: boolean;
};
let appendedEnv: { [k: string]: string } = {};
const configFileName = AppNames["SchemaToTypeDefConfigFileName"];
const configFilePath = path.join(process.cwd(), configFileName);
if (args.values.envfile && typeof args.values.envfile == "string") {
const finalEnvPath = path.resolve(process.cwd(), args.values.envfile);
if (fs.existsSync(finalEnvPath)) {
const parsedEnv = parseEnv(finalEnvPath);
appendedEnv = (parsedEnv || {}) as any;
if (args.values.debug) {
debugLog({
log: appendedEnv,
label: "Appended env",
title: "Schema to Typedef",
addTime: true,
});
}
}
if (!fs.existsSync(configFilePath)) {
debugLog({
log: `Config File doesn't exist. Please add a '${AppNames["SchemaToTypeDefConfigFileName"]}' in the working directory with relevant configurations.`,
addTime: true,
type: "error",
});
process.exit(1);
}
const finalEnv = { ...process.env, ...appendedEnv } as { [k: string]: string };
process.env = { ...process.env, ...appendedEnv };
const config: Config = JSON.parse(
readFileSync(path.join(process.cwd(), configFileName), "utf-8")
);
if (!config.database) {
debugLog({
log: `Config File needs a 'database' value.`,
addTime: true,
type: "error",
});
process.exit(1);
}
if (!config.outDir && !config.outFile) {
debugLog({
log: `Config File needs a 'outDir' or 'outFile' value.`,
addTime: true,
type: "error",
});
process.exit(1);
}
if (!config.apiConnectionConfig) {
debugLog({
log: `Config File needs a 'apiConnectionConfig' value. This value needs an 'apiKey' value.`,
addTime: true,
type: "error",
});
process.exit(1);
}
if (!config.apiConnectionConfig.apiKey) {
debugLog({
log: `Config value 'apiConnectionConfig' needs an 'apiKey' value.`,
addTime: true,
type: "error",
});
process.exit(1);
}
(async () => {
try {
const key = args.values.apiKey || finalEnv["DSQL_FULL_ACCESS_API_KEY"];
const database = args.values.database || finalEnv["DSQL_DB_NAME"];
const user_id =
args.values.userid || finalEnv["DSQL_API_USER_ID"] || "1";
if (args.values.debug) {
if (config.debug) {
debugLog({
log: args.values,
label: "Arguments",
title: "Schema to Typedef",
addTime: true,
});
debugLog({
log: process.env.DSQL_FULL_ACCESS_API_KEY,
label: "process.env.DSQL_FULL_ACCESS_API_KEY",
title: "Schema to Typedef",
addTime: true,
});
debugLog({
log: process.env.DSQL_DB_NAME,
label: "process.env.DSQL_DB_NAME",
title: "Schema to Typedef",
log: config,
label: "Config",
addTime: true,
});
}
if (!database || typeof database !== "string")
throw new Error("Database name is required");
if (!key || typeof key !== "string")
throw new Error("API key is required");
if (!args.values.outfile || typeof args.values.outfile !== "string")
throw new Error("Outfile are required");
if (!user_id || typeof user_id !== "string")
throw new Error("Outfile are required");
const schema = await datasquirel.getSchema({
key,
database,
user_id,
env: finalEnv,
const schemaRes = await datasquirel.api.schema.get({
dbName: config.database || "",
apiConnectionConfig: config.apiConnectionConfig,
});
const dbSchema = schema.payload as DSQL_DatabaseSchemaType | undefined;
const dbSchema = schemaRes.payload;
if (args.values.debug) {
if (config.debug) {
debugLog({
log: schema,
log: schemaRes,
label: "schema",
title: "Schema to Typedef",
addTime: true,
});
}
@@ -124,8 +97,13 @@ process.env = { ...process.env, ...appendedEnv };
const definitions = dbSchemaToType({ dbSchema });
const finalOutfile = path.resolve(process.cwd(), args.values.outfile);
const finalOutfile = path.resolve(
process.cwd(),
config.outDir ? config.outDir + "/dsql.ts" : config.outFile || ""
);
const ourfileDir = path.dirname(finalOutfile);
if (!fs.existsSync(ourfileDir)) {
fs.mkdirSync(ourfileDir, { recursive: true });
}