File
Helper functions for file operations.
Functions
Section titled “Functions”assertDirectory()
Section titled “assertDirectory()”function assertDirectory(directoryPath, msg): Promise<void>;Defined in: functions/file.ts:17
Helper function to throw an error if the provided path is not a directory.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
directoryPath |
string |
msg |
string |
Returns
Section titled “Returns”Promise<void>
assertFile()
Section titled “assertFile()”function assertFile(filePath, msg): Promise<void>;Defined in: functions/file.ts:28
Helper function to throw an error if the provided path is not a file.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
filePath |
string |
msg |
string |
Returns
Section titled “Returns”Promise<void>
copyFileOrDirectory()
Section titled “copyFileOrDirectory()”function copyFileOrDirectory(srcPath, dstPath): Promise<void>;Defined in: functions/file.ts:41
Helper function to asynchronously copy a file or directory. If a path to a directory is specified, the directory will be recursively copied.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
srcPath |
string |
dstPath |
string |
Returns
Section titled “Returns”Promise<void>
Throws
Section titled “Throws”If the file cannot be copied.
function cp(srcPath, dstPath): Promise<void>;Defined in: functions/file.ts:63
Helper function to asynchronously copy a file or directory. If a path to a directory is specified, the directory will be recursively copied.
This is an alias for the copyFileOrDirectory function. (It is intended to be
used in scripts.)
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
srcPath |
string |
dstPath |
string |
Returns
Section titled “Returns”Promise<void>
Throws
Section titled “Throws”If the file cannot be copied.
deleteFileOrDirectory()
Section titled “deleteFileOrDirectory()”function deleteFileOrDirectory(...filePaths): Promise<void>;Defined in: functions/file.ts:76
Helper function to asynchronously delete a file or directory. If a path to a directory is specified, the directory will be recursively deleted. If the path does not exist, this function will be a no-op.
This function is variadic, meaning that you can pass as many file paths as you want to delete.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
…filePaths |
readonly string[] |
Returns
Section titled “Returns”Promise<void>
Throws
Section titled “Throws”If the file cannot be deleted.
exists()
Section titled “exists()”function exists(filePath): Promise<boolean>;Defined in: functions/file.ts:100
Helper function to see if the given file path exists. This will work with files, directories, links, and so on.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
filePath |
string |
Returns
Section titled “Returns”Promise<boolean>
getDirectoryHashSHA1()
Section titled “getDirectoryHashSHA1()”function getDirectoryHashSHA1(directoryPath): Promise<string>;Defined in: functions/file.ts:117
Helper function to get a SHA1 hash for every file in a directory. (This function correctly handles nested subdirectories.)
This is useful to see if the contents of a directory have changed in any way.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
directoryPath |
string |
Returns
Section titled “Returns”Promise<string>
Throws
Section titled “Throws”If there is an error when checking the directory.
getFileHashSHA1()
Section titled “getFileHashSHA1()”function getFileHashSHA1(filePath): Promise<string>;Defined in: functions/file.ts:140
Helper function to get the SHA1 hash of a file.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
filePath |
string |
Returns
Section titled “Returns”Promise<string>
Throws
Section titled “Throws”If there is an error when reading the file.
getFileNamesInDirectory()
Section titled “getFileNamesInDirectory()”function getFileNamesInDirectory( directoryPath, filter?, recursive?, paths?,): Promise<readonly string[]>;Defined in: functions/file.ts:164
Helper function to asynchronously get the file names or file paths inside of a directory.
Parameters
Section titled “Parameters”| Parameter | Type | Default value | Description |
|---|---|---|---|
directoryPath |
string |
undefined |
The path to the directory. |
filter? |
"files" | "directories" |
undefined |
Optional. If specified, will only return this type of file. |
recursive? |
boolean |
false |
Optional. If true, will include files in all subdirectories. Default is false. |
paths? |
boolean |
false |
Optional. If true, will return the full file paths instead of just the file names. Default is false. |
Returns
Section titled “Returns”Promise<readonly string[]>
Throws
Section titled “Throws”If there is an error when checking the directory.
getFilePath()
Section titled “getFilePath()”function getFilePath(fileName, filePathOrDirPath): Promise<string>;Defined in: functions/file.ts:221
Helper function to synchronously get the path to file, given either a file path,
a directory path, or undefined.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
fileName |
string |
The name of the file to find. |
filePathOrDirPath |
string | undefined |
Either the path to a file or the path to a directory which contains the file. If undefined is passed, the current working directory will be used. |
Returns
Section titled “Returns”Promise<string>
Throws
Section titled “Throws”If the file cannot be found.
getFilePathsInDirectory()
Section titled “getFilePathsInDirectory()”function getFilePathsInDirectory( directoryPath, filter?, recursive?,): Promise<readonly string[]>;Defined in: functions/file.ts:259
Helper function to asynchronously get the file paths inside of a directory.
Parameters
Section titled “Parameters”| Parameter | Type | Default value | Description |
|---|---|---|---|
directoryPath |
string |
undefined |
The path to the directory. |
filter? |
"files" | "directories" |
undefined |
Optional. If specified, will only return this type of file. Defaults to returning both files and directories. |
recursive? |
boolean |
false |
Optional. If true, will include files in all subdirectories. Default is false. |
Returns
Section titled “Returns”Promise<readonly string[]>
Throws
Section titled “Throws”If there is an error when checking the directory.
isDirectory()
Section titled “isDirectory()”function isDirectory(filePath): Promise<boolean>;Defined in: functions/file.ts:268
Helper function to asynchronously check if the provided path exists and is a directory.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
filePath |
string |
Returns
Section titled “Returns”Promise<boolean>
isFile()
Section titled “isFile()”function isFile(filePath): Promise<boolean>;Defined in: functions/file.ts:278
Helper function to asynchronously check if the provided path exists and is a file.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
filePath |
string |
Returns
Section titled “Returns”Promise<boolean>
isFileSystemRootDirectory()
Section titled “isFileSystemRootDirectory()”function isFileSystemRootDirectory(directoryPath): boolean;Defined in: functions/file.ts:292
Helper function to see if a directory is the root directory of the file system.
Under the hood, this uses path.normalize and path.dirname to determine this.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
directoryPath |
string |
Returns
Section titled “Returns”boolean
isLink()
Section titled “isLink()”function isLink(filePath): Promise<boolean>;Defined in: functions/file.ts:300
Helper function to asynchronously check if the provided path exists and is a symbolic link.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
filePath |
string |
Returns
Section titled “Returns”Promise<boolean>
isSubdirectoryOf()
Section titled “isSubdirectoryOf()”function isSubdirectoryOf(directoryPath, parentPath): boolean;Defined in: functions/file.ts:310
Helper function to see if a directory is a subdirectory of another one.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
directoryPath |
string |
parentPath |
string |
Returns
Section titled “Returns”boolean
makeDirectory()
Section titled “makeDirectory()”function makeDirectory(directoryPath, recursive?): Promise<void>;Defined in: functions/file.ts:328
Helper function to asynchronously make a new directory. By default, it will recursively make as many subdirectories as needed. If the directory already exists, this function will be a no-op.
Parameters
Section titled “Parameters”| Parameter | Type | Default value | Description |
|---|---|---|---|
directoryPath |
string |
undefined |
The path to the directory to create. |
recursive |
boolean |
true |
Optional. Default is true. |
Returns
Section titled “Returns”Promise<void>
Throws
Section titled “Throws”If the directory cannot be created.
mkdir()
Section titled “mkdir()”function mkdir(directoryPath, recursive?): Promise<void>;Defined in: functions/file.ts:351
Helper function to asynchronously make a new directory. By default, it will recursively make as many subdirectories as needed. If the directory already exists, this function will be a no-op.
This is an alias for the makeDirectory function. (It is intended to be used in
scripts.)
Parameters
Section titled “Parameters”| Parameter | Type | Default value | Description |
|---|---|---|---|
directoryPath |
string |
undefined |
The path to the directory to create. |
recursive |
boolean |
true |
Optional. Default is true. |
Returns
Section titled “Returns”Promise<void>
Throws
Section titled “Throws”If the directory cannot be created.
moveAllFilesInDirectory()
Section titled “moveAllFilesInDirectory()”function moveAllFilesInDirectory(srcDirectory, dstDirectory): Promise<void>;Defined in: functions/file.ts:363
Helper function to move all files from one directory to another one.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
srcDirectory |
string |
dstDirectory |
string |
Returns
Section titled “Returns”Promise<void>
Throws
Section titled “Throws”If a file cannot be moved.
moveFileOrDirectory()
Section titled “moveFileOrDirectory()”function moveFileOrDirectory(srcPath, dstPath): Promise<void>;Defined in: functions/file.ts:383
Helper function to asynchronously move a file or directory.
This is an alias for the renameFileOrDirectory function, since the Node.js API
uses the same thing for both operations.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
srcPath |
string |
dstPath |
string |
Returns
Section titled “Returns”Promise<void>
Throws
Section titled “Throws”If the file cannot be moved.
function mv(srcPath, dstPath): Promise<void>;Defined in: functions/file.ts:397
Helper function to asynchronously move a file or directory.
This is an alias for the moveFileOrDirectory function. (It is intended to be
used in scripts.)
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
srcPath |
string |
dstPath |
string |
Returns
Section titled “Returns”Promise<void>
Throws
Section titled “Throws”If the file cannot be moved.
renameFileExtensions()
Section titled “renameFileExtensions()”function renameFileExtensions( directoryPath, srcFileExtension, dstFileExtension,): Promise<void>;Defined in: functions/file.ts:410
Helper function to recursively rename all of the files in a directory from one file extension to another.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
directoryPath |
string |
The path to the directory to crawl. |
srcFileExtension |
string |
The file extension to change from. Do not include a period in the string. |
dstFileExtension |
string |
The file extension to change to. Do not include a period in the string. |
Returns
Section titled “Returns”Promise<void>
Throws
Section titled “Throws”If a file cannot be renamed.
renameFileOrDirectory()
Section titled “renameFileOrDirectory()”function renameFileOrDirectory(srcPath, dstPath): Promise<void>;Defined in: functions/file.ts:440
Helper function to asynchronously rename a file or directory. Since renames are not allowed across file system boundaries, this will automatically handle that special case by performing a recursive copy and delete operation instead.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
srcPath |
string |
dstPath |
string |
Returns
Section titled “Returns”Promise<void>
Throws
Section titled “Throws”If the file or directory cannot be renamed.
function rm(...filePaths): Promise<void>;Defined in: functions/file.ts:478
Helper function to asynchronously delete a file or directory. If a path to a directory is specified, the directory will be recursively deleted. If the path does not exist, this function will be a no-op.
This function is variadic, meaning that you can pass as many file paths as you want to delete.
This is an alias for the deleteFileOrDirectory function. (It is intended to be
used in scripts.)
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
…filePaths |
readonly string[] |
Returns
Section titled “Returns”Promise<void>
Throws
Section titled “Throws”If the file cannot be deleted.
touch()
Section titled “touch()”function touch(filePath): Promise<void>;Defined in: functions/file.ts:487
Helper function to asynchronously write 0 bytes to a file, similar to the
touch command.
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
filePath |
string |
Returns
Section titled “Returns”Promise<void>
Throws
Section titled “Throws”If the file cannot be touched.
