Skip to content

Commit c774a67

Browse files
committed
Expand FileSystem.createWriteStream.
1 parent 342a83b commit c774a67

3 files changed

Lines changed: 44 additions & 28 deletions

File tree

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ export class FileSystem {
174174
static createSymbolicLinkJunction(options: IFileSystemCreateLinkOptions): void;
175175
static createSymbolicLinkJunctionAsync(options: IFileSystemCreateLinkOptions): Promise<void>;
176176
static createWriteStream(filePath: string, options?: IFileSystemCreateWriteStreamOptions): FileSystemWriteStream;
177+
static createWriteStreamAsync(filePath: string, options?: IFileSystemCreateWriteStreamOptions): Promise<FileSystemWriteStream>;
177178
static deleteFile(filePath: string, options?: IFileSystemDeleteFileOptions): void;
178179
static deleteFileAsync(filePath: string, options?: IFileSystemDeleteFileOptions): Promise<void>;
179180
static deleteFolder(folderPath: string): void;
@@ -345,8 +346,7 @@ export interface IFileSystemCreateLinkOptions {
345346
}
346347

347348
// @public
348-
export interface IFileSystemCreateWriteStreamOptions {
349-
ensureFolderExists?: boolean;
349+
export interface IFileSystemCreateWriteStreamOptions extends IFileSystemWriteFileOptionsBase {
350350
}
351351

352352
// @public
@@ -355,9 +355,8 @@ export interface IFileSystemDeleteFileOptions {
355355
}
356356

357357
// @public
358-
export interface IFileSystemMoveOptions {
358+
export interface IFileSystemMoveOptions extends IFileSystemWriteFileOptionsBase {
359359
destinationPath: string;
360-
ensureFolderExists?: boolean;
361360
overwrite?: boolean;
362361
sourcePath: string;
363362
}
@@ -380,8 +379,7 @@ export interface IFileSystemUpdateTimeParameters {
380379
}
381380

382381
// @public
383-
export interface IFileSystemWriteBinaryFileOptions {
384-
ensureFolderExists?: boolean;
382+
export interface IFileSystemWriteBinaryFileOptions extends IFileSystemWriteFileOptionsBase {
385383
}
386384

387385
// @public
@@ -390,6 +388,11 @@ export interface IFileSystemWriteFileOptions extends IFileSystemWriteBinaryFileO
390388
encoding?: Encoding;
391389
}
392390

391+
// @public (undocumented)
392+
export interface IFileSystemWriteFileOptionsBase {
393+
ensureFolderExists?: boolean;
394+
}
395+
393396
// @public
394397
export interface IFileWriterFlags {
395398
append?: boolean;

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

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,22 @@ export interface IFileSystemReadFolderOptions {
6262
}
6363

6464
/**
65-
* The options for {@link FileSystem.writeBuffersToFile}
6665
* @public
6766
*/
68-
export interface IFileSystemWriteBinaryFileOptions {
67+
export interface IFileSystemWriteFileOptionsBase {
6968
/**
7069
* If true, will ensure the folder is created before writing the file.
7170
* @defaultValue false
7271
*/
7372
ensureFolderExists?: boolean;
7473
}
7574

75+
/**
76+
* The options for {@link FileSystem.writeBuffersToFile}
77+
* @public
78+
*/
79+
export interface IFileSystemWriteBinaryFileOptions extends IFileSystemWriteFileOptionsBase {}
80+
7681
/**
7782
* The options for {@link FileSystem.writeFile}
7883
* @public
@@ -113,7 +118,7 @@ export interface IFileSystemReadFileOptions {
113118
* The options for {@link FileSystem.move}
114119
* @public
115120
*/
116-
export interface IFileSystemMoveOptions {
121+
export interface IFileSystemMoveOptions extends IFileSystemWriteFileOptionsBase {
117122
/**
118123
* The path of the existing object to be moved.
119124
* The path may be absolute or relative.
@@ -131,12 +136,6 @@ export interface IFileSystemMoveOptions {
131136
* @defaultValue true
132137
*/
133138
overwrite?: boolean;
134-
135-
/**
136-
* If true, will ensure the folder is created before writing the file.
137-
* @defaultValue false
138-
*/
139-
ensureFolderExists?: boolean;
140139
}
141140

142141
/**
@@ -280,13 +279,7 @@ export interface IFileSystemCopyFilesOptions extends IFileSystemCopyFilesAsyncOp
280279
* The options for {@link FileSystem.createWriteStream}
281280
* @public
282281
*/
283-
export interface IFileSystemCreateWriteStreamOptions {
284-
/**
285-
* If true, will ensure the folder is created before writing the file.
286-
* @defaultValue false
287-
*/
288-
ensureFolderExists?: boolean;
289-
}
282+
export interface IFileSystemCreateWriteStreamOptions extends IFileSystemWriteFileOptionsBase {}
290283

291284
/**
292285
* The options for {@link FileSystem.deleteFile}
@@ -780,10 +773,11 @@ export class FileSystem {
780773
* Writes a text string to a file on disk, overwriting the file if it already exists.
781774
* Behind the scenes it uses `fs.writeFileSync()`.
782775
* @remarks
783-
* Throws an error if the folder doesn't exist, unless ensureFolder=true.
776+
* Throws an error if the folder doesn't exist, unless {@link IFileSystemWriteFileOptionsBase.ensureFolderExists}
777+
* is set to `true`.
784778
* @param filePath - The absolute or relative path of the file.
785779
* @param contents - The text that should be written to the file.
786-
* @param options - Optional settings that can change the behavior. Type: `IWriteFileOptions`
780+
* @param options - Optional settings that can change the behavior.
787781
*/
788782
public static writeFile(
789783
filePath: string,
@@ -826,7 +820,8 @@ export class FileSystem {
826820
* multiple sources.
827821
*
828822
* @remarks
829-
* Throws an error if the folder doesn't exist, unless ensureFolder=true.
823+
* Throws an error if the folder doesn't exist, unless {@link IFileSystemWriteFileOptionsBase.ensureFolderExists}
824+
* is set to `true`.
830825
* @param filePath - The absolute or relative path of the file.
831826
* @param contents - The content that should be written to the file.
832827
* @param options - Optional settings that can change the behavior.
@@ -986,10 +981,11 @@ export class FileSystem {
986981
* Writes a text string to a file on disk, appending to the file if it already exists.
987982
* Behind the scenes it uses `fs.appendFileSync()`.
988983
* @remarks
989-
* Throws an error if the folder doesn't exist, unless ensureFolder=true.
984+
* Throws an error if the folder doesn't exist, unless {@link IFileSystemWriteFileOptionsBase.ensureFolderExists}
985+
* is set to `true`.
990986
* @param filePath - The absolute or relative path of the file.
991987
* @param contents - The text that should be written to the file.
992-
* @param options - Optional settings that can change the behavior. Type: `IWriteFileOptions`
988+
* @param options - Optional settings that can change the behavior.
993989
*/
994990
public static appendToFile(
995991
filePath: string,
@@ -1283,7 +1279,7 @@ export class FileSystem {
12831279
* Behind the scenes it uses `fs.createWriteStream()`.
12841280
*
12851281
* @remarks
1286-
* Throws an error if the folder doesn't exist, unless {@link IFileSystemCreateWriteStreamOptions.ensureFolderExists}
1282+
* Throws an error if the folder doesn't exist, unless {@link IFileSystemWriteFileOptionsBase.ensureFolderExists}
12871283
* is set to `true`.
12881284
* @param filePath - The path to the file. The path may be absolute or relative.
12891285
* @param options - Optional settings that can change the behavior.
@@ -1297,6 +1293,22 @@ export class FileSystem {
12971293
const folderPath: string = nodeJsPath.dirname(filePath);
12981294
FileSystem.ensureFolder(folderPath);
12991295
}
1296+
1297+
return fs.createWriteStream(filePath);
1298+
}
1299+
1300+
/**
1301+
* An async version of {@link FileSystem.createWriteStream}.
1302+
*/
1303+
public static async createWriteStreamAsync(
1304+
filePath: string,
1305+
options?: IFileSystemCreateWriteStreamOptions
1306+
): Promise<FileSystemWriteStream> {
1307+
if (options?.ensureFolderExists) {
1308+
const folderPath: string = nodeJsPath.dirname(filePath);
1309+
await FileSystem.ensureFolderAsync(folderPath);
1310+
}
1311+
13001312
return fs.createWriteStream(filePath);
13011313
}
13021314

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export { type IFileErrorOptions, type IFileErrorFormattingOptions, FileError } f
5050
export {
5151
AlreadyExistsBehavior,
5252
FileSystem,
53+
type IFileSystemWriteFileOptionsBase,
5354
type FileSystemCopyFilesAsyncFilter,
5455
type FileSystemCopyFilesFilter,
5556
type FileSystemReadStream,

0 commit comments

Comments
 (0)