Skip to content

Commit f973439

Browse files
author
naman-contentstack
committed
feat: add session file in session based logger
1 parent 97cd57e commit f973439

File tree

17 files changed

+433
-122
lines changed

17 files changed

+433
-122
lines changed

.talismanrc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ fileignoreconfig:
212212
- filename: packages/contentstack-export/test/unit/export/modules/personalize.test.ts
213213
checksum: 83cf034fabee00b42b4243a8c0b8ba280ab7c1e68ffd741c49c31aaee8ca0315
214214
- filename: packages/contentstack-utilities/test/unit/logger.test.ts
215-
checksum: 11778d0252202c18a1ca6a38883d6e12fc324ff86ad0fe058bc2505f9cd66ba3
215+
checksum: a1939dea16166b1893a248179524a76f2ed20b04b99c83bd1a5a13fcf6f0dadc
216216
- filename: packages/contentstack-audit/test/unit/audit-base-command.test.ts
217217
checksum: 17a16b4457c820494442f335d94d0949961e68e8ca72ca0f1fa9d4d0eeb0c17a
218218
- filename: packages/contentstack-import/src/import/modules/taxonomies.ts
@@ -227,4 +227,8 @@ fileignoreconfig:
227227
checksum: 86b11c2a2dd8c0b14aa558e4e52d6d721cd7707422c26a68e96cc5b55b9fefd8
228228
- filename: packages/contentstack-import-setup/src/utils/login-handler.ts
229229
checksum: 3860c96e31677356963e67049762f944aef7c7b22fabb75a70ff5c64cf1ac274
230+
- filename: packages/contentstack-utilities/src/helpers.ts
231+
checksum: 9d7df9d79cec75f238a0072bf79c4934b4724bf1466451ea6f923adfd5c0b75b
232+
- filename: packages/contentstack-utilities/src/logger/session-path.ts
233+
checksum: 4c66980a857bc12012a45e50790c0eaab06883db5e1476d84fb142a08b70b2e7
230234
version: "1.0"

packages/contentstack-audit/src/audit-base-command.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { v4 as uuid } from 'uuid';
55
import isEmpty from 'lodash/isEmpty';
66
import { join, resolve } from 'path';
77
import cloneDeep from 'lodash/cloneDeep';
8-
import { cliux, sanitizePath, TableFlags, TableHeader, log, configHandler } from '@contentstack/cli-utilities';
8+
import { cliux, sanitizePath, TableFlags, TableHeader, log, configHandler, createLogContext } from '@contentstack/cli-utilities';
99
import { createWriteStream, existsSync, mkdirSync, readFileSync, writeFileSync, rmSync } from 'fs';
1010
import config from './config';
1111
import { print } from './util/log';
@@ -50,18 +50,6 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
5050
};
5151
}
5252

53-
/**
54-
* Create audit context object similar to export command
55-
*/
56-
private createAuditContext(moduleName?: string): AuditContext {
57-
return {
58-
command: this.context?.info?.command || 'cm:stacks:audit',
59-
module: moduleName || 'audit',
60-
email: configHandler.get('email') || '',
61-
sessionId: this.context?.sessionId || '',
62-
authenticationMethod: configHandler.get('authenticationMethod') || '',
63-
};
64-
}
6553

6654
/**
6755
* The `start` function performs an audit on content types, global fields, entries, and workflows and displays
@@ -72,7 +60,8 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
7260
async start(command: CommandNames): Promise<boolean> {
7361
this.currentCommand = command;
7462
// Initialize audit context
75-
this.auditContext = this.createAuditContext();
63+
createLogContext(this.context?.info?.command,'', configHandler.get('authenticationMethod'));
64+
this.auditContext = { module: 'audit' };
7665
log.debug(`Starting audit command: ${command}`, this.auditContext);
7766
log.info(`Starting audit command: ${command}`, this.auditContext);
7867

@@ -224,7 +213,8 @@ export abstract class AuditBaseCommand extends BaseCommand<typeof AuditBaseComma
224213
log.debug(`Data module wise: ${JSON.stringify(dataModuleWise)}`, this.auditContext);
225214
for (const module of this.sharedConfig.flags.modules || this.sharedConfig.modules) {
226215
// Update audit context with current module
227-
this.auditContext = this.createAuditContext(module);
216+
createLogContext(this.context?.info?.command, '', configHandler.get('authenticationMethod'));
217+
this.auditContext = { module: module };
228218
log.debug(`Starting audit for module: ${module}`, this.auditContext);
229219
log.info(`Starting audit for module: ${module}`, this.auditContext);
230220

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
11
export interface AuditContext {
2-
command: string;
32
module: string;
4-
email: string | undefined;
5-
sessionId: string | undefined;
6-
clientId?: string;
7-
authenticationMethod?: string;
83
}

packages/contentstack-auth/src/base-command.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Command } from '@contentstack/cli-command';
2-
import { configHandler, Flags, Interfaces, log } from '@contentstack/cli-utilities';
2+
import { configHandler, createLogContext, Flags, getAuthenticationMethod, Interfaces, log } from '@contentstack/cli-utilities';
33
import { Context } from './interfaces';
44

55
export type Args<T extends typeof Command> = Interfaces.InferredArgs<T['args']>;
@@ -16,7 +16,9 @@ export abstract class BaseCommand<T extends typeof Command> extends Command {
1616
*/
1717
public async init(): Promise<void> {
1818
await super.init();
19-
this.contextDetails = { ...this.createExportContext() };
19+
// this.contextDetails = { ...this.createExportContext() };
20+
this.contextDetails = { ...createLogContext(this.context?.info?.command || 'auth', '',) };
21+
2022
}
2123

2224
/**

packages/contentstack-clone/src/commands/cm/stacks/clone.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@ class StackCloneCommand extends Command {
7373
sourceManagementTokenAlias,
7474
destinationManagementTokenAlias,
7575
);
76-
const cloneContext = this.createCloneContext(authenticationMethod);
76+
createLogContext(
77+
this.context?.info?.command || 'cm:stacks:clone',
78+
sourceStackApiKey,
79+
authenticationMethod
80+
);
81+
cloneContext = { module: 'clone' };
7782
log.debug('Starting clone operation setup', cloneContext);
7883

7984
if (externalConfigPath) {

packages/contentstack-export/src/commands/cm/stacks/export.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import {
1313
log,
1414
handleAndLogError,
1515
getLogPath,
16+
createLogContext,
1617
} from '@contentstack/cli-utilities';
1718

1819
import { ModuleExporter } from '../../../export';
19-
import { Context, ExportConfig } from '../../../types';
20+
import { ExportConfig } from '../../../types';
2021
import { setupExportConfig, writeExportMetaFile } from '../../../utils';
2122

2223
export default class ExportCommand extends Command {
@@ -120,9 +121,16 @@ export default class ExportCommand extends Command {
120121
try {
121122
const { flags } = await this.parse(ExportCommand);
122123
const exportConfig = await setupExportConfig(flags);
123-
// Prepare the context object
124-
const context = this.createExportContext(exportConfig.apiKey, exportConfig.authenticationMethod);
125-
exportConfig.context = { ...context };
124+
125+
// Store apiKey in configHandler for session.json (return value not needed)
126+
createLogContext(
127+
this.context?.info?.command || 'cm:stacks:export',
128+
exportConfig.apiKey,
129+
exportConfig.authenticationMethod
130+
);
131+
132+
// For log entries, only pass module (other fields are in session.json)
133+
exportConfig.context = { module: '' };
126134
//log.info(`Using Cli Version: ${this.context?.cliVersion}`, exportConfig.context);
127135

128136
// Assign exportConfig variables
@@ -137,29 +145,15 @@ export default class ExportCommand extends Command {
137145
}
138146
log.success(
139147
`The content of the stack ${exportConfig.apiKey} has been exported successfully!`,
140-
exportConfig.context,
141148
);
142-
log.info(`The exported content has been stored at '${exportDir}'`, exportConfig.context);
143-
log.success(`The log has been stored at '${getLogPath()}'`, exportConfig.context);
149+
log.info(`The exported content has been stored at '${exportDir}'`);
150+
log.success(`The log has been stored at '${getLogPath()}'`);
144151
} catch (error) {
145152
handleAndLogError(error);
146153
log.info(`The log has been stored at '${getLogPath()}'`);
147154
}
148155
}
149156

150-
// Create export context object
151-
private createExportContext(apiKey: string, authenticationMethod?: string): Context {
152-
return {
153-
command: this.context?.info?.command || 'cm:stacks:export',
154-
module: '',
155-
userId: configHandler.get('userUid') || '',
156-
email: configHandler.get('email') || '',
157-
sessionId: this.context?.sessionId || '',
158-
apiKey: apiKey || '',
159-
orgId: configHandler.get('oauthOrgUid') || '',
160-
authenticationMethod: authenticationMethod || 'Basic Auth',
161-
};
162-
}
163157

164158
// Assign values to exportConfig
165159
private assignExportConfig(exportConfig: ExportConfig): void {

packages/contentstack-export/src/types/index.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,7 @@ export interface StackConfig {
130130
limit?: number;
131131
}
132132
export interface Context {
133-
command: string;
134133
module: string;
135-
userId: string | undefined;
136-
email: string | undefined;
137-
sessionId: string | undefined;
138-
clientId?: string | undefined;
139-
apiKey: string;
140-
orgId: string;
141-
authenticationMethod?: string;
142134
}
143135

144136
export { default as DefaultConfig } from './default-config';

packages/contentstack-import-setup/src/commands/cm/stacks/import-setup.ts

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
log,
1313
handleAndLogError,
1414
configHandler,
15+
createLogContext,
1516
} from '@contentstack/cli-utilities';
1617

1718
import { ImportConfig, Context } from '../../../types';
@@ -71,8 +72,13 @@ export default class ImportSetupCommand extends Command {
7172
const { flags } = await this.parse(ImportSetupCommand);
7273
let importSetupConfig = await setupImportConfig(flags);
7374
// Prepare the context object
74-
const context = this.createImportSetupContext(importSetupConfig.apiKey, (importSetupConfig as any).authenticationMethod);
75-
importSetupConfig.context = { ...context };
75+
createLogContext(
76+
this.context?.info?.command || 'cm:stacks:import-setup',
77+
importSetupConfig.apiKey,
78+
configHandler.get('authenticationMethod')
79+
);
80+
81+
importSetupConfig.context = { module: '' };
7682

7783
// Note setting host to create cma client
7884
importSetupConfig.host = this.cmaHost;
@@ -94,17 +100,4 @@ export default class ImportSetupCommand extends Command {
94100
}
95101
}
96102

97-
// Create import setup context object
98-
private createImportSetupContext(apiKey: string, authenticationMethod?: string, module?: string): Context {
99-
return {
100-
command: this.context?.info?.command || 'cm:stacks:import-setup',
101-
module: module || '',
102-
userId: configHandler.get('userUid') || undefined,
103-
email: configHandler.get('email') || undefined,
104-
sessionId: this.context?.sessionId,
105-
apiKey: apiKey || '',
106-
orgId: configHandler.get('oauthOrgUid') || '',
107-
authenticationMethod: authenticationMethod || 'Basic Auth',
108-
};
109-
}
110103
}

packages/contentstack-import-setup/src/types/index.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,5 @@ export type TaxonomyQueryParams = {
154154
};
155155

156156
export interface Context {
157-
command: string;
158157
module: string;
159-
userId: string | undefined;
160-
email: string | undefined;
161-
sessionId: string | undefined;
162-
apiKey: string;
163-
orgId: string;
164-
authenticationMethod?: string;
165158
}

packages/contentstack-import/src/commands/cm/stacks/import.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
handleAndLogError,
1212
configHandler,
1313
getLogPath,
14+
createLogContext,
1415
} from '@contentstack/cli-utilities';
1516

1617
import { Context, ImportConfig } from '../../../types';
@@ -155,8 +156,13 @@ export default class ImportCommand extends Command {
155156
const { flags } = await this.parse(ImportCommand);
156157
importConfig = await setupImportConfig(flags);
157158
// Prepare the context object
158-
const context = this.createImportContext(importConfig.apiKey, importConfig.authenticationMethod);
159-
importConfig.context = { ...context };
159+
createLogContext(
160+
this.context?.info?.command || 'cm:stacks:export',
161+
importConfig.apiKey,
162+
importConfig.authenticationMethod
163+
);
164+
165+
importConfig.context = { module: '' };
160166
//log.info(`Using Cli Version: ${this.context?.cliVersion}`, importConfig.context);
161167

162168
// Note setting host to create cma client
@@ -190,18 +196,4 @@ export default class ImportCommand extends Command {
190196
}
191197
}
192198
}
193-
194-
// Create export context object
195-
private createImportContext(apiKey: string, authenticationMethod?: string): Context {
196-
return {
197-
command: this.context?.info?.command || 'cm:stacks:import',
198-
module: '',
199-
userId: configHandler.get('userUid') || '',
200-
email: configHandler.get('email') || '',
201-
sessionId: this.context?.sessionId,
202-
apiKey: apiKey || '',
203-
orgId: configHandler.get('oauthOrgUid') || '',
204-
authenticationMethod: authenticationMethod || 'Basic Auth',
205-
};
206-
}
207199
}

0 commit comments

Comments
 (0)