@naturalcycles/nodejs-lib 13.45.4 → 14.0.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.
Files changed (197) hide show
  1. package/dist/bin/generate-build-info.js +15 -18
  2. package/dist/bin/json2env.js +6 -9
  3. package/dist/bin/kpy.js +6 -9
  4. package/dist/bin/secrets-decrypt.d.ts +1 -1
  5. package/dist/bin/secrets-decrypt.js +9 -12
  6. package/dist/bin/secrets-encrypt.d.ts +1 -1
  7. package/dist/bin/secrets-encrypt.js +9 -12
  8. package/dist/bin/secrets-gen-key.js +8 -11
  9. package/dist/bin/slack-this.js +6 -9
  10. package/dist/buffer/buffer.util.js +1 -4
  11. package/dist/colors/colors.d.ts +26 -27
  12. package/dist/colors/colors.js +29 -33
  13. package/dist/csv/csvReader.js +4 -8
  14. package/dist/csv/csvWriter.js +6 -12
  15. package/dist/csv/transformToCSV.d.ts +2 -2
  16. package/dist/csv/transformToCSV.js +5 -8
  17. package/dist/diff/tableDiff.js +4 -7
  18. package/dist/fs/fs2.d.ts +1 -1
  19. package/dist/fs/fs2.js +82 -86
  20. package/dist/fs/json2env.js +18 -28
  21. package/dist/fs/kpy.js +28 -33
  22. package/dist/index.d.ts +71 -71
  23. package/dist/index.js +73 -78
  24. package/dist/infra/process.util.js +22 -28
  25. package/dist/jwt/jwt.service.js +16 -21
  26. package/dist/log/log.util.js +4 -7
  27. package/dist/script/runScript.js +11 -15
  28. package/dist/secret/secrets-decrypt.util.js +18 -22
  29. package/dist/secret/secrets-encrypt.util.js +17 -21
  30. package/dist/security/crypto.util.js +24 -34
  31. package/dist/security/hash.util.js +19 -37
  32. package/dist/security/id.util.js +11 -18
  33. package/dist/security/nanoid.js +15 -20
  34. package/dist/security/secret.util.js +20 -30
  35. package/dist/slack/slack.service.d.ts +1 -1
  36. package/dist/slack/slack.service.js +11 -16
  37. package/dist/slack/slack.service.model.d.ts +1 -1
  38. package/dist/slack/slack.service.model.js +1 -2
  39. package/dist/stream/ndjson/ndjson.model.js +7 -11
  40. package/dist/stream/ndjson/ndjsonMap.d.ts +1 -1
  41. package/dist/stream/ndjson/ndjsonMap.js +11 -14
  42. package/dist/stream/ndjson/ndjsonStreamForEach.d.ts +1 -1
  43. package/dist/stream/ndjson/ndjsonStreamForEach.js +9 -12
  44. package/dist/stream/ndjson/transformJsonParse.d.ts +1 -1
  45. package/dist/stream/ndjson/transformJsonParse.js +4 -9
  46. package/dist/stream/ndjson/transformToNDJson.d.ts +1 -1
  47. package/dist/stream/ndjson/transformToNDJson.js +5 -8
  48. package/dist/stream/pipeline/pipeline.js +11 -17
  49. package/dist/stream/progressLogger.d.ts +1 -1
  50. package/dist/stream/progressLogger.js +22 -28
  51. package/dist/stream/readable/readableCreate.d.ts +1 -1
  52. package/dist/stream/readable/readableCreate.js +5 -9
  53. package/dist/stream/readable/readableForEach.d.ts +2 -2
  54. package/dist/stream/readable/readableForEach.js +7 -11
  55. package/dist/stream/readable/readableFromArray.d.ts +1 -1
  56. package/dist/stream/readable/readableFromArray.js +4 -7
  57. package/dist/stream/readable/readableToArray.d.ts +1 -1
  58. package/dist/stream/readable/readableToArray.js +1 -4
  59. package/dist/stream/sizeStack.js +7 -11
  60. package/dist/stream/stream.model.js +1 -2
  61. package/dist/stream/stream.util.js +1 -4
  62. package/dist/stream/transform/transformChunk.d.ts +1 -1
  63. package/dist/stream/transform/transformChunk.js +3 -6
  64. package/dist/stream/transform/transformFilter.d.ts +2 -2
  65. package/dist/stream/transform/transformFilter.js +6 -10
  66. package/dist/stream/transform/transformLimit.d.ts +2 -2
  67. package/dist/stream/transform/transformLimit.js +6 -11
  68. package/dist/stream/transform/transformLogProgress.d.ts +2 -2
  69. package/dist/stream/transform/transformLogProgress.js +5 -8
  70. package/dist/stream/transform/transformMap.d.ts +1 -1
  71. package/dist/stream/transform/transformMap.js +18 -22
  72. package/dist/stream/transform/transformMapSimple.d.ts +1 -1
  73. package/dist/stream/transform/transformMapSimple.js +6 -9
  74. package/dist/stream/transform/transformMapSync.d.ts +3 -3
  75. package/dist/stream/transform/transformMapSync.js +14 -19
  76. package/dist/stream/transform/transformNoOp.d.ts +1 -1
  77. package/dist/stream/transform/transformNoOp.js +3 -6
  78. package/dist/stream/transform/transformOffset.d.ts +1 -1
  79. package/dist/stream/transform/transformOffset.js +5 -8
  80. package/dist/stream/transform/transformSplit.d.ts +1 -1
  81. package/dist/stream/transform/transformSplit.js +5 -9
  82. package/dist/stream/transform/transformTap.d.ts +1 -1
  83. package/dist/stream/transform/transformTap.js +3 -6
  84. package/dist/stream/transform/transformTee.d.ts +1 -1
  85. package/dist/stream/transform/transformTee.js +7 -10
  86. package/dist/stream/transform/transformThrottle.d.ts +1 -1
  87. package/dist/stream/transform/transformThrottle.js +9 -12
  88. package/dist/stream/transform/transformToArray.d.ts +1 -1
  89. package/dist/stream/transform/transformToArray.js +3 -6
  90. package/dist/stream/transform/worker/baseWorkerClass.d.ts +1 -1
  91. package/dist/stream/transform/worker/baseWorkerClass.js +1 -5
  92. package/dist/stream/transform/worker/transformMultiThreaded.d.ts +1 -1
  93. package/dist/stream/transform/worker/transformMultiThreaded.js +12 -11
  94. package/dist/stream/transform/worker/transformMultiThreaded.model.js +1 -2
  95. package/dist/stream/transform/worker/workerClassProxy.js +6 -4
  96. package/dist/stream/writable/writableForEach.d.ts +2 -2
  97. package/dist/stream/writable/writableForEach.js +6 -10
  98. package/dist/stream/writable/writableFork.d.ts +1 -1
  99. package/dist/stream/writable/writableFork.js +6 -9
  100. package/dist/stream/writable/writableLimit.d.ts +1 -1
  101. package/dist/stream/writable/writableLimit.js +3 -6
  102. package/dist/stream/writable/writablePushToArray.d.ts +1 -1
  103. package/dist/stream/writable/writablePushToArray.js +3 -6
  104. package/dist/stream/writable/writableVoid.d.ts +1 -1
  105. package/dist/stream/writable/writableVoid.js +3 -6
  106. package/dist/string/inspect.js +8 -13
  107. package/dist/util/buildInfo.util.js +12 -15
  108. package/dist/util/env.util.js +5 -9
  109. package/dist/util/exec2.js +17 -21
  110. package/dist/util/git2.js +17 -20
  111. package/dist/util/lruMemoCache.js +3 -7
  112. package/dist/util/zip.util.js +14 -25
  113. package/dist/validation/ajv/ajv.util.d.ts +3 -3
  114. package/dist/validation/ajv/ajv.util.js +6 -10
  115. package/dist/validation/ajv/ajvSchema.d.ts +2 -2
  116. package/dist/validation/ajv/ajvSchema.js +14 -18
  117. package/dist/validation/ajv/ajvValidationError.js +2 -6
  118. package/dist/validation/ajv/getAjv.d.ts +1 -1
  119. package/dist/validation/ajv/getAjv.js +9 -9
  120. package/dist/validation/joi/joi.extensions.d.ts +2 -2
  121. package/dist/validation/joi/joi.extensions.js +6 -10
  122. package/dist/validation/joi/joi.model.js +1 -2
  123. package/dist/validation/joi/joi.shared.schemas.d.ts +2 -2
  124. package/dist/validation/joi/joi.shared.schemas.js +61 -74
  125. package/dist/validation/joi/joi.validation.error.js +2 -6
  126. package/dist/validation/joi/joi.validation.util.d.ts +1 -1
  127. package/dist/validation/joi/joi.validation.util.js +10 -17
  128. package/dist/validation/joi/number.extensions.js +1 -4
  129. package/dist/validation/joi/string.extensions.js +4 -7
  130. package/dist/yargs.util.js +5 -8
  131. package/package.json +5 -4
  132. package/src/bin/generate-build-info.ts +3 -3
  133. package/src/bin/json2env.ts +2 -2
  134. package/src/bin/kpy.ts +2 -2
  135. package/src/bin/secrets-decrypt.ts +7 -8
  136. package/src/bin/secrets-encrypt.ts +7 -8
  137. package/src/bin/secrets-gen-key.ts +2 -2
  138. package/src/bin/slack-this.ts +2 -2
  139. package/src/csv/transformToCSV.ts +3 -3
  140. package/src/fs/fs2.ts +4 -4
  141. package/src/fs/json2env.ts +2 -2
  142. package/src/fs/kpy.ts +2 -2
  143. package/src/index.ts +71 -71
  144. package/src/jwt/jwt.service.ts +2 -2
  145. package/src/log/log.util.ts +1 -1
  146. package/src/script/runScript.ts +4 -3
  147. package/src/secret/secrets-decrypt.util.ts +3 -3
  148. package/src/secret/secrets-encrypt.util.ts +3 -3
  149. package/src/security/crypto.util.ts +1 -1
  150. package/src/security/id.util.ts +1 -1
  151. package/src/security/secret.util.ts +2 -2
  152. package/src/slack/slack.service.model.ts +1 -1
  153. package/src/slack/slack.service.ts +3 -3
  154. package/src/stream/ndjson/ndjson.model.ts +1 -1
  155. package/src/stream/ndjson/ndjsonMap.ts +2 -2
  156. package/src/stream/ndjson/ndjsonStreamForEach.ts +2 -2
  157. package/src/stream/ndjson/transformJsonParse.ts +1 -1
  158. package/src/stream/ndjson/transformToNDJson.ts +1 -1
  159. package/src/stream/pipeline/pipeline.ts +1 -1
  160. package/src/stream/progressLogger.ts +3 -3
  161. package/src/stream/readable/readableCreate.ts +1 -1
  162. package/src/stream/readable/readableForEach.ts +4 -4
  163. package/src/stream/readable/readableFromArray.ts +1 -1
  164. package/src/stream/readable/readableToArray.ts +1 -1
  165. package/src/stream/sizeStack.ts +2 -2
  166. package/src/stream/transform/transformChunk.ts +1 -1
  167. package/src/stream/transform/transformFilter.ts +3 -3
  168. package/src/stream/transform/transformLimit.ts +3 -3
  169. package/src/stream/transform/transformLogProgress.ts +3 -3
  170. package/src/stream/transform/transformMap.ts +5 -5
  171. package/src/stream/transform/transformMapSimple.ts +1 -1
  172. package/src/stream/transform/transformMapSync.ts +5 -5
  173. package/src/stream/transform/transformNoOp.ts +1 -1
  174. package/src/stream/transform/transformOffset.ts +2 -2
  175. package/src/stream/transform/transformSplit.ts +1 -1
  176. package/src/stream/transform/transformTap.ts +1 -1
  177. package/src/stream/transform/transformTee.ts +3 -3
  178. package/src/stream/transform/transformThrottle.ts +1 -1
  179. package/src/stream/transform/transformToArray.ts +1 -1
  180. package/src/stream/transform/worker/baseWorkerClass.ts +1 -1
  181. package/src/stream/transform/worker/transformMultiThreaded.ts +8 -3
  182. package/src/stream/transform/worker/workerClassProxy.js +6 -4
  183. package/src/stream/writable/writableForEach.ts +3 -3
  184. package/src/stream/writable/writableFork.ts +3 -3
  185. package/src/stream/writable/writableLimit.ts +1 -1
  186. package/src/stream/writable/writablePushToArray.ts +1 -1
  187. package/src/stream/writable/writableVoid.ts +1 -1
  188. package/src/util/buildInfo.util.ts +2 -2
  189. package/src/util/exec2.ts +1 -1
  190. package/src/util/git2.ts +2 -2
  191. package/src/validation/ajv/ajv.util.ts +4 -4
  192. package/src/validation/ajv/ajvSchema.ts +4 -5
  193. package/src/validation/ajv/getAjv.ts +7 -3
  194. package/src/validation/joi/joi.extensions.ts +4 -4
  195. package/src/validation/joi/joi.shared.schemas.ts +3 -3
  196. package/src/validation/joi/joi.validation.util.ts +2 -2
  197. package/src/yargs.util.ts +1 -0
@@ -1,9 +1,10 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.transformMultiThreaded = transformMultiThreaded;
4
- const node_worker_threads_1 = require("node:worker_threads");
5
- const js_lib_1 = require("@naturalcycles/js-lib");
6
- const through2Concurrent = require("through2-concurrent");
1
+ import { dirname } from 'node:path';
2
+ import { fileURLToPath } from 'node:url';
3
+ import { Worker } from 'node:worker_threads';
4
+ import { _range, pDefer } from '@naturalcycles/js-lib';
5
+ import through2Concurrent from 'through2-concurrent';
6
+ const __filename = fileURLToPath(import.meta.url);
7
+ const __dirname = dirname(__filename);
7
8
  const workerProxyFilePath = `${__dirname}/workerClassProxy.js`;
8
9
  /**
9
10
  * Spawns a pool of Workers (threads).
@@ -11,7 +12,7 @@ const workerProxyFilePath = `${__dirname}/workerClassProxy.js`;
11
12
  * Workers emit 1 output for each 1 input.
12
13
  * Output of Workers is passed down the stream. Order is RANDOM (since it's a multi-threaded environment).
13
14
  */
14
- function transformMultiThreaded(opt) {
15
+ export function transformMultiThreaded(opt) {
15
16
  const { workerFile, poolSize = 2, workerData } = opt;
16
17
  const maxConcurrency = opt.concurrency || poolSize;
17
18
  const highWaterMark = Math.max(16, maxConcurrency);
@@ -23,9 +24,9 @@ function transformMultiThreaded(opt) {
23
24
  const workerDonePromises = [];
24
25
  const messageDonePromises = {};
25
26
  let index = -1; // input chunk index, will start from 0
26
- const workers = (0, js_lib_1._range)(0, poolSize).map(workerIndex => {
27
- workerDonePromises.push((0, js_lib_1.pDefer)());
28
- const worker = new node_worker_threads_1.Worker(workerProxyFilePath, {
27
+ const workers = _range(0, poolSize).map(workerIndex => {
28
+ workerDonePromises.push(pDefer());
29
+ const worker = new Worker(workerProxyFilePath, {
29
30
  workerData: {
30
31
  workerIndex,
31
32
  workerFile, // pass it, so workerProxy can require() it
@@ -75,7 +76,7 @@ function transformMultiThreaded(opt) {
75
76
  // Freezing the index, because it may change due to concurrency
76
77
  const currentIndex = ++index;
77
78
  // Create the unresolved promise (to avait)
78
- messageDonePromises[currentIndex] = (0, js_lib_1.pDefer)();
79
+ messageDonePromises[currentIndex] = pDefer();
79
80
  const worker = workers[currentIndex % poolSize]; // round-robin
80
81
  worker.postMessage({
81
82
  index: currentIndex,
@@ -1,2 +1 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ export {};
@@ -1,6 +1,6 @@
1
1
  const started = Date.now()
2
- const { workerData, parentPort } = require('node:worker_threads')
3
- const { inspect } = require('node:util')
2
+ import { workerData, parentPort } from 'node:worker_threads'
3
+ import { inspect } from 'node:util'
4
4
  const { workerFile, workerIndex, logEvery = 1000, metric = 'worker' } = workerData || {}
5
5
 
6
6
  if (!workerFile) {
@@ -12,11 +12,13 @@ if (!workerFile) {
12
12
  try {
13
13
  // require('esbuild-register') // alternative
14
14
  // require('ts-node/register/transpile-only')
15
- require('tsx/cjs/api').register() // https://tsx.is/dev-api/register-cjs
15
+ // require('tsx/cjs/api').register() // https://tsx.is/dev-api/register-cjs
16
+ const { register } = await import('tsx/esm/api')
17
+ register() // https://tsx.is/dev-api/register-esm
16
18
  // require('tsconfig-paths/register')
17
19
  } catch {} // require if exists
18
20
 
19
- const { WorkerClass } = require(workerFile)
21
+ const { WorkerClass } = await import(workerFile)
20
22
  const worker = new WorkerClass(workerData)
21
23
 
22
24
  console.log(`${metric}#${workerIndex} loaded in ${Date.now() - started} ms`)
@@ -1,6 +1,6 @@
1
1
  import type { AsyncMapper, Mapper } from '@naturalcycles/js-lib';
2
- import type { TransformMapOptions } from '../..';
3
- import type { WritableTyped } from '../stream.model';
2
+ import type { TransformMapOptions } from '../../index.js';
3
+ import type { WritableTyped } from '../stream.model.js';
4
4
  /**
5
5
  * Just an alias to transformMap that declares OUT as void.
6
6
  */
@@ -1,18 +1,14 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.writableForEach = writableForEach;
4
- exports.writableForEachSync = writableForEachSync;
5
- const js_lib_1 = require("@naturalcycles/js-lib");
6
- const __1 = require("../..");
1
+ import { _passNothingPredicate } from '@naturalcycles/js-lib';
2
+ import { transformMap, transformMapSync } from '../../index.js';
7
3
  /**
8
4
  * Just an alias to transformMap that declares OUT as void.
9
5
  */
10
- function writableForEach(mapper, opt = {}) {
11
- return (0, __1.transformMap)(mapper, { ...opt, predicate: js_lib_1._passNothingPredicate });
6
+ export function writableForEach(mapper, opt = {}) {
7
+ return transformMap(mapper, { ...opt, predicate: _passNothingPredicate });
12
8
  }
13
9
  /**
14
10
  * Just an alias to transformMap that declares OUT as void.
15
11
  */
16
- function writableForEachSync(mapper, opt = {}) {
17
- return (0, __1.transformMapSync)(mapper, { ...opt, predicate: js_lib_1._passNothingPredicate });
12
+ export function writableForEachSync(mapper, opt = {}) {
13
+ return transformMapSync(mapper, { ...opt, predicate: _passNothingPredicate });
18
14
  }
@@ -1,4 +1,4 @@
1
- import type { TransformOptions, WritableTyped } from '../stream.model';
1
+ import type { TransformOptions, WritableTyped } from '../stream.model.js';
2
2
  /**
3
3
  * Allows "forking" a stream inside pipeline into a number of pipeline chains (2 or more).
4
4
  * Currently does NOT (!) maintain backpressure.
@@ -1,8 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.writableFork = writableFork;
4
- const node_stream_1 = require("node:stream");
5
- const __1 = require("../..");
1
+ import { Writable } from 'node:stream';
2
+ import { _pipeline, readableCreate } from '../../index.js';
6
3
  /**
7
4
  * Allows "forking" a stream inside pipeline into a number of pipeline chains (2 or more).
8
5
  * Currently does NOT (!) maintain backpressure.
@@ -11,17 +8,17 @@ const __1 = require("../..");
11
8
  *
12
9
  * @experimental
13
10
  */
14
- function writableFork(chains, opt) {
11
+ export function writableFork(chains, opt) {
15
12
  const readables = [];
16
13
  const allChainsDone = Promise.all(chains.map(async (chain) => {
17
- const readable = (0, __1.readableCreate)();
14
+ const readable = readableCreate();
18
15
  readables.push(readable);
19
- return await (0, __1._pipeline)([readable, ...chain]);
16
+ return await _pipeline([readable, ...chain]);
20
17
  })).catch(err => {
21
18
  console.error(err); // ensure the error is logged
22
19
  throw err;
23
20
  });
24
- return new node_stream_1.Writable({
21
+ return new Writable({
25
22
  objectMode: true,
26
23
  ...opt,
27
24
  write(chunk, _, cb) {
@@ -1,5 +1,5 @@
1
1
  import type { Readable } from 'node:stream';
2
- import type { WritableTyped } from '../stream.model';
2
+ import type { WritableTyped } from '../stream.model.js';
3
3
  /**
4
4
  * Allows to stop the Readable stream after the pipeline has processed X number of rows.
5
5
  * It counts OUTPUT rows (not input), because this Writable is always at the end of the Pipeline.
@@ -1,15 +1,12 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.writableLimit = writableLimit;
4
- const node_stream_1 = require("node:stream");
1
+ import { Writable } from 'node:stream';
5
2
  /**
6
3
  * Allows to stop the Readable stream after the pipeline has processed X number of rows.
7
4
  * It counts OUTPUT rows (not input), because this Writable is always at the end of the Pipeline.
8
5
  * It ensures that everything has been processed before issuing a STOP on the readable.
9
6
  */
10
- function writableLimit(readable, limit) {
7
+ export function writableLimit(readable, limit) {
11
8
  let i = 0;
12
- return new node_stream_1.Writable({
9
+ return new Writable({
13
10
  objectMode: true,
14
11
  write(_chunk, _, cb) {
15
12
  if (limit === 0)
@@ -1,4 +1,4 @@
1
- import type { TransformOptions, WritableTyped } from '../stream.model';
1
+ import type { TransformOptions, WritableTyped } from '../stream.model.js';
2
2
  /**
3
3
  * Will push all results to `arr`, will emit nothing in the end.
4
4
  */
@@ -1,12 +1,9 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.writablePushToArray = writablePushToArray;
4
- const node_stream_1 = require("node:stream");
1
+ import { Writable } from 'node:stream';
5
2
  /**
6
3
  * Will push all results to `arr`, will emit nothing in the end.
7
4
  */
8
- function writablePushToArray(arr, opt = {}) {
9
- return new node_stream_1.Writable({
5
+ export function writablePushToArray(arr, opt = {}) {
6
+ return new Writable({
10
7
  objectMode: true,
11
8
  ...opt,
12
9
  write(chunk, _, cb) {
@@ -1,6 +1,6 @@
1
1
  import { Writable } from 'node:stream';
2
2
  import type { DeferredPromise } from '@naturalcycles/js-lib';
3
- import type { TransformOptions } from '../stream.model';
3
+ import type { TransformOptions } from '../stream.model.js';
4
4
  export interface WritableVoidOptions extends TransformOptions {
5
5
  /**
6
6
  * If set - it will be Resolved when the Stream is done (after final.cb)
@@ -1,14 +1,11 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.writableVoid = writableVoid;
4
- const node_stream_1 = require("node:stream");
1
+ import { Writable } from 'node:stream';
5
2
  /**
6
3
  * Use as a "null-terminator" of stream.pipeline.
7
4
  * It consumes the stream as quickly as possible without doing anything.
8
5
  * Put it in the end of your pipeline in case it ends with Transform that needs a consumer.
9
6
  */
10
- function writableVoid(opt = {}) {
11
- return new node_stream_1.Writable({
7
+ export function writableVoid(opt = {}) {
8
+ return new Writable({
12
9
  objectMode: true,
13
10
  ...opt,
14
11
  write(_chunk, _, cb) {
@@ -1,9 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.inspectAny = exports.inspectStringifyFn = void 0;
4
- exports._inspect = _inspect;
5
- const node_util_1 = require("node:util");
6
- const js_lib_1 = require("@naturalcycles/js-lib");
1
+ import { inspect } from 'node:util';
2
+ import { _stringify } from '@naturalcycles/js-lib';
7
3
  const INSPECT_OPT = {
8
4
  breakLength: 80, // default: ??
9
5
  depth: 10, // default: 2
@@ -11,8 +7,7 @@ const INSPECT_OPT = {
11
7
  /**
12
8
  * Just a convenience export of a const that fulfills the JsonStringifyFunction interface.
13
9
  */
14
- const inspectStringifyFn = obj => (0, node_util_1.inspect)(obj, INSPECT_OPT);
15
- exports.inspectStringifyFn = inspectStringifyFn;
10
+ export const inspectStringifyFn = obj => inspect(obj, INSPECT_OPT);
16
11
  /**
17
12
  * Transforms ANY to human-readable string (via util.inspect mainly).
18
13
  * Safe (no error throwing).
@@ -30,17 +25,17 @@ exports.inspectStringifyFn = inspectStringifyFn;
30
25
  *
31
26
  * Based on `_stringify` from `js-lib`, just replaced `JSON.stringify` with `util.inspect`.
32
27
  */
33
- function _inspect(obj, opt = {}) {
28
+ export function _inspect(obj, opt = {}) {
34
29
  // Inspect handles functions better
35
30
  if (typeof obj === 'function') {
36
- return (0, node_util_1.inspect)(obj, {
31
+ return inspect(obj, {
37
32
  ...INSPECT_OPT,
38
33
  ...opt,
39
34
  });
40
35
  }
41
- return (0, js_lib_1._stringify)(obj, {
36
+ return _stringify(obj, {
42
37
  ...opt,
43
- stringifyFn: obj => (0, node_util_1.inspect)(obj, {
38
+ stringifyFn: obj => inspect(obj, {
44
39
  ...INSPECT_OPT,
45
40
  ...opt,
46
41
  }),
@@ -49,4 +44,4 @@ function _inspect(obj, opt = {}) {
49
44
  /**
50
45
  * @deprecated renamed to _inspect
51
46
  */
52
- exports.inspectAny = _inspect;
47
+ export const inspectAny = _inspect;
@@ -1,29 +1,26 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateBuildInfo = generateBuildInfo;
4
- const js_lib_1 = require("@naturalcycles/js-lib");
5
- const fs2_1 = require("../fs/fs2");
6
- const git2_1 = require("./git2");
7
- function generateBuildInfo(opt = {}) {
8
- const now = js_lib_1.localTime.orNow(opt.overrideTimestamp);
1
+ import { _filterUndefinedValues, localTime } from '@naturalcycles/js-lib';
2
+ import { fs2 } from '../fs/fs2.js';
3
+ import { git2 } from './git2.js';
4
+ export function generateBuildInfo(opt = {}) {
5
+ const now = localTime.orNow(opt.overrideTimestamp);
9
6
  const ts = now.unix;
10
- const rev = git2_1.git2.getCurrentCommitSha();
11
- const branchName = opt.overrideBranchName || git2_1.git2.getCurrentBranchName();
12
- const repoName = git2_1.git2.getCurrentRepoName();
13
- const tsCommit = git2_1.git2.getCurrentCommitTimestamp();
7
+ const rev = git2.getCurrentCommitSha();
8
+ const branchName = opt.overrideBranchName || git2.getCurrentBranchName();
9
+ const repoName = git2.getCurrentRepoName();
10
+ const tsCommit = git2.getCurrentCommitTimestamp();
14
11
  const ver = [now.toStringCompact(), repoName, branchName, rev].join('_');
15
12
  let { APP_ENV: env } = process.env;
16
13
  if (!env) {
17
14
  // Attempt to read `envByBranch` from package.json root
18
15
  try {
19
- if (fs2_1.fs2.pathExists('package.json')) {
20
- const packageJson = fs2_1.fs2.readJson('package.json');
16
+ if (fs2.pathExists('package.json')) {
17
+ const packageJson = fs2.readJson('package.json');
21
18
  env = packageJson?.['envByBranch']?.[branchName] || packageJson?.['envByBranch']?.['*'];
22
19
  }
23
20
  }
24
21
  catch { }
25
22
  }
26
- return (0, js_lib_1._filterUndefinedValues)({
23
+ return _filterUndefinedValues({
27
24
  ts,
28
25
  tsCommit,
29
26
  repoName,
@@ -1,9 +1,5 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.requireEnvKeys = requireEnvKeys;
4
- exports.requireFileToExist = requireFileToExist;
5
- require("dotenv/config"); // ensure .env is read before requiring keys
6
- const node_fs_1 = require("node:fs");
1
+ import 'dotenv/config'; // ensure .env is read before requiring keys
2
+ import { existsSync } from 'node:fs';
7
3
  /**
8
4
  * @example
9
5
  *
@@ -11,7 +7,7 @@ const node_fs_1 = require("node:fs");
11
7
  *
12
8
  * Will throw if any of the passed keys is not defined.
13
9
  */
14
- function requireEnvKeys(...keys) {
10
+ export function requireEnvKeys(...keys) {
15
11
  // eslint-disable-next-line unicorn/no-array-reduce
16
12
  return keys.reduce((r, k) => {
17
13
  const v = process.env[k];
@@ -21,8 +17,8 @@ function requireEnvKeys(...keys) {
21
17
  return r;
22
18
  }, {});
23
19
  }
24
- function requireFileToExist(filePath) {
25
- if (!(0, node_fs_1.existsSync)(filePath)) {
20
+ export function requireFileToExist(filePath) {
21
+ if (!existsSync(filePath)) {
26
22
  throw new Error(`Required file should exist: ${filePath}`);
27
23
  }
28
24
  }
@@ -1,9 +1,6 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.SpawnError = exports.exec2 = void 0;
4
- const node_child_process_1 = require("node:child_process");
5
- const js_lib_1 = require("@naturalcycles/js-lib");
6
- const colors_1 = require("../colors/colors");
1
+ import { execSync, spawn, spawnSync } from 'node:child_process';
2
+ import { _since, AppError } from '@naturalcycles/js-lib';
3
+ import { dimGrey, dimRed, hasColors, white } from '../colors/colors.js';
7
4
  /**
8
5
  * Set of utility functions to work with Spawn / Exec.
9
6
  *
@@ -41,13 +38,13 @@ class Exec2 {
41
38
  * log: true
42
39
  */
43
40
  spawn(cmd, opt = {}) {
44
- const { shell = true, cwd, env, passProcessEnv = true, forceColor = colors_1.hasColors } = opt;
41
+ const { shell = true, cwd, env, passProcessEnv = true, forceColor = hasColors } = opt;
45
42
  opt.log ??= true; // by default log should be true, as we are printing the output
46
43
  opt.logStart ??= opt.log;
47
44
  opt.logFinish ??= opt.log;
48
45
  const started = Date.now();
49
46
  this.logStart(cmd, opt);
50
- const r = (0, node_child_process_1.spawnSync)(cmd, opt.args, {
47
+ const r = spawnSync(cmd, opt.args, {
51
48
  encoding: 'utf8',
52
49
  stdio: 'inherit',
53
50
  shell,
@@ -88,7 +85,7 @@ class Exec2 {
88
85
  const started = Date.now();
89
86
  this.logStart(cmd, opt);
90
87
  try {
91
- const s = (0, node_child_process_1.execSync)(cmd, {
88
+ const s = execSync(cmd, {
92
89
  encoding: 'utf8',
93
90
  // stdio: 'inherit', // no, otherwise we don't get the output returned
94
91
  stdio: undefined,
@@ -130,14 +127,14 @@ class Exec2 {
130
127
  * log: true
131
128
  */
132
129
  async spawnAsync(cmd, opt = {}) {
133
- const { shell = true, cwd, env, passProcessEnv = true, forceColor = colors_1.hasColors } = opt;
130
+ const { shell = true, cwd, env, passProcessEnv = true, forceColor = hasColors } = opt;
134
131
  opt.log ??= true; // by default log should be true, as we are printing the output
135
132
  opt.logStart ??= opt.log;
136
133
  opt.logFinish ??= opt.log;
137
134
  const started = Date.now();
138
135
  this.logStart(cmd, opt);
139
136
  await new Promise((resolve, reject) => {
140
- const p = (0, node_child_process_1.spawn)(cmd, opt.args || [], {
137
+ const p = spawn(cmd, opt.args || [], {
141
138
  shell,
142
139
  cwd,
143
140
  stdio: 'inherit',
@@ -177,7 +174,7 @@ class Exec2 {
177
174
  * log: true
178
175
  */
179
176
  async spawnAsyncAndReturn(cmd, opt = {}) {
180
- const { shell = true, printWhileRunning = true, collectOutputWhileRunning = true, throwOnNonZeroCode = true, cwd, env, passProcessEnv = true, forceColor = colors_1.hasColors, } = opt;
177
+ const { shell = true, printWhileRunning = true, collectOutputWhileRunning = true, throwOnNonZeroCode = true, cwd, env, passProcessEnv = true, forceColor = hasColors, } = opt;
181
178
  opt.log ??= printWhileRunning; // by default log should be true, as we are printing the output
182
179
  opt.logStart ??= opt.log;
183
180
  opt.logFinish ??= opt.log;
@@ -186,7 +183,7 @@ class Exec2 {
186
183
  let stdout = '';
187
184
  let stderr = '';
188
185
  return await new Promise((resolve, reject) => {
189
- const p = (0, node_child_process_1.spawn)(cmd, opt.args || [], {
186
+ const p = spawn(cmd, opt.args || [], {
190
187
  shell,
191
188
  cwd,
192
189
  env: {
@@ -233,8 +230,8 @@ class Exec2 {
233
230
  if (!opt.logStart)
234
231
  return;
235
232
  console.log([
236
- (0, colors_1.dimGrey)(...Object.entries(opt.env || {}).map(([k, v]) => [k, v].join('='))),
237
- (0, colors_1.white)(opt.name || cmd),
233
+ dimGrey(...Object.entries(opt.env || {}).map(([k, v]) => [k, v].join('='))),
234
+ white(opt.name || cmd),
238
235
  ...((!opt.name && opt.args) || []),
239
236
  ]
240
237
  .filter(Boolean)
@@ -244,19 +241,18 @@ class Exec2 {
244
241
  if (isSuccessful && !opt.logFinish)
245
242
  return;
246
243
  console.log([
247
- (0, colors_1.white)(opt.name || cmd),
244
+ white(opt.name || cmd),
248
245
  ...((!opt.name && opt.args) || []),
249
- (0, colors_1.dimGrey)('took ' + (0, js_lib_1._since)(started)),
250
- !isSuccessful && (0, colors_1.dimGrey)('and ') + (0, colors_1.dimRed)('failed'),
246
+ dimGrey('took ' + _since(started)),
247
+ !isSuccessful && dimGrey('and ') + dimRed('failed'),
251
248
  ]
252
249
  .filter(Boolean)
253
250
  .join(' '));
254
251
  }
255
252
  }
256
- exports.exec2 = new Exec2();
257
- class SpawnError extends js_lib_1.AppError {
253
+ export const exec2 = new Exec2();
254
+ export class SpawnError extends AppError {
258
255
  constructor(message, data) {
259
256
  super(message, data, { name: 'SpawnError' });
260
257
  }
261
258
  }
262
- exports.SpawnError = SpawnError;
package/dist/util/git2.js CHANGED
@@ -1,16 +1,13 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.git2 = void 0;
4
- const node_child_process_1 = require("node:child_process");
5
- const node_path_1 = require("node:path");
6
- const colors_1 = require("../colors/colors");
7
- const exec2_1 = require("./exec2");
1
+ import { execSync } from 'node:child_process';
2
+ import { basename } from 'node:path';
3
+ import { grey } from '../colors/colors.js';
4
+ import { exec2 } from './exec2.js';
8
5
  /**
9
6
  * Set of utility functions to work with git.
10
7
  */
11
8
  class Git2 {
12
9
  getLastGitCommitMsg() {
13
- return exec2_1.exec2.exec('git log -1 --pretty=%B');
10
+ return exec2.exec('git log -1 --pretty=%B');
14
11
  }
15
12
  commitMessageToTitleMessage(msg) {
16
13
  const firstLine = msg.split('\n')[0];
@@ -20,7 +17,7 @@ class Git2 {
20
17
  hasUncommittedChanges() {
21
18
  // git diff-index --quiet HEAD -- || echo "untracked"
22
19
  try {
23
- (0, node_child_process_1.execSync)('git diff-index --quiet HEAD --', {
20
+ execSync('git diff-index --quiet HEAD --', {
24
21
  encoding: 'utf8',
25
22
  });
26
23
  return false;
@@ -37,9 +34,9 @@ class Git2 {
37
34
  const cmd = `git commit -a --no-verify -m "${msg}"`;
38
35
  // const cmd = `git`
39
36
  // const args = ['commit', '-a', '--no-verify', '-m', msg]
40
- console.log((0, colors_1.grey)(cmd));
37
+ console.log(grey(cmd));
41
38
  try {
42
- (0, node_child_process_1.execSync)(cmd, {
39
+ execSync(cmd, {
43
40
  stdio: 'inherit',
44
41
  });
45
42
  return true;
@@ -54,14 +51,14 @@ class Git2 {
54
51
  isAhead() {
55
52
  // ahead=`git rev-list HEAD --not --remotes | wc -l | awk '{print $1}'`
56
53
  const cmd = `git rev-list HEAD --not --remotes | wc -l | awk '{print $1}'`;
57
- const stdout = exec2_1.exec2.exec(cmd);
54
+ const stdout = exec2.exec(cmd);
58
55
  // console.log(`gitIsAhead: ${stdout}`)
59
56
  return Number(stdout) > 0;
60
57
  }
61
58
  pull() {
62
59
  const cmd = 'git pull';
63
60
  try {
64
- (0, node_child_process_1.execSync)(cmd, {
61
+ execSync(cmd, {
65
62
  stdio: 'inherit',
66
63
  });
67
64
  }
@@ -74,21 +71,21 @@ class Git2 {
74
71
  if (branchName) {
75
72
  cmd += ` --set-upstream origin ${branchName}`;
76
73
  }
77
- exec2_1.exec2.spawn(cmd, { logStart: true });
74
+ exec2.spawn(cmd, { logStart: true });
78
75
  }
79
76
  getCurrentCommitSha(full = false) {
80
- const sha = exec2_1.exec2.exec('git rev-parse HEAD');
77
+ const sha = exec2.exec('git rev-parse HEAD');
81
78
  return full ? sha : sha.slice(0, 7);
82
79
  }
83
80
  getCurrentCommitTimestamp() {
84
- return Number(exec2_1.exec2.exec('git log -1 --format=%ct'));
81
+ return Number(exec2.exec('git log -1 --format=%ct'));
85
82
  }
86
83
  getCurrentBranchName() {
87
- return exec2_1.exec2.exec('git rev-parse --abbrev-ref HEAD');
84
+ return exec2.exec('git rev-parse --abbrev-ref HEAD');
88
85
  }
89
86
  getCurrentRepoName() {
90
- const originUrl = exec2_1.exec2.exec('git config --get remote.origin.url');
91
- return (0, node_path_1.basename)(originUrl, '.git');
87
+ const originUrl = exec2.exec('git config --get remote.origin.url');
88
+ return basename(originUrl, '.git');
92
89
  }
93
90
  }
94
- exports.git2 = new Git2();
91
+ export const git2 = new Git2();
@@ -1,7 +1,4 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.LRUMemoCache = void 0;
4
- const lru_cache_1 = require("lru-cache");
1
+ import { LRUCache } from 'lru-cache';
5
2
  /**
6
3
  * @example
7
4
  * Use it like this:
@@ -9,9 +6,9 @@ const lru_cache_1 = require("lru-cache");
9
6
  * @_Memo({ cacheFactory: () => new LRUMemoCache({...}) })
10
7
  * method1 ()
11
8
  */
12
- class LRUMemoCache {
9
+ export class LRUMemoCache {
13
10
  constructor(opt) {
14
- this.lru = new lru_cache_1.LRUCache({
11
+ this.lru = new LRUCache({
15
12
  max: 100,
16
13
  ...opt,
17
14
  });
@@ -30,4 +27,3 @@ class LRUMemoCache {
30
27
  this.lru.clear();
31
28
  }
32
29
  }
33
- exports.LRUMemoCache = LRUMemoCache;