FileSystemWrapper
Use the FileSystemWrapper class to perform operations on system files and folders when using platform APIs.
createReadStream#
Creates a readable stream from the file you pass.
| Parameter | Required | Type | Description |
|---|---|---|---|
file | Yes | String | Path to the file |
options | No | Object | Define additional options for your read stream |
options.flags | No | String | A file system flag that describes how to handle the file, such as 'r' to read, 'a' to append, or 'w' to write. The default is 'w'. |
options.encoding | No | String or null | File encoding. The default is 'utf8'. |
options.fd | No | Number or FileHandler | A file descriptor to use for the stream instead of opening a new one. If specified, the path parameter is ignored. |
options.mode | No | Number | Define the file's permissions with an octal. The default is 0o666, which is readable and writable for the owner, group, and others. |
options.autoClose | No | Boolean | If set to false, the file descriptor won't automatically closed when the stream ends. The default value is true. |
options.emitClose | No | Boolean | If set to false, the 'close' event will not be emitted once the file is closed. |
options.start | No | Number | Byte offset to start reading the file. |
options.end | No | Number | Byte offset to stop reading the file. |
options.highWaterMark | No | Number | The maximum amount of data in bytes that can buffer at a time before the stream pauses, allowing the consumer to catch up. The default value is 64 * 1024. |
options.fs | No | Object | null |
options.signal | No | AbortSignal or null | Notifies observers when the stream aborts. |
Returns#
{Promise<ReadStream>} - Returns a promise with a read stream
Example#
let file = {"folder/folder/example.txt"};
let options = { mode: 0o777, flag: 'r'};
const readableStream = await fs.createReadStream(file, options);
createWriteStream#
Creates a writable stream for the file you pass.
| Parameter | Required | Type | Description |
|---|---|---|---|
file | Yes | String | Path to the file |
options | No | Object | Define additional options for your write stream |
options.flags | No | String | A file system flag that describes how to handle the file, such as 'r' to read, 'a' to append, or 'w' to write. The default is 'w'. |
options.encoding | No | String or null | File encoding. The default is 'utf8'. |
options.fd | No | Number or String | A file descriptor to use for the stream instead of opening a new one. If specified, the path parameter is ignored. |
options.mode | No | Number | Define the file's permissions with an octal. The default is 0o666, which is readable and writable for the owner, group, and others. |
options.autoClose | No | Boolean | If set to false, the file descriptor won't automatically be closed when the stream ends. The default value is true. |
options.emitClose | No | Boolean | If set to false, the 'close' event will not be emitted once the file is closed. |
options.start | No | Number | Byte offset to start reading the file. |
options.fs | No | Object or null | Do we need this? (Specify the appropriate type if needed) |
options.signal | No | AbortSignal or null | Notifies observers when the stream aborts. |
options.highWaterMark | No | Number | The maximum amount of data in bytes that can buffer at a time before the stream pauses, allowing the consumer to catch up. The default value is 16384. |
options.flush | No | Boolean | Set to 'true' to flush data after it is successfully written to the file. The default is 'false'. |
Returns#
{Promise<WriteStream>} - Returns a promise with a writable stream
Example#
let file = "folder/folder/example.txt";
let options = { mode: 0o777, flag: 'w'};
const writableStream = await fs.createWriteStream(file, options);
writableStream.write('Hello, World!');writableStream.end();
existsSync#
Synchronously checks if the file you pass exists in the File Service.
| Parameter | Required | Type | Description |
|---|---|---|---|
file | Yes | String | Path to the file |
Returns#
{Boolean} - Returns true if the file exists and false if it does not exist
Example#
let file = "folder/folder/example.txt";
if (await fs.existsSync(file)) { // Function to perform if the file exists} else { // Function to perform if the file doesn't exist}
unlink#
Asynchronously deletes the file you pass.
| Parameter | Required | Type | Description |
|---|---|---|---|
file | Yes | String | Path to the file |
Example#
let file = "folder/folder/example.txt";
await fs.unlink(file);
Returns#
{Promise} - Returns a promise that resolves when the file is successfully deleted
unlinkSync#
Synchronously deletes the file you pass.
| Parameter | Required | Type | Description |
|---|---|---|---|
file | Yes | String | Path to the file |
Example#
const file = "folder/folder/example.txt";
fs.unlinkSync(file);
Returns#
No return value
mkdirsSync#
Synchronously creates a directory and any required subdirectories.
| Parameter | Required | Type | Description |
|---|---|---|---|
dir | Yes | String | Directory path. |
options | Yes | Integer or Object | Define the file's permissions with an octal. The default is 0o666, which is readable and writable for the owner, group, and others. Pass the octal directly or as an object with a 'mode' property and the octal as its value. |
Example#
const dir = "folder/folder/example";const mode = 0o2775;const options = { mode: 0o2775};
fs.mkdirsSync(dir, mode);fs.mkdirsSync(dir, options);
Returns#
No return value
readFileSync#
Synchronously creates a readable stream from the file you pass.
| Parameter | Required | Type | Description |
|---|---|---|---|
file | Yes | String | Path to the file |
options | No | FileOptions | Pass an options object. The default is 'undefined'. |
options.encoding | No | String or null | File encoding. The default is 'utf8'. |
options.flags | No | String | A file system flag that describes how to handle the file, such as 'r' to read, 'a' to append, or 'w' to write. The default is 'w'. |
options.signal | No | AbortSignal | null |
Example#
const file = "folder/folder/example.txt";
const readableStream = await fs.readFileSync(file);
Returns#
{ReadStream} - Returns a promise with a read stream
readdirSync#
Synchronously reads the contents of a directory.
| Parameter | Required | Type | Description |
|---|---|---|---|
dir | Yes | String | Path to the directory. |
Example#
let dir = "root/folder/";
const readableDir = await fs.readdirSync(dir);
Returns#
{ReadStream} - Returns a promise with a read stream
rmSync#
Synchronously removes files and directories.
| Parameter | Required | Type | Description |
|---|---|---|---|
file | Yes | String | Path to the directory. |
options | No | Options | Optionally pass an options object. The default value is undefined. |
options.force | No | Boolean | Set to true to ignore exceptions if the path does not exist. The default value is false. |
options.maxRetries | No | Number | Set the number of retries if an EBUSY, EMFILE, ENFILE, ENOTEMPTY, or EPERM error occurs. The retries occur with a linear backoff wait of the time defined in 'retryDelay'. The default value is 0. This option is ignored if 'recursive' is not true. |
options.recursive | No | Boolean | Set to true to recursively remove the directory. On failure, the recursive operation retries. The default value is false. |
options.retryDelay | No | Number | The time in milliseconds to wait between retries. This option is ignored if 'recursive' is false. The default value is 100. |
Example#
const file = "root/folder/";const options = { maxRetries: 10, recursive: true};
await fs.rmSync(file, options);
Returns#
No return value
writeFileSync#
Synchronously writes data to a file.
| Parameter | Required | Type | Description |
|---|---|---|---|
file | Yes | String | Path to the file |
data | Yes | String or Buffer | Content to write to the file as a string or buffer |
options | No | Options | Optionally pass an options object. The default value is undefined. |
options.encoding | No | String or null | File encoding. The default is 'utf8'. |
options.mode | No | Number | Define the file's permissions with an octal. The default is 0o666, which is readable and writable for the owner, group, and others. |
options.flag | No | String | A file system flag that describes how to handle the file, such as 'r' to read, 'a' to append, or 'w' to write. The default is 'w'. |
options.flush | No | Boolean | Set to 'true' to flush data after it is successfully written to the file. The default is 'false'. |
Example#
let file = "folder/folder/example.txt";let data = "String to write";
const writtenFile = await fs.writeFileSync(file, data);
Returns#
No return value
rmdirSync#
Synchronously removes a directory.
| Parameter | Required | Type | Description |
|---|---|---|---|
dir | Yes | String | Path to the directory. |
options | No | Options | Optionally pass an options object. The default value is undefined. |
options.maxRetries | No | Number | Set the number of retries if an EBUSY, EMFILE, ENFILE, ENOTEMPTY, or EPERM error occurs. The retries occur with a linear backoff wait of the time defined in 'retryDelay'. The default value is 0. This option is ignored if 'recursive' is not true. |
options.recursive | No | Boolean | Set to true to recursively remove the directory. On failure, the recursive operation retries. The default value is false. |
options.retryDelay | No | Number | The time in milliseconds to wait between retries. This option is ignored if 'recursive' is false. The default value is 100. |
Example#
let dir = "root-folder/folder";
await fs.rmdirSync(dir, options);
const options = { maxRetries: 10, recursive: true};
Returns#
No return value