Skip to content

Commit 6ed6d46

Browse files
authored
Merge pull request #2021 from contentstack/feat/DX-3286
feat: added support of show-console-logs flag in config:set:log command
2 parents b327d30 + 55b0140 commit 6ed6d46

File tree

3 files changed

+323
-194
lines changed

3 files changed

+323
-194
lines changed

packages/contentstack-config/messages/index.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"CLI_CONFIG_SET_REGION_DESCRIPTION": "Set region for CLI",
33
"CLI_CONFIG_SET_REGION_FLAG_D_DESCRIPTION": "Custom host to set for content delivery API, if this flag is added then cma and name flags are required",
4-
"CLI_CONFIG_SET_REGION_FLAG_M_DESCRIPTION": "Custom host to set for content management API, if this flag is added then cda and name flags are required",
4+
"CLI_CONFIG_SET_REGION_FLAG_M_DESCRIPTION": "Custom host to set for content management API, , if this flag is added then cda and name flags are required",
55
"CLI_CONFIG_SET_REGION_FLAG_N_DESCRIPTION": "Name for the region, if this flag is added then cda and cma flags are required",
6-
"CLI_CONFIG_SET_REGION_DEFAULT": "No argument or custom flag provided. Setting region to default AWS-NA",
6+
"CLI_CONFIG_SET_REGION_DEFAULT": "No argument or custom flag provided. Setting region to default NA",
77
"CLI_CONFIG_GET_REGION_DESCRIPTION": "Get current region set for CLI",
88
"CLI_CONFIG_GET_REGION_NOT_FOUND": "No region found, please set by running command $ csdx config:set:region",
99
"CLI_CONFIG_INQUIRE_REGION_NAME": "Enter custom region name",
@@ -19,5 +19,6 @@
1919
"CLI_CONFIG_LOG_NO_CONFIG": "No config found, please set by running command $ csdx config:set:log",
2020
"CLI_CONFIG_LOG_SET_SUCCESS": "Log config set successfully",
2121
"CLI_CONFIG_LOG_LEVEL_SET": "Log level: '%s'",
22-
"CLI_CONFIG_LOG_PATH_SET": "Log path: '%s'"
23-
}
22+
"CLI_CONFIG_LOG_PATH_SET": "Log path: '%s'",
23+
"CLI_CONFIG_LOG_CONSOLE_SET": "Log console: '%s'"
24+
}

packages/contentstack-config/src/commands/config/set/log.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
11
import { Command } from '@contentstack/cli-command';
2-
import { cliux, flags, configHandler, FlagInput, messageHandler, CLIError } from '@contentstack/cli-utilities';
2+
import { cliux, flags, configHandler, FlagInput, messageHandler } from '@contentstack/cli-utilities';
33
import { interactive } from '../../../utils';
4-
import { existsSync, statSync } from 'fs';
5-
import { dirname } from 'path';
64

75
export default class LogSetCommand extends Command {
86
static description = 'Set logging configuration for CLI';
97

108
static flags: FlagInput = {
11-
level: flags.string({
9+
'level': flags.string({
1210
description: 'Set the log level for the CLI.',
1311
options: ['debug', 'info', 'warn', 'error'],
1412
}),
15-
path: flags.string({
16-
description: ' Specify the file path where logs should be saved.',
13+
'path': flags.string({
14+
description: 'Specify the file path where logs should be saved.',
1715
}),
16+
'show-console-logs': flags.boolean({
17+
description: 'Enable console logging.',
18+
allowNo: true, // no-show-console-logs
19+
default: false,
20+
})
1821
};
1922

20-
static examples = ['csdx config:set:log', 'csdx config:set:log --level debug --path ./logs/app.log'];
23+
24+
static examples = [
25+
'csdx config:set:log',
26+
'csdx config:set:log --level debug --path ./logs/app.log --show-console-logs',
27+
'csdx config:set:log --no-show-console-logs',
28+
];
2129

2230
async run() {
2331
try {
2432
const { flags } = await this.parse(LogSetCommand);
2533

2634
let logLevel: string = flags['level'];
2735
let logPath: string = flags['path'];
36+
const showConsoleLogs: boolean = flags['show-console-logs'];
2837

2938
// Interactive prompts if not passed via flags
3039
if (!logLevel) {
@@ -35,23 +44,16 @@ export default class LogSetCommand extends Command {
3544
logPath = await interactive.askLogPath();
3645
}
3746

38-
if (logPath) {
39-
const logDir = dirname(logPath);
40-
// Check if the directory part of the path exists and is actually a file
41-
if (existsSync(logDir) && statSync(logDir).isFile()) {
42-
throw new CLIError({
43-
message: `The directory path '${logDir}' is a file, not a directory. Please provide a valid directory path for the log file.`,
44-
});
45-
}
46-
}
47-
4847
const currentLoggingConfig = configHandler.get('log') || {};
4948
if (logLevel) currentLoggingConfig.level = logLevel;
5049
if (logPath) currentLoggingConfig.path = logPath;
50+
currentLoggingConfig['show-console-logs'] = showConsoleLogs;
5151

5252
configHandler.set('log', currentLoggingConfig);
53+
5354
cliux.success(messageHandler.parse('CLI_CONFIG_LOG_LEVEL_SET', logLevel));
5455
cliux.success(messageHandler.parse('CLI_CONFIG_LOG_PATH_SET', logPath));
56+
cliux.success(messageHandler.parse('CLI_CONFIG_LOG_CONSOLE_SET', String(showConsoleLogs)));
5557
cliux.print(messageHandler.parse('CLI_CONFIG_LOG_SET_SUCCESS'), { color: 'green' });
5658
} catch (error) {
5759
cliux.error('error', error);

0 commit comments

Comments
 (0)