@art-lib/operations 0.0.2 → 0.0.3
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/CHANGELOG.md +24 -0
- package/README.md +68 -64
- package/dist/index.mjs +33 -19
- package/dist/index.mjs.map +3 -3
- package/dist/log/createOperationsLog.d.ts +4 -2
- package/dist/log/createPipeSeparatedFormatter.d.ts +3 -0
- package/dist/log/index.d.ts +2 -1
- package/dist/log/index.mjs +32 -3
- package/dist/log/index.mjs.map +3 -3
- package/dist/log/private/constants.d.ts +4 -0
- package/dist/log/private/isFinished.d.ts +3 -2
- package/dist/log/types.d.ts +6 -10
- package/dist/model/createOperationFailure.d.ts +1 -3
- package/dist/model/index.d.ts +1 -1
- package/dist/model/index.mjs +2 -17
- package/dist/model/index.mjs.map +3 -3
- package/dist/model/types.d.ts +5 -10
- package/dist/test/helpers/log/createOperationsLogMock.d.ts +3 -0
- package/dist/test/helpers/model/makeFailureOperationFixture.d.ts +3 -0
- package/dist/test/helpers/model/makePendingOperationFixture.d.ts +3 -0
- package/dist/test/helpers/model/makeSuccessOperationFixture.d.ts +3 -0
- package/dist/test/helpers/model/types.d.ts +3 -0
- package/package.json +8 -12
- package/src/test/helpers/log/createOperationsLogMock.ts +13 -0
- package/src/test/helpers/model/makeFailureOperationFixture.ts +19 -0
- package/src/test/helpers/model/makePendingOperationFixture.ts +14 -0
- package/src/test/helpers/model/makeSuccessOperationFixture.ts +18 -0
- package/src/test/helpers/model/operationFixtures.test.ts +13 -0
- package/src/test/helpers/model/types.ts +3 -0
- package/dist/model/private/constants.d.ts +0 -3
- package/dist/test/helpers/logger/createLoggerMock.d.ts +0 -3
- package/dist/test/helpers/logger/types.d.ts +0 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# CHANGELOG
|
|
2
2
|
|
|
3
|
+
## 0.0.3
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- Add an injectable operation formatter and a pipe-separated formatter for status symbols and elapsed time.
|
|
8
|
+
- Export reusable operation test helpers.
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
|
|
12
|
+
- Use `@art-lib/logger` 0.0.4; translate operation outcomes into logger messages and levels.
|
|
13
|
+
- Consolidate model and log APIs at the package root.
|
|
14
|
+
|
|
15
|
+
### Removed
|
|
16
|
+
|
|
17
|
+
- Remove operation-level `level`, failure labels, and `errorSerialized()`.
|
|
18
|
+
|
|
19
|
+
### Tested
|
|
20
|
+
|
|
21
|
+
- Cover typed operation lifecycles, formatter output, logger forwarding, history queries, and error handling.
|
|
22
|
+
|
|
23
|
+
### Documented
|
|
24
|
+
|
|
25
|
+
- Update the README and architecture documents for the operation model, logger boundary, and formatter.
|
|
26
|
+
|
|
3
27
|
## 0.0.2
|
|
4
28
|
|
|
5
29
|
### Added
|
package/README.md
CHANGED
|
@@ -1,110 +1,114 @@
|
|
|
1
1
|
# @art-lib/operations
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> Model CLI operations and forward their formatted messages to `@art-lib/logger`.
|
|
4
4
|
|
|
5
5
|
This package is part of the [Art Lib toolkit](../../README.md).
|
|
6
6
|
|
|
7
|
-
##
|
|
8
|
-
|
|
9
|
-
The package defines the operation model: one unit of work in a command invocation, carrying a pending, success, or failure outcome. Factories build the pending operation and then derive the finished one from it. The package also provides a thin, append-only operations log. Created alongside a logger, it forwards every operation to that logger and keeps the finished ones queryable.
|
|
10
|
-
|
|
11
|
-
Every operation doubles as a log message for [`@art-lib/logger`](../logger/README.md). It carries a level derived from its outcome — pending logs as debug, success as info, failure as error — so the logger consumes operations directly without ever learning what they are. That logger is the package's only dependency.
|
|
12
|
-
|
|
13
|
-
See also:
|
|
14
|
-
|
|
15
|
-
- [package knowledge](knowledge/index.md) — overview, design, and decision records
|
|
16
|
-
- [repository design record](../../knowledge/design/operations.md) — what a CLI needs from the package
|
|
17
|
-
|
|
18
|
-
## Usage
|
|
19
|
-
|
|
20
|
-
### Install
|
|
7
|
+
## Install
|
|
21
8
|
|
|
22
9
|
```bash
|
|
23
10
|
npm install @art-lib/operations
|
|
24
11
|
```
|
|
25
12
|
|
|
26
|
-
`@art-lib/
|
|
13
|
+
Import public operations APIs from `@art-lib/operations`.
|
|
27
14
|
|
|
28
|
-
|
|
15
|
+
## Use the generic operation model
|
|
29
16
|
|
|
30
|
-
|
|
17
|
+
The generic factory creates a pending operation from a name, typed data, and an optional function that supplies its detail text. Shared factories create its success or failure state.
|
|
31
18
|
|
|
32
19
|
```ts
|
|
33
20
|
import { createLogger } from '@art-lib/logger';
|
|
34
|
-
import {
|
|
21
|
+
import {
|
|
22
|
+
createGenericOperation,
|
|
23
|
+
createOperationFailure,
|
|
24
|
+
createOperationSuccess,
|
|
25
|
+
createOperationsLog,
|
|
26
|
+
createPipeSeparatedFormatter,
|
|
27
|
+
} from '@art-lib/operations';
|
|
35
28
|
|
|
36
29
|
const logger = createLogger();
|
|
30
|
+
logger.setOutputMode('normal');
|
|
31
|
+
|
|
32
|
+
const operations = createOperationsLog(logger, {
|
|
33
|
+
formatter: createPipeSeparatedFormatter(operation => [operation.operation, operation.text()]),
|
|
34
|
+
});
|
|
37
35
|
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
const uri = 'notes.art';
|
|
37
|
+
const pending = createGenericOperation('parse', { uri }, () => uri);
|
|
38
|
+
operations.log(pending);
|
|
40
39
|
|
|
41
|
-
|
|
42
|
-
|
|
40
|
+
try {
|
|
41
|
+
const document = await parse(uri);
|
|
42
|
+
operations.log(createOperationSuccess(pending, `parsed ${document.name}`));
|
|
43
|
+
} catch (error) {
|
|
44
|
+
operations.log(createOperationFailure(pending, error));
|
|
45
|
+
}
|
|
43
46
|
```
|
|
44
47
|
|
|
45
|
-
The
|
|
48
|
+
The formatter callback returns the ordered field values for an operation. The default pipe-separated formatter adds an outcome symbol and, for completed operations, the elapsed time. The operations log derives the logger level from the outcome—`pending` to `debug`, `success` to `info`, and `failure` to `error`—then forwards a `{ level, text, ts }` message to the injected logger.
|
|
46
49
|
|
|
47
|
-
|
|
50
|
+
## Define CLI-specific operations
|
|
51
|
+
|
|
52
|
+
A CLI can define a typed pending operation and one factory for each operation kind. Each factory specialises the shared model with a literal operation name, a typed data shape, and operation-specific detail text. The shared success and failure factories handle completion for all operation kinds.
|
|
48
53
|
|
|
49
54
|
```ts
|
|
50
55
|
import {
|
|
51
56
|
createGenericOperation,
|
|
52
|
-
|
|
53
|
-
|
|
57
|
+
type OperationPending,
|
|
58
|
+
type OperationSuccess,
|
|
59
|
+
type OperationFailure,
|
|
54
60
|
} from '@art-lib/operations';
|
|
55
|
-
import { operations } from './log.js';
|
|
56
61
|
|
|
57
|
-
|
|
62
|
+
export type PullData = {
|
|
63
|
+
checkout?: Checkout;
|
|
64
|
+
branch: string;
|
|
65
|
+
};
|
|
58
66
|
|
|
59
|
-
|
|
60
|
-
|
|
67
|
+
export type PullPending = OperationPending<PullData> & {
|
|
68
|
+
operation: 'pull';
|
|
69
|
+
};
|
|
61
70
|
|
|
62
|
-
|
|
63
|
-
const
|
|
71
|
+
export function createPullOperation(checkout: Checkout, branch: string): PullPending {
|
|
72
|
+
const data = { checkout, branch };
|
|
73
|
+
const toText = () => `from origin/${branch}`;
|
|
64
74
|
|
|
65
|
-
|
|
66
|
-
} catch (error) {
|
|
67
|
-
// The label prefixes `errorSerialized()` only — `error` stays the raw message.
|
|
68
|
-
operations.log(createOperationFailure(pending, error, { label: 'CloneError' })); // stored
|
|
75
|
+
return createGenericOperation('pull', data, toText);
|
|
69
76
|
}
|
|
70
77
|
|
|
71
|
-
|
|
72
|
-
operations.all(); // every finished operation
|
|
73
|
-
operations.since(startedAt); // created at or after `startedAt`
|
|
74
|
-
operations.latest(5); // the last five finished operations
|
|
78
|
+
export type Operation = PullPending | OperationPending | OperationSuccess | OperationFailure;
|
|
75
79
|
```
|
|
76
80
|
|
|
77
|
-
|
|
81
|
+
A CLI's complete `Operation` union combines its specific pending types—such as `ClonePending`, `PushPending`, and `PullPending`—with the common pending, success, and failure types. Its formatter callback selects the fields for the CLI's standard output layout.
|
|
78
82
|
|
|
79
|
-
|
|
80
|
-
| -------------------------------------------------- | ---------------------------------------------------- | ------------------ | ------------------------------------------------------------------- |
|
|
81
|
-
| `createGenericOperation(name, data?, text?)` | operation name, optional payload, optional text | `OperationPending` | frozen; reports the payload as JSON by default; `timing()` is `NaN` |
|
|
82
|
-
| `createOperationSuccess(pending, text?)` | a pending operation, optional success text | `OperationSuccess` | falls back to the pending's own text |
|
|
83
|
-
| `createOperationFailure(pending, error, options?)` | a pending operation, any error-shaped value, options | `OperationFailure` | the options carry a label that prefixes `errorSerialized()` |
|
|
84
|
-
| `createOperationsLog(logger, options?)` | a logger, reserved options | `OperationsLog` | forwards every operation; stores finished operations only |
|
|
83
|
+
## Operations log
|
|
85
84
|
|
|
86
|
-
|
|
87
|
-
- **Options** — the log accepts a second argument and deliberately ignores it; it is reserved for a future iteration.
|
|
88
|
-
- **Operations log** — offers `log`, `all`, `since` and `latest`. The query methods return copies, never the internal buffer.
|
|
85
|
+
Create one operations log with a logger and formatter for each command invocation. Every logged operation is converted to a logger message and forwarded. Completed operations are retained for `all()`, `since(timestamp)`, and `latest(count)` queries; pending operations are forwarded as well.
|
|
89
86
|
|
|
90
|
-
|
|
87
|
+
The standard pipe-separated format can produce output such as:
|
|
91
88
|
|
|
92
|
-
|
|
89
|
+
```text
|
|
90
|
+
⏳ | Art MD | art-md | pull | from origin/main |
|
|
91
|
+
🔴 | Workspace Tooling | workspace-tooling | pull | Could not read from remote repository. | (1224ms)
|
|
92
|
+
⏳ | Art MD | art-md | scan-checkout-state | art-md |
|
|
93
|
+
🟢 | Art MD | art-md | pull | from origin/main | (3317ms)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Development
|
|
93
97
|
|
|
94
|
-
|
|
98
|
+
Read the [Art Lib README](../../README.md) before contributing.
|
|
95
99
|
|
|
96
|
-
This package
|
|
100
|
+
This package targets Node.js and uses `esbuild` for its entry point.
|
|
97
101
|
|
|
98
|
-
|
|
102
|
+
Available scripts:
|
|
99
103
|
|
|
100
|
-
-
|
|
101
|
-
-
|
|
102
|
-
-
|
|
103
|
-
-
|
|
104
|
-
-
|
|
105
|
-
-
|
|
106
|
-
-
|
|
104
|
+
- `npm run dev` — Watch for changes
|
|
105
|
+
- `npm run lint` — Check formatting, lint and types
|
|
106
|
+
- `npm run lint:fix` — Fix formatting and lint issues
|
|
107
|
+
- `npm run build` — Build the package
|
|
108
|
+
- `npm run build:clean` — Remove build artefacts
|
|
109
|
+
- `npm run test` — Run tests
|
|
110
|
+
- `npm run ci` — Run the CI pipeline
|
|
107
111
|
|
|
108
112
|
## Copyright (c) 2026 [Noodlestan](https://noodlestan.org/).
|
|
109
113
|
|
|
110
|
-
Published under
|
|
114
|
+
Published under the [MIT licence](https://noodlestan.mit-license.org/).
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
// src/log/private/constants.ts
|
|
2
|
+
var OUTCOME_SYMBOLS = {
|
|
3
|
+
pending: "\u23F3",
|
|
4
|
+
success: "\u{1F7E2}",
|
|
5
|
+
failure: "\u{1F534}"
|
|
6
|
+
};
|
|
7
|
+
var OUTCOME_LEVELS = {
|
|
8
|
+
pending: "debug",
|
|
9
|
+
success: "info",
|
|
10
|
+
failure: "error"
|
|
11
|
+
};
|
|
12
|
+
|
|
1
13
|
// src/log/private/isFinished.ts
|
|
2
14
|
function isFinished(operation) {
|
|
3
15
|
return operation.outcome !== "pending";
|
|
@@ -5,11 +17,15 @@ function isFinished(operation) {
|
|
|
5
17
|
|
|
6
18
|
// src/log/createOperationsLog.ts
|
|
7
19
|
function createOperationsLog(logger, options) {
|
|
8
|
-
void options;
|
|
9
20
|
const storedOperations = [];
|
|
10
21
|
return {
|
|
11
22
|
log(operation) {
|
|
12
|
-
|
|
23
|
+
const message = {
|
|
24
|
+
level: OUTCOME_LEVELS[operation.outcome],
|
|
25
|
+
text: options.formatter(operation),
|
|
26
|
+
ts: operation.ts
|
|
27
|
+
};
|
|
28
|
+
logger.log(message);
|
|
13
29
|
if (isFinished(operation)) {
|
|
14
30
|
storedOperations.push(operation);
|
|
15
31
|
}
|
|
@@ -29,12 +45,17 @@ function createOperationsLog(logger, options) {
|
|
|
29
45
|
};
|
|
30
46
|
}
|
|
31
47
|
|
|
32
|
-
// src/
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
48
|
+
// src/log/createPipeSeparatedFormatter.ts
|
|
49
|
+
function createPipeSeparatedFormatter(getFields) {
|
|
50
|
+
return (operation) => {
|
|
51
|
+
const fields = [OUTCOME_SYMBOLS[operation.outcome], ...getFields(operation)];
|
|
52
|
+
const line = `${fields.join(" | ")} |`;
|
|
53
|
+
if (operation.outcome === "pending") {
|
|
54
|
+
return line;
|
|
55
|
+
}
|
|
56
|
+
return `${line} (${operation.timing()}ms)`;
|
|
57
|
+
};
|
|
58
|
+
}
|
|
38
59
|
|
|
39
60
|
// src/model/private/formatData.ts
|
|
40
61
|
function formatData(data) {
|
|
@@ -52,7 +73,6 @@ function createGenericOperation(name, data, toText) {
|
|
|
52
73
|
operation: name,
|
|
53
74
|
ts,
|
|
54
75
|
outcome: "pending",
|
|
55
|
-
level: LEVELS.pending,
|
|
56
76
|
data,
|
|
57
77
|
text: readText,
|
|
58
78
|
timing() {
|
|
@@ -84,29 +104,23 @@ function readRawErrorMessage(error) {
|
|
|
84
104
|
}
|
|
85
105
|
|
|
86
106
|
// src/model/createOperationFailure.ts
|
|
87
|
-
function createOperationFailure(pending, error
|
|
107
|
+
function createOperationFailure(pending, error) {
|
|
88
108
|
const finishedTs = Date.now();
|
|
89
109
|
const rawError = readRawErrorMessage(error);
|
|
90
|
-
const label = options?.label;
|
|
91
110
|
function report() {
|
|
92
111
|
return rawError;
|
|
93
112
|
}
|
|
94
|
-
function errorSerialized() {
|
|
95
|
-
return label ? `${label}: ${rawError}` : rawError;
|
|
96
|
-
}
|
|
97
113
|
const failure = {
|
|
98
114
|
operation: pending.operation,
|
|
99
115
|
ts: pending.ts,
|
|
100
116
|
finishedTs,
|
|
101
117
|
outcome: "failure",
|
|
102
|
-
level: LEVELS.failure,
|
|
103
118
|
data: pending.data,
|
|
104
119
|
error: rawError,
|
|
105
120
|
text: report,
|
|
106
121
|
timing() {
|
|
107
122
|
return finishedTs - pending.ts;
|
|
108
|
-
}
|
|
109
|
-
errorSerialized
|
|
123
|
+
}
|
|
110
124
|
};
|
|
111
125
|
return Object.freeze(failure);
|
|
112
126
|
}
|
|
@@ -123,7 +137,6 @@ function createOperationSuccess(pending, text) {
|
|
|
123
137
|
ts: pending.ts,
|
|
124
138
|
finishedTs,
|
|
125
139
|
outcome: "success",
|
|
126
|
-
level: LEVELS.success,
|
|
127
140
|
data: pending.data,
|
|
128
141
|
text: report,
|
|
129
142
|
timing() {
|
|
@@ -136,6 +149,7 @@ export {
|
|
|
136
149
|
createGenericOperation,
|
|
137
150
|
createOperationFailure,
|
|
138
151
|
createOperationSuccess,
|
|
139
|
-
createOperationsLog
|
|
152
|
+
createOperationsLog,
|
|
153
|
+
createPipeSeparatedFormatter
|
|
140
154
|
};
|
|
141
155
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/log/private/isFinished.ts", "../src/log/createOperationsLog.ts", "../src/
|
|
4
|
-
"sourcesContent": ["import type {
|
|
5
|
-
"mappings": ";AAEO,SAAS,WAAW,
|
|
3
|
+
"sources": ["../src/log/private/constants.ts", "../src/log/private/isFinished.ts", "../src/log/createOperationsLog.ts", "../src/log/createPipeSeparatedFormatter.ts", "../src/model/private/formatData.ts", "../src/model/createGenericOperation.ts", "../src/model/private/readRawErrorMessage.ts", "../src/model/createOperationFailure.ts", "../src/model/createOperationSuccess.ts"],
|
|
4
|
+
"sourcesContent": ["import type { MessageLevel } from '@art-lib/logger';\n\nimport type { OperationOutcome } from '../../model/types.js';\n\nexport const OUTCOME_SYMBOLS: Record<OperationOutcome, string> = {\n\tpending: '\u23F3',\n\tsuccess: '\uD83D\uDFE2',\n\tfailure: '\uD83D\uDD34',\n};\n\nexport const OUTCOME_LEVELS: Record<OperationOutcome, MessageLevel> = {\n\tpending: 'debug',\n\tsuccess: 'info',\n\tfailure: 'error',\n};\n", "import type { OperationBase } from '../../model/types.js';\nimport type { AnyFinishedOperation } from '../types.js';\n\nexport function isFinished(operation: OperationBase): operation is AnyFinishedOperation {\n\treturn operation.outcome !== 'pending';\n}\n", "import type { LoggerAPI } from '@art-lib/logger';\n\nimport type { OperationBase } from '../model/index.js';\n\nimport { OUTCOME_LEVELS } from './private/constants.js';\nimport { isFinished } from './private/isFinished.js';\nimport type { AnyFinishedOperation, OperationsLog, OperationsLogOptions } from './types.js';\n\nexport function createOperationsLog<TOperation extends OperationBase = OperationBase>(\n\tlogger: LoggerAPI,\n\toptions: OperationsLogOptions<TOperation>,\n): OperationsLog<TOperation> {\n\tconst storedOperations: AnyFinishedOperation[] = [];\n\n\treturn {\n\t\tlog(operation): void {\n\t\t\tconst message = {\n\t\t\t\tlevel: OUTCOME_LEVELS[operation.outcome],\n\t\t\t\ttext: options.formatter(operation),\n\t\t\t\tts: operation.ts,\n\t\t\t};\n\n\t\t\tlogger.log(message);\n\n\t\t\tif (isFinished(operation)) {\n\t\t\t\tstoredOperations.push(operation);\n\t\t\t}\n\t\t},\n\t\tall(): AnyFinishedOperation[] {\n\t\t\treturn [...storedOperations];\n\t\t},\n\t\tsince(timestamp: number): AnyFinishedOperation[] {\n\t\t\treturn storedOperations.filter(operation => operation.ts >= timestamp);\n\t\t},\n\t\tlatest(count: number): AnyFinishedOperation[] {\n\t\t\tif (count <= 0) {\n\t\t\t\treturn [];\n\t\t\t}\n\n\t\t\treturn storedOperations.slice(Math.max(storedOperations.length - count, 0));\n\t\t},\n\t};\n}\n", "import type { OperationBase } from '../model/index.js';\n\nimport { OUTCOME_SYMBOLS } from './private/constants.js';\nimport type { OperationFormatter } from './types.js';\n\nexport function createPipeSeparatedFormatter<TOperation extends OperationBase = OperationBase>(\n\tgetFields: (operation: TOperation) => readonly string[],\n): OperationFormatter<TOperation> {\n\treturn operation => {\n\t\tconst fields = [OUTCOME_SYMBOLS[operation.outcome], ...getFields(operation)];\n\t\tconst line = `${fields.join(' | ')} |`;\n\n\t\tif (operation.outcome === 'pending') {\n\t\t\treturn line;\n\t\t}\n\n\t\treturn `${line} (${operation.timing()}ms)`;\n\t};\n}\n", "export function formatData(data: unknown): string {\n\treturn data ? JSON.stringify(data) : '';\n}\n", "import { formatData } from './private/formatData.js';\nimport type { OperationPending } from './types.js';\n\nexport function createGenericOperation<N extends string, D = unknown>(\n\tname: N,\n\tdata?: D,\n\ttoText?: (data: D) => string,\n): OperationPending<D> & { operation: N } {\n\tconst ts = Date.now();\n\tconst resolveToText: (value: D) => string = toText ?? formatData;\n\n\tfunction readText(): string {\n\t\treturn resolveToText(data as D);\n\t}\n\n\tconst pending: OperationPending<D> & { operation: N } = {\n\t\toperation: name,\n\t\tts,\n\t\toutcome: 'pending',\n\t\tdata,\n\t\ttext: readText,\n\t\ttiming(): number {\n\t\t\treturn Number.NaN;\n\t\t},\n\t};\n\n\treturn Object.freeze(pending);\n}\n", "export function readRawErrorMessage(error: unknown): string {\n\tif (typeof error === 'string') {\n\t\treturn error;\n\t}\n\n\tif (error === null || error === undefined) {\n\t\treturn '';\n\t}\n\n\tif (typeof error === 'object' && 'message' in error) {\n\t\tconst { message } = error as { message: unknown };\n\n\t\tif (typeof message === 'string') {\n\t\t\treturn message;\n\t\t}\n\t}\n\n\ttry {\n\t\treturn String(error);\n\t} catch {\n\t\treturn '';\n\t}\n}\n", "import { readRawErrorMessage } from './private/readRawErrorMessage.js';\nimport type { OperationFailure, OperationPending } from './types.js';\n\nexport function createOperationFailure<D>(\n\tpending: OperationPending<D>,\n\terror: unknown,\n): OperationFailure<D> {\n\tconst finishedTs = Date.now();\n\tconst rawError = readRawErrorMessage(error);\n\n\tfunction report(): string {\n\t\treturn rawError;\n\t}\n\n\tconst failure: OperationFailure<D> = {\n\t\toperation: pending.operation,\n\t\tts: pending.ts,\n\t\tfinishedTs,\n\t\toutcome: 'failure',\n\t\tdata: pending.data,\n\t\terror: rawError,\n\t\ttext: report,\n\t\ttiming(): number {\n\t\t\treturn finishedTs - pending.ts;\n\t\t},\n\t};\n\n\treturn Object.freeze(failure);\n}\n", "import type { OperationPending, OperationSuccess } from './types.js';\n\nexport function createOperationSuccess<D>(\n\tpending: OperationPending<D>,\n\ttext?: string,\n): OperationSuccess<D> {\n\tconst finishedTs = Date.now();\n\tconst givenText = text ?? pending.text();\n\n\tfunction report(): string {\n\t\treturn givenText;\n\t}\n\n\tconst success: OperationSuccess<D> = {\n\t\toperation: pending.operation,\n\t\tts: pending.ts,\n\t\tfinishedTs,\n\t\toutcome: 'success',\n\t\tdata: pending.data,\n\t\ttext: report,\n\t\ttiming(): number {\n\t\t\treturn finishedTs - pending.ts;\n\t\t},\n\t};\n\n\treturn Object.freeze(success);\n}\n"],
|
|
5
|
+
"mappings": "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",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,2 +1,4 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type { LoggerAPI } from '@art-lib/logger';
|
|
2
|
+
import type { OperationBase } from '../model/index.js';
|
|
3
|
+
import type { OperationsLog, OperationsLogOptions } from './types.js';
|
|
4
|
+
export declare function createOperationsLog<TOperation extends OperationBase = OperationBase>(logger: LoggerAPI, options: OperationsLogOptions<TOperation>): OperationsLog<TOperation>;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { OperationBase } from '../model/index.js';
|
|
2
|
+
import type { OperationFormatter } from './types.js';
|
|
3
|
+
export declare function createPipeSeparatedFormatter<TOperation extends OperationBase = OperationBase>(getFields: (operation: TOperation) => readonly string[]): OperationFormatter<TOperation>;
|
package/dist/log/index.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
export { createOperationsLog } from './createOperationsLog.js';
|
|
2
|
-
export
|
|
2
|
+
export { createPipeSeparatedFormatter } from './createPipeSeparatedFormatter.js';
|
|
3
|
+
export type { AnyFinishedOperation, OperationFormatter, OperationsLog, OperationsLogOptions, } from './types.js';
|
package/dist/log/index.mjs
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
// src/log/private/constants.ts
|
|
2
|
+
var OUTCOME_SYMBOLS = {
|
|
3
|
+
pending: "\u23F3",
|
|
4
|
+
success: "\u{1F7E2}",
|
|
5
|
+
failure: "\u{1F534}"
|
|
6
|
+
};
|
|
7
|
+
var OUTCOME_LEVELS = {
|
|
8
|
+
pending: "debug",
|
|
9
|
+
success: "info",
|
|
10
|
+
failure: "error"
|
|
11
|
+
};
|
|
12
|
+
|
|
1
13
|
// src/log/private/isFinished.ts
|
|
2
14
|
function isFinished(operation) {
|
|
3
15
|
return operation.outcome !== "pending";
|
|
@@ -5,11 +17,15 @@ function isFinished(operation) {
|
|
|
5
17
|
|
|
6
18
|
// src/log/createOperationsLog.ts
|
|
7
19
|
function createOperationsLog(logger, options) {
|
|
8
|
-
void options;
|
|
9
20
|
const storedOperations = [];
|
|
10
21
|
return {
|
|
11
22
|
log(operation) {
|
|
12
|
-
|
|
23
|
+
const message = {
|
|
24
|
+
level: OUTCOME_LEVELS[operation.outcome],
|
|
25
|
+
text: options.formatter(operation),
|
|
26
|
+
ts: operation.ts
|
|
27
|
+
};
|
|
28
|
+
logger.log(message);
|
|
13
29
|
if (isFinished(operation)) {
|
|
14
30
|
storedOperations.push(operation);
|
|
15
31
|
}
|
|
@@ -28,7 +44,20 @@ function createOperationsLog(logger, options) {
|
|
|
28
44
|
}
|
|
29
45
|
};
|
|
30
46
|
}
|
|
47
|
+
|
|
48
|
+
// src/log/createPipeSeparatedFormatter.ts
|
|
49
|
+
function createPipeSeparatedFormatter(getFields) {
|
|
50
|
+
return (operation) => {
|
|
51
|
+
const fields = [OUTCOME_SYMBOLS[operation.outcome], ...getFields(operation)];
|
|
52
|
+
const line = `${fields.join(" | ")} |`;
|
|
53
|
+
if (operation.outcome === "pending") {
|
|
54
|
+
return line;
|
|
55
|
+
}
|
|
56
|
+
return `${line} (${operation.timing()}ms)`;
|
|
57
|
+
};
|
|
58
|
+
}
|
|
31
59
|
export {
|
|
32
|
-
createOperationsLog
|
|
60
|
+
createOperationsLog,
|
|
61
|
+
createPipeSeparatedFormatter
|
|
33
62
|
};
|
|
34
63
|
//# sourceMappingURL=index.mjs.map
|
package/dist/log/index.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../src/log/private/isFinished.ts", "../../src/log/createOperationsLog.ts"],
|
|
4
|
-
"sourcesContent": ["import type {
|
|
5
|
-
"mappings": ";AAEO,SAAS,WAAW,
|
|
3
|
+
"sources": ["../../src/log/private/constants.ts", "../../src/log/private/isFinished.ts", "../../src/log/createOperationsLog.ts", "../../src/log/createPipeSeparatedFormatter.ts"],
|
|
4
|
+
"sourcesContent": ["import type { MessageLevel } from '@art-lib/logger';\n\nimport type { OperationOutcome } from '../../model/types.js';\n\nexport const OUTCOME_SYMBOLS: Record<OperationOutcome, string> = {\n\tpending: '\u23F3',\n\tsuccess: '\uD83D\uDFE2',\n\tfailure: '\uD83D\uDD34',\n};\n\nexport const OUTCOME_LEVELS: Record<OperationOutcome, MessageLevel> = {\n\tpending: 'debug',\n\tsuccess: 'info',\n\tfailure: 'error',\n};\n", "import type { OperationBase } from '../../model/types.js';\nimport type { AnyFinishedOperation } from '../types.js';\n\nexport function isFinished(operation: OperationBase): operation is AnyFinishedOperation {\n\treturn operation.outcome !== 'pending';\n}\n", "import type { LoggerAPI } from '@art-lib/logger';\n\nimport type { OperationBase } from '../model/index.js';\n\nimport { OUTCOME_LEVELS } from './private/constants.js';\nimport { isFinished } from './private/isFinished.js';\nimport type { AnyFinishedOperation, OperationsLog, OperationsLogOptions } from './types.js';\n\nexport function createOperationsLog<TOperation extends OperationBase = OperationBase>(\n\tlogger: LoggerAPI,\n\toptions: OperationsLogOptions<TOperation>,\n): OperationsLog<TOperation> {\n\tconst storedOperations: AnyFinishedOperation[] = [];\n\n\treturn {\n\t\tlog(operation): void {\n\t\t\tconst message = {\n\t\t\t\tlevel: OUTCOME_LEVELS[operation.outcome],\n\t\t\t\ttext: options.formatter(operation),\n\t\t\t\tts: operation.ts,\n\t\t\t};\n\n\t\t\tlogger.log(message);\n\n\t\t\tif (isFinished(operation)) {\n\t\t\t\tstoredOperations.push(operation);\n\t\t\t}\n\t\t},\n\t\tall(): AnyFinishedOperation[] {\n\t\t\treturn [...storedOperations];\n\t\t},\n\t\tsince(timestamp: number): AnyFinishedOperation[] {\n\t\t\treturn storedOperations.filter(operation => operation.ts >= timestamp);\n\t\t},\n\t\tlatest(count: number): AnyFinishedOperation[] {\n\t\t\tif (count <= 0) {\n\t\t\t\treturn [];\n\t\t\t}\n\n\t\t\treturn storedOperations.slice(Math.max(storedOperations.length - count, 0));\n\t\t},\n\t};\n}\n", "import type { OperationBase } from '../model/index.js';\n\nimport { OUTCOME_SYMBOLS } from './private/constants.js';\nimport type { OperationFormatter } from './types.js';\n\nexport function createPipeSeparatedFormatter<TOperation extends OperationBase = OperationBase>(\n\tgetFields: (operation: TOperation) => readonly string[],\n): OperationFormatter<TOperation> {\n\treturn operation => {\n\t\tconst fields = [OUTCOME_SYMBOLS[operation.outcome], ...getFields(operation)];\n\t\tconst line = `${fields.join(' | ')} |`;\n\n\t\tif (operation.outcome === 'pending') {\n\t\t\treturn line;\n\t\t}\n\n\t\treturn `${line} (${operation.timing()}ms)`;\n\t};\n}\n"],
|
|
5
|
+
"mappings": ";AAIO,IAAM,kBAAoD;AAAA,EAChE,SAAS;AAAA,EACT,SAAS;AAAA,EACT,SAAS;AACV;AAEO,IAAM,iBAAyD;AAAA,EACrE,SAAS;AAAA,EACT,SAAS;AAAA,EACT,SAAS;AACV;;;ACXO,SAAS,WAAW,WAA6D;AACvF,SAAO,UAAU,YAAY;AAC9B;;;ACGO,SAAS,oBACf,QACA,SAC4B;AAC5B,QAAM,mBAA2C,CAAC;AAElD,SAAO;AAAA,IACN,IAAI,WAAiB;AACpB,YAAM,UAAU;AAAA,QACf,OAAO,eAAe,UAAU,OAAO;AAAA,QACvC,MAAM,QAAQ,UAAU,SAAS;AAAA,QACjC,IAAI,UAAU;AAAA,MACf;AAEA,aAAO,IAAI,OAAO;AAElB,UAAI,WAAW,SAAS,GAAG;AAC1B,yBAAiB,KAAK,SAAS;AAAA,MAChC;AAAA,IACD;AAAA,IACA,MAA8B;AAC7B,aAAO,CAAC,GAAG,gBAAgB;AAAA,IAC5B;AAAA,IACA,MAAM,WAA2C;AAChD,aAAO,iBAAiB,OAAO,eAAa,UAAU,MAAM,SAAS;AAAA,IACtE;AAAA,IACA,OAAO,OAAuC;AAC7C,UAAI,SAAS,GAAG;AACf,eAAO,CAAC;AAAA,MACT;AAEA,aAAO,iBAAiB,MAAM,KAAK,IAAI,iBAAiB,SAAS,OAAO,CAAC,CAAC;AAAA,IAC3E;AAAA,EACD;AACD;;;ACrCO,SAAS,6BACf,WACiC;AACjC,SAAO,eAAa;AACnB,UAAM,SAAS,CAAC,gBAAgB,UAAU,OAAO,GAAG,GAAG,UAAU,SAAS,CAAC;AAC3E,UAAM,OAAO,GAAG,OAAO,KAAK,KAAK,CAAC;AAElC,QAAI,UAAU,YAAY,WAAW;AACpC,aAAO;AAAA,IACR;AAEA,WAAO,GAAG,IAAI,KAAK,UAAU,OAAO,CAAC;AAAA,EACtC;AACD;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
1
|
+
import type { OperationBase } from '../../model/types.js';
|
|
2
|
+
import type { AnyFinishedOperation } from '../types.js';
|
|
3
|
+
export declare function isFinished(operation: OperationBase): operation is AnyFinishedOperation;
|
package/dist/log/types.d.ts
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { OperationFailure, OperationSuccess } from '../model/index.js';
|
|
1
|
+
import type { OperationBase, OperationFailure, OperationSuccess } from '../model/index.js';
|
|
3
2
|
export type AnyFinishedOperation = OperationSuccess | OperationFailure;
|
|
4
|
-
export type
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
export type OperationsLog = {
|
|
8
|
-
log(operation: LoggableOperation): void;
|
|
3
|
+
export type OperationFormatter<TOperation extends OperationBase = OperationBase> = (operation: TOperation) => string;
|
|
4
|
+
export type OperationsLog<TOperation extends OperationBase = OperationBase> = {
|
|
5
|
+
log(operation: TOperation): void;
|
|
9
6
|
all(): AnyFinishedOperation[];
|
|
10
7
|
since(timestamp: number): AnyFinishedOperation[];
|
|
11
8
|
latest(count: number): AnyFinishedOperation[];
|
|
12
9
|
};
|
|
13
|
-
export type
|
|
14
|
-
|
|
10
|
+
export type OperationsLogOptions<TOperation extends OperationBase = OperationBase> = {
|
|
11
|
+
formatter: OperationFormatter<TOperation>;
|
|
15
12
|
};
|
|
16
|
-
export type OperationsLogOptions = unknown;
|
|
@@ -1,4 +1,2 @@
|
|
|
1
1
|
import type { OperationFailure, OperationPending } from './types.js';
|
|
2
|
-
export declare function createOperationFailure<D>(pending: OperationPending<D>, error: unknown
|
|
3
|
-
label?: string;
|
|
4
|
-
}): OperationFailure<D>;
|
|
2
|
+
export declare function createOperationFailure<D>(pending: OperationPending<D>, error: unknown): OperationFailure<D>;
|
package/dist/model/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type { OperationBase,
|
|
1
|
+
export type { OperationBase, OperationOutcome, OperationFailure, OperationPending, OperationSuccess, } from './types.js';
|
|
2
2
|
export { createGenericOperation } from './createGenericOperation.js';
|
|
3
3
|
export { createOperationFailure } from './createOperationFailure.js';
|
|
4
4
|
export { createOperationSuccess } from './createOperationSuccess.js';
|
package/dist/model/index.mjs
CHANGED
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
// src/model/private/constants.ts
|
|
2
|
-
var LEVELS = {
|
|
3
|
-
pending: "debug",
|
|
4
|
-
success: "info",
|
|
5
|
-
failure: "error"
|
|
6
|
-
};
|
|
7
|
-
|
|
8
1
|
// src/model/private/formatData.ts
|
|
9
2
|
function formatData(data) {
|
|
10
3
|
return data ? JSON.stringify(data) : "";
|
|
@@ -21,7 +14,6 @@ function createGenericOperation(name, data, toText) {
|
|
|
21
14
|
operation: name,
|
|
22
15
|
ts,
|
|
23
16
|
outcome: "pending",
|
|
24
|
-
level: LEVELS.pending,
|
|
25
17
|
data,
|
|
26
18
|
text: readText,
|
|
27
19
|
timing() {
|
|
@@ -53,29 +45,23 @@ function readRawErrorMessage(error) {
|
|
|
53
45
|
}
|
|
54
46
|
|
|
55
47
|
// src/model/createOperationFailure.ts
|
|
56
|
-
function createOperationFailure(pending, error
|
|
48
|
+
function createOperationFailure(pending, error) {
|
|
57
49
|
const finishedTs = Date.now();
|
|
58
50
|
const rawError = readRawErrorMessage(error);
|
|
59
|
-
const label = options?.label;
|
|
60
51
|
function report() {
|
|
61
52
|
return rawError;
|
|
62
53
|
}
|
|
63
|
-
function errorSerialized() {
|
|
64
|
-
return label ? `${label}: ${rawError}` : rawError;
|
|
65
|
-
}
|
|
66
54
|
const failure = {
|
|
67
55
|
operation: pending.operation,
|
|
68
56
|
ts: pending.ts,
|
|
69
57
|
finishedTs,
|
|
70
58
|
outcome: "failure",
|
|
71
|
-
level: LEVELS.failure,
|
|
72
59
|
data: pending.data,
|
|
73
60
|
error: rawError,
|
|
74
61
|
text: report,
|
|
75
62
|
timing() {
|
|
76
63
|
return finishedTs - pending.ts;
|
|
77
|
-
}
|
|
78
|
-
errorSerialized
|
|
64
|
+
}
|
|
79
65
|
};
|
|
80
66
|
return Object.freeze(failure);
|
|
81
67
|
}
|
|
@@ -92,7 +78,6 @@ function createOperationSuccess(pending, text) {
|
|
|
92
78
|
ts: pending.ts,
|
|
93
79
|
finishedTs,
|
|
94
80
|
outcome: "success",
|
|
95
|
-
level: LEVELS.success,
|
|
96
81
|
data: pending.data,
|
|
97
82
|
text: report,
|
|
98
83
|
timing() {
|
package/dist/model/index.mjs.map
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../../src/model/private/
|
|
4
|
-
"sourcesContent": ["
|
|
5
|
-
"mappings": ";
|
|
3
|
+
"sources": ["../../src/model/private/formatData.ts", "../../src/model/createGenericOperation.ts", "../../src/model/private/readRawErrorMessage.ts", "../../src/model/createOperationFailure.ts", "../../src/model/createOperationSuccess.ts"],
|
|
4
|
+
"sourcesContent": ["export function formatData(data: unknown): string {\n\treturn data ? JSON.stringify(data) : '';\n}\n", "import { formatData } from './private/formatData.js';\nimport type { OperationPending } from './types.js';\n\nexport function createGenericOperation<N extends string, D = unknown>(\n\tname: N,\n\tdata?: D,\n\ttoText?: (data: D) => string,\n): OperationPending<D> & { operation: N } {\n\tconst ts = Date.now();\n\tconst resolveToText: (value: D) => string = toText ?? formatData;\n\n\tfunction readText(): string {\n\t\treturn resolveToText(data as D);\n\t}\n\n\tconst pending: OperationPending<D> & { operation: N } = {\n\t\toperation: name,\n\t\tts,\n\t\toutcome: 'pending',\n\t\tdata,\n\t\ttext: readText,\n\t\ttiming(): number {\n\t\t\treturn Number.NaN;\n\t\t},\n\t};\n\n\treturn Object.freeze(pending);\n}\n", "export function readRawErrorMessage(error: unknown): string {\n\tif (typeof error === 'string') {\n\t\treturn error;\n\t}\n\n\tif (error === null || error === undefined) {\n\t\treturn '';\n\t}\n\n\tif (typeof error === 'object' && 'message' in error) {\n\t\tconst { message } = error as { message: unknown };\n\n\t\tif (typeof message === 'string') {\n\t\t\treturn message;\n\t\t}\n\t}\n\n\ttry {\n\t\treturn String(error);\n\t} catch {\n\t\treturn '';\n\t}\n}\n", "import { readRawErrorMessage } from './private/readRawErrorMessage.js';\nimport type { OperationFailure, OperationPending } from './types.js';\n\nexport function createOperationFailure<D>(\n\tpending: OperationPending<D>,\n\terror: unknown,\n): OperationFailure<D> {\n\tconst finishedTs = Date.now();\n\tconst rawError = readRawErrorMessage(error);\n\n\tfunction report(): string {\n\t\treturn rawError;\n\t}\n\n\tconst failure: OperationFailure<D> = {\n\t\toperation: pending.operation,\n\t\tts: pending.ts,\n\t\tfinishedTs,\n\t\toutcome: 'failure',\n\t\tdata: pending.data,\n\t\terror: rawError,\n\t\ttext: report,\n\t\ttiming(): number {\n\t\t\treturn finishedTs - pending.ts;\n\t\t},\n\t};\n\n\treturn Object.freeze(failure);\n}\n", "import type { OperationPending, OperationSuccess } from './types.js';\n\nexport function createOperationSuccess<D>(\n\tpending: OperationPending<D>,\n\ttext?: string,\n): OperationSuccess<D> {\n\tconst finishedTs = Date.now();\n\tconst givenText = text ?? pending.text();\n\n\tfunction report(): string {\n\t\treturn givenText;\n\t}\n\n\tconst success: OperationSuccess<D> = {\n\t\toperation: pending.operation,\n\t\tts: pending.ts,\n\t\tfinishedTs,\n\t\toutcome: 'success',\n\t\tdata: pending.data,\n\t\ttext: report,\n\t\ttiming(): number {\n\t\t\treturn finishedTs - pending.ts;\n\t\t},\n\t};\n\n\treturn Object.freeze(success);\n}\n"],
|
|
5
|
+
"mappings": ";AAAO,SAAS,WAAW,MAAuB;AACjD,SAAO,OAAO,KAAK,UAAU,IAAI,IAAI;AACtC;;;ACCO,SAAS,uBACf,MACA,MACA,QACyC;AACzC,QAAM,KAAK,KAAK,IAAI;AACpB,QAAM,gBAAsC,UAAU;AAEtD,WAAS,WAAmB;AAC3B,WAAO,cAAc,IAAS;AAAA,EAC/B;AAEA,QAAM,UAAkD;AAAA,IACvD,WAAW;AAAA,IACX;AAAA,IACA,SAAS;AAAA,IACT;AAAA,IACA,MAAM;AAAA,IACN,SAAiB;AAChB,aAAO,OAAO;AAAA,IACf;AAAA,EACD;AAEA,SAAO,OAAO,OAAO,OAAO;AAC7B;;;AC3BO,SAAS,oBAAoB,OAAwB;AAC3D,MAAI,OAAO,UAAU,UAAU;AAC9B,WAAO;AAAA,EACR;AAEA,MAAI,UAAU,QAAQ,UAAU,QAAW;AAC1C,WAAO;AAAA,EACR;AAEA,MAAI,OAAO,UAAU,YAAY,aAAa,OAAO;AACpD,UAAM,EAAE,QAAQ,IAAI;AAEpB,QAAI,OAAO,YAAY,UAAU;AAChC,aAAO;AAAA,IACR;AAAA,EACD;AAEA,MAAI;AACH,WAAO,OAAO,KAAK;AAAA,EACpB,QAAQ;AACP,WAAO;AAAA,EACR;AACD;;;ACnBO,SAAS,uBACf,SACA,OACsB;AACtB,QAAM,aAAa,KAAK,IAAI;AAC5B,QAAM,WAAW,oBAAoB,KAAK;AAE1C,WAAS,SAAiB;AACzB,WAAO;AAAA,EACR;AAEA,QAAM,UAA+B;AAAA,IACpC,WAAW,QAAQ;AAAA,IACnB,IAAI,QAAQ;AAAA,IACZ;AAAA,IACA,SAAS;AAAA,IACT,MAAM,QAAQ;AAAA,IACd,OAAO;AAAA,IACP,MAAM;AAAA,IACN,SAAiB;AAChB,aAAO,aAAa,QAAQ;AAAA,IAC7B;AAAA,EACD;AAEA,SAAO,OAAO,OAAO,OAAO;AAC7B;;;AC1BO,SAAS,uBACf,SACA,MACsB;AACtB,QAAM,aAAa,KAAK,IAAI;AAC5B,QAAM,YAAY,QAAQ,QAAQ,KAAK;AAEvC,WAAS,SAAiB;AACzB,WAAO;AAAA,EACR;AAEA,QAAM,UAA+B;AAAA,IACpC,WAAW,QAAQ;AAAA,IACnB,IAAI,QAAQ;AAAA,IACZ;AAAA,IACA,SAAS;AAAA,IACT,MAAM,QAAQ;AAAA,IACd,MAAM;AAAA,IACN,SAAiB;AAChB,aAAO,aAAa,QAAQ;AAAA,IAC7B;AAAA,EACD;AAEA,SAAO,OAAO,OAAO,OAAO;AAC7B;",
|
|
6
6
|
"names": []
|
|
7
7
|
}
|
package/dist/model/types.d.ts
CHANGED
|
@@ -1,25 +1,20 @@
|
|
|
1
|
-
import type { MessageLevel } from '@art-lib/logger';
|
|
2
1
|
export type OperationOutcome = 'pending' | 'success' | 'failure';
|
|
3
|
-
export type OperationBase = {
|
|
2
|
+
export type OperationBase<D = unknown> = {
|
|
4
3
|
readonly operation: string;
|
|
5
4
|
readonly ts: number;
|
|
6
5
|
readonly finishedTs?: number;
|
|
7
6
|
readonly outcome: OperationOutcome;
|
|
7
|
+
readonly data?: D;
|
|
8
8
|
text(): string;
|
|
9
9
|
timing(): number;
|
|
10
|
-
readonly level: MessageLevel;
|
|
11
10
|
};
|
|
12
|
-
export type OperationPending<D = unknown> = OperationBase & {
|
|
11
|
+
export type OperationPending<D = unknown> = OperationBase<D> & {
|
|
13
12
|
readonly outcome: 'pending';
|
|
14
|
-
readonly data?: D;
|
|
15
13
|
};
|
|
16
|
-
export type OperationSuccess<D = unknown> = OperationBase & {
|
|
14
|
+
export type OperationSuccess<D = unknown> = OperationBase<D> & {
|
|
17
15
|
readonly outcome: 'success';
|
|
18
|
-
readonly data?: D;
|
|
19
16
|
};
|
|
20
|
-
export type OperationFailure<D = unknown> = OperationBase & {
|
|
17
|
+
export type OperationFailure<D = unknown> = OperationBase<D> & {
|
|
21
18
|
readonly outcome: 'failure';
|
|
22
|
-
readonly data?: D;
|
|
23
19
|
readonly error: string;
|
|
24
|
-
errorSerialized(): string;
|
|
25
20
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@art-lib/operations",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.0.3",
|
|
4
|
+
"description": "Shared operation model and formatted operations log for CLI workflows.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"library",
|
|
7
7
|
"operations",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
},
|
|
24
24
|
"files": [
|
|
25
25
|
"dist",
|
|
26
|
+
"src/test/helpers",
|
|
26
27
|
"README.md",
|
|
27
28
|
"LICENSE-MIT",
|
|
28
29
|
"CHANGELOG.md"
|
|
@@ -37,14 +38,7 @@
|
|
|
37
38
|
"types": "./dist/index.d.ts",
|
|
38
39
|
"import": "./dist/index.mjs"
|
|
39
40
|
},
|
|
40
|
-
"./
|
|
41
|
-
"types": "./dist/model/index.d.ts",
|
|
42
|
-
"import": "./dist/model/index.mjs"
|
|
43
|
-
},
|
|
44
|
-
"./log": {
|
|
45
|
-
"types": "./dist/log/index.d.ts",
|
|
46
|
-
"import": "./dist/log/index.mjs"
|
|
47
|
-
}
|
|
41
|
+
"./test/helpers/*": "./src/test/helpers/*.ts"
|
|
48
42
|
},
|
|
49
43
|
"scripts": {
|
|
50
44
|
"dev": "esbuild-cli-watch",
|
|
@@ -58,7 +52,9 @@
|
|
|
58
52
|
"ci": "npm run lint && npm run build && npm run test:ci"
|
|
59
53
|
},
|
|
60
54
|
"dependencies": {
|
|
61
|
-
"@art-lib/logger": "0.0.
|
|
55
|
+
"@art-lib/logger": "0.0.4"
|
|
62
56
|
},
|
|
63
|
-
"devDependencies": {
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"vitest": "5.0.5"
|
|
59
|
+
}
|
|
64
60
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import type { OperationsLog } from '../../../log/types.js';
|
|
4
|
+
|
|
5
|
+
/** @mocks the operations log API */
|
|
6
|
+
export function createOperationsLogMock(): OperationsLog {
|
|
7
|
+
return {
|
|
8
|
+
log: vi.fn(),
|
|
9
|
+
all: vi.fn(),
|
|
10
|
+
since: vi.fn(),
|
|
11
|
+
latest: vi.fn(),
|
|
12
|
+
};
|
|
13
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { OperationFailure } from '../../../model/types.js';
|
|
2
|
+
|
|
3
|
+
import type { OperationFixtureData } from './types.js';
|
|
4
|
+
|
|
5
|
+
export function makeFailureOperationFixture(
|
|
6
|
+
ts = 1_000,
|
|
7
|
+
finishedTs = 1_075,
|
|
8
|
+
): OperationFailure<OperationFixtureData> {
|
|
9
|
+
return {
|
|
10
|
+
operation: 'pull',
|
|
11
|
+
ts,
|
|
12
|
+
finishedTs,
|
|
13
|
+
outcome: 'failure',
|
|
14
|
+
data: { branch: 'main' },
|
|
15
|
+
error: 'remote unavailable',
|
|
16
|
+
text: () => 'remote unavailable',
|
|
17
|
+
timing: () => finishedTs - ts,
|
|
18
|
+
};
|
|
19
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { OperationPending } from '../../../model/types.js';
|
|
2
|
+
|
|
3
|
+
import type { OperationFixtureData } from './types.js';
|
|
4
|
+
|
|
5
|
+
export function makePendingOperationFixture(ts = 1_000): OperationPending<OperationFixtureData> {
|
|
6
|
+
return {
|
|
7
|
+
operation: 'pull',
|
|
8
|
+
ts,
|
|
9
|
+
outcome: 'pending',
|
|
10
|
+
data: { branch: 'main' },
|
|
11
|
+
text: () => 'from origin/main',
|
|
12
|
+
timing: () => Number.NaN,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { OperationSuccess } from '../../../model/types.js';
|
|
2
|
+
|
|
3
|
+
import type { OperationFixtureData } from './types.js';
|
|
4
|
+
|
|
5
|
+
export function makeSuccessOperationFixture(
|
|
6
|
+
ts = 1_000,
|
|
7
|
+
finishedTs = 1_050,
|
|
8
|
+
): OperationSuccess<OperationFixtureData> {
|
|
9
|
+
return {
|
|
10
|
+
operation: 'pull',
|
|
11
|
+
ts,
|
|
12
|
+
finishedTs,
|
|
13
|
+
outcome: 'success',
|
|
14
|
+
data: { branch: 'main' },
|
|
15
|
+
text: () => 'from origin/main',
|
|
16
|
+
timing: () => finishedTs - ts,
|
|
17
|
+
};
|
|
18
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { makeFailureOperationFixture } from './makeFailureOperationFixture.js';
|
|
4
|
+
import { makePendingOperationFixture } from './makePendingOperationFixture.js';
|
|
5
|
+
import { makeSuccessOperationFixture } from './makeSuccessOperationFixture.js';
|
|
6
|
+
|
|
7
|
+
describe('operation fixtures', () => {
|
|
8
|
+
it('WHEN fixture timestamps are supplied, timing reflects the operation outcome', () => {
|
|
9
|
+
expect(makePendingOperationFixture(1_000).timing()).toBeNaN();
|
|
10
|
+
expect(makeSuccessOperationFixture(1_000, 1_050).timing()).toBe(50);
|
|
11
|
+
expect(makeFailureOperationFixture(1_000, 1_075).timing()).toBe(75);
|
|
12
|
+
});
|
|
13
|
+
});
|