@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
package/dist/fs/fs2.js CHANGED
@@ -1,4 +1,3 @@
1
- "use strict";
2
1
  /*
3
2
 
4
3
  Why?
@@ -14,18 +13,15 @@ That's why function names are slightly renamed, to avoid conflict.
14
13
  Credit to: fs-extra (https://github.com/jprichardson/node-fs-extra)
15
14
 
16
15
  */
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.fs2 = void 0;
19
- const tslib_1 = require("tslib");
20
- const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
21
- const promises_1 = tslib_1.__importDefault(require("node:fs/promises"));
22
- const node_path_1 = tslib_1.__importDefault(require("node:path"));
23
- const node_zlib_1 = require("node:zlib");
24
- const js_lib_1 = require("@naturalcycles/js-lib");
25
- const js_yaml_1 = tslib_1.__importDefault(require("js-yaml"));
26
- const transformToNDJson_1 = require("../stream/ndjson/transformToNDJson");
27
- const transformSplit_1 = require("../stream/transform/transformSplit");
28
- const env_util_1 = require("../util/env.util");
16
+ import fs from 'node:fs';
17
+ import fsp from 'node:fs/promises';
18
+ import path from 'node:path';
19
+ import { createGzip, createUnzip } from 'node:zlib';
20
+ import { _isTruthy, _jsonParse } from '@naturalcycles/js-lib';
21
+ import yaml from 'js-yaml';
22
+ import { transformToNDJson } from '../stream/ndjson/transformToNDJson.js';
23
+ import { transformSplitOnNewline } from '../stream/transform/transformSplit.js';
24
+ import { requireFileToExist } from '../util/env.util.js';
29
25
  /**
30
26
  * fs2 conveniently groups filesystem functions together.
31
27
  * Supposed to be almost a drop-in replacement for these things together:
@@ -42,62 +38,62 @@ class FS2 {
42
38
  * Convenience wrapper that defaults to utf-8 string output.
43
39
  */
44
40
  readText(filePath) {
45
- return node_fs_1.default.readFileSync(filePath, 'utf8');
41
+ return fs.readFileSync(filePath, 'utf8');
46
42
  }
47
43
  /**
48
44
  * Convenience wrapper that defaults to utf-8 string output.
49
45
  */
50
46
  async readTextAsync(filePath) {
51
- return await promises_1.default.readFile(filePath, 'utf8');
47
+ return await fsp.readFile(filePath, 'utf8');
52
48
  }
53
49
  readBuffer(filePath) {
54
- return node_fs_1.default.readFileSync(filePath);
50
+ return fs.readFileSync(filePath);
55
51
  }
56
52
  async readBufferAsync(filePath) {
57
- return await promises_1.default.readFile(filePath);
53
+ return await fsp.readFile(filePath);
58
54
  }
59
55
  readJson(filePath) {
60
- const str = node_fs_1.default.readFileSync(filePath, 'utf8');
61
- return (0, js_lib_1._jsonParse)(str);
56
+ const str = fs.readFileSync(filePath, 'utf8');
57
+ return _jsonParse(str);
62
58
  }
63
59
  async readJsonAsync(filePath) {
64
- const str = await promises_1.default.readFile(filePath, 'utf8');
60
+ const str = await fsp.readFile(filePath, 'utf8');
65
61
  // eslint-disable-next-line @typescript-eslint/return-await
66
- return (0, js_lib_1._jsonParse)(str);
62
+ return _jsonParse(str);
67
63
  }
68
64
  readYaml(filePath) {
69
- return js_yaml_1.default.load(node_fs_1.default.readFileSync(filePath, 'utf8'));
65
+ return yaml.load(fs.readFileSync(filePath, 'utf8'));
70
66
  }
71
67
  async readYamlAsync(filePath) {
72
- return js_yaml_1.default.load(await promises_1.default.readFile(filePath, 'utf8'));
68
+ return yaml.load(await fsp.readFile(filePath, 'utf8'));
73
69
  }
74
70
  writeFile(filePath, data) {
75
- node_fs_1.default.writeFileSync(filePath, data);
71
+ fs.writeFileSync(filePath, data);
76
72
  }
77
73
  async writeFileAsync(filePath, data) {
78
- await promises_1.default.writeFile(filePath, data);
74
+ await fsp.writeFile(filePath, data);
79
75
  }
80
76
  writeJson(filePath, data, opt) {
81
77
  const str = stringify(data, opt);
82
- node_fs_1.default.writeFileSync(filePath, str);
78
+ fs.writeFileSync(filePath, str);
83
79
  }
84
80
  async writeJsonAsync(filePath, data, opt) {
85
81
  const str = stringify(data, opt);
86
- await promises_1.default.writeFile(filePath, str);
82
+ await fsp.writeFile(filePath, str);
87
83
  }
88
84
  writeYaml(filePath, data, opt) {
89
- const str = js_yaml_1.default.dump(data, opt);
90
- node_fs_1.default.writeFileSync(filePath, str);
85
+ const str = yaml.dump(data, opt);
86
+ fs.writeFileSync(filePath, str);
91
87
  }
92
88
  async writeYamlAsync(filePath, data, opt) {
93
- const str = js_yaml_1.default.dump(data, opt);
94
- await promises_1.default.writeFile(filePath, str);
89
+ const str = yaml.dump(data, opt);
90
+ await fsp.writeFile(filePath, str);
95
91
  }
96
92
  appendFile(filePath, data) {
97
- node_fs_1.default.appendFileSync(filePath, data);
93
+ fs.appendFileSync(filePath, data);
98
94
  }
99
95
  async appendFileAsync(filePath, data) {
100
- await promises_1.default.appendFile(filePath, data);
96
+ await fsp.appendFile(filePath, data);
101
97
  }
102
98
  outputJson(filePath, data, opt) {
103
99
  const str = stringify(data, opt);
@@ -108,33 +104,33 @@ class FS2 {
108
104
  await this.outputFileAsync(filePath, str);
109
105
  }
110
106
  outputYaml(filePath, data, opt) {
111
- const str = js_yaml_1.default.dump(data, opt);
107
+ const str = yaml.dump(data, opt);
112
108
  this.outputFile(filePath, str);
113
109
  }
114
110
  async outputYamlAsync(filePath, data, opt) {
115
- const str = js_yaml_1.default.dump(data, opt);
111
+ const str = yaml.dump(data, opt);
116
112
  await this.outputFileAsync(filePath, str);
117
113
  }
118
114
  outputFile(filePath, data) {
119
- const dirPath = node_path_1.default.dirname(filePath);
120
- if (!node_fs_1.default.existsSync(dirPath)) {
115
+ const dirPath = path.dirname(filePath);
116
+ if (!fs.existsSync(dirPath)) {
121
117
  this.ensureDir(dirPath);
122
118
  }
123
- node_fs_1.default.writeFileSync(filePath, data);
119
+ fs.writeFileSync(filePath, data);
124
120
  }
125
121
  async outputFileAsync(filePath, data) {
126
- const dirPath = node_path_1.default.dirname(filePath);
122
+ const dirPath = path.dirname(filePath);
127
123
  if (!(await this.pathExistsAsync(dirPath))) {
128
124
  await this.ensureDirAsync(dirPath);
129
125
  }
130
- await promises_1.default.writeFile(filePath, data);
126
+ await fsp.writeFile(filePath, data);
131
127
  }
132
128
  pathExists(filePath) {
133
- return node_fs_1.default.existsSync(filePath);
129
+ return fs.existsSync(filePath);
134
130
  }
135
131
  async pathExistsAsync(filePath) {
136
132
  try {
137
- await promises_1.default.access(filePath);
133
+ await fsp.access(filePath);
138
134
  return true;
139
135
  }
140
136
  catch {
@@ -142,13 +138,13 @@ class FS2 {
142
138
  }
143
139
  }
144
140
  ensureDir(dirPath) {
145
- node_fs_1.default.mkdirSync(dirPath, {
141
+ fs.mkdirSync(dirPath, {
146
142
  mode: 0o777,
147
143
  recursive: true,
148
144
  });
149
145
  }
150
146
  async ensureDirAsync(dirPath) {
151
- await promises_1.default.mkdir(dirPath, {
147
+ await fsp.mkdir(dirPath, {
152
148
  mode: 0o777,
153
149
  recursive: true,
154
150
  });
@@ -156,17 +152,17 @@ class FS2 {
156
152
  ensureFile(filePath) {
157
153
  let stats;
158
154
  try {
159
- stats = node_fs_1.default.statSync(filePath);
155
+ stats = fs.statSync(filePath);
160
156
  }
161
157
  catch { }
162
158
  if (stats?.isFile())
163
159
  return;
164
- const dir = node_path_1.default.dirname(filePath);
160
+ const dir = path.dirname(filePath);
165
161
  try {
166
- if (!node_fs_1.default.statSync(dir).isDirectory()) {
162
+ if (!fs.statSync(dir).isDirectory()) {
167
163
  // parent is not a directory
168
164
  // This is just to cause an internal ENOTDIR error to be thrown
169
- node_fs_1.default.readdirSync(dir);
165
+ fs.readdirSync(dir);
170
166
  }
171
167
  }
172
168
  catch (err) {
@@ -177,22 +173,22 @@ class FS2 {
177
173
  }
178
174
  throw err;
179
175
  }
180
- node_fs_1.default.writeFileSync(filePath, '');
176
+ fs.writeFileSync(filePath, '');
181
177
  }
182
178
  async ensureFileAsync(filePath) {
183
179
  let stats;
184
180
  try {
185
- stats = await promises_1.default.stat(filePath);
181
+ stats = await fsp.stat(filePath);
186
182
  }
187
183
  catch { }
188
184
  if (stats?.isFile())
189
185
  return;
190
- const dir = node_path_1.default.dirname(filePath);
186
+ const dir = path.dirname(filePath);
191
187
  try {
192
- if (!(await promises_1.default.stat(dir)).isDirectory()) {
188
+ if (!(await fsp.stat(dir)).isDirectory()) {
193
189
  // parent is not a directory
194
190
  // This is just to cause an internal ENOTDIR error to be thrown
195
- await promises_1.default.readdir(dir);
191
+ await fsp.readdir(dir);
196
192
  }
197
193
  }
198
194
  catch (err) {
@@ -201,40 +197,40 @@ class FS2 {
201
197
  return await this.ensureDirAsync(dir);
202
198
  throw err;
203
199
  }
204
- await promises_1.default.writeFile(filePath, '');
200
+ await fsp.writeFile(filePath, '');
205
201
  }
206
202
  removePath(fileOrDirPath, opt) {
207
- node_fs_1.default.rmSync(fileOrDirPath, { recursive: true, force: true, ...opt });
203
+ fs.rmSync(fileOrDirPath, { recursive: true, force: true, ...opt });
208
204
  }
209
205
  async removePathAsync(fileOrDirPath, opt) {
210
- await promises_1.default.rm(fileOrDirPath, { recursive: true, force: true, ...opt });
206
+ await fsp.rm(fileOrDirPath, { recursive: true, force: true, ...opt });
211
207
  }
212
208
  emptyDir(dirPath) {
213
209
  let items;
214
210
  try {
215
- items = node_fs_1.default.readdirSync(dirPath);
211
+ items = fs.readdirSync(dirPath);
216
212
  }
217
213
  catch {
218
214
  this.ensureDir(dirPath);
219
215
  return;
220
216
  }
221
- items.forEach(item => this.removePath(node_path_1.default.join(dirPath, item)));
217
+ items.forEach(item => this.removePath(path.join(dirPath, item)));
222
218
  }
223
219
  async emptyDirAsync(dirPath) {
224
220
  let items;
225
221
  try {
226
- items = await promises_1.default.readdir(dirPath);
222
+ items = await fsp.readdir(dirPath);
227
223
  }
228
224
  catch {
229
225
  return await this.ensureDirAsync(dirPath);
230
226
  }
231
- await Promise.all(items.map(item => this.removePathAsync(node_path_1.default.join(dirPath, item))));
227
+ await Promise.all(items.map(item => this.removePathAsync(path.join(dirPath, item))));
232
228
  }
233
229
  /**
234
230
  * Cautious, underlying Node function is currently Experimental.
235
231
  */
236
232
  copyPath(src, dest, opt) {
237
- node_fs_1.default.cpSync(src, dest, {
233
+ fs.cpSync(src, dest, {
238
234
  recursive: true,
239
235
  ...opt,
240
236
  });
@@ -243,16 +239,16 @@ class FS2 {
243
239
  * Cautious, underlying Node function is currently Experimental.
244
240
  */
245
241
  async copyPathAsync(src, dest, opt) {
246
- await promises_1.default.cp(src, dest, {
242
+ await fsp.cp(src, dest, {
247
243
  recursive: true,
248
244
  ...opt,
249
245
  });
250
246
  }
251
247
  renamePath(src, dest) {
252
- node_fs_1.default.renameSync(src, dest);
248
+ fs.renameSync(src, dest);
253
249
  }
254
250
  async renamePathAsync(src, dest) {
255
- await promises_1.default.rename(src, dest);
251
+ await fsp.rename(src, dest);
256
252
  }
257
253
  movePath(src, dest, opt) {
258
254
  this.copyPath(src, dest, opt);
@@ -268,27 +264,27 @@ class FS2 {
268
264
  * Doesn't throw, returns false instead.
269
265
  */
270
266
  isDirectory(filePath) {
271
- return (exports.fs2
267
+ return (fs2
272
268
  .stat(filePath, {
273
269
  throwIfNoEntry: false,
274
270
  })
275
271
  ?.isDirectory() || false);
276
272
  }
277
273
  // Re-export the whole fs/fsp, for the edge cases where they are needed
278
- fs = node_fs_1.default;
279
- fsp = promises_1.default;
274
+ fs = fs;
275
+ fsp = fsp;
280
276
  // Re-export existing fs/fsp functions
281
277
  // rm/rmAsync are replaced with removePath/removePathAsync
282
- lstat = node_fs_1.default.lstatSync;
283
- lstatAsync = promises_1.default.lstat;
284
- stat = node_fs_1.default.statSync;
285
- statAsync = promises_1.default.stat;
286
- mkdir = node_fs_1.default.mkdirSync;
287
- mkdirAsync = promises_1.default.mkdir;
288
- readdir = node_fs_1.default.readdirSync;
289
- readdirAsync = promises_1.default.readdir;
290
- createWriteStream = node_fs_1.default.createWriteStream;
291
- createReadStream = node_fs_1.default.createReadStream;
278
+ lstat = fs.lstatSync;
279
+ lstatAsync = fsp.lstat;
280
+ stat = fs.statSync;
281
+ statAsync = fsp.stat;
282
+ mkdir = fs.mkdirSync;
283
+ mkdirAsync = fsp.mkdir;
284
+ readdir = fs.readdirSync;
285
+ readdirAsync = fsp.readdir;
286
+ createWriteStream = fs.createWriteStream;
287
+ createReadStream = fs.createReadStream;
292
288
  /*
293
289
  Returns a Readable of [already parsed] NDJSON objects.
294
290
 
@@ -307,18 +303,18 @@ class FS2 {
307
303
  ])
308
304
  */
309
305
  createReadStreamAsNDJSON(inputPath) {
310
- (0, env_util_1.requireFileToExist)(inputPath);
311
- let stream = node_fs_1.default
306
+ requireFileToExist(inputPath);
307
+ let stream = fs
312
308
  .createReadStream(inputPath, {
313
309
  highWaterMark: 64 * 1024, // no observed speedup
314
310
  })
315
311
  .on('error', err => stream.emit('error', err));
316
312
  if (inputPath.endsWith('.gz')) {
317
- stream = stream.pipe((0, node_zlib_1.createUnzip)({
313
+ stream = stream.pipe(createUnzip({
318
314
  chunkSize: 64 * 1024, // speedup from ~3200 to 3800 rps!
319
315
  }));
320
316
  }
321
- return stream.pipe((0, transformSplit_1.transformSplitOnNewline)()).map(line => JSON.parse(line));
317
+ return stream.pipe(transformSplitOnNewline()).map(line => JSON.parse(line));
322
318
  // For some crazy reason .map is much faster than transformJsonParse!
323
319
  // ~5000 vs ~4000 rps !!!
324
320
  // .on('error', err => stream.emit('error', err))
@@ -336,19 +332,19 @@ class FS2 {
336
332
  createWriteStreamAsNDJSON(outputPath) {
337
333
  this.ensureFile(outputPath);
338
334
  return [
339
- (0, transformToNDJson_1.transformToNDJson)(),
335
+ transformToNDJson(),
340
336
  outputPath.endsWith('.gz')
341
- ? (0, node_zlib_1.createGzip)({
337
+ ? createGzip({
342
338
  // chunkSize: 64 * 1024, // no observed speedup
343
339
  })
344
340
  : undefined,
345
- node_fs_1.default.createWriteStream(outputPath, {
341
+ fs.createWriteStream(outputPath, {
346
342
  // highWaterMark: 64 * 1024, // no observed speedup
347
343
  }),
348
- ].filter(js_lib_1._isTruthy);
344
+ ].filter(_isTruthy);
349
345
  }
350
346
  }
351
- exports.fs2 = new FS2();
347
+ export const fs2 = new FS2();
352
348
  function stringify(data, opt) {
353
349
  // If pretty-printing is enabled (spaces) - also add a newline at the end (to match our prettier config)
354
350
  return JSON.stringify(data, null, opt?.spaces) + (opt?.spaces ? '\n' : '');
@@ -1,28 +1,18 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.json2env = json2env;
4
- exports.appendToBashEnv = appendToBashEnv;
5
- exports.appendToGithubEnv = appendToGithubEnv;
6
- exports.appendToGithubOutput = appendToGithubOutput;
7
- exports.appendToGithubSummary = appendToGithubSummary;
8
- exports.objectToShellExport = objectToShellExport;
9
- exports.objectToGithubActionsEnv = objectToGithubActionsEnv;
10
- const tslib_1 = require("tslib");
11
- const node_fs_1 = tslib_1.__importDefault(require("node:fs"));
12
- const colors_1 = require("../colors/colors");
13
- const fs2_1 = require("./fs2");
1
+ import fs from 'node:fs';
2
+ import { dimGrey } from '../colors/colors.js';
3
+ import { fs2 } from './fs2.js';
14
4
  const JSON2ENV_OPT_DEF = {
15
5
  saveEnvFile: true,
16
6
  bashEnv: true,
17
7
  githubEnv: true,
18
8
  fail: true,
19
9
  };
20
- function json2env(opt) {
10
+ export function json2env(opt) {
21
11
  const { jsonPath, prefix, saveEnvFile, bashEnv, githubEnv, fail, debug, silent } = {
22
12
  ...JSON2ENV_OPT_DEF,
23
13
  ...opt,
24
14
  };
25
- if (!fs2_1.fs2.pathExists(jsonPath)) {
15
+ if (!fs2.pathExists(jsonPath)) {
26
16
  if (fail) {
27
17
  throw new Error(`Path doesn't exist: ${jsonPath}`);
28
18
  }
@@ -35,16 +25,16 @@ function json2env(opt) {
35
25
  return;
36
26
  }
37
27
  // read file
38
- const json = fs2_1.fs2.readJson(jsonPath);
28
+ const json = fs2.readJson(jsonPath);
39
29
  if (debug) {
40
30
  console.log(json);
41
31
  }
42
32
  if (saveEnvFile) {
43
33
  const shPath = `${jsonPath}.sh`;
44
34
  const exportStr = objectToShellExport(json, prefix);
45
- fs2_1.fs2.writeFile(shPath, exportStr);
35
+ fs2.writeFile(shPath, exportStr);
46
36
  if (!silent) {
47
- console.log(`json2env created ${(0, colors_1.dimGrey)(shPath)}:`);
37
+ console.log(`json2env created ${dimGrey(shPath)}:`);
48
38
  console.log(exportStr);
49
39
  }
50
40
  }
@@ -56,38 +46,38 @@ function json2env(opt) {
56
46
  appendToGithubOutput(json, prefix);
57
47
  }
58
48
  }
59
- function appendToBashEnv(obj, prefix = '') {
49
+ export function appendToBashEnv(obj, prefix = '') {
60
50
  const { BASH_ENV } = process.env;
61
51
  if (BASH_ENV) {
62
52
  const data = objectToShellExport(obj, prefix);
63
- node_fs_1.default.appendFileSync(BASH_ENV, data);
53
+ fs.appendFileSync(BASH_ENV, data);
64
54
  console.log(`BASH_ENV appended:\n${data}`);
65
55
  }
66
56
  }
67
- function appendToGithubEnv(obj, prefix = '') {
57
+ export function appendToGithubEnv(obj, prefix = '') {
68
58
  const { GITHUB_ENV } = process.env;
69
59
  if (GITHUB_ENV) {
70
60
  const data = objectToGithubActionsEnv(obj, prefix);
71
- node_fs_1.default.appendFileSync(GITHUB_ENV, data);
61
+ fs.appendFileSync(GITHUB_ENV, data);
72
62
  console.log(`GITHUB_ENV appended:\n${data}`);
73
63
  }
74
64
  }
75
- function appendToGithubOutput(obj, prefix = '') {
65
+ export function appendToGithubOutput(obj, prefix = '') {
76
66
  const { GITHUB_OUTPUT } = process.env;
77
67
  if (GITHUB_OUTPUT) {
78
68
  const data = objectToGithubActionsEnv(obj, prefix);
79
- node_fs_1.default.appendFileSync(GITHUB_OUTPUT, data);
69
+ fs.appendFileSync(GITHUB_OUTPUT, data);
80
70
  console.log(`GITHUB_OUTPUT appended:\n${data}`);
81
71
  }
82
72
  }
83
73
  /**
84
74
  * https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#adding-a-job-summary
85
75
  */
86
- function appendToGithubSummary(...lines) {
76
+ export function appendToGithubSummary(...lines) {
87
77
  const { GITHUB_STEP_SUMMARY } = process.env;
88
78
  if (GITHUB_STEP_SUMMARY) {
89
79
  const str = lines.join('\n') + '\n';
90
- node_fs_1.default.appendFileSync(GITHUB_STEP_SUMMARY, str);
80
+ fs.appendFileSync(GITHUB_STEP_SUMMARY, str);
91
81
  console.log(`GITHUB_STEP_SUMMARY appended:\n${str}`);
92
82
  }
93
83
  }
@@ -105,7 +95,7 @@ function appendToGithubSummary(...lines) {
105
95
  * Quotes are important, otherwise it'll break on e.g space character in the value.
106
96
  * Includes trailing newline for composability.
107
97
  */
108
- function objectToShellExport(obj, prefix = '') {
98
+ export function objectToShellExport(obj, prefix = '') {
109
99
  if (!Object.keys(obj).length)
110
100
  return '';
111
101
  return (Object.entries(obj)
@@ -133,7 +123,7 @@ function objectToShellExport(obj, prefix = '') {
133
123
  *
134
124
  * UPD: Quoted values behave inconsistently, so we're trying to NOT quote now, and-see-what-happens.
135
125
  */
136
- function objectToGithubActionsEnv(obj, prefix = '') {
126
+ export function objectToGithubActionsEnv(obj, prefix = '') {
137
127
  if (!Object.keys(obj).length)
138
128
  return '';
139
129
  return (Object.entries(obj)
package/dist/fs/kpy.js CHANGED
@@ -1,64 +1,59 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.kpy = kpy;
4
- exports.kpySync = kpySync;
5
- const tslib_1 = require("tslib");
6
- const node_path_1 = tslib_1.__importDefault(require("node:path"));
7
- const js_lib_1 = require("@naturalcycles/js-lib");
8
- const colors_1 = require("../colors/colors");
9
- const index_1 = require("../index");
10
- async function kpy(opt) {
11
- const started = js_lib_1.localTime.nowUnixMillis();
1
+ import path from 'node:path';
2
+ import { _since, localTime } from '@naturalcycles/js-lib';
3
+ import { boldWhite, dimGrey, grey, yellow } from '../colors/colors.js';
4
+ import { fastGlob, fs2 } from '../index.js';
5
+ export async function kpy(opt) {
6
+ const started = localTime.nowUnixMillis();
12
7
  kpyPrepare(opt);
13
- const filenames = await (0, index_1.fastGlob)(opt.inputPatterns, {
8
+ const filenames = await fastGlob(opt.inputPatterns, {
14
9
  cwd: opt.baseDir,
15
10
  dot: opt.dotfiles,
16
11
  });
17
12
  kpyLogFilenames(opt, filenames);
18
13
  const overwrite = !opt.noOverwrite;
19
14
  await Promise.all(filenames.map(async (filename) => {
20
- const basename = node_path_1.default.basename(filename);
21
- const srcFilename = node_path_1.default.resolve(opt.baseDir, filename);
22
- const destFilename = node_path_1.default.resolve(opt.outputDir, opt.flat ? basename : filename);
15
+ const basename = path.basename(filename);
16
+ const srcFilename = path.resolve(opt.baseDir, filename);
17
+ const destFilename = path.resolve(opt.outputDir, opt.flat ? basename : filename);
23
18
  if (!opt.dry) {
24
19
  if (opt.move) {
25
- await index_1.fs2.movePathAsync(srcFilename, destFilename, {
20
+ await fs2.movePathAsync(srcFilename, destFilename, {
26
21
  force: overwrite,
27
22
  });
28
23
  }
29
24
  else {
30
- await index_1.fs2.copyPathAsync(srcFilename, destFilename, { force: overwrite });
25
+ await fs2.copyPathAsync(srcFilename, destFilename, { force: overwrite });
31
26
  }
32
27
  }
33
28
  if (opt.verbose) {
34
- console.log((0, colors_1.grey)(` ${filename}`));
29
+ console.log(grey(` ${filename}`));
35
30
  }
36
31
  }));
37
32
  kpyLogResult(opt, filenames, started);
38
33
  }
39
- function kpySync(opt) {
40
- const started = js_lib_1.localTime.nowUnixMillis();
34
+ export function kpySync(opt) {
35
+ const started = localTime.nowUnixMillis();
41
36
  kpyPrepare(opt);
42
- const filenames = index_1.fastGlob.sync(opt.inputPatterns, {
37
+ const filenames = fastGlob.sync(opt.inputPatterns, {
43
38
  cwd: opt.baseDir,
44
39
  dot: opt.dotfiles,
45
40
  });
46
41
  kpyLogFilenames(opt, filenames);
47
42
  const overwrite = !opt.noOverwrite;
48
43
  filenames.forEach(filename => {
49
- const basename = node_path_1.default.basename(filename);
50
- const srcFilename = node_path_1.default.resolve(opt.baseDir, filename);
51
- const destFilename = node_path_1.default.resolve(opt.outputDir, opt.flat ? basename : filename);
44
+ const basename = path.basename(filename);
45
+ const srcFilename = path.resolve(opt.baseDir, filename);
46
+ const destFilename = path.resolve(opt.outputDir, opt.flat ? basename : filename);
52
47
  if (!opt.dry) {
53
48
  if (opt.move) {
54
- index_1.fs2.movePath(srcFilename, destFilename, { force: overwrite });
49
+ fs2.movePath(srcFilename, destFilename, { force: overwrite });
55
50
  }
56
51
  else {
57
- index_1.fs2.copyPath(srcFilename, destFilename, { force: overwrite });
52
+ fs2.copyPath(srcFilename, destFilename, { force: overwrite });
58
53
  }
59
54
  }
60
55
  if (opt.verbose) {
61
- console.log((0, colors_1.grey)(` ${filename}`));
56
+ console.log(grey(` ${filename}`));
62
57
  }
63
58
  });
64
59
  kpyLogResult(opt, filenames, started);
@@ -70,17 +65,17 @@ function kpyPrepare(opt) {
70
65
  // default to cwd
71
66
  opt.baseDir ||= '.';
72
67
  opt.outputDir ||= '.';
73
- if (!index_1.fs2.pathExists(opt.baseDir)) {
74
- console.log(`kpy: baseDir doesn't exist: ${(0, colors_1.boldWhite)(opt.baseDir)}`);
68
+ if (!fs2.pathExists(opt.baseDir)) {
69
+ console.log(`kpy: baseDir doesn't exist: ${boldWhite(opt.baseDir)}`);
75
70
  return;
76
71
  }
77
- index_1.fs2.ensureDir(opt.outputDir);
72
+ fs2.ensureDir(opt.outputDir);
78
73
  // Expand directories (ex-globby feature), experimental!
79
74
  const extraPatterns = [];
80
75
  for (const pattern of opt.inputPatterns) {
81
76
  if (pattern.includes('*'))
82
77
  continue;
83
- if (index_1.fs2.isDirectory(node_path_1.default.resolve(opt.baseDir, pattern))) {
78
+ if (fs2.isDirectory(path.resolve(opt.baseDir, pattern))) {
84
79
  extraPatterns.push(`${pattern}/**`);
85
80
  }
86
81
  }
@@ -93,10 +88,10 @@ function kpyLogFilenames(opt, filenames) {
93
88
  if (opt.silent)
94
89
  return;
95
90
  // console.log({filenames})
96
- console.log(`Will ${opt.move ? 'move' : 'copy'} ${(0, colors_1.yellow)(filenames.length)} files from ${(0, colors_1.dimGrey)(opt.baseDir)} to ${(0, colors_1.dimGrey)(opt.outputDir)} (${(0, colors_1.dimGrey)(opt.inputPatterns.join(' '))})`);
91
+ console.log(`Will ${opt.move ? 'move' : 'copy'} ${yellow(filenames.length)} files from ${dimGrey(opt.baseDir)} to ${dimGrey(opt.outputDir)} (${dimGrey(opt.inputPatterns.join(' '))})`);
97
92
  }
98
93
  function kpyLogResult(opt, filenames, started) {
99
94
  if (opt.silent || filenames.length === 0)
100
95
  return;
101
- console.log(`${opt.move ? 'Moved' : 'Copied'} ${(0, colors_1.yellow)(filenames.length)} files to ${(0, colors_1.dimGrey)(opt.outputDir)} ${(0, colors_1.dimGrey)((0, js_lib_1._since)(started))}`);
96
+ console.log(`${opt.move ? 'Moved' : 'Copied'} ${yellow(filenames.length)} files to ${dimGrey(opt.outputDir)} ${dimGrey(_since(started))}`);
102
97
  }