File tree 1 file changed +18
-3
lines changed 1 file changed +18
-3
lines changed Original file line number Diff line number Diff line change 1
- import { open } from 'node:fs/promises' ;
1
+ import { existsSync } from 'node:fs' ;
2
+ import { mkdir , open } from 'node:fs/promises' ;
3
+ import { dirname } from 'node:path' ;
2
4
3
5
import { logger } from './common.js' ;
4
6
@@ -9,10 +11,23 @@ import {logger} from './common.js';
9
11
*
10
12
* @example
11
13
* ```ts
12
- * await makeFile('./file.txt');
14
+ * await makeFile('./dir/ file.txt');
13
15
* ```
14
16
*/
15
17
export async function makeEmptyFile ( path : string ) : Promise < void > {
16
18
logger . logMethodArgs ?.( 'makeEmptyFile' , '...' + path . slice ( - 32 ) ) ;
17
- return ( await open ( path , 'w' ) ) . close ( ) ;
19
+ try {
20
+ const pathExists = existsSync ( path ) ;
21
+ if ( ! pathExists ) {
22
+ const dir = dirname ( path ) ;
23
+ if ( ! existsSync ( dir ) ) {
24
+ await mkdir ( dir , { recursive : true } ) ;
25
+ }
26
+ }
27
+ await ( await open ( path , 'w' ) ) . close ( ) ;
28
+ }
29
+ catch ( err ) {
30
+ logger . error ( 'makeEmptyFile' , 'make_file_failed' , { path} , err ) ;
31
+ throw new Error ( 'make_file_failed' , { cause : ( err as Error ) . cause } ) ;
32
+ }
18
33
}
You can’t perform that action at this time.
0 commit comments