@contentstack/cli-utilities
Version:
Utilities for contentstack projects
109 lines (108 loc) • 4.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getLogPath = exports.handleAndLogError = exports.cliErrorHandler = exports.v2Logger = exports.getSessionLogPath = void 0;
const tslib_1 = require("tslib");
const fs = tslib_1.__importStar(require("fs"));
const os = tslib_1.__importStar(require("os"));
const path = tslib_1.__importStar(require("path"));
const logger_1 = tslib_1.__importDefault(require("./logger"));
const cli_error_handler_1 = require("./cli-error-handler");
const __1 = require("..");
let loggerInstance = null;
function createLoggerInstance() {
var _a;
const logConfig = __1.configHandler.get('log');
const logLevel = (logConfig === null || logConfig === void 0 ? void 0 : logConfig.level) || 'info';
const showConsoleLogs = (_a = logConfig === null || logConfig === void 0 ? void 0 : logConfig['show-console-logs']) !== null && _a !== void 0 ? _a : false;
const config = {
basePath: getLogPath(),
logLevel: logLevel,
consoleLoggingEnabled: showConsoleLogs,
consoleLogLevel: logLevel,
};
return new logger_1.default(config);
}
// Lazy proxy object that behaves like a Logger
const v2Logger = new Proxy({}, {
get(_, prop) {
if (!loggerInstance) {
loggerInstance = createLoggerInstance();
}
const targetProp = loggerInstance[prop];
if (typeof targetProp === 'function') {
return targetProp.bind(loggerInstance);
}
return targetProp;
},
});
exports.v2Logger = v2Logger;
const cliErrorHandler = new cli_error_handler_1.CLIErrorHandler(); // Enable debug mode for error classification
exports.cliErrorHandler = cliErrorHandler;
/**
* Handles and logs an error by classifying it and logging the relevant details.
*
* This function uses the `cliErrorHandler` to classify the provided error and logs
* the error details using `v2Logger`. If debug information is available, it logs
* additional debug details, including a stack trace if not already present.
*
* @param error - The error to be handled and logged. Can be of any type.
* @param context - Optional context information to assist in error classification
* and logging.
*
* @remarks
* - The error is always logged with its type, message, and other metadata.
* - If debug information is available, it is logged separately with a more specific
* debug type and additional details.
*/
function handleAndLogError(error, context, errorMessage) {
var _a;
const classified = cliErrorHandler.classifyError(error, context, errorMessage);
const apiError = ((_a = classified.error) === null || _a === void 0 ? void 0 : _a.message) || (classified === null || classified === void 0 ? void 0 : classified.message) || 'Unknown error';
// Always log the error
v2Logger.logError({
type: classified.type,
message: errorMessage ? `${errorMessage}\nAPI Error: ${apiError}` : `${apiError}`,
error: classified.error,
context: typeof classified.context === 'string' ? { message: classified.context } : classified.context,
hidden: classified.hidden,
meta: classified.meta,
});
}
exports.handleAndLogError = handleAndLogError;
/**
* Get the log path for centralized logging
* Priority:
* 1. CS_CLI_LOG_PATH environment variable (user override)
* 2. User config (log.path from CLI config)
* 3. Current working directory + logs (where user ran the command)
* 4. Home directory (~/contentstack/logs) (fallback)
*/
function getLogPath() {
// 1. Environment variable override
if (process.env.CS_CLI_LOG_PATH) {
return process.env.CS_CLI_LOG_PATH;
}
// 2. User configured path
const configuredPath = __1.configHandler.get('log.path');
if (configuredPath) {
return configuredPath;
}
// 3. Use current working directory (where user ran the command)
try {
const cwdPath = path.join(process.cwd(), 'logs');
if (!fs.existsSync(cwdPath)) {
fs.mkdirSync(cwdPath, { recursive: true });
}
fs.accessSync(cwdPath, fs.constants.W_OK);
return cwdPath;
}
catch (error) {
// If current directory is not writable, fall back to home directory
}
// 4. Fallback to home directory
return path.join(os.homedir(), 'contentstack', 'logs');
}
exports.getLogPath = getLogPath;
// Re-export getSessionLogPath for external use
var session_path_1 = require("./session-path");
Object.defineProperty(exports, "getSessionLogPath", { enumerable: true, get: function () { return session_path_1.getSessionLogPath; } });