142 lines
5.4 KiB
JavaScript
142 lines
5.4 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",
|
|
},
|
|
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,
|
|
});
|
|
let appendedEnv = {};
|
|
if (args.values.envfile && typeof args.values.envfile == "string") {
|
|
const finalEnvPath = path_1.default.resolve(process.cwd(), args.values.envfile);
|
|
if (fs_1.default.existsSync(finalEnvPath)) {
|
|
appendedEnv = (0, parse_env_1.default)(finalEnvPath);
|
|
if (args.values.debug) {
|
|
(0, debug_log_1.default)({
|
|
log: appendedEnv,
|
|
label: "Appended env",
|
|
title: "Schema to Typedef",
|
|
addTime: true,
|
|
});
|
|
}
|
|
}
|
|
}
|
|
const finalEnv = Object.assign(Object.assign({}, process.env), appendedEnv);
|
|
process.env = Object.assign(Object.assign({}, process.env), appendedEnv);
|
|
(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
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) {
|
|
(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 (!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 = yield __1.default.getSchema({
|
|
key,
|
|
database,
|
|
user_id,
|
|
env: finalEnv,
|
|
});
|
|
const dbSchema = schema.payload;
|
|
if (args.values.debug) {
|
|
(0, debug_log_1.default)({
|
|
log: schema,
|
|
label: "schema",
|
|
title: "Schema to Typedef",
|
|
addTime: true,
|
|
});
|
|
}
|
|
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(), args.values.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);
|
|
}
|
|
}))();
|