@naturalcycles/nodejs-lib 13.16.0 → 13.17.0

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.
@@ -30,7 +30,7 @@ export declare function appendToGithubOutput(obj: AnyObject, prefix?: string): v
30
30
  /**
31
31
  * https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-job-summary
32
32
  */
33
- export declare function appendToGithubSummary(str: string): void;
33
+ export declare function appendToGithubSummary(...lines: string[]): void;
34
34
  /**
35
35
  * Turns Object with keys/values into a *.sh script that exports all keys as values.
36
36
  *
@@ -81,10 +81,11 @@ exports.appendToGithubOutput = appendToGithubOutput;
81
81
  /**
82
82
  * https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-job-summary
83
83
  */
84
- function appendToGithubSummary(str) {
84
+ function appendToGithubSummary(...lines) {
85
85
  const { GITHUB_STEP_SUMMARY } = process.env;
86
86
  if (GITHUB_STEP_SUMMARY) {
87
- node_fs_1.default.appendFileSync(GITHUB_STEP_SUMMARY, str + '\n');
87
+ const str = lines.join('\n') + '\n';
88
+ node_fs_1.default.appendFileSync(GITHUB_STEP_SUMMARY, str);
88
89
  console.log(`GITHUB_STEP_SUMMARY appended:\n${str}`);
89
90
  }
90
91
  }
@@ -1,4 +1,4 @@
1
- import { AbortableAsyncMapper, AsyncPredicate, CommonLogger, END, ErrorMode, SKIP } from '@naturalcycles/js-lib';
1
+ import { AbortableAsyncMapper, AsyncPredicate, CommonLogger, END, ErrorMode, SKIP, StringMap, UnixTimestampMillisNumber } from '@naturalcycles/js-lib';
2
2
  import { TransformTyped } from '../stream.model';
3
3
  export interface TransformMapOptions<IN = any, OUT = IN> {
4
4
  /**
@@ -61,6 +61,7 @@ export interface TransformMapStats {
61
61
  countErrors: number;
62
62
  countIn: number;
63
63
  countOut: number;
64
+ started: UnixTimestampMillisNumber;
64
65
  }
65
66
  /**
66
67
  * Like pMap, but for streams.
@@ -75,3 +76,7 @@ export interface TransformMapStats {
75
76
  * If an Array is returned by `mapper` - it will be flattened and multiple results will be emitted from it. Tested by Array.isArray().
76
77
  */
77
78
  export declare function transformMap<IN = any, OUT = IN>(mapper: AbortableAsyncMapper<IN, OUT | typeof SKIP | typeof END>, opt?: TransformMapOptions<IN, OUT>): TransformTyped<IN, OUT>;
79
+ export declare function appendTransformMapStatsToGithubSummary(stats: TransformMapStats & {
80
+ name?: string;
81
+ extra?: StringMap<any>;
82
+ }): void;
@@ -1,9 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.transformMap = void 0;
3
+ exports.appendTransformMapStatsToGithubSummary = exports.transformMap = void 0;
4
4
  const js_lib_1 = require("@naturalcycles/js-lib");
5
5
  const through2Concurrent = require("through2-concurrent");
6
6
  const colors_1 = require("../../colors/colors");
7
+ const json2env_1 = require("../../fs/json2env");
7
8
  const stream_util_1 = require("../stream.util");
8
9
  // doesn't work, cause here we don't construct our Transform instance ourselves
9
10
  // export class TransformMap extends AbortableTransform {}
@@ -22,6 +23,7 @@ const stream_util_1 = require("../stream.util");
22
23
  function transformMap(mapper, opt = {}) {
23
24
  const { concurrency = 16, predicate, // we now default to "no predicate" (meaning pass-everything)
24
25
  errorMode = js_lib_1.ErrorMode.THROW_IMMEDIATELY, flattenArrayOutput, onError, onDone, metric = 'stream', logger = console, } = opt;
26
+ const started = Date.now();
25
27
  let index = -1;
26
28
  let countOut = 0;
27
29
  let isSettled = false;
@@ -39,6 +41,7 @@ function transformMap(mapper, opt = {}) {
39
41
  countErrors: errors,
40
42
  countIn: index + 1,
41
43
  countOut,
44
+ started,
42
45
  });
43
46
  // emit Aggregated error
44
47
  cb(new AggregateError(collectedErrors, `transformMap resulted in ${collectedErrors.length} error(s)`));
@@ -51,6 +54,7 @@ function transformMap(mapper, opt = {}) {
51
54
  countErrors: errors,
52
55
  countIn: index + 1,
53
56
  countOut,
57
+ started,
54
58
  });
55
59
  cb();
56
60
  }
@@ -95,6 +99,7 @@ function transformMap(mapper, opt = {}) {
95
99
  countErrors: errors,
96
100
  countIn: index + 1,
97
101
  countOut,
102
+ started,
98
103
  });
99
104
  return cb(err); // Emit error immediately
100
105
  }
@@ -112,3 +117,14 @@ function transformMap(mapper, opt = {}) {
112
117
  }
113
118
  }
114
119
  exports.transformMap = transformMap;
120
+ function appendTransformMapStatsToGithubSummary(stats) {
121
+ const { countIn, countOut, countErrors, started, name = 'Transform', extra = {} } = stats;
122
+ (0, json2env_1.appendToGithubSummary)(...[
123
+ `### ${name} summary\n`,
124
+ `${(0, js_lib_1._since)(started)} spent`,
125
+ `${(0, js_lib_1._hc)(countIn)} / ${(0, js_lib_1._hc)(countOut)} rows in / out`,
126
+ countErrors ? `${countErrors} errors` : '',
127
+ ...Object.entries(extra).map(([k, v]) => `${k}: ${v}`),
128
+ ].filter(Boolean));
129
+ }
130
+ exports.appendTransformMapStatsToGithubSummary = appendTransformMapStatsToGithubSummary;
@@ -15,6 +15,7 @@ exports.TransformMapSync = TransformMapSync;
15
15
  function transformMapSync(mapper, opt = {}) {
16
16
  const { predicate, // defaults to "no predicate" (pass everything)
17
17
  errorMode = js_lib_1.ErrorMode.THROW_IMMEDIATELY, flattenArrayOutput = false, onError, onDone, metric = 'stream', objectMode = true, logger = console, } = opt;
18
+ const started = Date.now();
18
19
  let index = -1;
19
20
  let countOut = 0;
20
21
  let isSettled = false;
@@ -64,6 +65,7 @@ function transformMapSync(mapper, opt = {}) {
64
65
  countErrors: errors,
65
66
  countIn: index + 1,
66
67
  countOut,
68
+ started,
67
69
  });
68
70
  // Emit error immediately
69
71
  return cb(err);
@@ -84,6 +86,7 @@ function transformMapSync(mapper, opt = {}) {
84
86
  countErrors: errors,
85
87
  countIn: index + 1,
86
88
  countOut,
89
+ started,
87
90
  });
88
91
  // emit Aggregated error
89
92
  cb(new AggregateError(collectedErrors, `transformMapSync resulted in ${collectedErrors.length} error(s)`));
@@ -96,6 +99,7 @@ function transformMapSync(mapper, opt = {}) {
96
99
  countErrors: errors,
97
100
  countIn: index + 1,
98
101
  countOut,
102
+ started,
99
103
  });
100
104
  cb();
101
105
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
- "version": "13.16.0",
3
+ "version": "13.17.0",
4
4
  "scripts": {
5
5
  "prepare": "husky",
6
6
  "docs-serve": "vuepress dev docs",
@@ -120,10 +120,11 @@ export function appendToGithubOutput(obj: AnyObject, prefix = ''): void {
120
120
  /**
121
121
  * https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-job-summary
122
122
  */
123
- export function appendToGithubSummary(str: string): void {
123
+ export function appendToGithubSummary(...lines: string[]): void {
124
124
  const { GITHUB_STEP_SUMMARY } = process.env
125
125
  if (GITHUB_STEP_SUMMARY) {
126
- fs.appendFileSync(GITHUB_STEP_SUMMARY, str + '\n')
126
+ const str = lines.join('\n') + '\n'
127
+ fs.appendFileSync(GITHUB_STEP_SUMMARY, str)
127
128
  console.log(`GITHUB_STEP_SUMMARY appended:\n${str}`)
128
129
  }
129
130
  }
@@ -1,5 +1,7 @@
1
1
  import {
2
2
  _anyToError,
3
+ _hc,
4
+ _since,
3
5
  AbortableAsyncMapper,
4
6
  AsyncPredicate,
5
7
  CommonLogger,
@@ -7,9 +9,12 @@ import {
7
9
  ErrorMode,
8
10
  pFilter,
9
11
  SKIP,
12
+ StringMap,
13
+ UnixTimestampMillisNumber,
10
14
  } from '@naturalcycles/js-lib'
11
15
  import through2Concurrent = require('through2-concurrent')
12
16
  import { yellow } from '../../colors/colors'
17
+ import { appendToGithubSummary } from '../../fs/json2env'
13
18
  import { AbortableTransform } from '../pipeline/pipeline'
14
19
  import { TransformTyped } from '../stream.model'
15
20
  import { pipelineClose } from '../stream.util'
@@ -83,6 +88,7 @@ export interface TransformMapStats {
83
88
  countErrors: number
84
89
  countIn: number
85
90
  countOut: number
91
+ started: UnixTimestampMillisNumber
86
92
  }
87
93
 
88
94
  // doesn't work, cause here we don't construct our Transform instance ourselves
@@ -115,6 +121,7 @@ export function transformMap<IN = any, OUT = IN>(
115
121
  logger = console,
116
122
  } = opt
117
123
 
124
+ const started = Date.now()
118
125
  let index = -1
119
126
  let countOut = 0
120
127
  let isSettled = false
@@ -136,6 +143,7 @@ export function transformMap<IN = any, OUT = IN>(
136
143
  countErrors: errors,
137
144
  countIn: index + 1,
138
145
  countOut,
146
+ started,
139
147
  })
140
148
 
141
149
  // emit Aggregated error
@@ -154,6 +162,7 @@ export function transformMap<IN = any, OUT = IN>(
154
162
  countErrors: errors,
155
163
  countIn: index + 1,
156
164
  countOut,
165
+ started,
157
166
  })
158
167
 
159
168
  cb()
@@ -207,6 +216,7 @@ export function transformMap<IN = any, OUT = IN>(
207
216
  countErrors: errors,
208
217
  countIn: index + 1,
209
218
  countOut,
219
+ started,
210
220
  })
211
221
  return cb(err) // Emit error immediately
212
222
  }
@@ -226,3 +236,19 @@ export function transformMap<IN = any, OUT = IN>(
226
236
  logger.log(`${metric} ${final ? 'final ' : ''}errors: ${yellow(errors)}`)
227
237
  }
228
238
  }
239
+
240
+ export function appendTransformMapStatsToGithubSummary(
241
+ stats: TransformMapStats & { name?: string; extra?: StringMap<any> },
242
+ ): void {
243
+ const { countIn, countOut, countErrors, started, name = 'Transform', extra = {} } = stats
244
+
245
+ appendToGithubSummary(
246
+ ...[
247
+ `### ${name} summary\n`,
248
+ `${_since(started)} spent`,
249
+ `${_hc(countIn)} / ${_hc(countOut)} rows in / out`,
250
+ countErrors ? `${countErrors} errors` : '',
251
+ ...Object.entries(extra).map(([k, v]) => `${k}: ${v}`),
252
+ ].filter(Boolean),
253
+ )
254
+ }
@@ -88,6 +88,7 @@ export function transformMapSync<IN = any, OUT = IN>(
88
88
  logger = console,
89
89
  } = opt
90
90
 
91
+ const started = Date.now()
91
92
  let index = -1
92
93
  let countOut = 0
93
94
  let isSettled = false
@@ -144,6 +145,7 @@ export function transformMapSync<IN = any, OUT = IN>(
144
145
  countErrors: errors,
145
146
  countIn: index + 1,
146
147
  countOut,
148
+ started,
147
149
  })
148
150
  // Emit error immediately
149
151
  return cb(err as Error)
@@ -168,6 +170,7 @@ export function transformMapSync<IN = any, OUT = IN>(
168
170
  countErrors: errors,
169
171
  countIn: index + 1,
170
172
  countOut,
173
+ started,
171
174
  })
172
175
 
173
176
  // emit Aggregated error
@@ -186,6 +189,7 @@ export function transformMapSync<IN = any, OUT = IN>(
186
189
  countErrors: errors,
187
190
  countIn: index + 1,
188
191
  countOut,
192
+ started,
189
193
  })
190
194
 
191
195
  cb()