@naturalcycles/nodejs-lib 13.4.0 → 13.6.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.
- package/dist/fs/del.js +8 -13
- package/dist/fs/fs2.d.ts +79 -0
- package/dist/fs/fs2.js +267 -0
- package/dist/fs/json2env.js +4 -4
- package/dist/fs/kpy.js +6 -6
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/secret/secrets-decrypt.util.js +2 -2
- package/dist/secret/secrets-encrypt.util.js +2 -2
- package/dist/stream/ndjson/pipelineToNDJsonFile.js +2 -2
- package/dist/util/buildInfo.util.js +3 -3
- package/dist/validation/ajv/ajv.util.js +1 -1
- package/dist/validation/ajv/ajvSchema.js +1 -1
- package/package.json +3 -1
- package/src/fs/del.ts +9 -20
- package/src/fs/fs2.ts +303 -0
- package/src/fs/json2env.ts +4 -4
- package/src/fs/kpy.ts +7 -15
- package/src/index.ts +1 -1
- package/src/secret/secrets-decrypt.util.ts +3 -3
- package/src/secret/secrets-encrypt.util.ts +3 -3
- package/src/slack/slack.service.ts +1 -1
- package/src/stream/ndjson/pipelineToNDJsonFile.ts +3 -3
- package/src/util/buildInfo.util.ts +3 -3
- package/src/validation/ajv/ajv.util.ts +2 -2
- package/src/validation/ajv/ajvSchema.ts +2 -2
- package/dist/fs/fs.util.d.ts +0 -182
- package/dist/fs/fs.util.js +0 -285
- package/src/fs/fs.util.ts +0 -285
package/src/fs/fs.util.ts
DELETED
|
@@ -1,285 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
|
|
3
|
-
Why?
|
|
4
|
-
|
|
5
|
-
Convenience re-export/re-implementation of most common fs functions from
|
|
6
|
-
node:fs, node:fs/promises and fs-extra
|
|
7
|
-
|
|
8
|
-
Defaults to string input/output, as it's used in 80% of time. For the rest - you can use native fs.
|
|
9
|
-
|
|
10
|
-
Allows to import it easier, so you don't have to choose between the 3 import locations.
|
|
11
|
-
That's why function names are slightly renamed, to avoid conflict.
|
|
12
|
-
|
|
13
|
-
Credit to: fs-extra (https://github.com/jprichardson/node-fs-extra)
|
|
14
|
-
|
|
15
|
-
*/
|
|
16
|
-
|
|
17
|
-
import fs from 'node:fs'
|
|
18
|
-
import fsp from 'node:fs/promises'
|
|
19
|
-
import path from 'node:path'
|
|
20
|
-
import { _jsonParse } from '@naturalcycles/js-lib'
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* fs2 conveniently groups filesystem functions together.
|
|
24
|
-
* Supposed to be almost a drop-in replacement for these things together:
|
|
25
|
-
*
|
|
26
|
-
* 1. node:fs
|
|
27
|
-
* 2. node:fs/promises
|
|
28
|
-
* 3. fs-extra
|
|
29
|
-
*/
|
|
30
|
-
export const fs2 = {
|
|
31
|
-
// "Omit" is here to workaround this TS error:
|
|
32
|
-
// Exported variable 'fs2' has or is using name 'StreamOptions' from external module "fs" but cannot be named.
|
|
33
|
-
...(fs as Omit<typeof fs, 'StreamOptions'>),
|
|
34
|
-
readFile: _readFile,
|
|
35
|
-
readFileSync: _readFileSync,
|
|
36
|
-
readFileAsBuffer: _readFileAsBuffer,
|
|
37
|
-
readFileAsBufferSync: _readFileAsBufferSync,
|
|
38
|
-
readJson: _readJson,
|
|
39
|
-
readJsonSync: _readJsonSync,
|
|
40
|
-
writeFile: _writeFile,
|
|
41
|
-
writeFileSync: _writeFileSync,
|
|
42
|
-
outputJson: _outputJson,
|
|
43
|
-
outputJsonSync: _outputJsonSync,
|
|
44
|
-
writeJson: _writeJson,
|
|
45
|
-
writeJsonSync: _writeJsonSync,
|
|
46
|
-
outputFile: _outputFile,
|
|
47
|
-
outputFileSync: _outputFileSync,
|
|
48
|
-
pathExists: _pathExists,
|
|
49
|
-
pathExistsSync: _pathExistsSync,
|
|
50
|
-
ensureDir: _ensureDir,
|
|
51
|
-
ensureDirSync: _ensureDirSync,
|
|
52
|
-
ensureFile: _ensureFile,
|
|
53
|
-
ensureFileSync: _ensureFileSync,
|
|
54
|
-
removePath: _removePath,
|
|
55
|
-
removePathSync: _removePathSync,
|
|
56
|
-
emptyDir: _emptyDir,
|
|
57
|
-
emptyDirSync: _emptyDirSync,
|
|
58
|
-
copyPath: _copyPath,
|
|
59
|
-
copyPathSync: _copyPathSync,
|
|
60
|
-
movePath: _movePath,
|
|
61
|
-
movePathSync: _movePathSync,
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export interface JsonOptions {
|
|
65
|
-
spaces?: number
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Convenience wrapper that defaults to utf-8 string output.
|
|
70
|
-
*/
|
|
71
|
-
export async function _readFile(filePath: string): Promise<string> {
|
|
72
|
-
return await fsp.readFile(filePath, 'utf8')
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export async function _readFileAsBuffer(filePath: string): Promise<Buffer> {
|
|
76
|
-
return await fsp.readFile(filePath)
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Convenience wrapper that defaults to utf-8 string output.
|
|
81
|
-
*/
|
|
82
|
-
export function _readFileSync(filePath: string): string {
|
|
83
|
-
return fs.readFileSync(filePath, 'utf8')
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Convenience wrapper that defaults to utf-8 string output.
|
|
88
|
-
*/
|
|
89
|
-
export function _readFileAsBufferSync(filePath: string): Buffer {
|
|
90
|
-
return fs.readFileSync(filePath)
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export async function _readJson<T = unknown>(filePath: string): Promise<T> {
|
|
94
|
-
const str = await fsp.readFile(filePath, 'utf8')
|
|
95
|
-
return _jsonParse(str)
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
export function _readJsonSync<T = unknown>(filePath: string): T {
|
|
99
|
-
const str = fs.readFileSync(filePath, 'utf8')
|
|
100
|
-
return _jsonParse(str)
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export async function _writeFile(filePath: string, data: string | Buffer): Promise<void> {
|
|
104
|
-
await fsp.writeFile(filePath, data)
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export function _writeFileSync(filePath: string, data: string | Buffer): void {
|
|
108
|
-
fs.writeFileSync(filePath, data)
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export async function _outputFile(filePath: string, data: string | Buffer): Promise<void> {
|
|
112
|
-
const dirPath = path.dirname(filePath)
|
|
113
|
-
if (!(await _pathExists(dirPath))) {
|
|
114
|
-
await _ensureDir(dirPath)
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
await fsp.writeFile(filePath, data)
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export function _outputFileSync(filePath: string, data: string | Buffer): void {
|
|
121
|
-
const dirPath = path.dirname(filePath)
|
|
122
|
-
if (!fs.existsSync(dirPath)) {
|
|
123
|
-
_ensureDirSync(dirPath)
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
fs.writeFileSync(filePath, data)
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function stringify(data: any, opt?: JsonOptions): string {
|
|
130
|
-
// If pretty-printing is enabled (spaces) - also add a newline at the end (to match our prettier config)
|
|
131
|
-
return JSON.stringify(data, null, opt?.spaces) + (opt?.spaces ? '\n' : '')
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export async function _writeJson(filePath: string, data: any, opt?: JsonOptions): Promise<void> {
|
|
135
|
-
const str = stringify(data, opt)
|
|
136
|
-
await fsp.writeFile(filePath, str)
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
export function _writeJsonSync(filePath: string, data: any, opt?: JsonOptions): void {
|
|
140
|
-
const str = stringify(data, opt)
|
|
141
|
-
fs.writeFileSync(filePath, str)
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
export async function _outputJson(filePath: string, data: any, opt?: JsonOptions): Promise<void> {
|
|
145
|
-
const str = stringify(data, opt)
|
|
146
|
-
await _outputFile(filePath, str)
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
export function _outputJsonSync(filePath: string, data: any, opt?: JsonOptions): void {
|
|
150
|
-
const str = stringify(data, opt)
|
|
151
|
-
_outputFileSync(filePath, str)
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
export async function _pathExists(filePath: string): Promise<boolean> {
|
|
155
|
-
try {
|
|
156
|
-
await fsp.access(filePath)
|
|
157
|
-
return true
|
|
158
|
-
} catch {
|
|
159
|
-
return false
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
export function _pathExistsSync(filePath: string): boolean {
|
|
164
|
-
return fs.existsSync(filePath)
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
export async function _ensureDir(dirPath: string): Promise<void> {
|
|
168
|
-
await fsp.mkdir(dirPath, {
|
|
169
|
-
mode: 0o777,
|
|
170
|
-
recursive: true,
|
|
171
|
-
})
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
export function _ensureDirSync(dirPath: string): void {
|
|
175
|
-
fs.mkdirSync(dirPath, {
|
|
176
|
-
mode: 0o777,
|
|
177
|
-
recursive: true,
|
|
178
|
-
})
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
export async function _ensureFile(filePath: string): Promise<void> {
|
|
182
|
-
let stats
|
|
183
|
-
try {
|
|
184
|
-
stats = await fsp.stat(filePath)
|
|
185
|
-
} catch {}
|
|
186
|
-
if (stats?.isFile()) return
|
|
187
|
-
|
|
188
|
-
const dir = path.dirname(filePath)
|
|
189
|
-
try {
|
|
190
|
-
if (!(await fsp.stat(dir)).isDirectory()) {
|
|
191
|
-
// parent is not a directory
|
|
192
|
-
// This is just to cause an internal ENOTDIR error to be thrown
|
|
193
|
-
await fsp.readdir(dir)
|
|
194
|
-
}
|
|
195
|
-
} catch (err) {
|
|
196
|
-
// If the stat call above failed because the directory doesn't exist, create it
|
|
197
|
-
if ((err as any)?.code === 'ENOENT') return await _ensureDir(dir)
|
|
198
|
-
throw err
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
await fsp.writeFile(filePath, '')
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
export function _ensureFileSync(filePath: string): void {
|
|
205
|
-
let stats
|
|
206
|
-
try {
|
|
207
|
-
stats = fs.statSync(filePath)
|
|
208
|
-
} catch {}
|
|
209
|
-
if (stats?.isFile()) return
|
|
210
|
-
|
|
211
|
-
const dir = path.dirname(filePath)
|
|
212
|
-
try {
|
|
213
|
-
if (!fs.statSync(dir).isDirectory()) {
|
|
214
|
-
// parent is not a directory
|
|
215
|
-
// This is just to cause an internal ENOTDIR error to be thrown
|
|
216
|
-
fs.readdirSync(dir)
|
|
217
|
-
}
|
|
218
|
-
} catch (err) {
|
|
219
|
-
// If the stat call above failed because the directory doesn't exist, create it
|
|
220
|
-
if ((err as any)?.code === 'ENOENT') return _ensureDirSync(dir)
|
|
221
|
-
throw err
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
fs.writeFileSync(filePath, '')
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
export async function _removePath(fileOrDirPath: string): Promise<void> {
|
|
228
|
-
await fsp.rm(fileOrDirPath, { recursive: true, force: true })
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
export function _removePathSync(fileOrDirPath: string): void {
|
|
232
|
-
fs.rmSync(fileOrDirPath, { recursive: true, force: true })
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
export async function _emptyDir(dirPath: string): Promise<void> {
|
|
236
|
-
let items
|
|
237
|
-
try {
|
|
238
|
-
items = await fsp.readdir(dirPath)
|
|
239
|
-
} catch {
|
|
240
|
-
return await _ensureDir(dirPath)
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
await Promise.all(items.map(item => _removePath(path.join(dirPath, item))))
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
export function _emptyDirSync(dirPath: string): void {
|
|
247
|
-
let items
|
|
248
|
-
try {
|
|
249
|
-
items = fs.readdirSync(dirPath)
|
|
250
|
-
} catch {
|
|
251
|
-
return _ensureDirSync(dirPath)
|
|
252
|
-
}
|
|
253
|
-
|
|
254
|
-
items.forEach(item => _removePathSync(path.join(dirPath, item)))
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
/**
|
|
258
|
-
* Cautious, underlying Node function is currently Experimental.
|
|
259
|
-
*/
|
|
260
|
-
export async function _copyPath(src: string, dest: string, opt?: fs.CopyOptions): Promise<void> {
|
|
261
|
-
await fsp.cp(src, dest, {
|
|
262
|
-
recursive: true,
|
|
263
|
-
...opt,
|
|
264
|
-
})
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Cautious, underlying Node function is currently Experimental.
|
|
269
|
-
*/
|
|
270
|
-
export function _copyPathSync(src: string, dest: string, opt?: fs.CopySyncOptions): void {
|
|
271
|
-
fs.cpSync(src, dest, {
|
|
272
|
-
recursive: true,
|
|
273
|
-
...opt,
|
|
274
|
-
})
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
export async function _movePath(src: string, dest: string, opt?: fs.CopyOptions): Promise<void> {
|
|
278
|
-
await _copyPath(src, dest, opt)
|
|
279
|
-
await _removePath(src)
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
export function _movePathSync(src: string, dest: string, opt?: fs.CopySyncOptions): void {
|
|
283
|
-
_copyPathSync(src, dest, opt)
|
|
284
|
-
_removePathSync(src)
|
|
285
|
-
}
|