/local

Filesystem abstraction layer and common function wrappers

Primary LanguageJavaScriptMIT LicenseMIT

HileSystem Local

HileSystem Local

Filesystem abstraction layer and common function wrappers.

NPM Version NPM Downloads TypeScript Typings Maintainability Rating Codacy Badge

Usage

Import

import {
  HileSystemLocal,
  absolutePathFrom, pathFrom,
  createDirectory, createFile,
  readFileToBase64, readFileToBuffer, readFileToString,
  dirIsEmpty, dirIsExecutable, dirIsReadable, dirIsVisible, dirIsWritable,
  fileIsExecutable, fileIsReadable, fileIsVisible, fileIsWritable,
  getStatus, isDirExists, isFileExists, isPathExists,
  fileExtension, fileName, fileNameExt, filePath,
  listContents, listDirectories, listFiles,
  move,
  remove, removeNonBlocking, removeSilent,
  fileTruncate, writeFile, writeFileTail, writeJSON,
  hash, hashCrc32, hashMd5, hashSha1, hashSha256, hashSha512, size,
} from "@hilesystem/local";

Import all as named import

import * as hileSystem from "@hilesystem/local";
const isDirectoryCreated = await hileSystem.createDirectory("./test/");

Working with instance

If you prefer object-oriented style.

import { HileSystemLocal } from "@hilesystem/local";
const hileSystem = new HileSystemLocal();
const isDirectoryCreated = await hileSystem.createDirectory("./test/");

API

Path

absolutePathFrom(paths, ...morePaths) Join all arguments together and normalize the resulting path.
const paths = ["path", "to", "file.txt"];
const str = absolutePathFrom(paths) // "/user/me/path/to/file.txt"
fileExtension(path) Get file extension.
const str = fileExtension("path/to/file.txt"); // "txt"
fileName(path) Return the file name without extension.
const str = fileName("path/to/file.txt"); // "file"
fileNameExt(path) Return the last portion of a path.
const str = fileNameExt("path/to/file.txt"); // "file.txt"
filePath(path) Return the directory name of a path.
const str = filePath("path/to/file.txt"); // "path/to"
pathFrom(paths, ...morePaths) Join all arguments together and normalize the resulting path.
const str = pathFrom(["path", "to", "file.txt"]) // "path/to/file.txt"

Create

createDirectory(dirPath, mode, recursive) Asynchronous create a directory.
const trueOrError = await createDirectory("path/to/dir", "0777", true);
createFile(pathLike, mode) Asynchronous create a file.
const trueOrError = await createFile("path/to/dir", "0777");

Read

readFileToBase64(pathLike) Reads the file into a string.
const stringOrError = await readFileToBase64("path/to/img.png");
readFileToBuffer(pathLike) Reads the file into a buffer.
const bufferOrError = await readFileToBuffer("path/to/file.txt");
readFileToString(pathLike) Reads the file into a string.
const stringOrError = await readFileToString("path/to/file.txt");

Move

move(pathFrom, pathTo) Change the name or location of a file or directory.
const trueOrError = await move("path/to/file.ext", "path/to-another/file.ext");

Write

fileTruncate(pathToFile, length) Truncate a file to a specified length.
const trueOrError = await fileTruncate("path/to/file.ext");
writeFile(filePath, data, options) Asynchronously writes data to a file, replacing the file if it already exists.
const filePath = "./file.txt";
const data = "Text";
const options = { encoding: "utf8" };
const trueOrError = await writeFile(filePath, data, options);
writeFileTail(filePath, data, options) Asynchronously writes data to the end of a file.
const filePath = "./file.txt";
const data = "Text to add to the end of the file.";
const options = { encoding: "utf8" };
const trueOrError = await writeFileTail(filePath, data, options);
writeJSON(filePath, data, options, configuration) Asynchronously writes data to a file, replacing the file if it already exists.
const filePath = "./file.json";
const data = { key: "value" };
const options = { encoding: "utf8" };
const config = {
  sort: true,
  space: "\t"
};
const trueOrError = await writeJSON(filePath, data, options, config);

Delete

remove(pathLike) Removes a file or directory.
const voidOrError = await remove("path/to/file.txt");
removeNonBlocking(pathLike) Non-blocking remove of a file or directory.
removeNonBlocking("path/to/file.txt");
removeSilent(pathLike, options) Removes files and directories (modeled on the standard POSIX rm utility).
removeSilent("path/to/file.txt");

Information

getStatus(path) Get file status.
const [status, error] = await getStatus("path/to/file.ext");
isDirExists(path) Get directory status.
const trueOrFalse = await isDirExists("path/to/");
isFileExists(path) Get file status.
const trueOrFalse = await isFileExists("path/to/file.ext");
isPathExists(path) Get path status.
const trueOrFalse = await isPathExists("path/to/file_or_directory");

Check

dirIsEmpty(pathToDir, excludeSystemFiles) Check if a directory is empty.
const booleanOrError = await dirIsEmpty("path/to/dir", true);
dirIsExecutable(pathToDir) Is directory executable.
const trueOrError = await dirIsExecutable("path/to/dir");
dirIsReadable(pathToDir) Is directory readable.
const trueOrError = await dirIsReadable("path/to/dir");
dirIsVisible(pathToDir) Is directory visible.
const trueOrError = await dirIsVisible("path/to/dir");
dirIsWritable(pathToDir) Is directory writable.
const trueOrError = await dirIsWritable("path/to/dir");
fileIsExecutable(pathToFile) Is file executable.
const trueOrError = await fileIsExecutable("path/to/file.ext");
fileIsReadable(pathToFile) Is file readable.
const trueOrError = await fileIsReadable("path/to/file.ext");
fileIsVisible(pathToFile) Is file visible.
const trueOrError = await fileIsVisible("path/to/file.ext");
fileIsWritable(pathToFile) Is file writable.
const trueOrError = await fileIsWritable("path/to/file.ext");

List

listContents(pathToDir, ignoreJunkOrSystem?) Lists files and directories in path.
const arrayOfStringsOrError = await listContents("path/to/dir");
listDirectories(pathToDir) Lists files and directories in path.
const arrayOfStringsOrError = await listDirectories("path/to/dir");
listFiles(pathToDir) Lists files in path.
const arrayOfStringsOrError = await listFiles("path/to/dir");

Calculate

hash(pathToFile, algorithm) Calculate hash.
const hash = await hash("path/to/file", "md5");
hashCrc32(pathToFile) CRC32.
const hash = await hashCrc32("path/to/file");
hashMd5(pathToFile) MD5.
const hash = await hashMd5("path/to/file");
hashSha1(pathToFile) SHA-1.
const hash = await hashSha1("path/to/file");
hashSha256(pathToFile) SHA-256.
const hash = await hashSha256("path/to/file");
hashSha512(pathToFile) SHA-512.
const hash = await hashSha512("path/to/file");
size(pathToFile) File size in bytes.
const sizeInBytes = await size("path/to/file");
const sizeAsString = await size("path/to/file", true);

See also

My other projects