|
| 1 | +# [](https://discord.gg/M6aTR9eTwN) |
| 2 | + |
| 3 | +## @eliware/common [](https://www.npmjs.com/package/@eliware/common) [](LICENSE) [](https://github.com/eliware/common/actions) |
| 4 | + |
| 5 | +> An ESM/Jest/Node-friendly utility for resolving file and directory paths, logging, error handling, and signal handling in both CommonJS and ESM environments. |
| 6 | +
|
| 7 | +--- |
| 8 | + |
| 9 | +## Table of Contents |
| 10 | + |
| 11 | +- [Features](#features) |
| 12 | +- [Installation](#installation) |
| 13 | +- [Usage](#usage) |
| 14 | + - [ESM Example](#esm-example) |
| 15 | + - [CommonJS Example](#commonjs-example) |
| 16 | +- [API](#api) |
| 17 | +- [TypeScript](#typescript) |
| 18 | +- [License](#license) |
| 19 | + |
| 20 | +## Features |
| 21 | + |
| 22 | +- Unified API for CommonJS and ESM |
| 23 | +- Logging with [@eliware/log] |
| 24 | +- Path utilities with [@eliware/path] |
| 25 | +- Error handler registration with [@eliware/errors] |
| 26 | +- Signal/shutdown handler registration with [@eliware/signals] |
| 27 | +- **Re-exports Node.js built-in `fs` module** |
| 28 | +- TypeScript type definitions included |
| 29 | +- Fully tested with Jest |
| 30 | + |
| 31 | +## Installation |
| 32 | + |
| 33 | +```bash |
| 34 | +npm install @eliware/common |
| 35 | +``` |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +### ESM Example |
| 40 | + |
| 41 | +```js |
| 42 | +import { fs, log, path, pathUrl, getCurrentDirname, getCurrentFilename, registerHandlers, registerSignals } from '@eliware/common'; |
| 43 | + |
| 44 | +log.info('This is an info log from @eliware/log'); |
| 45 | + |
| 46 | +const { removeHandlers } = registerHandlers(); |
| 47 | +log.info('Registered error handlers.'); |
| 48 | + |
| 49 | +const envFile = path(import.meta, '.env'); |
| 50 | +log.info(`path to env file: ${envFile}`); |
| 51 | + |
| 52 | +const envFileUrl = pathUrl(import.meta, '.env'); |
| 53 | +log.info(`pathUrl to env file: ${envFileUrl}`); |
| 54 | + |
| 55 | +const { shutdown } = registerSignals(); |
| 56 | +log.info('Registered signal handlers.'); |
| 57 | + |
| 58 | +// Example usage of fs |
| 59 | +const files = fs.readdirSync(getCurrentDirname(import.meta)); |
| 60 | +log.info(`Files in current directory: ${files.join(', ')}`); |
| 61 | +``` |
| 62 | +
|
| 63 | +### CommonJS Example |
| 64 | +
|
| 65 | +```js |
| 66 | +const { fs, log, path, pathUrl, getCurrentDirname, getCurrentFilename, registerHandlers, registerSignals } = require('@eliware/common'); |
| 67 | + |
| 68 | +log.info('This is an info log from @eliware/log'); |
| 69 | + |
| 70 | +const { removeHandlers } = registerHandlers(); |
| 71 | +log.info('Registered error handlers.'); |
| 72 | + |
| 73 | +const envFile = path(__dirname, '.env'); |
| 74 | +log.info(`path to env file: ${envFile}`); |
| 75 | + |
| 76 | +const envFileUrl = pathUrl(__dirname, '.env'); |
| 77 | +log.info(`pathUrl to env file: ${envFileUrl}`); |
| 78 | + |
| 79 | +const { shutdown } = registerSignals(); |
| 80 | +log.info('Registered signal handlers.'); |
| 81 | + |
| 82 | +// Example usage of fs |
| 83 | +const files = fs.readdirSync(getCurrentDirname(__dirname)); |
| 84 | +log.info(`Files in current directory: ${files.join(', ')}`); |
| 85 | +``` |
| 86 | +
|
| 87 | +## API |
| 88 | +
|
| 89 | +### fs |
| 90 | +
|
| 91 | +Re-export of the Node.js built-in [`fs`](https://nodejs.org/api/fs.html) module. Use for file system operations. |
| 92 | +
|
| 93 | +### log |
| 94 | +
|
| 95 | +Default logger instance from [@eliware/log]. Supports `.info`, `.warn`, `.error`, etc. |
| 96 | +
|
| 97 | +### path(metaOrDir, ...segments) |
| 98 | +
|
| 99 | +Joins the current dirname with provided segments to form an absolute path. |
| 100 | +
|
| 101 | +- `metaOrDir`: `import.meta` (ESM), a string (dirname, e.g. `__dirname`), or undefined |
| 102 | +- `segments`: Path segments to join |
| 103 | +- Returns: Absolute path string |
| 104 | +
|
| 105 | +### getCurrentDirname(metaOrDir, dirnameFn?) |
| 106 | +
|
| 107 | +Returns the current directory name. |
| 108 | +
|
| 109 | +- `metaOrDir`: `import.meta` (ESM), a string (dirname, e.g. `__dirname`), or undefined |
| 110 | +- `dirnameFn`: Optional custom dirname function |
| 111 | +- Returns: Absolute path to the current directory |
| 112 | +
|
| 113 | +### getCurrentFilename(metaOrDir) |
| 114 | +
|
| 115 | +Returns the current filename. |
| 116 | +
|
| 117 | +- `metaOrDir`: `import.meta` (ESM), a string (dirname, e.g. `__dirname`), or undefined |
| 118 | +- Returns: Absolute path to the current file |
| 119 | +
|
| 120 | +### registerHandlers(processObj?, logger?) |
| 121 | +
|
| 122 | +Registers process-level exception handlers for uncaught exceptions, unhandled rejections, warnings, and exit events. |
| 123 | +
|
| 124 | +- `processObj`: The process object to attach handlers to (default: process) |
| 125 | +- `logger`: Logger for output (default: log) |
| 126 | +- Returns: `{ removeHandlers }` function to remove all registered handlers |
| 127 | +
|
| 128 | +### registerSignals(options?) |
| 129 | +
|
| 130 | +Sets up shutdown handlers for the process. |
| 131 | +
|
| 132 | +- `options`: `{ processObj, logger, signals }` |
| 133 | +- Returns: `{ shutdown, getShuttingDown }` |
| 134 | +
|
| 135 | +## TypeScript |
| 136 | +
|
| 137 | +Type definitions are included and exported for all main functions and options: |
| 138 | +
|
| 139 | +```ts |
| 140 | +import { fs, log, path, getCurrentDirname, getCurrentFilename, registerHandlers, registerSignals, RegisterSignalsOptions } from '@eliware/common'; |
| 141 | +``` |
| 142 | +
|
| 143 | +## Support |
| 144 | +
|
| 145 | +For help, questions, or to chat with the author and community, visit: |
| 146 | +
|
| 147 | +[](https://discord.gg/M6aTR9eTwN)[](https://discord.gg/M6aTR9eTwN) |
| 148 | +
|
| 149 | +**[eliware.org on Discord](https://discord.gg/M6aTR9eTwN)** |
| 150 | +
|
| 151 | +## License |
| 152 | +
|
| 153 | +[MIT © 2025 Eli Sterling, eliware.org](LICENSE) |
| 154 | +
|
| 155 | +## Links |
| 156 | +
|
| 157 | +- [Home Page](https://eliware.org) |
| 158 | +- [GitHub](https://github.com/eliware/common) |
| 159 | +- [npm](https://www.npmjs.com/package/@eliware/common) |
| 160 | +- [Discord](https://discord.gg/M6aTR9eTwN) |
0 commit comments