@expressots/shared
Version:
Shared library for ExpressoTS modules 🐎
100 lines (99 loc) • 3.58 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Compiler = void 0;
exports.printError = printError;
/* eslint-disable @typescript-eslint/no-explicit-any */
const node_fs_1 = require("node:fs");
const path_1 = __importDefault(require("path"));
const chalk_1 = __importDefault(require("chalk"));
function printError(message, component) {
console.error(chalk_1.default.red(`${message}:`, chalk_1.default.bold(chalk_1.default.white(`[${component}] ❌`))));
}
/**
* The path to the expressots.config.ts file
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const EXPRESSOTS_CONFIG = path_1.default.join(process.cwd(), "expressots.config.ts");
/**
* The config object
*/
let globalConfigObject = null;
/**
* The ts-node register options
*/
const regOpt = {
compilerOptions: {
module: "commonjs",
},
moduleTypes: {
"**": "cjs",
},
};
/**
* Singleton compiler class
*/
class Compiler {
constructor() { }
static get Instance() {
if (!Compiler.instance) {
Compiler.instance = new Compiler();
}
return Compiler.instance;
}
async getService() {
const tsnode = await Promise.resolve().then(() => __importStar(require("ts-node")));
const compiler = tsnode.register(regOpt);
return compiler;
}
static interopRequireDefault(obj) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const module = require(obj);
return module && module.__esModule ? module : { default: module };
}
static async findConfig(dir) {
const configPath = path_1.default.join(dir, "expressots.config.ts");
const exists = (0, node_fs_1.existsSync)(configPath);
if (exists)
return configPath;
const parentDir = path_1.default.join(dir, "..");
if (parentDir === dir) {
printError("No config file found!", "expressots.config.ts");
process.exit(1);
}
return Compiler.findConfig(parentDir);
}
static async loadConfig() {
const compiler = await Compiler.Instance.getService();
compiler.enabled(true);
globalConfigObject = Compiler.interopRequireDefault(await Compiler.findConfig(process.cwd()));
compiler.enabled(false);
return globalConfigObject.default;
}
}
exports.Compiler = Compiler;
;