@vyriy/script 0.1.9

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Vyriy contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,136 @@
1
+ # @vyriy/script
2
+
3
+ Composable script wrappers for Vyriy projects.
4
+
5
+ ## Purpose
6
+
7
+ This package provides a small composition layer for CLI-style or bootstrap-style async tasks.
8
+
9
+ It includes:
10
+
11
+ - a generic `factory()` helper for building wrappers
12
+ - `compose()` for combining decorators
13
+ - a ready-made `script` decorator with the default Vyriy wrapper chain
14
+ - focused wrappers for error handling, logging, timeout control, and process exit codes
15
+
16
+ ## Install
17
+
18
+ With npm:
19
+
20
+ ```bash
21
+ npm install @vyriy/script
22
+ ```
23
+
24
+ With Yarn:
25
+
26
+ ```bash
27
+ yarn add @vyriy/script
28
+ ```
29
+
30
+ ## Usage
31
+
32
+ Use the default `script` decorator:
33
+
34
+ ```ts
35
+ import { script } from '@vyriy/script';
36
+
37
+ await script(async () => {
38
+ // your task
39
+ });
40
+ ```
41
+
42
+ Or compose wrappers manually:
43
+
44
+ ```ts
45
+ import { compose, withError, withLogger, withTimeout } from '@vyriy/script';
46
+
47
+ const run = compose(
48
+ withError({
49
+ errorHandler: async (error) => {
50
+ console.error('Task failed:', error);
51
+ },
52
+ }),
53
+ withLogger(),
54
+ withTimeout(),
55
+ );
56
+
57
+ await run(async () => {
58
+ // your task
59
+ });
60
+ ```
61
+
62
+ ## Default Script Chain
63
+
64
+ The exported `script` decorator is built from:
65
+
66
+ - `withExit()`
67
+ - `withError()`
68
+ - `withLogger()`
69
+ - `withTimeout()`
70
+
71
+ Applied through `compose(...)`, this means the task gets timeout handling, logging, error interception, and final process exit handling in one reusable decorator.
72
+
73
+ ## API
74
+
75
+ - `compose(...decorators)`
76
+ Combines decorators from right to left into one decorator.
77
+
78
+ - `factory(wrapper)`
79
+ Creates decorator factories from `(task, options?) => Promise<void>` wrappers.
80
+
81
+ - `script`
82
+ Default decorator composed from the built-in wrappers.
83
+
84
+ - `withError(options?)`
85
+ Wraps a task with optional async error handling.
86
+
87
+ - `withExit()`
88
+ Exits the process with `0` on success and `1` on failure.
89
+
90
+ - `withLogger(options?)`
91
+ Logs task start, success, and failure messages. Uses `createLogger()` by default.
92
+
93
+ - `withTimeout()`
94
+ Reads `TIMEOUT` through `@vyriy/config` and races the task against `@vyriy/timeout`.
95
+
96
+ ## Wrapper Options
97
+
98
+ `withError(options?)`
99
+
100
+ ```ts
101
+ {
102
+ errorHandler?: (error: unknown) => Promise<void>;
103
+ }
104
+ ```
105
+
106
+ `withLogger(options?)`
107
+
108
+ ```ts
109
+ {
110
+ logger?: typeof console;
111
+ }
112
+ ```
113
+
114
+ ## Exports
115
+
116
+ The package exposes:
117
+
118
+ ```ts
119
+ import { compose, factory, script, withError, withExit, withLogger, withTimeout } from '@vyriy/script';
120
+
121
+ import { compose } from '@vyriy/script/compose';
122
+ import { factory } from '@vyriy/script/factory';
123
+
124
+ import { withError } from '@vyriy/script/wrapper/error';
125
+ import { withExit } from '@vyriy/script/wrapper/exit';
126
+ import { withLogger } from '@vyriy/script/wrapper/logger';
127
+ import { withTimeout } from '@vyriy/script/wrapper/timeout';
128
+
129
+ import type { Compose, Decorator, Factory, Result, Task, Wrapper } from '@vyriy/script/types';
130
+ ```
131
+
132
+ ## Notes
133
+
134
+ - decorators operate on async tasks shaped as `() => Promise<void>`
135
+ - `withTimeout()` uses no timeout when `TIMEOUT` is missing and parses configured values as durations
136
+ - `withExit()` is intended for real script entrypoints because it calls `process.exit(...)`
package/compose.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { Compose } from './types.js';
2
+ export declare const compose: Compose;
package/compose.js ADDED
@@ -0,0 +1,3 @@
1
+ export const compose = (...fns) => fns.length
2
+ ? fns.reduceRight((prevFn, nextFn) => async (task) => nextFn(async () => prevFn(task)))
3
+ : async (task) => task();
package/factory.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ import type { Decorator, Wrapper } from './types.js';
2
+ export declare const factory: <O = void>(wrapper: Wrapper<O>) => (options?: O) => Decorator;
package/factory.js ADDED
@@ -0,0 +1 @@
1
+ export const factory = (wrapper) => (options) => (task) => wrapper(task, options);
package/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from './script.js';
2
+ export * from './compose.js';
3
+ export * from './factory.js';
4
+ export * from './wrapper/error.js';
5
+ export * from './wrapper/exit.js';
6
+ export * from './wrapper/logger.js';
7
+ export * from './wrapper/timeout.js';
8
+ export type * from './types.js';
package/index.js ADDED
@@ -0,0 +1,7 @@
1
+ export * from './script.js';
2
+ export * from './compose.js';
3
+ export * from './factory.js';
4
+ export * from './wrapper/error.js';
5
+ export * from './wrapper/exit.js';
6
+ export * from './wrapper/logger.js';
7
+ export * from './wrapper/timeout.js';
package/package.json ADDED
@@ -0,0 +1,101 @@
1
+ {
2
+ "name": "@vyriy/script",
3
+ "version": "0.1.9",
4
+ "description": "Script utilities for Vyriy projects",
5
+ "type": "module",
6
+ "main": "./index.js",
7
+ "dependencies": {
8
+ "@vyriy/config": "0.1.9",
9
+ "@vyriy/logger": "0.1.9",
10
+ "@vyriy/timeout": "0.1.9"
11
+ },
12
+ "license": "MIT",
13
+ "types": "./index.d.ts",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./index.d.ts",
17
+ "import": "./index.js",
18
+ "default": "./index.js"
19
+ },
20
+ "./compose": {
21
+ "types": "./compose.d.ts",
22
+ "import": "./compose.js",
23
+ "default": "./compose.js"
24
+ },
25
+ "./compose.js": {
26
+ "types": "./compose.d.ts",
27
+ "import": "./compose.js",
28
+ "default": "./compose.js"
29
+ },
30
+ "./factory": {
31
+ "types": "./factory.d.ts",
32
+ "import": "./factory.js",
33
+ "default": "./factory.js"
34
+ },
35
+ "./factory.js": {
36
+ "types": "./factory.d.ts",
37
+ "import": "./factory.js",
38
+ "default": "./factory.js"
39
+ },
40
+ "./index": {
41
+ "types": "./index.d.ts",
42
+ "import": "./index.js",
43
+ "default": "./index.js"
44
+ },
45
+ "./index.js": {
46
+ "types": "./index.d.ts",
47
+ "import": "./index.js",
48
+ "default": "./index.js"
49
+ },
50
+ "./script": {
51
+ "types": "./script.d.ts",
52
+ "import": "./script.js",
53
+ "default": "./script.js"
54
+ },
55
+ "./script.js": {
56
+ "types": "./script.d.ts",
57
+ "import": "./script.js",
58
+ "default": "./script.js"
59
+ },
60
+ "./wrapper/error": {
61
+ "types": "./wrapper/error.d.ts",
62
+ "import": "./wrapper/error.js",
63
+ "default": "./wrapper/error.js"
64
+ },
65
+ "./wrapper/error.js": {
66
+ "types": "./wrapper/error.d.ts",
67
+ "import": "./wrapper/error.js",
68
+ "default": "./wrapper/error.js"
69
+ },
70
+ "./wrapper/exit": {
71
+ "types": "./wrapper/exit.d.ts",
72
+ "import": "./wrapper/exit.js",
73
+ "default": "./wrapper/exit.js"
74
+ },
75
+ "./wrapper/exit.js": {
76
+ "types": "./wrapper/exit.d.ts",
77
+ "import": "./wrapper/exit.js",
78
+ "default": "./wrapper/exit.js"
79
+ },
80
+ "./wrapper/logger": {
81
+ "types": "./wrapper/logger.d.ts",
82
+ "import": "./wrapper/logger.js",
83
+ "default": "./wrapper/logger.js"
84
+ },
85
+ "./wrapper/logger.js": {
86
+ "types": "./wrapper/logger.d.ts",
87
+ "import": "./wrapper/logger.js",
88
+ "default": "./wrapper/logger.js"
89
+ },
90
+ "./wrapper/timeout": {
91
+ "types": "./wrapper/timeout.d.ts",
92
+ "import": "./wrapper/timeout.js",
93
+ "default": "./wrapper/timeout.js"
94
+ },
95
+ "./wrapper/timeout.js": {
96
+ "types": "./wrapper/timeout.d.ts",
97
+ "import": "./wrapper/timeout.js",
98
+ "default": "./wrapper/timeout.js"
99
+ }
100
+ }
101
+ }
package/script.d.ts ADDED
@@ -0,0 +1 @@
1
+ export declare const script: import("./types.js").Decorator;
package/script.js ADDED
@@ -0,0 +1,6 @@
1
+ import { withExit } from './wrapper/exit.js';
2
+ import { withError } from './wrapper/error.js';
3
+ import { withLogger } from './wrapper/logger.js';
4
+ import { withTimeout } from './wrapper/timeout.js';
5
+ import { compose } from './compose.js';
6
+ export const script = compose(withExit(), withError(), withLogger(), withTimeout());
package/types.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export type Result = Promise<void>;
2
+ export type Task = () => Result;
3
+ export type Wrapper<O = void> = (task: Task, options?: O) => Result;
4
+ export type Decorator = (task: Task) => Result;
5
+ export type Factory = <O = void>(wrapper: Wrapper<O>) => (options?: O) => Decorator;
6
+ export type Compose = (...decorators: Decorator[]) => Decorator;
@@ -0,0 +1,4 @@
1
+ export type ErrorOptions = {
2
+ errorHandler?: (err: unknown) => Promise<void>;
3
+ };
4
+ export declare const withError: (options?: ErrorOptions | undefined) => import("../types.js").Decorator;
@@ -0,0 +1,13 @@
1
+ import { factory } from '../factory.js';
2
+ export const withError = factory(async (handler, options) => {
3
+ const errorHandler = options?.errorHandler;
4
+ try {
5
+ await handler();
6
+ }
7
+ catch (error) {
8
+ if (errorHandler) {
9
+ await errorHandler(error);
10
+ }
11
+ throw error;
12
+ }
13
+ });
@@ -0,0 +1 @@
1
+ export declare const withExit: (options?: void | undefined) => import("../types.js").Decorator;
@@ -0,0 +1,10 @@
1
+ import { factory } from '../factory.js';
2
+ export const withExit = factory(async (handler) => {
3
+ try {
4
+ await handler();
5
+ process.exit(0);
6
+ }
7
+ catch {
8
+ process.exit(1);
9
+ }
10
+ });
@@ -0,0 +1,4 @@
1
+ export type LoggerOptions = {
2
+ logger?: typeof console;
3
+ };
4
+ export declare const withLogger: (options?: LoggerOptions | undefined) => import("../types.js").Decorator;
@@ -0,0 +1,17 @@
1
+ import { createLogger } from '@vyriy/logger';
2
+ import { factory } from '../factory.js';
3
+ export const withLogger = factory(async (handler, options = {}) => {
4
+ const { logger = createLogger() } = options;
5
+ logger.info('Task started...');
6
+ try {
7
+ await handler();
8
+ logger.info('Task finished!');
9
+ }
10
+ catch (error) {
11
+ if (error instanceof Error) {
12
+ logger.error('Task error:', error.message);
13
+ }
14
+ logger.error(error);
15
+ throw error;
16
+ }
17
+ });
@@ -0,0 +1 @@
1
+ export declare const withTimeout: (options?: void | undefined) => import("../types.js").Decorator;
@@ -0,0 +1,12 @@
1
+ import { getConfig, Parser } from '@vyriy/config';
2
+ import { timeout as error } from '@vyriy/timeout';
3
+ import { factory } from '../factory.js';
4
+ export const withTimeout = factory(async (handler) => {
5
+ const timeout = getConfig('TIMEOUT', 0, Parser.duration);
6
+ if (timeout) {
7
+ await Promise.race([handler(), error(timeout)]);
8
+ }
9
+ else {
10
+ await handler();
11
+ }
12
+ });