Skip to content

Commit 342a83b

Browse files
Copiloticlanton
andcommitted
Add ensureFolderExists option to FileSystem.createWriteStream
Agent-Logs-Url: https://github.com/microsoft/rushstack/sessions/199d4b3e-1f3f-44e1-9fc6-7b4a0e027c7e Co-authored-by: iclanton <5010588+iclanton@users.noreply.github.com>
1 parent 4e7821d commit 342a83b

3 files changed

Lines changed: 31 additions & 2 deletions

File tree

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export class FileSystem {
173173
static createSymbolicLinkFolderAsync(options: IFileSystemCreateLinkOptions): Promise<void>;
174174
static createSymbolicLinkJunction(options: IFileSystemCreateLinkOptions): void;
175175
static createSymbolicLinkJunctionAsync(options: IFileSystemCreateLinkOptions): Promise<void>;
176-
static createWriteStream(filePath: string): FileSystemWriteStream;
176+
static createWriteStream(filePath: string, options?: IFileSystemCreateWriteStreamOptions): FileSystemWriteStream;
177177
static deleteFile(filePath: string, options?: IFileSystemDeleteFileOptions): void;
178178
static deleteFileAsync(filePath: string, options?: IFileSystemDeleteFileOptions): Promise<void>;
179179
static deleteFolder(folderPath: string): void;
@@ -344,6 +344,11 @@ export interface IFileSystemCreateLinkOptions {
344344
newLinkPath: string;
345345
}
346346

347+
// @public
348+
export interface IFileSystemCreateWriteStreamOptions {
349+
ensureFolderExists?: boolean;
350+
}
351+
347352
// @public
348353
export interface IFileSystemDeleteFileOptions {
349354
throwIfNotExists?: boolean;

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,18 @@ export interface IFileSystemCopyFilesOptions extends IFileSystemCopyFilesAsyncOp
276276
filter?: FileSystemCopyFilesFilter; // narrow the type to exclude FileSystemCopyFilesAsyncFilter
277277
}
278278

279+
/**
280+
* The options for {@link FileSystem.createWriteStream}
281+
* @public
282+
*/
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+
}
290+
279291
/**
280292
* The options for {@link FileSystem.deleteFile}
281293
* @public
@@ -1270,10 +1282,21 @@ export class FileSystem {
12701282
* Creates a writable stream for writing to a file.
12711283
* Behind the scenes it uses `fs.createWriteStream()`.
12721284
*
1285+
* @remarks
1286+
* Throws an error if the folder doesn't exist, unless {@link IFileSystemCreateWriteStreamOptions.ensureFolderExists}
1287+
* is set to `true`.
12731288
* @param filePath - The path to the file. The path may be absolute or relative.
1289+
* @param options - Optional settings that can change the behavior.
12741290
* @returns A new writable stream for the file.
12751291
*/
1276-
public static createWriteStream(filePath: string): FileSystemWriteStream {
1292+
public static createWriteStream(
1293+
filePath: string,
1294+
options?: IFileSystemCreateWriteStreamOptions
1295+
): FileSystemWriteStream {
1296+
if (options?.ensureFolderExists) {
1297+
const folderPath: string = nodeJsPath.dirname(filePath);
1298+
FileSystem.ensureFolder(folderPath);
1299+
}
12771300
return fs.createWriteStream(filePath);
12781301
}
12791302

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ export {
6161
type IFileSystemCopyFilesAsyncOptions,
6262
type IFileSystemCopyFilesOptions,
6363
type IFileSystemCreateLinkOptions,
64+
type IFileSystemCreateWriteStreamOptions,
6465
type IFileSystemDeleteFileOptions,
6566
type IFileSystemMoveOptions,
6667
type IFileSystemReadFileOptions,

0 commit comments

Comments
 (0)