Skip to content

Commit

Permalink
Audit fix log sequence issue fix
Browse files Browse the repository at this point in the history
  • Loading branch information
antonyagustine committed Mar 4, 2024
1 parent a3bdd7e commit e025d79
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 34 deletions.
21 changes: 19 additions & 2 deletions packages/contentstack-audit/src/audit-base-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,13 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
let { ctSchema, gfSchema } = this.getCtAndGfSchema();
let missingCtRefs, missingGfRefs, missingEntryRefs;
for (const module of this.sharedConfig.flags.modules || this.sharedConfig.modules) {
ux.action.start(this.$t(this.messages.AUDIT_START_SPINNER, { module }));
print([
{
bold: true,
color: 'whiteBright',
message: this.$t(this.messages.AUDIT_START_SPINNER, { module }),
},
]);

const constructorParam = {
ctSchema,
Expand All @@ -109,7 +115,18 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
break;
}

ux.action.stop();
print([
{
bold: true,
color: 'whiteBright',
message: this.$t(this.messages.AUDIT_START_SPINNER, { module }),
},
{
bold: true,
message: ' done',
color: 'whiteBright',
},
]);
}

return { missingCtRefs, missingGfRefs, missingEntryRefs };
Expand Down
18 changes: 12 additions & 6 deletions packages/contentstack-audit/src/messages/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import memoize from 'lodash/memoize';

const errors = {};

const tableColumnDescriptions = {
Expand Down Expand Up @@ -59,14 +61,18 @@ const messages: typeof errors &
* @returns a string.
*/
function $t(msg: string, args: Record<string, string>): string {
if (!msg) return '';
const transfer = memoize(function (msg: string, args: Record<string, string>) {
if (!msg) return '';

for (const key of Object.keys(args)) {
const escapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
msg = msg.replace(new RegExp(`{${escapedKey}}`, 'g'), args[key] || escapedKey);
}

for (const key of Object.keys(args)) {
const escapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
msg = msg.replace(new RegExp(`{${escapedKey}}`, 'g'), args[key]);
}
return msg;
});

return msg;
return transfer(msg, args);
}

export default messages;
Expand Down
18 changes: 9 additions & 9 deletions packages/contentstack-audit/src/modules/entries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
ExtensionOrAppFieldDataType,
EntryExtensionOrAppFieldDataType,
} from '../types';
import { print } from '../util';
import GlobalField from './global-fields';
import { MarketplaceAppsInstallationData } from '../types/extension';

Expand Down Expand Up @@ -106,14 +107,13 @@ export default class Entries {
}

this.lookForReference([{ locale: code, uid, name: title }], ctSchema, this.entries[entryUid]);
this.log(
$t(auditMsg.SCAN_ENTRY_SUCCESS_MSG, {
title,
local: code,
module: this.config.moduleConfig.entries.name,
}),
'info',
);
const message = $t(auditMsg.SCAN_ENTRY_SUCCESS_MSG, {
title,
local: code,
module: this.config.moduleConfig.entries.name,
});
this.log(message, 'hidden');
print([{ message: `info: ${message}`, color: 'green' }]);
}

if (this.fix) {
Expand All @@ -122,7 +122,7 @@ export default class Entries {
}
}
}
this.log('', 'info'); // Adding empty line
// this.log('', 'info'); // Adding empty line

this.removeEmptyVal();

Expand Down
5 changes: 3 additions & 2 deletions packages/contentstack-audit/src/types/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import config from "../config";

type LogFn = (
message: string | any,
logType?: LoggerType | PrintOptions | undefined
logType?: LoggerType | PrintOptions | undefined,
skipCredentialCheck?: boolean,
) => void;

type ExitFn = (code?: number | undefined) => void;
Expand All @@ -20,7 +21,7 @@ type ConfigType = {
Record<string, any>;

export { LogFn, ExitFn, Partial, ConfigType };
export type LoggerType = "info" | "warn" | "error" | "debug";
export type LoggerType = "info" | "warn" | "error" | "debug" | 'hidden';

export type PrintType = {
message: string;
Expand Down
43 changes: 29 additions & 14 deletions packages/contentstack-audit/src/util/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default class Logger {
private infoLogger!: winston.Logger;
private errorLogger!: winston.Logger;
private config!: Record<string, any>;
private hiddenInfoLogger!: winston.Logger;

get loggerOptions(): winston.transports.FileTransportOptions {
return {
Expand All @@ -41,6 +42,7 @@ export default class Logger {
this.config = config;
this.infoLogger = this.getLoggerInstance('info');
this.errorLogger = this.getLoggerInstance('error');
this.hiddenInfoLogger = this.getLoggerInstance('hidden');
}

/**
Expand All @@ -55,24 +57,30 @@ export default class Logger {
const consoleOptions: winston.transports.ConsoleTransportOptions = {
format: winston.format.combine(winston.format.simple(), winston.format.colorize({ all: true })),
};
const isHidden = logType === 'hidden';
logType = logType === 'hidden' ? 'info' : logType;

if (logType === 'error') {
consoleOptions.level = logType;
}

const filename = normalize(resolve(this.config.basePath, 'logs', `${logType}.log`)).replace(
/^(\.\.(\/|\\|$))+/,
'',
);
const filename = normalize(
resolve(this.config.basePath, 'logs', `${logType}.log`),
).replace(/^(\.\.(\/|\\|$))+/, '');
const transports: winston.transport[] = [
new winston.transports.File({
...this.loggerOptions,
level: logType,
filename,
}),
];

if (!isHidden) {
transports.push(new winston.transports.Console(consoleOptions));
}

const loggerOptions: winston.LoggerOptions = {
transports: [
new winston.transports.File({
...this.loggerOptions,
level: logType,
filename,
}),
new winston.transports.Console(consoleOptions),
],
transports,
levels: customLevels.levels,
};

Expand All @@ -91,8 +99,12 @@ export default class Logger {
* @param {LoggerType | PrintOptions | undefined} [logType] - The `logType` parameter is an optional
* parameter that specifies the type of log. It can be one of the following values:
*/
log(message: string | any, logType?: LoggerType | PrintOptions | undefined): void {
const logString = this.returnString(message);
log(
message: string | any,
logType?: LoggerType | PrintOptions | undefined,
skipCredentialCheck: boolean = false,
): void {
const logString = skipCredentialCheck ? message : this.returnString(message);

switch (logType) {
case 'info':
Expand All @@ -103,6 +115,9 @@ export default class Logger {
case 'error':
this.errorLogger.error(logString);
break;
case 'hidden':
this.hiddenInfoLogger.log('info', logString);
break;
default:
ux.print(logString, logType || {});
break;
Expand Down
2 changes: 1 addition & 1 deletion packages/contentstack-audit/test/unit/util/log.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('Log utility', () => {
const logSpy = sinon.spy(winston, 'createLogger');
const logger = new Logger({ basePath: resolve(__dirname, '..', 'mock') });

expect(logSpy.callCount).to.be.equals(2);
expect(logSpy.callCount).to.be.equals(3);
expect(Object.getPrototypeOf(logger)).has.ownProperty('log');
});
});
Expand Down

0 comments on commit e025d79

Please sign in to comment.