@clickup/pg-mig
Version:
PostgreSQL schema migration tool with microsharding and clustering support
69 lines • 2.58 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.readConfigs = readConfigs;
const fs_1 = require("fs");
const path_1 = require("path");
const omitBy_1 = __importDefault(require("lodash/omitBy"));
/**
* Reads the config files from parent directories and return them from the most
* parent'ish to the most child'ish.
*
* A config is a code file which may:
* 1. Export an object.
* 2. Export a function (sync or async) which returns an object.
*/
async function readConfigs(fileName, ...args) {
var _a;
const configs = [];
for (let dir = process.cwd(); (0, path_1.dirname)(dir) !== dir; dir = (0, path_1.dirname)(dir)) {
const path = `${dir}/${fileName}`;
let loaded;
const pathJS = `${path}.js`;
if ((0, fs_1.existsSync)(pathJS)) {
try {
// First, try CommonJS require().
loaded = require(pathJS);
}
catch (e) {
if (`${e}`.includes("ERR_REQUIRE_ESM")) {
// If it's a ESM project (e.g. "type": "module" in package.json), then
// try ESM native import.
loaded = await import(pathJS);
}
else {
throw e;
}
}
}
const pathTS = `${path}.ts`;
if (!loaded && (0, fs_1.existsSync)(pathTS)) {
try {
// eslint-disable-next-line import/no-extraneous-dependencies
require("ts-node/register");
}
catch (e) {
throw (`${(e instanceof Error ? ((_a = e.stack) !== null && _a !== void 0 ? _a : e.message) : "" + e).trim()}\n` +
`Cause:\n To load ${pathTS}, please install ts-node module.`);
}
loaded = require(pathTS);
}
if (!loaded) {
continue;
}
let config = loaded instanceof Function
? loaded(...args)
: loaded.default instanceof Function
? loaded.default(...args)
: loaded.default
? loaded.default
: loaded;
config = config instanceof Promise ? await config : config;
config = (0, omitBy_1.default)(config, (v) => v === undefined);
configs.push(config);
}
return configs.reverse();
}
//# sourceMappingURL=readConfigs.js.map