UNPKG

@goldstack/utils-cli

Version:

Utilities for building command line interfaces.

110 lines 4.08 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.wrapCli = void 0; exports.configureLogger = configureLogger; exports.logger = logger; const pino_1 = __importDefault(require("pino")); const isDebug = process.env.GOLDSTACK_DEBUG || process.env.DEBUG; const isLambda = !!process.env.AWS_LAMBDA_FUNCTION_NAME; let loggerInstance; let defaultLogger; function getDefaultLogger() { if (!defaultLogger) { // biome-ignore lint/suspicious/noExplicitAny: pino-pretty module is dynamically loaded let prettyModule; try { // biome-ignore lint/security/noGlobalEval: Prevent webpack from bundling pino-pretty in client builds const pinoPretty = eval('require')('pino-pretty'); prettyModule = pinoPretty.default || pinoPretty; } catch (_e) { // Fallback if pino-pretty is not available or require is not defined (e.g. browser) } defaultLogger = (0, pino_1.default)({ level: isDebug ? 'debug' : 'info', ...(isLambda && { timestamp: () => `,"time":"${new Date(Date.now()).toISOString()}"`, formatters: { level: (label) => ({ level: label }), log: (obj) => { if (obj.err) { const err = obj.err; return { ...obj, error: { type: err.name, stack: err.stack, message: err.message, }, }; } return obj; }, }, base: { aws_request_id: process.env.AWS_LAMBDA_REQUEST_ID || undefined, function_name: process.env.AWS_LAMBDA_FUNCTION_NAME, function_version: process.env.AWS_LAMBDA_FUNCTION_VERSION, }, }), }, isLambda || !prettyModule ? undefined // Use default JSON transport for Lambda or if prettyModule is not available : prettyModule({ colorize: true, hideObject: false, colorizeObjects: true, translateTime: 'HH:MM:ss', ignore: 'pid,hostname', singleLine: false, sync: true, // required for work in Jest see https://github.com/pinojs/pino-pretty?tab=readme-ov-file#usage-with-jest })); } return defaultLogger; } /** * Configure the logger instance * @param config - Configuration options for the logger */ function configureLogger(config) { loggerInstance = config.instance; } /** * Get the current logger instance * @returns The configured logger instance or the default pino logger */ function logger() { return loggerInstance || getDefaultLogger(); } const wrapCli = async (func) => { try { await func(); process.exit(0); } catch (e) { const error = e; if (isDebug) { logger().error({}, `Error while executing CLI command: ${error.message}`); console.error(e); throw e; } else { logger().error({}, '❌ Error while executing CLI command:' + error.message + '\n For more information about this error, run this command with the environment variable GOLDSTACK_DEBUG set to true'); process.exit(1); } } }; exports.wrapCli = wrapCli; process.on('unhandledRejection', (e) => { const error = e; if (isDebug) { console.log(e); } console.log('❌ Unhandled error in asynchronous method:', error.message); process.exit(1); }); //# sourceMappingURL=utilsCli.js.map