Skip to main content

functions/file

Functions

appendFile()

function appendFile(filePath, data): void

Helper function to synchronously append data to a file.

This will throw an error if the file cannot be appended to.

Parameters

ParameterType
filePathstring
datastring

Returns

void

Defined in

functions/file.ts:11


copyFileOrDirectory()

function copyFileOrDirectory(srcPath, dstPath): void

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

This will throw an error if the file cannot be copied.

Parameters

ParameterType
srcPathstring
dstPathstring

Returns

void

Defined in

functions/file.ts:25


cp()

function cp(srcPath, dstPath): void

Alias for the copyFileOrDirectory function. Intended to be used in scripts.

Parameters

ParameterType
srcPathstring
dstPathstring

Returns

void

Defined in

functions/file.ts:38


deleteFileOrDirectory()

function deleteFileOrDirectory(...filePaths): void

Helper function to synchronously 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 will throw an error if the file cannot be deleted.

This function is variadic, meaning that you can pass as many file paths as you want to delete.

Parameters

ParameterType
...filePathsreadonly string[]

Returns

void

Defined in

functions/file.ts:51


fileOrDirectoryExists()

function fileOrDirectoryExists(filePath): boolean

Helper function to synchronously check if a file exists.

This will throw an error if there is an error when checking the file path.

Parameters

ParameterType
filePathstring

Returns

boolean

Defined in

functions/file.ts:72


getFileNamesInDirectory()

function getFileNamesInDirectory(directoryPath): readonly string[]

Helper function to synchronously get the file names inside of a directory. (If the full path is required, you must manually join the file name with the path to the directory.)

This will throw an error if there is an error when checking the directory.

Parameters

ParameterType
directoryPathstring

Returns

readonly string[]

Defined in

functions/file.ts:92


getFileNamesInDirectoryAsync()

function getFileNamesInDirectoryAsync(directoryPath): Promise<readonly string[]>

Helper function to asynchronously get the file names inside of a directory. (If the full path is required, you must manually join the file name with the path to the directory.)

This will throw an error if there is an error when checking the directory.

Parameters

ParameterType
directoryPathstring

Returns

Promise<readonly string[]>

Defined in

functions/file.ts:113


getFilePath()

function getFilePath(fileName, filePathOrDirPath): string

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

This will throw an error if the file cannot be found.

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

string

Defined in

functions/file.ts:137


getFilePathAsync()

function getFilePathAsync(fileName, filePathOrDirPath): Promise<string>

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

This will throw an error if the file cannot be found.

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>

Defined in

functions/file.ts:176


getMatchingFilePaths()

function getMatchingFilePaths(directoryPath, matchFunc): Promise<string[]>

Helper function to recursively traverse a directory and get the file names that match the provided logic.

Parameters

ParameterTypeDescription
directoryPathstringThe path to the directory to crawl.
matchFunc(filePath) => booleanThe function that contains the matching logic.

Returns

Promise<string[]>

Defined in

functions/file.ts:215


isDirectory()

function isDirectory(filePath): boolean

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

Parameters

ParameterType
filePathstring

Returns

boolean

Defined in

functions/file.ts:246


isDirectoryAsync()

function isDirectoryAsync(filePath): Promise<boolean>

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

Parameters

ParameterType
filePathstring

Returns

Promise<boolean>

Defined in

functions/file.ts:255


isFile()

function isFile(filePath): boolean

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

Parameters

ParameterType
filePathstring

Returns

boolean

Defined in

functions/file.ts:265


isFileAsync()

function isFileAsync(filePath): Promise<boolean>

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

Parameters

ParameterType
filePathstring

Returns

Promise<boolean>

Defined in

functions/file.ts:274


function isLink(filePath): boolean

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

Parameters

ParameterType
filePathstring

Returns

boolean

Defined in

functions/file.ts:284


isSubdirectoryOf()

function isSubdirectoryOf(dir, parent): boolean

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

Parameters

ParameterType
dirstring
parentstring

Returns

boolean

Defined in

functions/file.ts:293


makeDirectory()

function makeDirectory(dirPath): void

Helper function to synchronously make a new directory. Will recursively make as many subdirectories as needed.

If the recursive behavior is not desired, then use fs.mkdirSync directly.

This will throw an error if the directory cannot be created.

Parameters

ParameterType
dirPathstring

Returns

void

Defined in

functions/file.ts:308


mkdir()

function mkdir(dirPath): void

Alias for the makeDirectory function. Intended to be used in scripts.

Parameters

ParameterType
dirPathstring

Returns

void

Defined in

functions/file.ts:321


moveFile()

function moveFile(srcPath, dstPath): void

Helper function to synchronously move a file.

This will throw an error if the file cannot be moved.

(This is simply an alias for the renameFile function, since the Node.js API uses the same thing for both operations.)

Parameters

ParameterType
srcPathstring
dstPathstring

Returns

void

Defined in

functions/file.ts:333


mv()

function mv(srcPath, dstPath): void

Alias for the moveFile function. Intended to be used in scripts.

Parameters

ParameterType
srcPathstring
dstPathstring

Returns

void

Defined in

functions/file.ts:338


prependFile()

function prependFile(filePath, data): void

Helper function to synchronously prepend data to a file.

This will throw an error if the file cannot be prepended to.

Parameters

ParameterType
filePathstring
datastring

Returns

void

Defined in

functions/file.ts:347


readFile()

function readFile(filePath): string

Helper function to synchronously read a file.

This assumes that the file is a text file and uses an encoding of "utf8".

This will throw an error if the file cannot be read.

Parameters

ParameterType
filePathstring

Returns

string

Defined in

functions/file.ts:360


readFileAsync()

function readFileAsync(filePath): Promise<string>

Helper function to asynchronously read a file.

This assumes that the file is a text file and uses an encoding of "utf8".

This will throw an error if the file cannot be read.

Parameters

ParameterType
filePathstring

Returns

Promise<string>

Defined in

functions/file.ts:379


renameFile()

function renameFile(srcPath, dstPath): void

Helper function to synchronously rename a file.

This will throw an error if the file cannot be renamed.

Parameters

ParameterType
srcPathstring
dstPathstring

Returns

void

Defined in

functions/file.ts:396


renameFileExtensions()

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

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>

Defined in

functions/file.ts:412


replaceLineInFile()

function replaceLineInFile(
filePath,
lineNumber,
newLine): void

Helper function to replace a specific line in a text file.

This assumes that the file is a text file and uses an encoding of "utf8".

This will print an error message and exit the program if the file cannot be read.

Parameters

ParameterType
filePathstring
lineNumbernumber
newLinestring

Returns

void

Defined in

functions/file.ts:446


replaceTextInFile()

function replaceTextInFile(
filePath,
searchValue,
replaceValue): void

Helper function to synchronously replace text in a file.

This assumes that the file is a text file and uses an encoding of "utf8".

This will print an error message and exit the program if the file cannot be read.

Parameters

ParameterType
filePathstring
searchValuestring | RegExp
replaceValuestring

Returns

void

Defined in

functions/file.ts:473


rm()

function rm(...filePaths): void

Alias for the deleteFileOrDirectory function. Intended to be used in scripts.

Parameters

ParameterType
...filePathsreadonly string[]

Returns

void

Defined in

functions/file.ts:484


touch()

function touch(filePath): void

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

This will throw an error if the file cannot be written to.

Parameters

ParameterType
filePathstring

Returns

void

Defined in

functions/file.ts:493


writeFile()

function writeFile(filePath, data): void

Helper function to synchronously write data to a file.

This will throw an error if the file cannot be written to.

Parameters

ParameterType
filePathstring
datastring

Returns

void

Defined in

functions/file.ts:516


writeFileAsync()

function writeFileAsync(filePath, data): Promise<void>

Helper function to asynchronously write data to a file.

This will throw an error if the file cannot be written to.

Parameters

ParameterType
filePathstring
datastring

Returns

Promise<void>

Defined in

functions/file.ts:529