From 4291e9151164f998d9a4fec31f19f789c514ec85 Mon Sep 17 00:00:00 2001 From: azlam Date: Mon, 7 Aug 2023 19:22:00 +1000 Subject: [PATCH] fix(logs): refactor to use printHeader from sfpLogger --- .../core/src/package/SfpPackageBuilder.ts | 11 +- .../package/packageCreators/CreatePackage.ts | 23 +-- .../CreateSourcePackageImpl.ts | 39 ++-- .../InstallSourcePackageImpl.ts | 2 +- packages/sfplogger/src/SFPLogger.ts | 187 ++++++++++-------- 5 files changed, 130 insertions(+), 132 deletions(-) diff --git a/packages/core/src/package/SfpPackageBuilder.ts b/packages/core/src/package/SfpPackageBuilder.ts index 928bd9125..3a4c0b12e 100644 --- a/packages/core/src/package/SfpPackageBuilder.ts +++ b/packages/core/src/package/SfpPackageBuilder.ts @@ -22,6 +22,7 @@ import PackageVersionUpdater from './version/PackageVersionUpdater'; import { AnalyzerRegistry } from './analyser/AnalyzerRegistry'; import { ComponentSet } from '@salesforce/source-deploy-retrieve'; import CreateDiffPackageImp from './packageCreators/CreateDiffPackageImpl'; +import { COLOR_WARNING } from '@dxatscale/sfp-logger'; export default class SfpPackageBuilder { public static async buildPackageFromProjectDirectory( @@ -238,16 +239,16 @@ export default class SfpPackageBuilder { sfpPackage.isApexFound == true && sfpPackage.apexTestClassses == null) ) { + SFPLogger.printHeaderLine('WARNING! NON OPTIMAL DEPLOYMENT',COLOR_WARNING,LoggerLevel.INFO,logger); SFPLogger.log( - ` ----------------------------------WARNING! NON OPTIMAL DEPLOYMENT--------------------------------------------${EOL}` + - `This package has apex classes/triggers, In order to deploy optimally, each class need to have a minimum ${EOL}` + - `75% test coverage,We are unable to find any test classes in the given package, hence will be deploying ${EOL}` + + `This package has apex classes/triggers, In order to deploy optimally, each class need to have a minimum` + + `75% test coverage,We are unable to find any test classes in the given package, hence will be deploying` + `via triggering all local tests,This definitely is not optimal approach on large orgs` + - `Please consider adding test classes for the classes in the package ${EOL}` + - `-------------------------------------------------------------------------------------------------------------`, + `Please consider adding test classes for the classes in the package` + LoggerLevel.INFO, logger ); + SFPLogger.printHeaderLine('',COLOR_WARNING,LoggerLevel.INFO,logger); return true; } else return false; } diff --git a/packages/core/src/package/packageCreators/CreatePackage.ts b/packages/core/src/package/packageCreators/CreatePackage.ts index 2c83c2a1d..8d9133dcd 100644 --- a/packages/core/src/package/packageCreators/CreatePackage.ts +++ b/packages/core/src/package/packageCreators/CreatePackage.ts @@ -103,10 +103,9 @@ export abstract class CreatePackage { abstract isEmptyPackage(projectDirectory: string, packageDirectory: string); protected printEmptyArtifactWarning() { - SFPLogger.log( - `${COLOR_WARNING( - `---------------------WARNING! Empty aritfact encountered-------------------------------` - )}`, + SFPLogger.printHeaderLine( + `WARNING! Empty aritfact encountered`, + COLOR_WARNING, LoggerLevel.INFO, this.logger ); @@ -116,13 +115,7 @@ export abstract class CreatePackage { this.logger ); SFPLogger.log('Proceeding to create an empty artifact', LoggerLevel.INFO, this.logger); - SFPLogger.log( - `${COLOR_WARNING( - `---------------------------------------------------------------------------------------` - )}`, - LoggerLevel.INFO, - this.logger - ); + SFPLogger.printHeaderLine('',COLOR_WARNING,LoggerLevel.INFO,this.logger); } private printHeader() { @@ -146,13 +139,7 @@ export abstract class CreatePackage { this.printAdditionalPackageSpecificHeaders(); - SFPLogger.log( - `${COLOR_HEADER( - `-------------------------------------------------------------------------------------------` - )}`, - LoggerLevel.INFO, - this.logger - ); + SFPLogger.printHeaderLine('',COLOR_HEADER,LoggerLevel.INFO,this.logger); } abstract printAdditionalPackageSpecificHeaders(); diff --git a/packages/core/src/package/packageCreators/CreateSourcePackageImpl.ts b/packages/core/src/package/packageCreators/CreateSourcePackageImpl.ts index 9561acf72..0d9afb51a 100644 --- a/packages/core/src/package/packageCreators/CreateSourcePackageImpl.ts +++ b/packages/core/src/package/packageCreators/CreateSourcePackageImpl.ts @@ -7,6 +7,9 @@ import SfpPackage, { PackageType, SfpPackageParams } from '../SfpPackage'; import { CreatePackage } from './CreatePackage'; import { PackageCreationParams } from '../SfpPackageBuilder'; import { ZERO_BORDER_TABLE } from '../../display/TableConstants'; +import { COLOR_INFO } from '@dxatscale/sfp-logger'; +import { COLOR_HEADER } from '@dxatscale/sfp-logger'; +import { COLOR_WARNING } from '@dxatscale/sfp-logger'; const Table = require('cli-table'); export default class CreateSourcePackageImpl extends CreatePackage { @@ -51,11 +54,7 @@ export default class CreateSourcePackageImpl extends CreatePackage { sfpPackage.isTriggerAllTests = true; } else if (sfpPackage.isApexFound && classTypes?.testClass?.length > 0) { if (classTypes?.parseError?.length > 0) { - SFPLogger.log( - '---------------------------------------------------------------------------------------', - LoggerLevel.INFO, - this.logger - ); + SFPLogger.printHeaderLine('',COLOR_HEADER,LoggerLevel.INFO,this.logger); SFPLogger.log( 'Unable to parse these classes to correctly identify test classes, Its not your issue, its ours!'+ 'Please raise a issue in our repo!', @@ -77,11 +76,7 @@ export default class CreateSourcePackageImpl extends CreatePackage { } private printHintForOptimizedDeployment() { - SFPLogger.log( - `---------------- OPTION FOR DEPLOYMENT OPTIMIZATION AVAILABLE-----------------------------------`, - null, - this.logger - ); + SFPLogger.printHeaderLine('OPTION FOR DEPLOYMENT OPTIMIZATION AVAILABLE',COLOR_HEADER,LoggerLevel.INFO,this.logger); SFPLogger.log( `Following apex test classes were identified and can be used for deploying this package,${EOL}` + `in an optimal manner, provided each individual class meets the test coverage requirement of 75% and above${EOL}` + @@ -89,31 +84,19 @@ export default class CreateSourcePackageImpl extends CreatePackage { null, this.logger ); - SFPLogger.log( - `-----------------------------------------------------------------------------------------------`, - LoggerLevel.INFO, - this.logger - ); + SFPLogger.printHeaderLine('',COLOR_HEADER,LoggerLevel.INFO,this.logger); } private printSlowDeploymentWarning() { + SFPLogger.printHeaderLine('WARNING! YOU MIGHT NOT BE ABLE TO DEPLOY OR WILL HAVE A SLOW DEPLOYMENT',COLOR_WARNING,LoggerLevel.INFO,this.logger); SFPLogger.log( - `-------WARNING! YOU MIGHT NOT BE ABLE TO DEPLOY OR WILL HAVE A SLOW DEPLOYMENT---------------`, - LoggerLevel.INFO, - this.logger - ); - SFPLogger.log( - `This package has apex classes/triggers, however apex test classes were not found, You would not be able to deploy${EOL}` + - `to production org optimally if each class do not have coverage of 75% and above,We will attempt deploying${EOL}` + - `this package by triggering all local tests in the org which could be realy costly in terms of deployment time!${EOL}`, + `This package has apex classes/triggers, however apex test classes were not found, You would not be able to deploy` + + `to production org optimally if each class do not have coverage of 75% and above,We will attempt deploying` + + `this package by triggering all local tests in the org which could be realy costly in terms of deployment time!`, null, this.logger ); - SFPLogger.log( - `---------------------------------------------------------------------------------------------`, - LoggerLevel.INFO, - this.logger - ); + SFPLogger.printHeaderLine('',COLOR_HEADER,LoggerLevel.INFO,this.logger); } private printClassesIdentified(fetchedClasses: FileDescriptor[]) { diff --git a/packages/core/src/package/packageInstallers/InstallSourcePackageImpl.ts b/packages/core/src/package/packageInstallers/InstallSourcePackageImpl.ts index 091a187bd..8a2eb0615 100644 --- a/packages/core/src/package/packageInstallers/InstallSourcePackageImpl.ts +++ b/packages/core/src/package/packageInstallers/InstallSourcePackageImpl.ts @@ -239,7 +239,7 @@ export default class InstallSourcePackageImpl extends InstallPackage { have dependency to components in the manifest. \ Please check whether this manifest works! \ Acutal Error Observed: \ - ----------------------------------------------------------------------- \ + -------------------------------------- \ ${COLOR_ERROR(error.message)}`, LoggerLevel.INFO, this.logger diff --git a/packages/sfplogger/src/SFPLogger.ts b/packages/sfplogger/src/SFPLogger.ts index fd4dfc4aa..8b058e2b1 100644 --- a/packages/sfplogger/src/SFPLogger.ts +++ b/packages/sfplogger/src/SFPLogger.ts @@ -3,41 +3,41 @@ import { EOL } from 'os'; import chalk = require('chalk'); export enum LoggerLevel { - TRACE = 10, - DEBUG = 20, - INFO = 30, - WARN = 40, - ERROR = 50, - FATAL = 60, - HIDE = 70, + TRACE = 10, + DEBUG = 20, + INFO = 30, + WARN = 40, + ERROR = 50, + FATAL = 60, + HIDE = 70, } const enum LoggerType { - console = 1, - file = 2, - void = 3, + console = 1, + file = 2, + void = 3, } export class ConsoleLogger implements Logger { - public logType: LoggerType; - constructor() { - this.logType = LoggerType.console; - } + public logType: LoggerType; + constructor() { + this.logType = LoggerType.console; + } } export class VoidLogger implements Logger { - public logType: LoggerType; - constructor() { - this.logType = LoggerType.void; - } + public logType: LoggerType; + constructor() { + this.logType = LoggerType.void; + } } export class FileLogger implements Logger { - public logType: LoggerType; - constructor(public path: string) { - this.logType = LoggerType.file; - } + public logType: LoggerType; + constructor(public path: string) { + this.logType = LoggerType.file; + } } export interface Logger { - logType: LoggerType; - path?: string; + logType: LoggerType; + path?: string; } export const COLOR_ERROR = chalk.bold.red; @@ -52,67 +52,94 @@ export const COLOR_KEY_MESSAGE = chalk.magentaBright.bold; export const COLOR_KEY_VALUE = chalk.black.bold.bgGreenBright; export default class SFPLogger { - public static logLevel: LoggerLevel = LoggerLevel.INFO; - public static isLogsDisabled: boolean = false; + public static logLevel: LoggerLevel = LoggerLevel.INFO; + public static isLogsDisabled: boolean = false; - static enableColor() { - chalk.level = 2; - } + static enableColor() { + chalk.level = 2; + } - static disableColor() { - chalk.level = 0; - } + static disableColor() { + chalk.level = 0; + } - static log(message: string, logLevel = LoggerLevel.INFO, logger?: Logger) { - if (SFPLogger.isLogsDisabled) - return; - if (logLevel == null) logLevel = LoggerLevel.INFO; - - if (logLevel < this.logLevel) return; - - //Todo: Proper fix - if (logger && logger.logType === LoggerType.console) { - logger = null; //Make it nullable, so it goes to console - } - - if (logger) { - if (logger.logType === LoggerType.void) { - return; - } else if (logger.logType === LoggerType.file) { - let fileLogger = logger as FileLogger; - message = message - ?.toString() - .replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, ''); - fs.appendFileSync(fileLogger.path, message + EOL, 'utf8'); - } - } else { - switch (logLevel) { - case LoggerLevel.TRACE: - console.log(COLOR_TRACE(message)); - break; - - case LoggerLevel.DEBUG: - console.log(COLOR_DEBUG(message)); - break; - - case LoggerLevel.INFO: - console.log(message); - break; - - case LoggerLevel.WARN: - console.log(COLOR_WARNING(message)); - break; - - case LoggerLevel.ERROR: - console.log(COLOR_ERROR(message)); - break; - } - - } + static log(message: string, logLevel = LoggerLevel.INFO, logger?: Logger) { + if (SFPLogger.isLogsDisabled) return; + if (logLevel == null) logLevel = LoggerLevel.INFO; + if (logLevel < this.logLevel) return; + + // Split message into lines of 90 characters + const maxLineLength = 120; + const lines = []; + for (let i = 0; i < message.length; i += maxLineLength) { + lines.push(message.substring(i, i + maxLineLength)); + } + + //Todo: Proper fix + if (logger && logger.logType === LoggerType.console) { + logger = null; // Make it nullable, so it goes to console } - static disableLogs() { - SFPLogger.isLogsDisabled = true; + if (logger) { + if (logger.logType === LoggerType.void) { + return; + } else if (logger.logType === LoggerType.file) { + let fileLogger = logger as FileLogger; + lines.forEach(line => { + line = line.replace(/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, ''); + fs.appendFileSync(fileLogger.path, line + EOL, 'utf8'); + }); + } + } else { + lines.forEach(line => { + switch (logLevel) { + case LoggerLevel.TRACE: + console.log(COLOR_TRACE(line)); + break; + + case LoggerLevel.DEBUG: + console.log(COLOR_DEBUG(line)); + break; + + case LoggerLevel.INFO: + console.log(line); + break; + + case LoggerLevel.WARN: + console.log(COLOR_WARNING(line)); + break; + + case LoggerLevel.ERROR: + console.log(COLOR_ERROR(line)); + break; + } + }); } + } + + static disableLogs() { + SFPLogger.isLogsDisabled = true; + } + + static printHeaderLine(header, color: chalk.Chalk, logLevel, logger?: Logger) { + if (header == null) + header = ''; + const lineLength = 90; + const leftPadLength = Math.floor((lineLength - header.length) / 2); + const rightPadLength = lineLength - leftPadLength - header.length; + const line = '-'.repeat(leftPadLength) + `${header}` + '-'.repeat(rightPadLength); + if (logger) { + if (logger.logType === LoggerType.void) { + return; + } else if (logger.logType === LoggerType.file) { + return; + } + else + console.log(color(line)); + } + else + console.log(color(line)); + + } }