Skip to content

Commit 2d37cbd

Browse files
committed
refactor: progress & summary manager
1 parent 0d04d20 commit 2d37cbd

File tree

6 files changed

+179
-101
lines changed

6 files changed

+179
-101
lines changed

packages/contentstack-utilities/src/constants/logging.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ export const logLevels = {
22
error: 0,
33
warn: 1,
44
info: 2,
5-
success: 2, // Maps to info level but with different type
5+
success: 2, // Maps to info level but with different type
66
debug: 3,
7-
verbose: 4
7+
verbose: 4,
88
} as const;
99

1010
// 2. Create color mappings (for console only)
1111
export const levelColors = {
1212
error: 'red',
1313
warn: 'yellow',
14-
success: 'green', // Custom color for success
14+
success: 'green', // Custom color for success
1515
info: 'white',
16-
debug: 'blue'
17-
};
16+
debug: 'blue',
17+
};
18+
19+
export const PROGRESS_SUPPORTED_MODULES = ['export', 'import'] as const;

packages/contentstack-utilities/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,5 @@ export { default as TablePrompt } from './inquirer-table-prompt';
7777

7878
export { Logger };
7979
export { default as authenticationHandler } from './authentication-handler';
80-
export {v2Logger as log, cliErrorHandler, handleAndLogError, getLogPath} from './logger/log'
81-
export {CLIProgressManager, SummaryManager} from './progress-summary'
80+
export { v2Logger as log, cliErrorHandler, handleAndLogError, getLogPath } from './logger/log';
81+
export { CLIProgressManager, SummaryManager } from './progress-summary';

packages/contentstack-utilities/src/interfaces/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,5 @@ export interface ModuleResult {
160160
export interface SummaryOptions {
161161
operationName: string; // 'EXPORT', 'IMPORT', 'MIGRATION', etc.
162162
context?: any;
163+
branchName?: string; // Optional branch name for operations
163164
}

packages/contentstack-utilities/src/logger/logger.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import traverse from 'traverse';
22
import { klona } from 'klona/full';
33
import { normalize } from 'path';
44
import * as winston from 'winston';
5-
import { levelColors, logLevels } from '../constants/logging';
5+
import { levelColors, logLevels, PROGRESS_SUPPORTED_MODULES } from '../constants/logging';
66
import { LoggerConfig, LogLevel, LogType } from '../interfaces/index';
77
import { configHandler } from '..';
88

@@ -58,8 +58,22 @@ export default class Logger {
5858
}),
5959
];
6060

61-
// Add console transport only if showConsoleLogs is true
62-
if (configHandler && typeof configHandler.get === 'function' && configHandler.get('showConsoleLogs')) {
61+
// Determine console logging based on configuration
62+
let showConsoleLogs = true;
63+
64+
if (configHandler && typeof configHandler.get === 'function') {
65+
const logConfig = configHandler.get('log') || {};
66+
const hasProgressSupport = PROGRESS_SUPPORTED_MODULES.includes(logConfig.progressSupportedModule);
67+
if (hasProgressSupport) {
68+
// Plugin has progress bars - respect user's showConsoleLogs setting
69+
showConsoleLogs = logConfig.showConsoleLogs ?? true;
70+
} else {
71+
// Plugin doesn't have progress support - always show console logs
72+
showConsoleLogs = true;
73+
}
74+
}
75+
76+
if (showConsoleLogs) {
6377
transports.push(
6478
new winston.transports.Console({
6579
format: winston.format.combine(
@@ -110,9 +124,8 @@ export default class Logger {
110124

111125
private shouldLog(level: LogType, target: 'console' | 'file'): boolean {
112126
const configLevel = target === 'console' ? this.config.consoleLogLevel : this.config.logLevel;
113-
const minLevel = configLevel ? logLevels[configLevel] : logLevels['info']; // Default to info level
114-
const currentLevel = logLevels[level] || logLevels['info']; // Handle undefined levels
115-
return currentLevel <= minLevel;
127+
const minLevel = configLevel ? logLevels[configLevel] : 2;
128+
return logLevels[level] <= minLevel;
116129
}
117130

118131
/* === Public Log Methods === */
@@ -137,7 +150,7 @@ export default class Logger {
137150

138151
public success(message: string, meta?: any): void {
139152
if (this.shouldLog('success', 'console') || this.shouldLog('success', 'file')) {
140-
this.loggers.success.info(message, { ...meta, type: 'success' });
153+
this.loggers.success.log('success', message, { ...meta });
141154
}
142155
}
143156

@@ -245,7 +258,7 @@ export default class Logger {
245258
},
246259
};
247260
if (this.shouldLog('success', 'console') || this.shouldLog('success', 'file')) {
248-
this.loggers.success.info(logPayload);
261+
this.loggers.success.log(logPayload);
249262
}
250263
}
251264

0 commit comments

Comments
 (0)