forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.ts
More file actions
384 lines (337 loc) · 12.3 KB
/
FileSystem.ts
File metadata and controls
384 lines (337 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import * as pathUtilities from 'path';
import * as fs from 'fs';
import * as fsx from 'fs-extra';
import { Text } from './Text';
/**
* The allowed types of encodings, as supported by Node.js
* @public
*/
export const enum Encoding {
Utf8 = 'utf8'
}
/**
* Enumeration controlling conversion of newline characters.
* @public
*/
export enum NewlineConversion {
CrLf,
Lf,
None
}
/**
* The options for FileSystem.readFolder()
* @public
*/
export interface IReadFolderOptions {
/**
* If true, returns the absolute paths of the files in the folder.
* Defaults to `false`.
*/
absolutePath?: boolean;
}
/**
* The options for FileSystem.writeFile()
* @public
*/
export interface IWriteFileOptions {
/**
* If true, will ensure the folder is created before writing the file.
* Defaults to `false`.
*/
ensureFolder?: boolean;
/**
* If specified, will normalize line endings to the specified style of newline.
* Defaults to `NewlineConversion.None`.
*/
convertLineEndings?: NewlineConversion;
/**
* If specified, will change the encoding of the file that will be written.
* Defaults to `"utf8"`.
*/
encoding?: Encoding;
}
/**
* The options for FileSystem.readFile()
* @public
*/
export interface IReadFileOptions {
/**
* If specified, will change the encoding of the file that will be written.
* Defaults to `"utf8"`.
*/
encoding?: Encoding;
/**
* If specified, will normalize line endings to the specified style of newline.
* Defaults to `NewlineConversion.None`.
*/
convertLineEndings?: NewlineConversion;
}
/**
* The options for FileSystem.move()
* @public
*/
export interface IMoveOptions {
/**
* If true, will overwrite the file if it already exists. Defaults to false.
*/
overwrite?: boolean;
/**
* If true, will ensure the folder is created before writing the file.
* Defaults to `false`.
*/
ensureFolder?: boolean;
}
/**
* The options for FileSystem.deleteFile()
* @public
*/
export interface IDeleteFileOptions {
/**
* If true, will throw an exception if the file did not exist before `deleteFile()` was called.
* Defaults to `false`.
*/
throwIfNotExists?: boolean;
}
/**
* A standardized file system API. Mostly contains wrappers around `fs` and `fs-extra`.
* Also contains useful options to simplify and normalize file system operations.
* @public
*/
export class FileSystem {
// ===============
// COMMON OPERATIONS
// ===============
/**
* Returns true if the path exists on disk. It can be a file, folder, or link.
* Behind the scenes it uses `fs.existsSync()`.
* @param path - The absolute or relative path to the file, folder, or link.
*/
public static exists(path: string): boolean {
return fsx.existsSync(path);
}
/**
* Gets the statistics for a particular file or folder.
* If the path is a link, this function follows the link and returns statistics about the link target.
* Behind the scenes it uses `fs.statSync()`.
* @param path - The absolute or relative path to the file, folder, or link.
*/
public static getStatistics(path: string): fs.Stats {
return fsx.statSync(path);
}
/**
* Updates the accessed and modified timestamps of the file, folder, or link referenced by path.
* Behind the scenes it uses `fs.utimesSync()`.
* @param path - The path of the file that should be modified.
* @param accessedTime - The UNIX epoch time when this was last accessed.
* @param modifiedTime - The UNIX epoch time when this was last modified.
*/
public static updateTimes(path: string, accessedTime: number, modifiedTime: number): void {
fsx.utimes(path, accessedTime, modifiedTime);
}
/**
* Changes the permissions mode of a file, folder, or link.
* Behind the scenes it uses `fs.chmodSync()`.
* @param path - The absolute or relative path to the object that should be updated.
* @param mode - should be a UNIX-style permissions modifier number (e.g. 777 or 666 etc)
*/
public static changeMode(path: string, mode: number): void {
fsx.chmodSync(path, mode);
}
/**
* Moves a file. The folder must exist, unless the `ensureFolder` option is provided.
* Behind the scenes it uses `fsx.moveSync()`
* @param sourcePath - The absolute or relative path to the source file.
* @param destinationPath - The absolute or relative path where the file should be moved to.
* @param options - Optional settings that can change the behavior. Type: `IMoveOptions`
*/
public static move(sourcePath: string, destinationPath: string, options?: IMoveOptions): void {
options = {
overwrite: false,
ensureFolder: false,
...options
};
if (options.ensureFolder) {
FileSystem.createFolder(pathUtilities.basename(sourcePath));
}
fsx.moveSync(sourcePath, destinationPath, { overwrite: options.overwrite });
}
// ===============
// FOLDER OPERATIONS
// ===============
/**
* Recursively creates a folder at a given path.
* Behind the scenes is uses `fsx.ensureDirSync()`.
* @param folderPath - The absolute or relative path of the folder which should be created.
*/
public static createFolder(folderPath: string): void {
fsx.ensureDirSync(folderPath);
}
/**
* Reads the contents of the folder, not including "." or "..".
* Behind the scenes it uses `fs.readdirSync()`.
* @param folderPath - The absolute or relative path to the folder which should be read.
* @param options - Optional settings that can change the behavior. Type: `IReadFolderOptions`
*/
public static readFolder(folderPath: string, options?: IReadFolderOptions): Array<string> {
options = {
absolutePath: false,
...options
};
if (!FileSystem.exists(folderPath)) {
throw new Error(`Cannot read contents of folder, as it does not exist: "${folderPath}"`);
}
const fileNames: Array<string> = fsx.readdirSync(folderPath);
if (options.absolutePath) {
return fileNames.map(fileName => pathUtilities.resolve(folderPath, fileName));
}
return fileNames;
}
/**
* Deletes a folder, including all of its contents.
* Behind the scenes is uses `fsx.removeSync()`
* @param folderPath - The absolute or relative path to the folder which should be deleted.
*/
public static deleteFolder(folderPath: string): void {
fsx.removeSync(folderPath);
}
/**
* Deletes the content of a folder, but not the folder itself.
* Behind the scenes it uses `fsx.emptyDirSync()`
* @param folderPath - The absolute or relative path to the folder which should have its contents deleted.
*/
public static emptyFolder(folderPath: string): void {
fsx.emptyDirSync(folderPath);
}
// ===============
// FILE OPERATIONS
// ===============
/**
* Writes a text string to a file on disk, overwriting the file if it already exists.
* Behind the scenes it uses `fs.writeFileSync()`
* @param filePath - The absolute or relative path of the file.
* @param contents - The text that should be written to the file.
* @param options - Optional settings that can change the behavior. Type: `IWriteFileOptions`
*/
public static writeFile(filePath: string, contents: string, options?: IWriteFileOptions): void {
options = {
ensureFolder: false,
convertLineEndings: NewlineConversion.None,
encoding: Encoding.Utf8,
...options
};
if (options.ensureFolder) {
const folderPath: string = pathUtilities.dirname(filePath);
FileSystem.createFolder(folderPath);
}
contents = FileSystem._convertLineEndings(contents, options.convertLineEndings);
fsx.writeFileSync(filePath, contents, { encoding: options.encoding });
}
/**
* Reads the contents of a file into a string.
* Behind the scenes it uses `fs.readFileSync()`.
* @param filePath - The relative or absolute path to the file whose contents should be read.
* @param options - Optional settings that can change the behavior. Type: `IReadFileOptions`
*/
public static readFile(filePath: string, options?: IReadFileOptions): string {
options = {
encoding: Encoding.Utf8,
convertLineEndings: NewlineConversion.None,
...options
};
const contents: string = FileSystem.readFileToBuffer(filePath).toString(options.encoding);
return FileSystem._convertLineEndings(contents, options.convertLineEndings);
}
/**
* Reads the contents of a file into a buffer.
* Behind the scenes is uses `fs.readFileSync()`.
* @param filePath - The relative or absolute path to the file whose contents should be read.
*/
public static readFileToBuffer(filePath: string): Buffer {
return fsx.readFileSync(filePath);
}
/**
* Copies a file from one location to another.
* By default, destinationPath is overwritten if it already exists.
* Behind the scenes it uses `fs.copyFileSync()`.
* @param sourcePath - The absolute or relative path to the source file to be copied.
* @param destinationPath - The absolute or relative path to the new copy that will be created.
*/
public static copyFile(sourcePath: string, destinationPath: string): void {
fsx.copySync(sourcePath, destinationPath);
}
/**
* Deletes a file. Can optionally throw if the file doesn't exist.
* Behind the scenes it uses `fs.unlinkSync()`.
* @param filePath - The absolute or relative path to the file that should be deleted.
* @param options - Optional settings that can change the behavior. Type: `IDeleteFileOptions`
*/
public static deleteFile(filePath: string, options?: IDeleteFileOptions): void {
options = {
throwIfNotExists: false,
...options
};
if (options.throwIfNotExists || FileSystem.exists(filePath)) {
fsx.unlinkSync(filePath);
}
}
// ===============
// LINK OPERATIONS
// ===============
/**
* Gets the statistics of a file, folder, or link. Does NOT follow the link to its target.
* Behind the scenes it uses `fs.lstatSync()`.
* @param path - The absolute or relative path to the file, folder, or link.
*/
public static getLinkStatistics(path: string): fs.Stats {
return fsx.lstatSync(path);
}
/**
* Creates a symbolic link to a folder (on Windows this is a "directory junction").
* Behind the scenes it uses `fs.symlinkSync()`.
* @param linkSource - The absolute or relative path to the destination where the link should be created.
* @param linkTarget - The absolute or relative path to the target of the link.
*/
public static createSymbolicLinkToFolder(linkSource: string, linkTarget: string): void {
// For directories, we use a Windows "junction". On Unix, this produces a regular symlink.
fsx.symlinkSync(linkTarget, linkSource, 'junction');
}
/**
* Creates a symbolic link to a file (on Windows this requires elevated permissions).
* Behind the scenes it uses `fs.symlinkSync()`.
* @param linkSource - The absolute or relative path to the destination where the link should be created.
* @param linkTarget - The absolute or relative path to the target of the link.
*/
public static createSymbolicLinkToFile(linkSource: string, linkTarget: string): void {
fsx.symlinkSync(linkSource, linkTarget, 'file');
}
/**
* Creates a hard link to a file.
* Behind the scenes it uses `fs.linkSync()`.
* @param linkSource - The absolute or relative path to the destination where the link should be created.
* @param linkTarget - The absolute or relative path to the target of the link.
*/
public static createHardLinkToFile(linkSource: string, linkTarget: string): void {
fsx.linkSync(linkSource, linkTarget);
}
/**
* Follows a link to its destination and returns the absolute path to the final target of the link.
* Behind the scenes it uses `fs.realpathSync()`.
* @param linkPath - The path to the link.
*/
public static followLink(linkPath: string): string {
return fsx.realpathSync(linkPath);
}
/**
* A helper function that converts line endings on a string.
* @param text - The text to be normalized.
* @param lineEndings - The style of line endings to use.
*/
private static _convertLineEndings(text: string, lineEndings: NewlineConversion | undefined): string {
if (lineEndings === NewlineConversion.CrLf) {
return Text.convertToCrLf(text);
} else if (lineEndings === NewlineConversion.Lf) {
return Text.convertToLf(text);
}
return text;
}
}