Skip to content

Commit 19a29df

Browse files
committed
API change: The FileSystem move/copy/createLink operations now require the source/target parameters to be explicitly specified, to avoid confusion
1 parent 27e3219 commit 19a29df

4 files changed

Lines changed: 89 additions & 33 deletions

File tree

‎common/reviews/api/node-core-library.api.ts‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ class FileDiffTest {
1919
// @public
2020
class FileSystem {
2121
static changePosixModeBits(path: string, mode: PosixModeBits): void;
22-
static copyFile(sourcePath: string, destinationPath: string): void;
23-
static createHardLink(linkTarget: string, linkSource: string): void;
24-
static createSymbolicLinkFile(linkTarget: string, linkSource: string): void;
25-
static createSymbolicLinkFolder(linkTarget: string, linkSource: string): void;
26-
static createSymbolicLinkJunction(linkTarget: string, linkSource: string): void;
22+
static copyFile(options: IFileSystemCopyFileOptions): void;
23+
static createHardLink(options: IFileSystemCreateLinkOptions): void;
24+
static createSymbolicLinkFile(options: IFileSystemCreateLinkOptions): void;
25+
static createSymbolicLinkFolder(options: IFileSystemCreateLinkOptions): void;
26+
static createSymbolicLinkJunction(options: IFileSystemCreateLinkOptions): void;
2727
static deleteFile(filePath: string, options?: IDeleteFileOptions): void;
2828
static deleteFolder(folderPath: string): void;
2929
static ensureEmptyFolder(folderPath: string): void;
@@ -34,7 +34,7 @@ class FileSystem {
3434
static getPosixModeBits(path: string): PosixModeBits;
3535
static getRealPath(linkPath: string): string;
3636
static getStatistics(path: string): fs.Stats;
37-
static move(sourcePath: string, targetPath: string, options?: IFileSystemMoveOptions): void;
37+
static move(options: IFileSystemMoveOptions): void;
3838
static readFile(filePath: string, options?: IReadFileOptions): string;
3939
static readFileToBuffer(filePath: string): Buffer;
4040
static readFolder(folderPath: string, options?: IReadFolderOptions): Array<string>;
@@ -74,10 +74,24 @@ interface IExecutableSpawnSyncOptions extends IExecutableResolveOptions {
7474
timeoutMs?: number;
7575
}
7676

77+
// @public
78+
interface IFileSystemCopyFileOptions {
79+
destinationPath: string;
80+
sourcePath: string;
81+
}
82+
83+
// @public
84+
interface IFileSystemCreateLinkOptions {
85+
linkPath: string;
86+
linkTargetPath: string;
87+
}
88+
7789
// @public
7890
interface IFileSystemMoveOptions {
91+
destinationPath: string;
7992
ensureFolderExists?: boolean;
8093
overwrite?: boolean;
94+
sourcePath: string;
8195
}
8296

8397
// @public

‎libraries/node-core-library/src/FileDiffTest.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ export class FileDiffTest {
7777
throw new Error('The FileDiffTest failed, but the expected output cannot be copied because'
7878
+ ' the file already exists:\n' + expectedCopyFilename);
7979
}
80-
FileSystem.copyFile(expectedFilePath, expectedCopyFilename);
80+
FileSystem.copyFile({
81+
sourcePath: expectedFilePath,
82+
destinationPath: expectedCopyFilename
83+
});
8184

8285
// Set to read-only so that developer doesn't accidentally modify the wrong file
8386
FileSystem.changePosixModeBits(expectedCopyFilename, PosixModeBits.AllRead);

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

Lines changed: 62 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ export interface IReadFileOptions {
7070
* @public
7171
*/
7272
export interface IFileSystemMoveOptions {
73+
/**
74+
* The path of the object to be moved.
75+
* The path may be absolute or relative.
76+
*/
77+
sourcePath: string;
78+
79+
/**
80+
* The new path for the object.
81+
* The path may be absolute or relative.
82+
*/
83+
destinationPath: string;
84+
7385
/**
7486
* If true, will overwrite the file if it already exists. Defaults to true.
7587
*/
@@ -82,6 +94,24 @@ export interface IFileSystemMoveOptions {
8294
ensureFolderExists?: boolean;
8395
}
8496

97+
/**
98+
* The options for FileSystem.copyFile()
99+
* @public
100+
*/
101+
export interface IFileSystemCopyFileOptions {
102+
/**
103+
* The path of the object that will be copied.
104+
* The path may be absolute or relative.
105+
*/
106+
sourcePath: string;
107+
108+
/**
109+
* The path that the object will be copied to.
110+
* The path may be absolute or relative.
111+
*/
112+
destinationPath: string;
113+
}
114+
85115
/**
86116
* The options for FileSystem.deleteFile()
87117
* @public
@@ -111,6 +141,24 @@ export interface IUpdateTimeParameters {
111141
modifiedTime: number | Date;
112142
}
113143

144+
/**
145+
* The options for FileSystem.createSymbolicLinkJunction(), createSymbolicLinkFile(), createSymbolicLinkFolder(),
146+
* and createHardLink().
147+
*
148+
* @public
149+
*/
150+
export interface IFileSystemCreateLinkOptions {
151+
/**
152+
* The path where the new symlink link will be created.
153+
*/
154+
linkPath: string;
155+
156+
/**
157+
* The newly created symbolic link will point to this target.
158+
*/
159+
linkTargetPath: string;
160+
}
161+
114162
/**
115163
* The FileSystem API provides a complete set of recommended operations for interacting with the file system.
116164
*
@@ -217,22 +265,19 @@ export class FileSystem {
217265
/**
218266
* Moves a file. The folder must exist, unless the `ensureFolderExists` option is provided.
219267
* Behind the scenes it uses `fs-extra.moveSync()`
220-
* @param sourcePath - The absolute or relative path to the source file.
221-
* @param targetPath - The absolute or relative path where the file should be moved to.
222-
* @param options - Optional settings that can change the behavior. Type: `IFileSystemMoveOptions`
223268
*/
224-
public static move(sourcePath: string, targetPath: string, options?: IFileSystemMoveOptions): void {
269+
public static move(options: IFileSystemMoveOptions): void {
225270
options = {
226271
overwrite: true,
227272
ensureFolderExists: false,
228273
...options
229274
};
230275

231276
if (options.ensureFolderExists) {
232-
FileSystem.ensureFolder(pathUtilities.basename(sourcePath));
277+
FileSystem.ensureFolder(pathUtilities.basename(options.sourcePath));
233278
}
234279

235-
fsx.moveSync(sourcePath, targetPath, { overwrite: options.overwrite });
280+
fsx.moveSync(options.sourcePath, options.destinationPath, { overwrite: options.overwrite });
236281
}
237282

238283
// ===============
@@ -359,11 +404,9 @@ export class FileSystem {
359404
* Copies a file from one location to another.
360405
* By default, destinationPath is overwritten if it already exists.
361406
* Behind the scenes it uses `fs.copyFileSync()`.
362-
* @param sourcePath - The absolute or relative path to the source file to be copied.
363-
* @param destinationPath - The absolute or relative path to the new copy that will be created.
364407
*/
365-
public static copyFile(sourcePath: string, destinationPath: string): void {
366-
fsx.copySync(sourcePath, destinationPath);
408+
public static copyFile(options: IFileSystemCopyFileOptions): void {
409+
fsx.copySync(options.sourcePath, options.destinationPath);
367410
}
368411

369412
/**
@@ -408,39 +451,33 @@ export class FileSystem {
408451
* @param linkSource - The absolute or relative path to the destination where the link should be created.
409452
* @param linkTarget - The absolute or relative path to the target of the link.
410453
*/
411-
public static createSymbolicLinkJunction(linkTarget: string, linkSource: string): void {
454+
public static createSymbolicLinkJunction(options: IFileSystemCreateLinkOptions): void {
412455
// For directories, we use a Windows "junction". On POSIX operating systems, this produces a regular symlink.
413-
fsx.symlinkSync(linkTarget, linkSource, 'junction');
456+
fsx.symlinkSync(options.linkTargetPath, options.linkPath, 'junction');
414457
}
415458

416459
/**
417460
* Creates a symbolic link to a file (on Windows this requires elevated permissionsBits).
418461
* Behind the scenes it uses `fs.symlinkSync()`.
419-
* @param linkSource - The absolute or relative path to the destination where the link should be created.
420-
* @param linkTarget - The absolute or relative path to the target of the link.
421462
*/
422-
public static createSymbolicLinkFile(linkTarget: string, linkSource: string): void {
423-
fsx.symlinkSync(linkSource, linkTarget, 'file');
463+
public static createSymbolicLinkFile(options: IFileSystemCreateLinkOptions): void {
464+
fsx.symlinkSync(options.linkTargetPath, options.linkPath, 'file');
424465
}
425466

426467
/**
427468
* Creates a symbolic link to a folder (on Windows this requires elevated permissionsBits).
428469
* Behind the scenes it uses `fs.symlinkSync()`.
429-
* @param linkSource - The absolute or relative path to the destination where the link should be created.
430-
* @param linkTarget - The absolute or relative path to the target of the link.
431470
*/
432-
public static createSymbolicLinkFolder(linkTarget: string, linkSource: string): void {
433-
fsx.symlinkSync(linkSource, linkTarget, 'dir');
471+
public static createSymbolicLinkFolder(options: IFileSystemCreateLinkOptions): void {
472+
fsx.symlinkSync(options.linkTargetPath, options.linkPath, 'dir');
434473
}
435474

436475
/**
437476
* Creates a hard link.
438477
* Behind the scenes it uses `fs.linkSync()`.
439-
* @param linkSource - The absolute or relative path to the destination where the link should be created.
440-
* @param linkTarget - The absolute or relative path to the target of the link.
441478
*/
442-
public static createHardLink(linkTarget: string, linkSource: string): void {
443-
fsx.linkSync(linkSource, linkTarget);
479+
public static createHardLink(options: IFileSystemCreateLinkOptions): void {
480+
fsx.linkSync(options.linkTargetPath, options.linkPath);
444481
}
445482

446483
/**
@@ -467,4 +504,4 @@ export class FileSystem {
467504
return text;
468505
}
469506
}
470-
}
507+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,10 @@ export {
6060
IWriteFileOptions,
6161
IReadFileOptions,
6262
IFileSystemMoveOptions,
63+
IFileSystemCopyFileOptions,
6364
IDeleteFileOptions,
64-
IUpdateTimeParameters
65+
IUpdateTimeParameters,
66+
IFileSystemCreateLinkOptions
6567
} from './FileSystem';
6668
export {
6769
FileWriter,

0 commit comments

Comments
 (0)