Static
basenameStatic
changeChanges the owner and/or group of a file or directory.
The path of the file or directory.
The new owner's username.
Optional
groupName: stringThe new group's name. If undefined, the group will not be changed.
Static
changeStatic
copyStatic
deleteStatic
deleteStatic
existsStatic
getStatic
getStatic
getStatic
getStatic
globFinds files matching a pattern using the glob
npm package internally.
The glob pattern to match.
Optional glob options (see glob.GlobOptions
).
An array of absolute paths of the matched files.
import {FileUtils} from 'nodejs-shared';
// Find all files in the specified directory.
FileUtils.glob('path/to/*.*');
// [
// 'path/to/file.txt',
// 'path/to/another-file.txt',
// ]
// Find all files, including those in subdirectories.
FileUtils.glob('path/to/**/*.*');
// [
// 'path/to/my/dir/file.txt',
// 'path/to/file.txt',
// ]
// Find only files with the .jpg extension.
FileUtils.glob('path/to/**/*.jpg');
// [
// 'path/to/my/dir/image.jpg',
// 'path/to/image.jpg',
// ]
// Find only files with the .png or .jpg extension.
FileUtils.glob('path/to/**/*.+(png|jpg)'); // Note the corrected path
// [
// 'path/to/my/dir/image.jpg',
// 'path/to/my/dir/image.png',
// 'path/to/image.jpg',
// ]
Static
isStatic
isStatic
isStatic
isChecks if a string is a valid file system path. This method checks for invalid characters and empty strings after trimming whitespace. It does not check if the path actually exists.
The string to check.
true
if the string is a valid file system path, false
otherwise.
Static
makeMakes a directory recursively.
The path of the directory to create.
Options for directory creation.
Static
makeCreates a temporary directory. Uses the TMPDIR
environment variable if present,
otherwise uses the system's temporary directory.
Options for directory creation.
The path of the created temporary directory.
Static
readStatic
readStatic
readStatic
readStatic
renameStatic
writeWrites content to a file. Creates the directory if it doesn't exist.
The path of the file to write to.
The content to write to the file. Defaults to an empty string.
Optional settings for file writing, including standard fs.WriteFileOptions
.
// Write a string to a file.
FileUtils.write('path/to/another-file.txt', 'Hello, world!');
// Write a Buffer to a file.
const buffer = Buffer.from('Hello, world!');
FileUtils.write('path/to/file.txt', buffer);
// Write with file system options.
FileUtils.write('path/to/file.txt', buffer, {mode: 0o644, owner: {username: 'nginx', groupName: 'nginx'}});
A utility class for file system operations.