From 4dbc83f4b6002b7409e79b6f12a3f398176e2509 Mon Sep 17 00:00:00 2001 From: naman-contentstack Date: Tue, 27 Jan 2026 15:56:38 +0530 Subject: [PATCH 1/2] feat: remove logic for cli-log file creation --- .../contentstack-utilities/src/auth-handler.ts | 9 ++------- .../contentstack/src/hooks/prerun/auth-guard.ts | 14 +++++++------- .../src/hooks/prerun/latest-version-warning.ts | 7 +++---- 3 files changed, 12 insertions(+), 18 deletions(-) diff --git a/packages/contentstack-utilities/src/auth-handler.ts b/packages/contentstack-utilities/src/auth-handler.ts index c7157fd33e..cc9c21a2f6 100644 --- a/packages/contentstack-utilities/src/auth-handler.ts +++ b/packages/contentstack-utilities/src/auth-handler.ts @@ -4,7 +4,7 @@ import dotenv from 'dotenv'; import open from 'open'; import http from 'http'; import url from 'url'; -import { LoggerService } from './logger'; +import { handleAndLogError } from './logger/log'; import managementSDKClient, { ContentstackClient } from './contentstack-management-sdk'; import { formatError } from './helpers'; @@ -33,7 +33,6 @@ class AuthHandler { private authorisationTypeOAUTHValue: string; private authorisationTypeAUTHValue: string; private allAuthConfigItems: any; - private logger: any; private oauthHandler: any; private managementAPIClient: ContentstackClient; private isRefreshingToken: boolean = false; // Flag to track if a refresh operation is in progress @@ -98,9 +97,6 @@ class AuthHandler { } return cma; } - initLog() { - this.logger = new LoggerService(process.cwd(), 'cli-log'); - } async setOAuthBaseURL() { if (configHandler.get('region')['uiHost']) { this.OAuthBaseURL = configHandler.get('region')['uiHost'] || ''; @@ -132,12 +128,11 @@ class AuthHandler { */ async oauth(): Promise { try { - this.initLog(); await this.initSDK(); await this.createHTTPServer(); await this.openOAuthURL(); } catch (error) { - this.logger.error('OAuth login failed!', error.message); + handleAndLogError(error, { module: 'auth-handler' }, 'OAuth login failed!'); throw error; } } diff --git a/packages/contentstack/src/hooks/prerun/auth-guard.ts b/packages/contentstack/src/hooks/prerun/auth-guard.ts index 94898da40f..42fd5a064a 100644 --- a/packages/contentstack/src/hooks/prerun/auth-guard.ts +++ b/packages/contentstack/src/hooks/prerun/auth-guard.ts @@ -1,13 +1,13 @@ import { cliux, managementSDKClient, - isAuthenticated + isAuthenticated, + log, + handleAndLogError } from '@contentstack/cli-utilities'; -import {LoggerService} from '@contentstack/cli-utilities' // TBD: run region command if region is not there export default async function (opts): Promise { - const newLogger = new LoggerService(process.cwd(),'cli-log'); const { context: { plugin: { config: { protectedCommands = {} } = {} } = {}, region = null } = {} } = this.config; if (opts.Command.id !== 'config:set:region') { if (!region) { @@ -20,7 +20,7 @@ export default async function (opts): Promise { // Auth guard if (protectedCommands[opts.Command.id]) { if (!isAuthenticated()) { - newLogger.error('No auth token found for command.', opts.Command.id); + handleAndLogError(new Error('No auth token found for command.'), { module: 'auth-guard', commandId: opts.Command.id }); cliux.error('Please log in to execute the command'); this.exit(); } @@ -28,13 +28,13 @@ export default async function (opts): Promise { try { const result = await client.getUser(); if (!result) { - newLogger.error('Error in auth validation'); + handleAndLogError(new Error('Error in auth validation'), { module: 'auth-guard' }); cliux.error('Please log in to execute the command'); this.exit(); } - newLogger.debug('Logged-in user', result.data); + log.debug('Logged-in user', { module: 'auth-guard', userData: result.data }); } catch (error) { - newLogger.error('Error in auth validation', error); + handleAndLogError(error, { module: 'auth-guard' }, 'Error in auth validation'); cliux.error('Please log in to execute the command'); process.exit(); } diff --git a/packages/contentstack/src/hooks/prerun/latest-version-warning.ts b/packages/contentstack/src/hooks/prerun/latest-version-warning.ts index 581a695b0e..05a9a80e18 100644 --- a/packages/contentstack/src/hooks/prerun/latest-version-warning.ts +++ b/packages/contentstack/src/hooks/prerun/latest-version-warning.ts @@ -1,4 +1,4 @@ -import { cliux, configHandler, HttpClient, LoggerService } from '@contentstack/cli-utilities'; +import { cliux, configHandler, HttpClient, handleAndLogError } from '@contentstack/cli-utilities'; import * as semver from 'semver'; import { IVersionUpgradeCache, IVersionUpgradeWarningFrequency } from '../../interfaces'; @@ -8,7 +8,6 @@ const versionUpgradeWarningFrequency: IVersionUpgradeWarningFrequency = { export default async function (_opts): Promise { const now = Date.now(); const today = new Date().toISOString().split('T')[0]; - const logger: LoggerService = new LoggerService(process.env.CS_CLI_LOG_PATH || process.cwd(), 'cli-log'); let cache: IVersionUpgradeCache = { lastChecked: 0, lastWarnedDate: '', latestVersion: '' }; // if CLI_VERSION is not set or is not the same as the current version, set it @@ -36,7 +35,7 @@ export default async function (_opts): Promise { const latestVersion = (await httpClient.get(`https://registry.npmjs.org/@contentstack/cli/latest`))?.data ?.version; if (!latestVersion) { - logger.error('Failed to retrieve the latest version from the registry.'); + handleAndLogError(new Error('Failed to retrieve the latest version from the registry.'), { module: 'latest-version-warning' }); return; } cache.latestVersion = latestVersion; @@ -45,7 +44,7 @@ export default async function (_opts): Promise { // Save updated cache configHandler.set('versionUpgradeWarningCache', cache); } catch (error) { - logger.error('Failed to check the latest version', error); + handleAndLogError(error, { module: 'latest-version-warning' }, 'Failed to check the latest version'); return; } } From 7405f4500f44fa34919fcf93d6d7a80b48eb1341 Mon Sep 17 00:00:00 2001 From: naman-contentstack Date: Tue, 27 Jan 2026 20:27:45 +0530 Subject: [PATCH 2/2] fix: updated error message --- packages/contentstack/src/hooks/prerun/auth-guard.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/contentstack/src/hooks/prerun/auth-guard.ts b/packages/contentstack/src/hooks/prerun/auth-guard.ts index 42fd5a064a..e42bc35bef 100644 --- a/packages/contentstack/src/hooks/prerun/auth-guard.ts +++ b/packages/contentstack/src/hooks/prerun/auth-guard.ts @@ -20,7 +20,7 @@ export default async function (opts): Promise { // Auth guard if (protectedCommands[opts.Command.id]) { if (!isAuthenticated()) { - handleAndLogError(new Error('No auth token found for command.'), { module: 'auth-guard', commandId: opts.Command.id }); + handleAndLogError(new Error('Authentication required for this command'), { module: 'auth-guard', commandId: opts.Command.id }); cliux.error('Please log in to execute the command'); this.exit(); }