forked from microsoft/rushstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileWriter.ts
More file actions
96 lines (85 loc) · 3.01 KB
/
FileWriter.ts
File metadata and controls
96 lines (85 loc) · 3.01 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
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
import * as fsx from 'fs-extra';
/**
* Available file handle opening flags.
* @public
*/
type NodeFileFlags = 'r' | 'r+' | 'rs+' | 'w' | 'wx' | 'w+' | 'wx+' | 'a' | 'ax' | 'a+' | 'ax+';
/**
* Interface which represents the flags about which mode the file should be opened in.
* @public
*/
export interface IFileWriterFlags {
/**
* Open file for appending.
*/
append?: boolean;
/**
* Fails if path exists. The exclusive flag ensures that path is newly created.
*
* @remarks
* On POSIX-like operating systems, path is considered to exist even if it is a symlink to a
* non-existent file. The exclusive flag may or may not work with network file systems.
*
* POSIX is a registered trademark of the Institute of Electrical and Electronic Engineers, Inc.
*/
exclusive?: boolean;
}
/**
* API for interacting with file handles.
* @public
*/
export class FileWriter {
private _fileDescriptor: number | undefined;
/**
* Opens a new file handle to the file at the specified path and given mode.
* Behind the scenes it uses `fs.openSync()`.
* The behaviour of this function is platform specific.
* See: https://nodejs.org/docs/latest-v8.x/api/fs.html#fs_fs_open_path_flags_mode_callback
* @param path - The absolute or relative path to the file handle that should be opened.
* @param flags - The flags for opening the handle
*/
public static open(path: string, flags?: IFileWriterFlags): FileWriter {
return new FileWriter(fsx.openSync(path, FileWriter._convertFlagsForNode(flags)));
}
/**
* Helper function to convert the file writer array to a Node.js style string (e.g. "wx" or "a").
* @param flags - The flags that should be converted.
*/
private static _convertFlagsForNode(flags: IFileWriterFlags | undefined): NodeFileFlags {
flags = {
append: false,
exclusive: false,
...flags
};
return [flags.append ? 'a' : 'w',
flags.exclusive ? 'x' : '']
.join('') as NodeFileFlags;
}
/**
* Writes some text to the given file handle. Throws if the file handle has been closed.
* Behind the scenes it uses `fs.writeSync()`.
* @param text - The text to write to the file.
*/
public write(text: string): void {
if (!this._fileDescriptor) {
throw new Error(`Cannot write to file, file descriptor has already been released.`);
}
fsx.writeSync(this._fileDescriptor, text);
}
/**
* Closes the file handle permanently. No operations can be made on this file handle after calling this.
* Behind the scenes it uses `fs.closeSync()` and releases the file descriptor to be re-used.
*/
public close(): void {
const fd: number | undefined = this._fileDescriptor;
if (fd) {
this._fileDescriptor = undefined;
fsx.closeSync(fd);
}
}
private constructor(fileDescriptor: number) {
this._fileDescriptor = fileDescriptor;
}
}