0.0.5 • Published 6 months ago

@banjoanton/node-utils v0.0.5

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

@banjoanton/node-utils

A collection of some of my most used node-specific JavaScript / TypeScript utility functions.

NPM version

  • :palm_tree: - Three-shakable ESM modules.
  • :speech_balloon: - Fully typed TSDocs with examples
  • :file_folder: - Small size
  • :bookmark: - Own well-tested utilities or imported from large open source projects.

Install

# npm
npm install @banjoanton/node-utils

# yarn
yarn add @banjoanton/node-utils

# pnpm
pnpm install @banjoanton/node-utils

Import

import { Logger } from "@banjoanton/node-utils";
// or
const { Logger } = require("@banjoanton/node-utils");

Docs

Auto generated from TSDocs.

Table of Contents

File

A file utility for reading and writing files using the fs module.


pathExistsSync

Check if a path exists. Configurable with the second argument.

const fileOrFolderExists = FileKit.pathExistsSync("file.txt");
const explicitFileOrFolderExists = FileKit.pathExistsSync("file.txt", { type: "all" });
const fileExistsSync = FileKit.pathExistsSync("file.txt", { type: "file" });
const folderExists = FileKit.pathExistsSync("dir", { type: "directory" });

pathExists

Check if a path exists async. Configurable with the second argument.

const fileOrFolderExists = await FileKit.pathExists("file.txt");
const explicitFileOrFolderExists = await FileKit.pathExists("file.txt", { type: "all" });
const fileExistsSync = await FileKit.pathExists("file.txt", { type: "file" });
const folderExists = await FileKit.pathExists("dir", { type: "directory" });

writeFileSync

Write to a file with the given content. If it exists, it will be overwritten. Otherwise it will be created. If the directory does not exist, it will be created. Configurable with the third argument.

FileKit.writeFileSync("file.txt", "Hello world!");

// With config
FileKit.writeFileSync("file.txt", "Hello world!", { logError: true });

writeFile

Write to a file with the given content async. If it exists, it will be overwritten. Otherwise it will be created. If the directory does not exist, it will be created. Configurable with the third argument.

await FileKit.writeFile("file.txt", "Hello world!");

// With config
await FileKit.writeFile("file.txt", "Hello world!", { logError: true });

appendFileSync

Append content to a file. If the file does not exist, it will be created. If the directory does not exist, it will be created. Configurable with the third argument.

FileKit.appendFileSync("file.txt", "Hello world!");
FileKit.appendFileSync("file.txt", "Hello world!", { logError: true });
FileKit.appendFileSync("file.txt", "Hello world!", { onError: error => console.log(error) });

appendFile

Append content to a file async. If the file does not exist, it will be created. If the directory does not exist, it will be created. Configurable with the third argument.

await FileKit.appendFile("file.txt", "Hello world!");
await FileKit.appendFile("file.txt", "Hello world!", { logError: true });
await FileKit.appendFile("file.txt", "Hello world!", { onError: error => console.log(error) });

readFileSync

Read a file. Will return undefined if the file does not exist. Configurable with the second argument.

const content = FileKit.readFileSync("file.txt"); // undefined or string

const content = FileKit.readFileSync("file.txt", { logError: true });
const content = FileKit.readFileSync("file.txt", { onError: error => console.log(error) });

readFile

Read a file async. Will return undefined if the file does not exist. Configurable with the second argument.

const content = await FileKit.readFile("file.txt"); // undefined or string

const content = await FileKit.readFile("file.txt", { logError: true });
const content = await FileKit.readFile("file.txt", { onError: error => console.log(error) });

removeSync

Delete a file or directory. Will do nothing if the path does not exist. Configurable with the second argument.

FileKit.removeSync("file.txt");
FileKit.removeSync("file.txt", { logError: true });
FileKit.removeSync("file.txt", { onError: error => console.log(error) });

FileKit.removeSync("dir");
FileKit.removeSync("dir", { logError: true });
FileKit.removeSync("dir", { onError: error => console.log(error) });

remove

Delete a file or directory async. Will do nothing if the path does not exist. Configurable with the second argument.

await FileKit.remove("file.txt");
await FileKit.remove("file.txt", { logError: true });
await FileKit.remove("file.txt", { onError: error => console.log(error) });

await FileKit.remove("dir");
await FileKit.remove("dir", { logError: true });
await FileKit.remove("dir", { onError: error => console.log(error) });

removeMultipleSync

Delete multiple files. Will do nothing if the file does not exist. Configurable with the second argument.

FileKit.removeMultipleSync(["file.txt", "file2.txt"]);
FileKit.removeMultipleSync(["file.txt", "file2.txt"], { logError: true });
FileKit.removeMultipleSync(["file.txt", "file2.txt"], { onError: error => console.log(error) });

FileKit.removeMultipleSync(["dir", "dir2"]);
FileKit.removeMultipleSync(["dir", "dir2"], { logError: true });
FileKit.removeMultipleSync(["dir", "dir2"], { onError: error => console.log(error) });

removeMultiple

Delete multiple files async. Will do nothing if the file does not exist. Configurable with the second argument.

await FileKit.removeMultiple(["file.txt", "file2.txt"]);
await FileKit.removeMultiple(["file.txt", "file2.txt"], { logError: true });
await FileKit.removeMultiple(["file.txt", "file2.txt"], { onError: error => console.log(error) });

await FileKit.removeMultiple(["dir", "dir2"]);
await FileKit.removeMultiple(["dir", "dir2"], { logError: true });
await FileKit.removeMultiple(["dir", "dir2"], { onError: error => console.log(error) });

createDirectorySync

Create a directory. Will do nothing if the directory already exists. Configurable with the second argument.

FileKit.createDirectorySync("dir");
FileKit.createDirectorySync("dir", { logError: true });
FileKit.createDirectorySync("dir", { onError: error => console.log(error) });

createDirectory

Create a directory async. Will do nothing if the directory already exists. Configurable with the second argument.

await FileKit.createDirectory("dir");
await FileKit.createDirectory("dir", { logError: true });
await FileKit.createDirectory("dir", { onError: error => console.log(error) });

fileExistsSync

Will return true if the file exists. Does not work for directories. Configurable with the second argument.

const exists = FileKit.fileExistsSync("file.txt"); // true or false
const exists = FileKit.fileExistsSync("file.txt", { logError: true });
const exists = FileKit.fileExistsSync("file.txt", { onError: error => console.log(error) });

fileExists

Will return true if the file exists async. Does not work for directories. Configurable with the second argument.

const exists = await FileKit.fileExists("file.txt"); // true or false
const exists = await FileKit.fileExists("file.txt", { logError: true });
const exists = await FileKit.fileExists("file.txt", { onError: error => console.log(error) });

directoryExistsSync

Check if a directory exists. Does not work for files. Configurable with the second argument.

const exists = FileKit.directoryExistsSync("dir"); // true or false
const exists = FileKit.directoryExistsSync("dir", { logError: true });
const exists = FileKit.directoryExistsSync("dir", { onError: error => console.log(error) });

directoryExists

Check if a directory exists async. Does not work for files. Configurable with the second argument.

const exists = await FileKit.directoryExists("dir"); // true or false
const exists = await FileKit.directoryExists("dir", { logError: true });
const exists = await FileKit.directoryExists("dir", { onError: error => console.log(error) });

copySync

Copy a file or directory. Overwrites the destination by default. Error if source does not exist or destination is a file name. Handle with onError callback. Configurable with the third argument.

FileKit.copySync("file.txt", "file2.txt");

FileKit.copySync("dir", "dir2");

copy

Copy a file or directory async. Overwrites the destination by default. Error if source does not exist or destination is a file name. Handle with onError callback. Configurable with the third argument.

await FileKit.copy("file.txt", "file2.txt");

await FileKit.copy("dir", "dir2");

moveSync

Move a file or directory. Overwrites the destination by default. Error if source does not exist. Handle with onError callback. Configurable with the third argument.

FileKit.moveSync("file.txt", "file2.txt");
FileKit.moveSync("dir", "dir2");

move

Move a file or directory async. Overwrites the destination by default. Error if source does not exist. Handle with onError callback. Configurable with the third argument.

await FileKit.move("file.txt", "file2.txt");
await FileKit.move("dir", "dir2");

Logger

A simple logger that logs to the console and optionally to a file. Based on consola. Should mainly be used for CLI tools.


Logger

A simple logger with styled output. Support for debug mode, file logging, and more.

// Log to console
Logger.debug("Hello world!");
Logger.log("Hello world!");
Logger.info("Hello world!");
Logger.success("Hello world!");
Logger.warning("Hello world!");
Logger.error("Hello world!");

// Configure logger
Logger.setLoggerConfig({ debug: true });

// Log to file
Logger.setLoggerConfig({ logFile: "logs.txt" });

0.0.3

6 months ago

0.0.5

6 months ago

0.0.4

6 months ago

0.0.2

6 months ago