datasquirel/dist/engine/schema-to-typedef.js
Benjamin Toby a9c0dd4b63 Updates
2025-04-17 11:24:53 +01:00

124 lines
4.6 KiB
JavaScript

#! /usr/bin/env node
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const __1 = __importDefault(require(".."));
const util_1 = require("util");
const db_schema_to_type_1 = __importDefault(require("../package-shared/functions/dsql/db-schema-to-type"));
const path_1 = __importDefault(require("path"));
const debug_log_1 = __importDefault(require("../package-shared/utils/logging/debug-log"));
const parse_env_1 = __importDefault(require("../package-shared/utils/parse-env"));
const args = (0, util_1.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_1.default.resolve(process.cwd(), envfile);
if (fs_1.default.existsSync(finalEnvPath)) {
const appendedEnv = (0, parse_env_1.default)(finalEnvPath);
if (debug) {
(0, debug_log_1.default)({
log: appendedEnv,
label: "Appended env",
title: "Schema to Typedef",
addTime: true,
});
}
for (const [key, value] of Object.entries(appendedEnv)) {
process.env[key] = value;
}
}
}
(() => __awaiter(void 0, void 0, void 0, function* () {
try {
if (debug) {
(0, debug_log_1.default)({
log: args.values,
label: "Arguments",
title: "Schema to Typedef",
addTime: true,
});
(0, debug_log_1.default)({
log: process.env.DSQL_FULL_ACCESS_API_KEY,
label: "process.env.DSQL_FULL_ACCESS_API_KEY",
title: "Schema to Typedef",
addTime: true,
});
(0, debug_log_1.default)({
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 = yield __1.default.getSchema({
key,
database,
user_id: 1,
});
const dbSchema = schema.payload;
if (!dbSchema)
throw new Error("No schema found");
const definitions = (0, db_schema_to_type_1.default)({ dbSchema });
const finalOutfile = path_1.default.resolve(process.cwd(), outfile);
const ourfileDir = path_1.default.dirname(finalOutfile);
if (!fs_1.default.existsSync(ourfileDir)) {
fs_1.default.mkdirSync(ourfileDir, { recursive: true });
}
fs_1.default.writeFileSync(finalOutfile, (definitions === null || definitions === void 0 ? void 0 : definitions.join("\n\n")) || "", "utf-8");
}
catch (error) {
(0, debug_log_1.default)({
log: error.message,
label: "Error",
title: "Schema to Typedef",
addTime: true,
type: "error",
});
process.exit(1);
}
}))();