"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { AppServer: () => AppServer, Commands: () => commands_exports, CoreDomain: () => coreDomain, Domain: () => Domain, ERR_MODULE_NOT_FOUND: () => ERR_MODULE_NOT_FOUND, FRAMEWORK_NAME: () => FRAMEWORK_NAME, ImportsError: () => ImportsError, PACKAGE_NAME: () => PACKAGE_NAME, PALMARES_SETTINGS_MODULE_ENVIRONMENT_VARIABLE: () => PALMARES_SETTINGS_MODULE_ENVIRONMENT_VARIABLE, Std: () => Std, appServer: () => appServer, default: () => coreDomain, defineSettings: () => defineSettings, domain: () => domain, getDefaultStd: () => getDefaultStd, getSettings: () => getSettings, imports: () => imports, initializeApp: () => initializeApp, initializeDomains: () => initializeDomains, retrieveDomains: () => retrieveDomains, setDefaultStd: () => setDefaultStd, setSettings: () => setSettings, std: () => std, utils: () => utils_exports }); module.exports = __toCommonJS(src_exports); // src/app/utils.ts async function initializeApp(domains, settings, commandLineArgs, appServer2) { let customArgumentsOfAppServer = void 0; const instanceOfAppServer = new appServer2(domains, settings); await instanceOfAppServer.load({ domains, settings, commandLineArgs }); await instanceOfAppServer.baseAppServer.initialize(settings, domains); await instanceOfAppServer.start((args) => { customArgumentsOfAppServer = args; instanceOfAppServer.baseAppServer.configureCleanup.bind(instanceOfAppServer.baseAppServer)(instanceOfAppServer, args); }); return () => instanceOfAppServer.close(customArgumentsOfAppServer); } __name(initializeApp, "initializeApp"); // src/commands/index.ts var commands_exports = {}; __export(commands_exports, { getCommands: () => getCommands, handleCommands: () => handleCommands }); // src/commands/exceptions.ts var CommandNotFoundException = class extends Error { static { __name(this, "CommandNotFoundException"); } constructor(commandName) { super(`Command "${commandName}" not found, you can use "node manage.(ts/js) help" to see all commands available in the application.`); } }; var RequiredPositionalArgs = class extends Error { static { __name(this, "RequiredPositionalArgs"); } constructor(missingArgs) { super(`Missing required positional arguments: ${missingArgs.join(", ")}`); this.name = "RequiredPositionalArgs"; } }; // src/domain/exceptions.ts var DomainObligatoryParamsUndefinedError = class _DomainObligatoryParamsUndefinedError extends Error { static { __name(this, "DomainObligatoryParamsUndefinedError"); } constructor() { super(`'name' and 'path' should be defined.`); this.name = _DomainObligatoryParamsUndefinedError.name; } }; var NotAValidDomainDefaultExportedError = class _NotAValidDomainDefaultExportedError extends Error { static { __name(this, "NotAValidDomainDefaultExportedError"); } constructor() { super(`One of your domains does not 'default' export a valid domain. For example, if you have the domain 'core', you should export 'default' as 'export default class CoreDomain extends Domain { ... }'`); this.name = _NotAValidDomainDefaultExportedError.name; } }; // src/conf/exceptions.ts var SettingsNotFoundException = class extends Error { static { __name(this, "SettingsNotFoundException"); } constructor() { super(`No settings file was found for the application. Make sure you either pass an option through'Command.handleCommands'`); } }; // src/std/exceptions.ts var ImportsError = class extends Error { static { __name(this, "ImportsError"); } constructor(packageName) { super(`Could not import '${packageName}' on the current platform. Or the package is not installed.`); this.name = "ImportsError"; } }; var StdNotSetError = class extends Error { static { __name(this, "StdNotSetError"); } constructor() { super("Default standard library not set. Please make sure you install `@palmares/std`, and add it at the top of your installed domains like: `installedDomains: [[StdDomain, { STD: NodeStd }], ...// other domains]`"); this.name = "StdNotSetError"; } }; var FileOrDirectoryDoesNotExistError = class extends Error { static { __name(this, "FileOrDirectoryDoesNotExistError"); } constructor(path) { super(`File or directory ${path} does not exist`); this.name = "FileOrDirectoryDoesNotExistError"; } }; // src/std/config.ts function setDefaultStd(std2) { globalThis.$PCachedDefaultStd = std2; } __name(setDefaultStd, "setDefaultStd"); function getDefaultStd() { if (globalThis.$PCachedDefaultStd === void 0) throw new StdNotSetError(); return globalThis.$PCachedDefaultStd; } __name(getDefaultStd, "getDefaultStd"); // src/utils/index.ts var utils_exports = {}; __export(utils_exports, { ERR_MODULE_NOT_FOUND: () => ERR_MODULE_NOT_FOUND, FRAMEWORK_NAME: () => FRAMEWORK_NAME, PACKAGE_NAME: () => PACKAGE_NAME, PALMARES_SETTINGS_MODULE_ENVIRONMENT_VARIABLE: () => PALMARES_SETTINGS_MODULE_ENVIRONMENT_VARIABLE, camelCaseToHyphenOrSnakeCase: () => camelCaseToHyphenOrSnakeCase, imports: () => imports, snakeCaseToCamelCase: () => snakeCaseToCamelCase, structuredClone: () => structuredClone }); // src/utils/constants.ts var FRAMEWORK_NAME = "palmares"; var PACKAGE_NAME = "@palmares/core"; var ERR_MODULE_NOT_FOUND = "MODULE_NOT_FOUND"; var PALMARES_SETTINGS_MODULE_ENVIRONMENT_VARIABLE = "PALMARES_SETTINGS_MODULE"; // src/std/imports.ts async function imports(packageName, args) { const errorCodeToConsider = typeof args?.errorCode === "string" ? args.errorCode : "MODULE_NOT_FOUND"; const packagePathToUse = typeof args?.apiName === "string" ? args.apiName : "default"; try { const module2 = await import(packageName); const splittedPackagePath = packagePathToUse.split("."); let result = module2; for (const key of splittedPackagePath) { result = result[key]; } return result; } catch (e) { const error = e; if (error.code === errorCodeToConsider) { if (args?.fallback) return await args.fallback(); } else { throw e; } } } __name(imports, "imports"); // src/utils/index.ts function snakeCaseToCamelCase(string) { return string.replace(/([-_][a-z])/gi, (letter) => { return letter.toUpperCase().replace("-", "").replace("_", ""); }); } __name(snakeCaseToCamelCase, "snakeCaseToCamelCase"); function camelCaseToHyphenOrSnakeCase(string, isSnake = true) { return string.replace(/[A-Z]+/g, (letter) => `${isSnake ? "_" : "-"}${letter.toLowerCase()}`); } __name(camelCaseToHyphenOrSnakeCase, "camelCaseToHyphenOrSnakeCase"); function structuredClone(objectOrArray) { if (!objectOrArray) return objectOrArray; if (typeof objectOrArray !== "object") return objectOrArray; if (Array.isArray(objectOrArray)) { const newArray = []; for (let i = 0; i < objectOrArray.length; i++) { const valueAtIndex = !objectOrArray[i] || typeof objectOrArray[i] !== "object" ? objectOrArray[i] : structuredClone(objectOrArray[i]); newArray[i] = valueAtIndex === void 0 ? null : valueAtIndex; } return newArray; } const newObject = {}; for (const key of Object.keys(objectOrArray)) { const valueAtKey = !objectOrArray[key] || typeof objectOrArray[key] !== "object" ? objectOrArray[key] : structuredClone(objectOrArray[key]); if (valueAtKey === void 0) continue; newObject[key] = valueAtKey; } return newObject; } __name(structuredClone, "structuredClone"); // src/conf/settings.ts var $PCachedSettings; function getSettings() { if ($PCachedSettings) return $PCachedSettings; } __name(getSettings, "getSettings"); async function extractSettingsFromPath(stdToUse, path) { setDefaultStd(stdToUse); const pathToUse = typeof path === "string" ? path : await stdToUse.files.readFromEnv(PALMARES_SETTINGS_MODULE_ENVIRONMENT_VARIABLE); if (!pathToUse) throw new SettingsNotFoundException(); try { $PCachedSettings = (await import(stdToUse.files.getPathToFileURL(pathToUse))).default; } catch (e) { throw new SettingsNotFoundException(); } } __name(extractSettingsFromPath, "extractSettingsFromPath"); async function setSettings(settingsOrStd) { let settings = void 0; if (settingsOrStd instanceof Promise) { const awaitedSettingsOrSrd = await settingsOrStd; if (awaitedSettingsOrSrd === void 0) throw new SettingsNotFoundException(); if ("files" in awaitedSettingsOrSrd.default) await extractSettingsFromPath(awaitedSettingsOrSrd.default); else { settings = awaitedSettingsOrSrd.default; } } else if ("files" in settingsOrStd) await extractSettingsFromPath(settingsOrStd); else if (typeof settingsOrStd === "object" && "settingsPathLocation" in settingsOrStd) { await extractSettingsFromPath(settingsOrStd.std, settingsOrStd.settingsPathLocation); } else { settings = settingsOrStd; } if (!settings) throw new SettingsNotFoundException(); if (settings?.default) settings = settings.default; $PCachedSettings = settings; setDefaultStd(new settings.std()); return settings; } __name(setSettings, "setSettings"); // src/domain/utils.ts function setCachedDomains(domains) { globalThis.$PCachedDomains = domains; } __name(setCachedDomains, "setCachedDomains"); function setCachedInitializedDomains(domains) { globalThis.$PCachedInitializedDomains = domains; } __name(setCachedInitializedDomains, "setCachedInitializedDomains"); async function retrieveDomains(settings, options) { const isNotDynamicDomains = settings.isDynamicDomains !== true; if (globalThis.$PCachedDomains && isNotDynamicDomains && options?.ignoreCache !== true) return globalThis.$PCachedDomains; const mergedSettings = settings; const domainClasses = []; for (const domain2 of mergedSettings?.installedDomains || []) { let domainKls = domain2; if (Array.isArray(domainKls)) { domainKls = domainKls[0]; const entriesOfDomainSettings = Object.entries(domain2[1]); for (const [domainSettingsKey, domainSettings] of entriesOfDomainSettings) mergedSettings[domainSettingsKey] = domainSettings; } if (domainKls instanceof Promise) domainKls = (await domainKls).default; if (domainKls?.["$$type"] === "$PDomain") { domainClasses.push(domainKls); } else { throw new NotAValidDomainDefaultExportedError(); } } if (isNotDynamicDomains) setCachedDomains(domainClasses); setSettings(mergedSettings); return domainClasses; } __name(retrieveDomains, "retrieveDomains"); async function initializeDomains(settingsOrSettingsPath, options) { const ignoreCache = options?.ignoreCache === true; if (globalThis.$PCachedInitializedDomains && ignoreCache !== true) return { settings: getSettings(), domains: globalThis.$PCachedInitializedDomains, commands: getCommands() }; const settings = await setSettings(settingsOrSettingsPath); let commands = {}; const initializedDomains = []; const domainClasses = await retrieveDomains(settings, { ignoreCache }); const readyFunctionsToCallAfterAllDomainsAreLoaded = []; for (const domainClass of domainClasses) { const initializedDomain = new domainClass(); const domainIsNotLoadedAndHasLoadFunction = typeof initializedDomain.load === "function" && !initializedDomain.isLoaded || ignoreCache === true; if (domainIsNotLoadedAndHasLoadFunction) { if (initializedDomain.load) { const readyFunctionToCallOrNot = await initializedDomain.load(settings); if (typeof readyFunctionToCallOrNot === "function") readyFunctionsToCallAfterAllDomainsAreLoaded.push(readyFunctionToCallOrNot); } initializedDomain.isLoaded = true; } if (options?.ignoreCommands !== true) commands = { ...commands, ...initializedDomain.commands }; initializedDomains.push(initializedDomain); } if (readyFunctionsToCallAfterAllDomainsAreLoaded.length > 0) { for (const readyFunction of readyFunctionsToCallAfterAllDomainsAreLoaded) await Promise.resolve(readyFunction({ settings, customOptions: {}, app: {}, domains: initializedDomains })); } if (ignoreCache !== true) { setCachedInitializedDomains(initializedDomains); return { commands, settings, domains: globalThis.$PCachedInitializedDomains }; } else { return { settings, commands: {}, domains: initializedDomains }; } } __name(initializeDomains, "initializeDomains"); // src/logging.ts var cachedLogger = { log: /* @__PURE__ */ __name((message) => console.log(getDefaultFormattedMessage(message)), "log"), info: /* @__PURE__ */ __name((message) => console.info(getDefaultFormattedMessage(message)), "info"), debug: /* @__PURE__ */ __name((message) => console.debug(getDefaultFormattedMessage(message)), "debug"), warn: /* @__PURE__ */ __name((message) => console.warn(getDefaultFormattedMessage(message)), "warn"), error: /* @__PURE__ */ __name((message) => console.error(getDefaultFormattedMessage(message)), "error") }; function getDefaultFormattedMessage(message) { return `\x1B[32m[${FRAMEWORK_NAME}]\x1B[0m \x1B[1m[${PACKAGE_NAME}]\x1B[0m ${message}`; } __name(getDefaultFormattedMessage, "getDefaultFormattedMessage"); function getLogger() { return cachedLogger; } __name(getLogger, "getLogger"); function setLogger(logger) { cachedLogger = logger; } __name(setLogger, "setLogger"); // src/commands/index.ts function getCommands() { const cachedCommands = globalThis.$PCachedCommands; if (cachedCommands === void 0) return {}; return cachedCommands; } __name(getCommands, "getCommands"); function setCommands(commands) { globalThis.$PCachedCommands = commands; } __name(setCommands, "setCommands"); function getValueFromType(type, value) { switch (type) { case "string": return value.toString(); case "number": { const valueToReturn = Number(value); if (isNaN(valueToReturn)) throw new Error(`Invalid number ${value}`); else return valueToReturn; } case "boolean": return value === "true" || value === "1" || value === "yes" || value === "y"; default: if (Array.isArray(type) && type.includes(value)) return value; else throw new Error(`Invalid type ${type}`); } } __name(getValueFromType, "getValueFromType"); function formatArgs(command, args) { const isArgAKeywordParameter = /* @__PURE__ */ __name((arg) => arg.startsWith("--") || arg.startsWith("-"), "isArgAKeywordParameter"); const positionalArguments = JSON.parse(JSON.stringify(command.positionalArgs || {})); const allKeywordArgsFlagsByRealName = ( // eslint-disable-next-line ts/no-unnecessary-condition Object.entries(command.keywordArgs || {}).map(([argName, arg]) => arg.hasFlag ? { [argName[0]]: argName } : void 0).filter((arg) => arg !== void 0).reduce((accumulator, current) => { const [key, value] = Object.entries(current)[0]; if (accumulator) { accumulator[key] = value; return accumulator; } else return { [key]: value }; }, {}) || {} ); const positionalArgs = {}; const keywordArgs = {}; const requiredPositionalArgs = new Set(Object.entries(positionalArguments).filter(([, value]) => value.required).map(([key]) => key)); while (args.length > 0) { const arg = args.shift(); const isArgAKeywordParam = isArgAKeywordParameter(arg); if (isArgAKeywordParam) { const argKey = arg.replace(/^(--|-)/, ""); const [keywordArgument, possibleArgument] = argKey.split(/=(.*)/g); const formattedKeywordArgument = allKeywordArgsFlagsByRealName[keywordArgument] || keywordArgument; if (command.keywordArgs && command.keywordArgs[formattedKeywordArgument]) { const isABooleanParameter = typeof command.keywordArgs[formattedKeywordArgument].type === "string" && command.keywordArgs[formattedKeywordArgument].type === "boolean" || typeof command.keywordArgs[formattedKeywordArgument].type !== "string"; let valueToUse = possibleArgument; if (!valueToUse || isABooleanParameter) { const hasDefaultValue = command.keywordArgs[formattedKeywordArgument].default; if (hasDefaultValue) valueToUse = command.keywordArgs[formattedKeywordArgument].default; else if (isABooleanParameter) valueToUse = "true"; else valueToUse = args.shift(); } const valueToUseFormatted = getValueFromType(command.keywordArgs[formattedKeywordArgument].type || "boolean", valueToUse); const canBeMultiple = command.keywordArgs[formattedKeywordArgument].canBeMultiple; if (canBeMultiple && Array.isArray(keywordArgs[formattedKeywordArgument])) keywordArgs[formattedKeywordArgument].push(valueToUseFormatted); else keywordArgs[formattedKeywordArgument] = canBeMultiple ? [ valueToUseFormatted ] : valueToUseFormatted; } } else if (Object.keys(positionalArguments).length > 0) { const [positionalArgument, positionalArgsData] = Object.entries(positionalArguments)[0]; console.log("positionalArgument", positionalArgument); positionalArgs[positionalArgument] = getValueFromType(positionalArgsData.type || "string", arg); delete positionalArguments[positionalArgument]; requiredPositionalArgs.delete(positionalArgument); } } if (requiredPositionalArgs.size > 0) throw new RequiredPositionalArgs(Array.from(requiredPositionalArgs)); return { positionalArgs, keywordArgs }; } __name(formatArgs, "formatArgs"); async function handleCommands(settingsOrSettingsPath, args) { let logger = getLogger(); logger.info((/* @__PURE__ */ new Date()).toISOString()); logger.info("Loading the settings and domains..."); const commandType = args[0]; const { settings, domains, commands: availableCommands } = await initializeDomains(settingsOrSettingsPath, { ignoreCache: false }); const loggerConstructorFromPalmaresLoggingPackage = settings?.__logger; if (loggerConstructorFromPalmaresLoggingPackage) setLogger(new loggerConstructorFromPalmaresLoggingPackage({ domainName: PACKAGE_NAME })); logger = getLogger(); setCommands(availableCommands); const isCommandDefined = ( // eslint-disable-next-line ts/no-unnecessary-condition typeof availableCommands[commandType] === "object" && availableCommands[commandType] !== void 0 ); let returnOfCommand; let formattedCommandLineArgs = { positionalArgs: {}, keywordArgs: {} }; if (isCommandDefined) { formattedCommandLineArgs = formatArgs(availableCommands[commandType], args.slice(1, args.length)); logger.info(`Domains loaded, running the command [${commandType}]`); returnOfCommand = await Promise.resolve(availableCommands[commandType].handler({ settings, domains, commandLineArgs: formattedCommandLineArgs })); } else { throw new CommandNotFoundException(commandType); } if (returnOfCommand?.["$$type"] === "$PAppServer") { logger.info(`Command wants to start the app server, starting it...`); initializeApp(domains, settings, formattedCommandLineArgs, returnOfCommand); } } __name(handleCommands, "handleCommands"); // src/domain/domain.ts var Domain = class { static { __name(this, "Domain"); } static $$type = "$PDomain"; commands = {}; /** * Because of how node_modules work, we might have multiple instances of the core package. * Your package requires core? So it might install it as well inside of the node_modules. * * Like that: * * The user's App * |-> node_modules * |-> @palmares/core * |-> YOUR PACKAGE * |-> node_modules * |-> @palmares/core * * This will mess things up since we will have multiple instances of the core package. * Since we rely a lot on global data. Shared data lets you */ instances; name; path; isLoaded = false; modifiers; __isReady = false; __isClosed = false; static __instance; constructor(name, path) { if (this.constructor.__instance) return this.constructor.__instance; const isAppNameAndAppPathDefined = typeof name === "string" && typeof path === "string"; if (isAppNameAndAppPathDefined) { this.name = name; this.path = path; this.constructor.__instance = this; } else { throw new DomainObligatoryParamsUndefinedError(); } } }; // src/domain/function.ts function domain(name, path, args) { const argsEntries = Object.entries(args); let ReturnedClass = class ReturnedClass extends Domain { static { __name(this, "ReturnedClass"); } constructor() { super(name, path); } load = args.load; ready = args.ready; close = args.close; commands = args.commands || {}; static toJSON = /* @__PURE__ */ __name(() => ({ name, path }), "toJSON"); }; for (const [key, value] of argsEntries) ReturnedClass.prototype[key] = value; return ReturnedClass; } __name(domain, "domain"); // src/conf/index.ts function defineSettings(args) { return args; } __name(defineSettings, "defineSettings"); // src/app/index.ts function getBaseAppServerInstance() { return globalThis.$PBaseAppServerInstance; } __name(getBaseAppServerInstance, "getBaseAppServerInstance"); function getAppServerInstance() { return globalThis.$PAppServerInstance; } __name(getAppServerInstance, "getAppServerInstance"); function setAppServerInstance(instance) { globalThis.$PAppServerInstance = instance; } __name(setAppServerInstance, "setAppServerInstance"); function setBaseAppServerInstance(instance) { globalThis.$PBaseAppServerInstance = instance; } __name(setBaseAppServerInstance, "setBaseAppServerInstance"); function appServer(args) { return class extends AppServer { start = args.start; load = args.load; close = args.close; }; } __name(appServer, "appServer"); var BaseAppServer = class { static { __name(this, "BaseAppServer"); } domains; settings; isClosingServer = false; constructor(domains, settings) { const baseAppServerInstance = getBaseAppServerInstance(); if (baseAppServerInstance) return baseAppServerInstance; else { this.domains = domains; this.settings = settings; setBaseAppServerInstance(this); return this; } } /** * Configure the cleanup of the server, this will run when the user press Ctrl+C and the server stops running. * This will stop the server gracefully instead of hard kill the process so we are able to do some cleanup. * * @params args - The arguments of the server, this way you can send anything you want to the `close` method of the * app server. */ configureCleanup(appServer2, args) { process.on("SIGINT", async () => { await this.#cleanup(); await appServer2.close(args); process.exit(0); }); } /** * This is the cleanup phase, we will call `close` method on all of the domains so * they shut down gracefully. * * By default what this does is simply calling all of the `close` methods of the domains. */ async #cleanup() { if (this.isClosingServer === false) { this.isClosingServer = true; const logger = getLogger(); logger.info("Closing the app server"); const promises = this.domains.map(async (domain2) => { if (domain2.__isClosed === false && domain2.close) await domain2.close(); }); await Promise.all(promises); } } /** * Initialize the app, this will load the settings, initialize the server and call `ready` function * inside of the domains. This ready function is called when the application starts. * * @param settings - The settings of the application. Those are the server settings with the data needed * for this application. * @param domains - All of the domains of the application, including the domain of the server. */ async initialize(settings, domains) { this.settings = settings; this.domains = domains; const customOptions = {}; for (const domain2 of domains) { if (domain2.__isReady === false && domain2.ready) { await domain2.ready({ settings, domains, app: this, customOptions }); } } } }; var AppServer = class { static { __name(this, "AppServer"); } static $$type = "$PAppServer"; baseAppServer; constructor(domains, settings) { const appServerInstance = getAppServerInstance(); if (appServerInstance) return appServerInstance; else { this.baseAppServer = new BaseAppServer(domains, settings); setAppServerInstance(this); return this; } } /** * Method for loading the server instance, this will create a new instance of the server. For express it would * be calling: * * ```ts * const app = express(); * ``` */ // eslint-disable-next-line ts/require-await async load(_) { return void 0; } /** * To start the server we must first load the routes, then load the 404 handler and just * after that we initialize the application. * * JUST AS AN EXAMPLE simple way this would be like: * @example * ```ts * // Load the routes - generally speaking this is the what `this.#startRoutes()` does. * app.get('/test', (req, res) => { res.send('Hello World') }); * app.post('/test', (req, res) => { res.send('Hello World') }); * * // Load the 404 handler - generally speaking this is the what `this.#load404()` does. * app.use( (req, res) => { res.send('404') }); * * // Initialize the application - generally speaking this is the what `this.server.init()` does. * app.listen(3000, () => { console.log('Server started') }); * * // Cleanup function - generally speaking this is the what `this.#configureCleanup()` does. * process.on('SIGINT', async () => { * process.exit(0); * }) * ``` */ // eslint-disable-next-line ts/require-await async start(_configureCleanup) { return void 0; } /** * Runs the clean up function of the server when the application stops, most frameworks might not need this * but if some framework relies on stopping gracefully it might be needed. */ // eslint-disable-next-line ts/require-await async close(args) { return void 0; } }; // src/std/std.ts var clearConsole = /* @__PURE__ */ __name((length) => { const ESC = "\x1B["; const eraseLine = ESC + "2K"; const cursorUp = /* @__PURE__ */ __name((count = 1) => ESC + count + "A", "cursorUp"); const cursorLeft = ESC + "G"; let clear = ""; for (let i = 0; i < length; i++) clear += eraseLine + (i < length - 1 ? cursorUp() : ""); if (length) clear += cursorLeft; return clear; }, "clearConsole"); var std = { files: { readFromEnv: /* @__PURE__ */ __name((envName) => getDefaultStd().files.readFromEnv(envName), "readFromEnv"), readFile: /* @__PURE__ */ __name(async (path) => { if (Array.isArray(path)) path = await getDefaultStd().files.join(...path); const exists = await getDefaultStd().files.exists(path); if (!exists) throw new FileOrDirectoryDoesNotExistError(path); return getDefaultStd().files.readFile(path); }, "readFile"), join: /* @__PURE__ */ __name(async (...paths) => getDefaultStd().files.join(...paths), "join"), exists: /* @__PURE__ */ __name(async (path) => { if (Array.isArray(path)) path = await getDefaultStd().files.join(...path); return getDefaultStd().files.exists(path); }, "exists"), readDirectory: /* @__PURE__ */ __name(async (path) => { if (Array.isArray(path)) path = await getDefaultStd().files.join(...path); const exists = await getDefaultStd().files.exists(path); if (!exists) throw new FileOrDirectoryDoesNotExistError(path); return getDefaultStd().files.readDirectory(path); }, "readDirectory"), makeDirectory: /* @__PURE__ */ __name(async (path) => { if (Array.isArray(path)) path = await getDefaultStd().files.join(...path); return getDefaultStd().files.makeDirectory(path); }, "makeDirectory"), appendFile: /* @__PURE__ */ __name(async (path, content) => { if (Array.isArray(path)) path = await getDefaultStd().files.join(...path); const exists = await getDefaultStd().files.exists(path); if (!exists) throw new FileOrDirectoryDoesNotExistError(path); return getDefaultStd().files.appendFile(path, content); }, "appendFile"), getPathToFileURL: /* @__PURE__ */ __name((path) => getDefaultStd().files.getPathToFileURL(path), "getPathToFileURL"), writeFile: /* @__PURE__ */ __name(async (path, content) => { if (Array.isArray(path)) path = await getDefaultStd().files.join(...path); return getDefaultStd().files.writeFile(path, content); }, "writeFile"), removeFile: /* @__PURE__ */ __name(async (path) => { if (Array.isArray(path)) path = await getDefaultStd().files.join(...path); const exists = await getDefaultStd().files.exists(path); if (!exists) throw new FileOrDirectoryDoesNotExistError(path); return getDefaultStd().files.removeFile(path); }, "removeFile"), dirname: /* @__PURE__ */ __name(async (path) => { if (Array.isArray(path)) path = await getDefaultStd().files.join(...path); return await getDefaultStd().files.dirname(path); }, "dirname"), basename: /* @__PURE__ */ __name(async (path) => { if (Array.isArray(path)) path = await getDefaultStd().files.join(...path); return await getDefaultStd().files.basename(path); }, "basename"), relative: /* @__PURE__ */ __name((from, to) => getDefaultStd().files.relative(from, to), "relative") }, os: { platform: /* @__PURE__ */ __name(() => getDefaultStd().os.platform(), "platform"), release: /* @__PURE__ */ __name(() => getDefaultStd().os.release(), "release"), cwd: /* @__PURE__ */ __name(() => getDefaultStd().os.cwd(), "cwd") }, childProcess: { spawn: /* @__PURE__ */ __name((command, args, options) => getDefaultStd().childProcess.spawn(command, args, options), "spawn"), executeAndOutput: /* @__PURE__ */ __name((command, options) => getDefaultStd().childProcess.executeAndOutput(command, options), "executeAndOutput") }, asker: { ask: /* @__PURE__ */ __name((question) => getDefaultStd().asker.ask(question), "ask"), askClearingScreen: /* @__PURE__ */ __name(async (question, afterRespond) => { const numberOfLines = question.length + 1; const actualQuestion = `${question.join("\n")} > `; const response = getDefaultStd().asker.askClearingScreen(actualQuestion, () => clearConsole(numberOfLines + 1)); return response.then((answer) => { console.log(afterRespond(answer)); return answer; }); }, "askClearingScreen"), askSelection: /* @__PURE__ */ __name((question, options) => { const defaultStd = getDefaultStd(); let selectedOptionIndex = 0; let outputWrite = void 0; const renderOption = /* @__PURE__ */ __name((index) => { const isSelected = index === selectedOptionIndex; const isEnding = index === options.length - 1; const optionText = `${isSelected ? `\x1B[32m\x1B[1m${">"} ${options[index]}\x1B[0m` : ` ${options[index]}`}${isEnding ? "" : "\n"}`; return optionText; }, "renderOption"); const renderOptions = /* @__PURE__ */ __name((isFirstRender = false) => { if (!isFirstRender) outputWrite?.(clearConsole(options.length)); for (let i = 0; i < options.length; i++) outputWrite?.(renderOption(i)); }, "renderOptions"); const promiseToReturn = defaultStd.asker.select.ask(question, options, (writeCallback) => { outputWrite = writeCallback; }, { onDown: /* @__PURE__ */ __name(() => { if (selectedOptionIndex >= options.length - 1) return; selectedOptionIndex += 1; if (outputWrite) renderOptions(); }, "onDown"), onUp: /* @__PURE__ */ __name(() => { if (selectedOptionIndex <= 0) return; selectedOptionIndex -= 1; if (outputWrite) renderOptions(); }, "onUp"), onSelect: /* @__PURE__ */ __name(() => { if (outputWrite) { outputWrite(clearConsole(options.length + 1)); console.log(`${question} \x1B[32m${options[selectedOptionIndex]}\x1B[0m`); } defaultStd.asker.select.close(); return options[selectedOptionIndex]; }, "onSelect"), clearConsole: /* @__PURE__ */ __name(() => clearConsole(options.length), "clearConsole"), renderOption }); if (outputWrite) { outputWrite(`${question} `); renderOptions(true); } return promiseToReturn; }, "askSelection") } }; // src/std-adapter/index.ts var Std = class { static { __name(this, "Std"); } asker; files; childProcess; os; }; // src/commands/help.ts function textWithEmptySpaces(length, text) { return `${" ".repeat(length)}${text}`; } __name(textWithEmptySpaces, "textWithEmptySpaces"); function logPositionalArgs(commandData) { let stringToLog = ""; let syntax = ""; const appendStringToLog = /* @__PURE__ */ __name((text) => stringToLog += "\n" + text, "appendStringToLog"); const appendStringToSyntax = /* @__PURE__ */ __name((text) => syntax += " " + text, "appendStringToSyntax"); if (commandData.positionalArgs && Object.keys(commandData.positionalArgs).length > 0) { appendStringToLog(textWithEmptySpaces(4, `\x1B[33mPOSITIONAL ARGS\x1B[0m`)); const positionalArgsEntries = Object.entries(commandData.positionalArgs); for (let positionalIndex = 0; positionalIndex < positionalArgsEntries.length; positionalIndex++) { const [positionalArg, positionalArgData] = positionalArgsEntries[positionalIndex]; const isPositionalArgOptional = positionalArgData.required !== true; const doesPositionalArgCanRepeat = positionalArgData.canBeMultiple; appendStringToSyntax(`${isPositionalArgOptional ? "[" : ""}${positionalArg.toUpperCase()}${doesPositionalArgCanRepeat ? ` ...` : ""}${isPositionalArgOptional ? "]" : ""}`); appendStringToLog(textWithEmptySpaces(6, `\x1B[1m${positionalArg}\x1B[0m`)); appendStringToLog(textWithEmptySpaces(8, `\x1B[1mDESCRIPTION\x1B[0m`)); appendStringToLog(textWithEmptySpaces(10, positionalArgData.description)); appendStringToLog(textWithEmptySpaces(8, `\x1B[1mREQUIRED\x1B[0m: ${!isPositionalArgOptional}`)); appendStringToLog(textWithEmptySpaces(8, `\x1B[1mTYPE\x1B[0m: ${[ "string", "boolean", "number" ].includes(positionalArgData.type) ? positionalArgData.type : "string"}`)); if (positionalIndex < positionalArgsEntries.length - 1) appendStringToLog(""); } } return [ syntax, stringToLog ]; } __name(logPositionalArgs, "logPositionalArgs"); function logKeywordArgs(commandData) { let stringToLog = ""; let syntax = ""; const appendStringToLog = /* @__PURE__ */ __name((text) => stringToLog += "\n" + text, "appendStringToLog"); const appendStringToSyntax = /* @__PURE__ */ __name((text) => syntax += " " + text, "appendStringToSyntax"); if (commandData.keywordArgs && Object.keys(commandData.keywordArgs).length > 0) { appendStringToLog(textWithEmptySpaces(4, `\x1B[33mKEYWORD ARGS\x1B[0m`)); const keywordArgsArgsEntries = Object.entries(commandData.keywordArgs); for (let keywordIndex = 0; keywordIndex < keywordArgsArgsEntries.length; keywordIndex++) { const [keywordArg, keywordArgData] = keywordArgsArgsEntries[keywordIndex]; const doesKeywordArgHaveFlag = keywordArgData.hasFlag; const doesKeywordArgHaveDefault = keywordArgData.default !== void 0; const isKeywordChoicesArray = Array.isArray(keywordArgData.type); const doesKeywordArgHaveType = typeof keywordArgData.type === "string" && [ "string", "number" ].includes(keywordArgData.type) || isKeywordChoicesArray; appendStringToSyntax(`[--${keywordArg}${doesKeywordArgHaveFlag ? `, -${keywordArg[0]}` : ""}${doesKeywordArgHaveType ? keywordArgData.canBeMultiple ? `=[${isKeywordChoicesArray ? `{${keywordArgData.type.join(",")}}` : keywordArg.toUpperCase()},...]` : `=${isKeywordChoicesArray ? `{${keywordArgData.type.join(",")}}` : keywordArg.toUpperCase()}` : ""}${doesKeywordArgHaveDefault ? `; default="${keywordArgData.default}"` : ""}]`); appendStringToLog(textWithEmptySpaces(6, `\x1B[1m${keywordArg}\x1B[0m`)); appendStringToLog(textWithEmptySpaces(8, `\x1B[1mDESCRIPTION\x1B[0m`)); appendStringToLog(textWithEmptySpaces(10, keywordArgData.description)); if (doesKeywordArgHaveFlag) appendStringToLog(textWithEmptySpaces(8, `\x1B[1mFLAG\x1B[0m: -${keywordArg[0]}`)); if (doesKeywordArgHaveDefault) appendStringToLog(textWithEmptySpaces(8, `\x1B[1mDEFAULT\x1B[0m: ${keywordArgData.default}`)); if (doesKeywordArgHaveType) appendStringToLog(textWithEmptySpaces(8, `\x1B[1mTYPE\x1B[0m: ${keywordArgData.type}`)); if (keywordArgData.canBeMultiple) appendStringToLog(textWithEmptySpaces(8, `\x1B[1mCAN APPEAR MORE THAN ONCE\x1B[0m`)); if (keywordIndex < keywordArgsArgsEntries.length - 1) appendStringToLog(""); } } return [ syntax, stringToLog ]; } __name(logKeywordArgs, "logKeywordArgs"); function help(domains, keywordArgs) { for (let i = 0; i < domains.length; i++) { const domain2 = domains[i]; const shouldNotShowAnythingFromDomain = keywordArgs.domain && !keywordArgs.domain.includes(domain2.name) || keywordArgs.command && !Object.keys(domain2.commands || {}).some((command) => keywordArgs.command?.includes(command)) || !domain2.commands; if (shouldNotShowAnythingFromDomain) continue; console.log(textWithEmptySpaces(0, `\x1B[36m[${domain2.name}]\x1B[0m`)); const commandEntries = Object.entries(domain2.commands); for (let commandIndex = 0; commandIndex < commandEntries.length; commandIndex++) { const shouldNotShowCommand = keywordArgs.command && !keywordArgs.command.includes(commandEntries[commandIndex][0]); if (shouldNotShowCommand) continue; const [command, commandData] = commandEntries[commandIndex]; const [positionalArgsSyntax, positionalArgsToLog] = logPositionalArgs(commandData); const [keywordArgsSyntax, keywordArgsToLog] = logKeywordArgs(commandData); console.log(textWithEmptySpaces(2, `\x1B[1m${command}\x1B[0m`)); console.log(textWithEmptySpaces(4, `\x1B[33mDESCRIPTION\x1B[0m`)); console.log(textWithEmptySpaces(6, `${commandData.description}`)); console.log(""); console.log(textWithEmptySpaces(4, `\x1B[33mSYNTAX\x1B[0m`)); console.log(textWithEmptySpaces(6, `manage.(ts|js) ${command}${positionalArgsSyntax !== "" ? positionalArgsSyntax : ""}${keywordArgsSyntax !== "" ? keywordArgsSyntax : ""}`)); if (positionalArgsToLog !== "") console.log(positionalArgsToLog); if (keywordArgsToLog !== "") console.log(keywordArgsToLog); if (commandIndex < commandEntries.length - 1) console.log(""); } if (i < domains.length - 1) console.log(""); } } __name(help, "help"); // src/domain/default.ts var coreDomain = domain(PACKAGE_NAME, "", { modifiers: [], commands: { help: { description: "Helps you with all of the other commands. If you do not specify a command it will list all of the available commands alongside of their descriptions. If you do specify a command it will give you the description and example of usage of that command.", positionalArgs: void 0, keywordArgs: { command: { description: "If you add this argument it will show you the help only for that specific command.", hasFlag: true, type: "string", canBeMultiple: true }, domain: { description: "If you add this argument it will only show you the help for all of the commands of a specific domain", hasFlag: true, type: "string", canBeMultiple: true } }, handler: /* @__PURE__ */ __name((options) => { help(options.domains, options.commandLineArgs.keywordArgs); }, "handler") } }, // eslint-disable-next-line ts/require-await load: /* @__PURE__ */ __name(async (_) => void 0, "load") }); // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { AppServer, Commands, CoreDomain, Domain, ERR_MODULE_NOT_FOUND, FRAMEWORK_NAME, ImportsError, PACKAGE_NAME, PALMARES_SETTINGS_MODULE_ENVIRONMENT_VARIABLE, Std, appServer, defineSettings, domain, getDefaultStd, getSettings, imports, initializeApp, initializeDomains, retrieveDomains, setDefaultStd, setSettings, std, utils });