#! /usr/bin/env node import inquirer from "inquirer"; import fs 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"; 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", }, outfile: { type: "string", short: "o", }, envfile: { type: "string", short: "e", }, debug: { type: "boolean", short: "u", }, }, strict: false, }); const { apiKey: key, database, outfile, debug, envfile } = args.values; if (envfile && typeof envfile == "string") { const finalEnvPath = path.resolve(process.cwd(), envfile); if (fs.existsSync(finalEnvPath)) { const appendedEnv = parseEnv(finalEnvPath); if (debug) { debugLog({ log: appendedEnv, label: "Appended env", title: "Schema to Typedef", addTime: true, }); } for (const [key, value] of Object.entries(appendedEnv)) { process.env[key] = value; } } } (async () => { try { if (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", 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 (!outfile || typeof outfile !== "string") throw new Error("Outfile are required"); const schema = await datasquirel.getSchema({ key, database, user_id: 1, }); const dbSchema = schema.payload as DSQL_DatabaseSchemaType | undefined; if (!dbSchema) throw new Error("No schema found"); const definitions = dbSchemaToType({ dbSchema }); const finalOutfile = path.resolve(process.cwd(), outfile); const ourfileDir = path.dirname(finalOutfile); if (!fs.existsSync(ourfileDir)) { fs.mkdirSync(ourfileDir, { recursive: true }); } fs.writeFileSync( finalOutfile, definitions?.join("\n\n") || "", "utf-8" ); } catch (error: any) { debugLog({ log: error.message, label: "Error", title: "Schema to Typedef", addTime: true, type: "error", }); process.exit(1); } })();