Skip to content

File

Helper functions for file operations.

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.

Parameter Type
directoryPath string
msg string

Promise<void>


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.

Parameter Type
filePath string
msg string

Promise<void>


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.

Parameter Type
srcPath string
dstPath string

Promise<void>

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.)

Parameter Type
srcPath string
dstPath string

Promise<void>

If the file cannot be copied.


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.

Parameter Type
filePaths readonly string[]

Promise<void>

If the file cannot be deleted.


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.

Parameter Type
filePath string

Promise<boolean>


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.

Parameter Type
directoryPath string

Promise<string>

If there is an error when checking the directory.


function getFileHashSHA1(filePath): Promise<string>;

Defined in: functions/file.ts:140

Helper function to get the SHA1 hash of a file.

Parameter Type
filePath string

Promise<string>

If there is an error when reading the file.


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.

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.

Promise<readonly string[]>

If there is an error when checking the directory.


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.

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.

Promise<string>

If the file cannot be found.


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.

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.

Promise<readonly string[]>

If there is an error when checking the directory.


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.

Parameter Type
filePath string

Promise<boolean>


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.

Parameter Type
filePath string

Promise<boolean>


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.

Parameter Type
directoryPath string

boolean


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.

Parameter Type
filePath string

Promise<boolean>


function isSubdirectoryOf(directoryPath, parentPath): boolean;

Defined in: functions/file.ts:310

Helper function to see if a directory is a subdirectory of another one.

Parameter Type
directoryPath string
parentPath string

boolean


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.

Parameter Type Default value Description
directoryPath string undefined The path to the directory to create.
recursive boolean true Optional. Default is true.

Promise<void>

If the directory cannot be created.


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.)

Parameter Type Default value Description
directoryPath string undefined The path to the directory to create.
recursive boolean true Optional. Default is true.

Promise<void>

If the directory cannot be created.


function moveAllFilesInDirectory(srcDirectory, dstDirectory): Promise<void>;

Defined in: functions/file.ts:363

Helper function to move all files from one directory to another one.

Parameter Type
srcDirectory string
dstDirectory string

Promise<void>

If a file cannot be moved.


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.

Parameter Type
srcPath string
dstPath string

Promise<void>

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.)

Parameter Type
srcPath string
dstPath string

Promise<void>

If the file cannot be moved.


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.

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.

Promise<void>

If a file cannot be renamed.


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.

Parameter Type
srcPath string
dstPath string

Promise<void>

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.)

Parameter Type
filePaths readonly string[]

Promise<void>

If the file cannot be deleted.


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.

Parameter Type
filePath string

Promise<void>

If the file cannot be touched.