1
1
import { Command } from '@contentstack/cli-command' ;
2
- import { cliux , flags , configHandler , FlagInput , messageHandler } from '@contentstack/cli-utilities' ;
2
+ import { cliux , flags , configHandler , FlagInput , messageHandler , CLIError } from '@contentstack/cli-utilities' ;
3
3
import { interactive } from '../../../utils' ;
4
+ import { existsSync , statSync } from 'fs' ;
5
+ import { dirname } from 'path' ;
4
6
5
7
export default class LogSetCommand extends Command {
6
8
static description = 'Set logging configuration for CLI' ;
7
9
8
10
static flags : FlagInput = {
9
- ' level' : flags . string ( {
11
+ level : flags . string ( {
10
12
description : 'Set the log level for the CLI.' ,
11
13
options : [ 'debug' , 'info' , 'warn' , 'error' ] ,
12
14
} ) ,
13
- ' path' : flags . string ( {
15
+ path : flags . string ( {
14
16
description : ' Specify the file path where logs should be saved.' ,
15
17
} ) ,
16
18
} ;
@@ -33,6 +35,16 @@ export default class LogSetCommand extends Command {
33
35
logPath = await interactive . askLogPath ( ) ;
34
36
}
35
37
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
+
36
48
const currentLoggingConfig = configHandler . get ( 'log' ) || { } ;
37
49
if ( logLevel ) currentLoggingConfig . level = logLevel ;
38
50
if ( logPath ) currentLoggingConfig . path = logPath ;
0 commit comments