11// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
22// See LICENSE in the project root for license information.
33
4- import type { FileSystemStats } from './FileSystem' ;
5- import { Import } from './Import' ;
4+ import * as fs from 'node:fs' ;
65
7- const fsx : typeof import ( 'fs-extra' ) = Import . lazy ( 'fs-extra' , require ) ;
6+ import type { FileSystemStats } from './FileSystem' ;
87
98/**
109 * Available file handle opening flags.
1110 * @public
1211 */
1312type NodeFileFlags = 'r' | 'r+' | 'rs+' | 'w' | 'wx' | 'w+' | 'wx+' | 'a' | 'ax' | 'a+' | 'ax+' ;
1413
14+ /**
15+ * Helper function to convert the file writer array to a Node.js style string (e.g. "wx" or "a").
16+ * @param flags - The flags that should be converted.
17+ */
18+ function convertFlagsForNode ( flags : IFileWriterFlags | undefined ) : NodeFileFlags {
19+ flags = {
20+ append : false ,
21+ exclusive : false ,
22+ ...flags
23+ } ;
24+ const result : NodeFileFlags = `${ flags . append ? 'a' : 'w' } ${ flags . exclusive ? 'x' : '' } ` as NodeFileFlags ;
25+ return result ;
26+ }
27+
1528/**
1629 * Interface which represents the flags about which mode the file should be opened in.
1730 * @public
@@ -60,20 +73,7 @@ export class FileWriter {
6073 * @param flags - The flags for opening the handle
6174 */
6275 public static open ( filePath : string , flags ?: IFileWriterFlags ) : FileWriter {
63- return new FileWriter ( fsx . openSync ( filePath , FileWriter . _convertFlagsForNode ( flags ) ) , filePath ) ;
64- }
65-
66- /**
67- * Helper function to convert the file writer array to a Node.js style string (e.g. "wx" or "a").
68- * @param flags - The flags that should be converted.
69- */
70- private static _convertFlagsForNode ( flags : IFileWriterFlags | undefined ) : NodeFileFlags {
71- flags = {
72- append : false ,
73- exclusive : false ,
74- ...flags
75- } ;
76- return [ flags . append ? 'a' : 'w' , flags . exclusive ? 'x' : '' ] . join ( '' ) as NodeFileFlags ;
76+ return new FileWriter ( fs . openSync ( filePath , convertFlagsForNode ( flags ) ) , filePath ) ;
7777 }
7878
7979 /**
@@ -86,7 +86,7 @@ export class FileWriter {
8686 throw new Error ( `Cannot write to file, file descriptor has already been released.` ) ;
8787 }
8888
89- fsx . writeSync ( this . _fileDescriptor , text ) ;
89+ fs . writeSync ( this . _fileDescriptor , text ) ;
9090 }
9191
9292 /**
@@ -100,7 +100,7 @@ export class FileWriter {
100100 const fd : number | undefined = this . _fileDescriptor ;
101101 if ( fd ) {
102102 this . _fileDescriptor = undefined ;
103- fsx . closeSync ( fd ) ;
103+ fs . closeSync ( fd ) ;
104104 }
105105 }
106106
@@ -113,6 +113,6 @@ export class FileWriter {
113113 throw new Error ( `Cannot get file statistics, file descriptor has already been released.` ) ;
114114 }
115115
116- return fsx . fstatSync ( this . _fileDescriptor ) ;
116+ return fs . fstatSync ( this . _fileDescriptor ) ;
117117 }
118118}
0 commit comments