Skip to main content

File

Helper functions for file operations.

Functions

copyFileOrDirectory()

function copyFileOrDirectory(srcPath, dstPath): Promise<void>;

Defined in: functions/file.ts:22

Helper function to asynchronously copy a file or directory. If a path to a directory is specified, the directory will be recursively copied.

Parameters

ParameterType
srcPathstring
dstPathstring

Returns

Promise<void>

Throws

If the file cannot be copied.


cp()

function cp(srcPath, dstPath): Promise<void>;

Defined in: functions/file.ts:48

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

ParameterType
srcPathstring
dstPathstring

Returns

Promise<void>

Throws

If the file cannot be copied.


deleteFileOrDirectory()

function deleteFileOrDirectory(...filePaths): Promise<void>;

Defined in: functions/file.ts:61

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

ParameterType
...filePathsreadonly string[]

Returns

Promise<void>

Throws

If the file cannot be deleted.


getDirectoryHashSHA1()

function getDirectoryHashSHA1(directoryPath): Promise<string>;

Defined in: functions/file.ts:93

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

ParameterType
directoryPathstring

Returns

Promise<string>

Throws

If there is an error when checking the directory.


getFileHashSHA1()

function getFileHashSHA1(filePath): Promise<string>;

Defined in: functions/file.ts:118

Helper function to get the SHA1 hash of a file.

Parameters

ParameterType
filePathstring

Returns

Promise<string>

Throws

If there is an error when reading the file.


getFileNamesInDirectory()

function getFileNamesInDirectory(
directoryPath,
filter?,
recursive?,
paths?): Promise<readonly string[]>;

Defined in: functions/file.ts:142

Helper function to asynchronously get the file names or file paths inside of a directory.

Parameters

ParameterTypeDefault valueDescription
directoryPathstringundefinedThe path to the directory.
filter?"files" | "directories"undefinedOptional. If specified, will only return this type of file.
recursive?booleanfalseOptional. If true, will include files in all subdirectories. Default is false.
paths?booleanfalseOptional. If true, will return the full file paths instead of just the file names. Default is false.

Returns

Promise<readonly string[]>

Throws

If there is an error when checking the directory.


getFilePath()

function getFilePath(fileName, filePathOrDirPath): Promise<string>;

Defined in: functions/file.ts:201

Helper function to synchronously get the path to file, given either a file path, a directory path, or undefined.

Parameters

ParameterTypeDescription
fileNamestringThe name of the file to find.
filePathOrDirPathundefined | stringEither 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

Promise<string>

Throws

If the file cannot be found.


getFilePathsInDirectory()

function getFilePathsInDirectory(
directoryPath,
filter?,
recursive?): Promise<readonly string[]>;

Defined in: functions/file.ts:238

Helper function to asynchronously get the file paths inside of a directory.

Parameters

ParameterTypeDefault valueDescription
directoryPathstringundefinedThe path to the directory.
filter?"files" | "directories"undefinedOptional. If specified, will only return this type of file.
recursive?booleanfalseOptional. If true, will include files in all subdirectories. Default is false.

Returns

Promise<readonly string[]>

Throws

If there is an error when checking the directory.


isDirectory()

function isDirectory(filePath): Promise<boolean>;

Defined in: functions/file.ts:247

Helper function to asynchronously check if the provided path exists and is a directory.

Parameters

ParameterType
filePathstring

Returns

Promise<boolean>


isFile()

function isFile(filePath): Promise<boolean>;

Defined in: functions/file.ts:257

Helper function to asynchronously check if the provided path exists and is a file.

Parameters

ParameterType
filePathstring

Returns

Promise<boolean>


isFileOrDirectory()

function isFileOrDirectory(filePath): Promise<boolean>;

Defined in: functions/file.ts:267

Helper function to synchronously check if a file exists.

Parameters

ParameterType
filePathstring

Returns

Promise<boolean>


isFileSystemRootDirectory()

function isFileSystemRootDirectory(directoryPath): boolean;

Defined in: functions/file.ts:281

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

ParameterType
directoryPathstring

Returns

boolean


function isLink(filePath): Promise<boolean>;

Defined in: functions/file.ts:289

Helper function to asynchronously check if the provided path exists and is a symbolic link.

Parameters

ParameterType
filePathstring

Returns

Promise<boolean>


isSubdirectoryOf()

function isSubdirectoryOf(directoryPath, parentPath): boolean;

Defined in: functions/file.ts:299

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

Parameters

ParameterType
directoryPathstring
parentPathstring

Returns

boolean


makeDirectory()

function makeDirectory(directoryPath, recursive): Promise<void>;

Defined in: functions/file.ts:317

Helper function to asynchronously make a new directory. By default, it will recursively make as many subdirectories as needed.

Parameters

ParameterTypeDefault valueDescription
directoryPathstringundefinedThe path to the directory to create.
recursivebooleantrueOptional. Default is true.

Returns

Promise<void>

Throws

If the directory cannot be created.


mkdir()

function mkdir(directoryPath, recursive): Promise<void>;

Defined in: functions/file.ts:342

Helper function to asynchronously make a new directory. By default, it will recursively make as many subdirectories as needed.

This is an alias for the makeDirectory function. (It is intended to be used in scripts.)

Parameters

ParameterTypeDefault valueDescription
directoryPathstringundefinedThe path to the directory to create.
recursivebooleantrueOptional. Default is true.

Returns

Promise<void>

Throws

If the directory cannot be created.


moveAllFilesInDirectory()

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

Defined in: functions/file.ts:354

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

Parameters

ParameterType
srcDirectorystring
dstDirectorystring

Returns

Promise<void>

Throws

If a file cannot be moved.


moveFileOrDirectory()

function moveFileOrDirectory(srcPath, dstPath): Promise<void>;

Defined in: functions/file.ts:376

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

ParameterType
srcPathstring
dstPathstring

Returns

Promise<void>

Throws

If the file cannot be moved.


mv()

function mv(srcPath, dstPath): Promise<void>;

Defined in: functions/file.ts:390

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

ParameterType
srcPathstring
dstPathstring

Returns

Promise<void>

Throws

If the file cannot be moved.


renameFileExtensions()

function renameFileExtensions(
directoryPath,
srcFileExtension,
dstFileExtension): Promise<void>;

Defined in: functions/file.ts:403

Helper function to recursively rename all of the files in a directory from one file extension to another.

Parameters

ParameterTypeDescription
directoryPathstringThe path to the directory to crawl.
srcFileExtensionstringThe file extension to change from. Do not include a period in the string.
dstFileExtensionstringThe file extension to change to. Do not include a period in the string.

Returns

Promise<void>

Throws

If a file cannot be renamed.


renameFileOrDirectory()

function renameFileOrDirectory(srcPath, dstPath): Promise<void>;

Defined in: functions/file.ts:433

Helper function to asynchronously rename a file.

Parameters

ParameterType
srcPathstring
dstPathstring

Returns

Promise<void>

Throws

If the file cannot be renamed.


rm()

function rm(...filePaths): Promise<void>;

Defined in: functions/file.ts:458

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

ParameterType
...filePathsreadonly string[]

Returns

Promise<void>

Throws

If the file cannot be deleted.


touch()

function touch(filePath): Promise<void>;

Defined in: functions/file.ts:467

Helper function to asynchronously write 0 bytes to a file, similar to the touch command.

Parameters

ParameterType
filePathstring

Returns

Promise<void>

Throws

If the file cannot be touched.