@eliware/common 1.1.4 → 1.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -131
- package/index.d.ts +29 -12
- package/index.mjs +9 -12
- package/package.json +12 -14
- package/src/errors.mjs +4 -0
- package/src/fs.mjs +4 -0
- package/src/log.mjs +4 -0
- package/src/path.mjs +12 -0
- package/src/signals.mjs +4 -0
- package/index.cjs +0 -20
package/README.md
CHANGED
|
@@ -1,32 +1,6 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @eliware/common
|
|
2
2
|
|
|
3
|
-
|
|
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
|
|
3
|
+
A small ESM convenience package that provides one stable import surface for Eliware's shared Node.js utilities.
|
|
30
4
|
|
|
31
5
|
## Installation
|
|
32
6
|
|
|
@@ -34,127 +8,78 @@
|
|
|
34
8
|
npm install @eliware/common
|
|
35
9
|
```
|
|
36
10
|
|
|
37
|
-
##
|
|
11
|
+
## Exports
|
|
38
12
|
|
|
39
|
-
|
|
13
|
+
`@eliware/common` re-exports the current public APIs from:
|
|
40
14
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const { removeHandlers } = registerHandlers();
|
|
47
|
-
log.info('Registered error handlers.');
|
|
15
|
+
- `@eliware/log`: `log`, `createLogger`, `safeSerialize`
|
|
16
|
+
- `@eliware/path`: `path`, `pathUrl`, `getCurrentDirname`, `getCurrentFilename`, `resolvePath`, `relativePath`, `fileUrlToPath`
|
|
17
|
+
- `@eliware/errors`: `registerHandlers`
|
|
18
|
+
- `@eliware/signals`: `registerSignals`
|
|
19
|
+
- Node.js `fs`
|
|
48
20
|
|
|
49
|
-
|
|
50
|
-
log.info(`path to env file: ${envFile}`);
|
|
21
|
+
The implementation is split into focused modules under [`src/`](src/) while the package root remains the public entry point.
|
|
51
22
|
|
|
52
|
-
|
|
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
|
|
23
|
+
## Usage
|
|
64
24
|
|
|
65
25
|
```js
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
log
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
const
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
log.info('
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const
|
|
84
|
-
|
|
26
|
+
import {
|
|
27
|
+
fs,
|
|
28
|
+
log,
|
|
29
|
+
path,
|
|
30
|
+
pathUrl,
|
|
31
|
+
resolvePath,
|
|
32
|
+
registerHandlers,
|
|
33
|
+
registerSignals,
|
|
34
|
+
} from '@eliware/common';
|
|
35
|
+
|
|
36
|
+
const configPath = path(import.meta, '.env');
|
|
37
|
+
const configUrl = pathUrl(import.meta, '.env');
|
|
38
|
+
const absolutePath = resolvePath(import.meta, 'config');
|
|
39
|
+
|
|
40
|
+
log.info('Application starting', { configPath, configUrl, absolutePath });
|
|
41
|
+
log.info(`Files: ${fs.readdirSync(path(import.meta)).join(', ')}`);
|
|
42
|
+
|
|
43
|
+
const errors = registerHandlers({ events: ['uncaughtException', 'unhandledRejection'] });
|
|
44
|
+
const signals = registerSignals({ shutdownHook: async () => errors.removeHandlers() });
|
|
45
|
+
|
|
46
|
+
process.once('SIGTERM', () => void signals.shutdown('SIGTERM'));
|
|
85
47
|
```
|
|
86
48
|
|
|
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
49
|
## TypeScript
|
|
136
50
|
|
|
137
|
-
Type
|
|
51
|
+
Type declarations are included and expose the dependency option and return types:
|
|
138
52
|
|
|
139
53
|
```ts
|
|
140
|
-
import {
|
|
54
|
+
import {
|
|
55
|
+
createLogger,
|
|
56
|
+
getCurrentDirname,
|
|
57
|
+
registerSignals,
|
|
58
|
+
type RegisterSignalsOptions,
|
|
59
|
+
} from '@eliware/common';
|
|
60
|
+
|
|
61
|
+
const logger = createLogger({ format: 'json' });
|
|
62
|
+
const directory = getCurrentDirname(import.meta);
|
|
63
|
+
const options: RegisterSignalsOptions = { exit: false };
|
|
64
|
+
const registration = registerSignals(options);
|
|
141
65
|
```
|
|
142
66
|
|
|
143
|
-
##
|
|
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)**
|
|
67
|
+
## Development
|
|
150
68
|
|
|
151
|
-
|
|
69
|
+
```bash
|
|
70
|
+
npm install
|
|
71
|
+
npm test
|
|
72
|
+
npm run lint
|
|
73
|
+
```
|
|
152
74
|
|
|
153
|
-
|
|
75
|
+
This package is a re-export/compatibility layer. Its tests verify the public export contract and representative delegation to the underlying packages; implementation behavior is tested in those dependency packages.
|
|
154
76
|
|
|
155
77
|
## Links
|
|
156
78
|
|
|
157
|
-
- [Home Page](https://eliware.org)
|
|
158
79
|
- [GitHub](https://github.com/eliware/common)
|
|
159
80
|
- [npm](https://www.npmjs.com/package/@eliware/common)
|
|
160
|
-
- [Discord](https://discord.gg/M6aTR9eTwN)
|
|
81
|
+
- [eliware Discord](https://discord.gg/M6aTR9eTwN)
|
|
82
|
+
|
|
83
|
+
## License
|
|
84
|
+
|
|
85
|
+
[MIT](LICENSE)
|
package/index.d.ts
CHANGED
|
@@ -1,15 +1,32 @@
|
|
|
1
1
|
import * as fs from 'fs';
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
import type logger from '@eliware/log';
|
|
3
|
+
import type {
|
|
4
|
+
RegisterHandlersOptions,
|
|
5
|
+
RegisteredHandlers,
|
|
6
|
+
} from '@eliware/errors';
|
|
7
|
+
import type {
|
|
8
|
+
RegisterSignalsOptions,
|
|
9
|
+
SignalsRegistration,
|
|
10
|
+
} from '@eliware/signals';
|
|
6
11
|
|
|
7
12
|
export { fs };
|
|
8
|
-
export { log };
|
|
9
|
-
export {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
export { createLogger, default as log, safeSerialize } from '@eliware/log';
|
|
14
|
+
export {
|
|
15
|
+
path,
|
|
16
|
+
pathUrl,
|
|
17
|
+
getCurrentDirname,
|
|
18
|
+
getCurrentFilename,
|
|
19
|
+
resolvePath,
|
|
20
|
+
relativePath,
|
|
21
|
+
fileUrlToPath,
|
|
22
|
+
} from '@eliware/path';
|
|
23
|
+
export { default as registerHandlers } from '@eliware/errors';
|
|
24
|
+
export { default as registerSignals } from '@eliware/signals';
|
|
25
|
+
export type {
|
|
26
|
+
RegisterHandlersOptions,
|
|
27
|
+
RegisteredHandlers,
|
|
28
|
+
RegisterSignalsOptions,
|
|
29
|
+
SignalsRegistration,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type Logger = typeof logger;
|
package/index.mjs
CHANGED
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { path, pathUrl, getCurrentDirname, getCurrentFilename } from '@eliware/path';
|
|
4
|
-
import { registerHandlers } from '@eliware/errors';
|
|
5
|
-
import { registerSignals } from '@eliware/signals';
|
|
6
|
-
|
|
1
|
+
export { default as fs } from './src/fs.mjs';
|
|
2
|
+
export { default as log, createLogger, safeSerialize } from './src/log.mjs';
|
|
7
3
|
export {
|
|
8
|
-
|
|
9
|
-
log,
|
|
10
|
-
path,
|
|
4
|
+
default as path,
|
|
11
5
|
pathUrl,
|
|
12
6
|
getCurrentDirname,
|
|
13
7
|
getCurrentFilename,
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
8
|
+
resolvePath,
|
|
9
|
+
relativePath,
|
|
10
|
+
fileUrlToPath,
|
|
11
|
+
} from './src/path.mjs';
|
|
12
|
+
export { default as registerHandlers } from './src/errors.mjs';
|
|
13
|
+
export { default as registerSignals } from './src/signals.mjs';
|
package/package.json
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eliware/common",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.1.
|
|
5
|
-
"description": "An ESM/Jest/Node-friendly utility for resolving file and directory paths, logging, error handling, and signal handling in both
|
|
6
|
-
"main": "index.cjs",
|
|
4
|
+
"version": "1.1.5",
|
|
5
|
+
"description": "An ESM/Jest/Node-friendly utility for resolving file and directory paths, logging, error handling, and signal handling in both ESM environments.",
|
|
7
6
|
"module": "index.mjs",
|
|
8
7
|
"types": "index.d.ts",
|
|
9
8
|
"files": [
|
|
10
9
|
"index.mjs",
|
|
11
|
-
"index.cjs",
|
|
12
10
|
"index.d.ts",
|
|
13
11
|
"README.md",
|
|
14
|
-
"LICENSE"
|
|
12
|
+
"LICENSE",
|
|
13
|
+
"src"
|
|
15
14
|
],
|
|
16
15
|
"exports": {
|
|
17
16
|
".": {
|
|
18
17
|
"import": "./index.mjs",
|
|
19
|
-
"require": "./index.cjs",
|
|
20
18
|
"types": "./index.d.ts"
|
|
21
19
|
}
|
|
22
20
|
},
|
|
23
21
|
"scripts": {
|
|
24
|
-
"test": "npx --node-options=\"--experimental-vm-modules --no-warnings\" jest --detectOpenHandles --silent"
|
|
22
|
+
"test": "npx --node-options=\"--experimental-vm-modules --no-warnings\" jest --detectOpenHandles --silent --coverage",
|
|
23
|
+
"test:gaps": "npm test -- --coverageReporters=text 2>&1 | awk '!/100%/'",
|
|
24
|
+
"lint": "npx oxlint ."
|
|
25
25
|
},
|
|
26
26
|
"repository": {
|
|
27
27
|
"type": "git",
|
|
@@ -35,7 +35,6 @@
|
|
|
35
35
|
"error handling",
|
|
36
36
|
"signal handling",
|
|
37
37
|
"esm",
|
|
38
|
-
"commonjs",
|
|
39
38
|
"typescript"
|
|
40
39
|
],
|
|
41
40
|
"author": "Eli Sterling, eliware.org <eli@eliware.org>",
|
|
@@ -51,14 +50,13 @@
|
|
|
51
50
|
"testEnvironment": "node",
|
|
52
51
|
"testMatch": [
|
|
53
52
|
"**/?(*.)+(spec|test).[jt]s?(x)",
|
|
54
|
-
"**/?(*.)+(spec|test).mjs"
|
|
55
|
-
"**/?(*.)+(spec|test).cjs"
|
|
53
|
+
"**/?(*.)+(spec|test).mjs"
|
|
56
54
|
]
|
|
57
55
|
},
|
|
58
56
|
"dependencies": {
|
|
59
|
-
"@eliware/errors": "^1.1.
|
|
60
|
-
"@eliware/log": "^1.1.
|
|
61
|
-
"@eliware/path": "^1.1.
|
|
62
|
-
"@eliware/signals": "^1.1.
|
|
57
|
+
"@eliware/errors": "^1.1.5",
|
|
58
|
+
"@eliware/log": "^1.1.9",
|
|
59
|
+
"@eliware/path": "^1.1.6",
|
|
60
|
+
"@eliware/signals": "^1.1.4"
|
|
63
61
|
}
|
|
64
62
|
}
|
package/src/errors.mjs
ADDED
package/src/fs.mjs
ADDED
package/src/log.mjs
ADDED
package/src/path.mjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {
|
|
2
|
+
path,
|
|
3
|
+
pathUrl,
|
|
4
|
+
getCurrentDirname,
|
|
5
|
+
getCurrentFilename,
|
|
6
|
+
resolvePath,
|
|
7
|
+
relativePath,
|
|
8
|
+
fileUrlToPath,
|
|
9
|
+
} from '@eliware/path';
|
|
10
|
+
|
|
11
|
+
export { path, pathUrl, getCurrentDirname, getCurrentFilename, resolvePath, relativePath, fileUrlToPath };
|
|
12
|
+
export default path;
|
package/src/signals.mjs
ADDED
package/index.cjs
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const log = require('@eliware/log').default || require('@eliware/log');
|
|
3
|
-
const pathModule = require('@eliware/path');
|
|
4
|
-
const path = pathModule.default || pathModule;
|
|
5
|
-
const pathUrl = pathModule.pathUrl;
|
|
6
|
-
const getCurrentDirname = pathModule.getCurrentDirname;
|
|
7
|
-
const getCurrentFilename = pathModule.getCurrentFilename;
|
|
8
|
-
const { registerHandlers } = require('@eliware/errors');
|
|
9
|
-
const { registerSignals } = require('@eliware/signals');
|
|
10
|
-
|
|
11
|
-
module.exports = {
|
|
12
|
-
fs,
|
|
13
|
-
log,
|
|
14
|
-
path,
|
|
15
|
-
pathUrl,
|
|
16
|
-
getCurrentDirname,
|
|
17
|
-
getCurrentFilename,
|
|
18
|
-
registerHandlers,
|
|
19
|
-
registerSignals,
|
|
20
|
-
};
|