0.0.2 • Published 8 months ago

@stlib/is-exists v0.0.2

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

Node.js CI NPM Version MIT License Open Source Love

@stlib/utils

Table of contents

About

stlib - is a library with utilities and functions which makes developing easier. This library also provides types for every utility and their params.

Getting started

Installation

$ yarn add @stlib/utils

!IMPORTANT Node.js 18.x+ version must be installed in your OS.

Usage

Error utilities:

abstract class AbstractError {}

// Provides template for your custom errors.

// Usage:
class CustomError extends AbstractError {
  constructor() {
    super('Message', { option: 'smth' }); // second parameter is optional
  }
  
  readonly code = 500;
}

const error = new CustomError();

error.code; // return error code
error.name; // return name of the error
error.message; // return error message
error.options; // return error options
error.serialize() // return formatted output with detailed error info
isError(error: unknown): boolean;

// checks object or param to be instance of error
handleErrorSync(error: unknown, options?: HandleErrorOptions,): { handledError: unknown };

// Handles error synchronously.
// It can log error to console, throw error and write it to log file.
handleError(error: unknown, callback: () => T | Promise<T>, options?: HandleErrorOptions,): Promise<{ callbackResult: T | undefined, handledError: unknown }>;

// Handles error asynchronously.
// It can log error to console, throw error, write it to log file and calls a callback before error will be thrown.

Object type for options:

HandleErrorOptions {
  message?: string;
  throw?: boolean;
  toLog?: { path: PathLike; withStack?: boolean };
}
logErrorSync(error: unknown, path: PathLike, stack?: boolean): void;

// Writes an error message and it's stack to the log file synchronously
logError(error: unknown, path: PathLike, stack?: boolean): void;

// Writes an error message and it's stack to the log file asynchronously

File system utilities

isExistsSync(path: PathLike, options?: CreateOptions): boolean;

// Checks is file or directory exists and creates it synchronously
isExists(path: PathLike, options?: CreateOptions): boolean;

// Checks is file or directory exists and creates it asynchronously

Object type for options:

CreateOptions {
  create: boolean;
  recursive?: boolean;
  content?: string;
}

CLI

Object type for arguments

OptionsType {
  [key: string]: string;
}
// Parses arhuments and return object
function parseArgs(argv: string[]): OptionsType;

// Object with parsed arguments
const options: OptionsType;

Example

$ yarn ts-node index.ts -a --b -c=1 --d= 2 --e=smth=3 -f 4
import { options } from '@stlib/utils';

console.log(options);

// Output: { a: true, b: true, c: '1', d: '2', e: 'smth=3', f: '4' }

Contributing

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

Changelog

Project changes are writen in changelog, see the CHANGELOG.md.

We use SemVer for versioning. For the versions available, see the tags on this repository. For the versions supported, see the SECURITY.md.

Authors

License

This project is licensed under the MIT License - see the LICENSE.md

0.0.2

8 months ago

0.0.1

9 months ago