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
Parameter | Type |
---|---|
filePath | string |
data | string |
Returns
void
Defined in
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
Parameter | Type |
---|---|
srcPath | string |
dstPath | string |
Returns
void
Defined in
cp()
function cp(srcPath, dstPath): void
Alias for the copyFileOrDirectory
function. Intended to be used in scripts.
Parameters
Parameter | Type |
---|---|
srcPath | string |
dstPath | string |
Returns
void
Defined in
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
Parameter | Type |
---|---|
...filePaths | readonly string [] |
Returns
void
Defined in
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
Parameter | Type |
---|---|
filePath | string |
Returns
boolean
Defined in
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
Parameter | Type |
---|---|
directoryPath | string |
Returns
readonly string
[]
Defined in
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
Parameter | Type |
---|---|
directoryPath | string |
Returns
Promise
<readonly string
[]>
Defined in
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
Parameter | Type | Description |
---|---|---|
fileName | string | The name of the file to find. |
filePathOrDirPath | undefined | string | 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
string
Defined in
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
Parameter | Type | Description |
---|---|---|
fileName | string | The name of the file to find. |
filePathOrDirPath | undefined | string | 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
Promise
<string
>
Defined in
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
Parameter | Type | Description |
---|---|---|
directoryPath | string | The path to the directory to crawl. |
matchFunc | (filePath ) => boolean | The function that contains the matching logic. |
Returns
Promise
<string
[]>
Defined in
isDirectory()
function isDirectory(filePath): boolean
Helper function to synchronously check if the provided path exists and is a directory.
Parameters
Parameter | Type |
---|---|
filePath | string |
Returns
boolean
Defined in
isDirectoryAsync()
function isDirectoryAsync(filePath): Promise<boolean>
Helper function to asynchronously check if the provided path exists and is a directory.
Parameters
Parameter | Type |
---|---|
filePath | string |
Returns
Promise
<boolean
>
Defined in
isFile()
function isFile(filePath): boolean
Helper function to synchronously check if the provided path exists and is a file.
Parameters
Parameter | Type |
---|---|
filePath | string |
Returns
boolean
Defined in
isFileAsync()
function isFileAsync(filePath): Promise<boolean>
Helper function to asynchronously check if the provided path exists and is a file.
Parameters
Parameter | Type |
---|---|
filePath | string |
Returns
Promise
<boolean
>
Defined in
isLink()
function isLink(filePath): boolean
Helper function to synchronously check if the provided path exists and is a symbolic link.
Parameters
Parameter | Type |
---|---|
filePath | string |
Returns
boolean
Defined in
isSubdirectoryOf()
function isSubdirectoryOf(dir, parent): boolean
Helper function to see if a directory is a subdirectory of another one.
Parameters
Parameter | Type |
---|---|
dir | string |
parent | string |
Returns
boolean
Defined in
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
Parameter | Type |
---|---|
dirPath | string |
Returns
void
Defined in
mkdir()
function mkdir(dirPath): void
Alias for the makeDirectory
function. Intended to be used in scripts.
Parameters
Parameter | Type |
---|---|
dirPath | string |
Returns
void
Defined in
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
Parameter | Type |
---|---|
srcPath | string |
dstPath | string |
Returns
void
Defined in
mv()
function mv(srcPath, dstPath): void
Alias for the moveFile
function. Intended to be used in scripts.
Parameters
Parameter | Type |
---|---|
srcPath | string |
dstPath | string |
Returns
void
Defined in
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
Parameter | Type |
---|---|
filePath | string |
data | string |
Returns
void
Defined in
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
Parameter | Type |
---|---|
filePath | string |
Returns
string
Defined in
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
Parameter | Type |
---|---|
filePath | string |
Returns
Promise
<string
>
Defined in
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
Parameter | Type |
---|---|
srcPath | string |
dstPath | string |
Returns
void
Defined in
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
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
Promise
<void
>
Defined in
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
Parameter | Type |
---|---|
filePath | string |
lineNumber | number |
newLine | string |
Returns
void
Defined in
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
Parameter | Type |
---|---|
filePath | string |
searchValue | string | RegExp |
replaceValue | string |
Returns
void
Defined in
rm()
function rm(...filePaths): void
Alias for the deleteFileOrDirectory
function. Intended to be used in scripts.
Parameters
Parameter | Type |
---|---|
...filePaths | readonly string [] |
Returns
void
Defined in
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
Parameter | Type |
---|---|
filePath | string |
Returns
void
Defined in
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
Parameter | Type |
---|---|
filePath | string |
data | string |
Returns
void
Defined in
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
Parameter | Type |
---|---|
filePath | string |
data | string |
Returns
Promise
<void
>