Skip to content

Commit 4e7821d

Browse files
Copiloticlanton
andcommitted
Add createReadStream/createWriteStream to FileSystem in node-core-library and update consumers to use FileSystem instead of fs directly
Agent-Logs-Url: https://github.com/microsoft/rushstack/sessions/5ad20d4d-c9a4-4855-bb13-8dd9e2c1350b Co-authored-by: iclanton <5010588+iclanton@users.noreply.github.com>
1 parent f0d5171 commit 4e7821d

4 files changed

Lines changed: 61 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"changes": [
3+
{
4+
"comment": "Add createReadStream() and createWriteStream() APIs, along with FileSystemReadStream and FileSystemWriteStream type aliases",
5+
"type": "minor",
6+
"packageName": "@rushstack/node-core-library"
7+
}
8+
],
9+
"packageName": "@rushstack/node-core-library",
10+
"email": "198982749+Copilot@users.noreply.github.com"
11+
}

common/reviews/api/node-core-library.api.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,12 +166,14 @@ export class FileSystem {
166166
static copyFilesAsync(options: IFileSystemCopyFilesAsyncOptions): Promise<void>;
167167
static createHardLink(options: IFileSystemCreateLinkOptions): void;
168168
static createHardLinkAsync(options: IFileSystemCreateLinkOptions): Promise<void>;
169+
static createReadStream(filePath: string): FileSystemReadStream;
169170
static createSymbolicLinkFile(options: IFileSystemCreateLinkOptions): void;
170171
static createSymbolicLinkFileAsync(options: IFileSystemCreateLinkOptions): Promise<void>;
171172
static createSymbolicLinkFolder(options: IFileSystemCreateLinkOptions): void;
172173
static createSymbolicLinkFolderAsync(options: IFileSystemCreateLinkOptions): Promise<void>;
173174
static createSymbolicLinkJunction(options: IFileSystemCreateLinkOptions): void;
174175
static createSymbolicLinkJunctionAsync(options: IFileSystemCreateLinkOptions): Promise<void>;
176+
static createWriteStream(filePath: string): FileSystemWriteStream;
175177
static deleteFile(filePath: string, options?: IFileSystemDeleteFileOptions): void;
176178
static deleteFileAsync(filePath: string, options?: IFileSystemDeleteFileOptions): Promise<void>;
177179
static deleteFolder(folderPath: string): void;
@@ -225,9 +227,15 @@ export type FileSystemCopyFilesAsyncFilter = (sourcePath: string, destinationPat
225227
// @public
226228
export type FileSystemCopyFilesFilter = (sourcePath: string, destinationPath: string) => boolean;
227229

230+
// @public
231+
export type FileSystemReadStream = fs.ReadStream;
232+
228233
// @public
229234
export type FileSystemStats = fs.Stats;
230235

236+
// @public
237+
export type FileSystemWriteStream = fs.WriteStream;
238+
231239
// @public
232240
export class FileWriter {
233241
close(): void;

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,24 @@ export type FileSystemStats = fs.Stats;
2828
*/
2929
export type FolderItem = fs.Dirent;
3030

31+
/**
32+
* An alias for the Node.js `fs.ReadStream` object.
33+
*
34+
* @remarks
35+
* This avoids the need to import the `fs` package when using the {@link FileSystem} API.
36+
* @public
37+
*/
38+
export type FileSystemReadStream = fs.ReadStream;
39+
40+
/**
41+
* An alias for the Node.js `fs.WriteStream` object.
42+
*
43+
* @remarks
44+
* This avoids the need to import the `fs` package when using the {@link FileSystem} API.
45+
* @public
46+
*/
47+
export type FileSystemWriteStream = fs.WriteStream;
48+
3149
// The PosixModeBits are intended to be used with bitwise operations.
3250
/* eslint-disable no-bitwise */
3351

@@ -1237,6 +1255,28 @@ export class FileSystem {
12371255
});
12381256
}
12391257

1258+
/**
1259+
* Creates a readable stream for an existing file.
1260+
* Behind the scenes it uses `fs.createReadStream()`.
1261+
*
1262+
* @param filePath - The path to the file. The path may be absolute or relative.
1263+
* @returns A new readable stream for the file.
1264+
*/
1265+
public static createReadStream(filePath: string): FileSystemReadStream {
1266+
return fs.createReadStream(filePath);
1267+
}
1268+
1269+
/**
1270+
* Creates a writable stream for writing to a file.
1271+
* Behind the scenes it uses `fs.createWriteStream()`.
1272+
*
1273+
* @param filePath - The path to the file. The path may be absolute or relative.
1274+
* @returns A new writable stream for the file.
1275+
*/
1276+
public static createWriteStream(filePath: string): FileSystemWriteStream {
1277+
return fs.createWriteStream(filePath);
1278+
}
1279+
12401280
// ===============
12411281
// LINK OPERATIONS
12421282
// ===============

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ export {
5252
FileSystem,
5353
type FileSystemCopyFilesAsyncFilter,
5454
type FileSystemCopyFilesFilter,
55+
type FileSystemReadStream,
56+
type FileSystemWriteStream,
5557
type FolderItem,
5658
type FileSystemStats,
5759
type IFileSystemCopyFileBaseOptions,

0 commit comments

Comments
 (0)