Skip to content

Commit 51c0310

Browse files
[node-core-library] Replace fs-extra with node:fs in FileWriter (#5487)
* Initial plan * Replace fs-extra with node:fs and move flag conversion helper to module scope Co-authored-by: dmichon-msft <[email protected]> * Add Rush change file for node-core-library Co-authored-by: dmichon-msft <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: dmichon-msft <[email protected]>
1 parent 98d7c1a commit 51c0310

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Replace fs-extra with node:fs in FileWriter",
5+
"type": "patch",
6+
"packageName": "@rushstack/node-core-library"
7+
}
8+
],
9+
"packageName": "@rushstack/node-core-library",
10+
"email": "[email protected]"
11+
}

libraries/node-core-library/src/FileWriter.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,30 @@
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
*/
1312
type 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

Comments
 (0)