@brianbuie/node-kit 0.15.1 → 0.16.1
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/README.md +321 -210
- package/dist/index.d.mts +64 -520
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +202 -150
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -6
- package/src/Cache.ts +4 -3
- package/src/Cmd.ts +65 -0
- package/src/Dir.ts +19 -19
- package/src/Fetcher.ts +2 -2
- package/src/File.ts +117 -57
- package/src/Format.ts +5 -5
- package/src/Log.test.ts +16 -6
- package/src/Log.ts +58 -98
- package/src/TypeWriter.ts +15 -7
- package/src/snapshot.ts +3 -2
- package/src/timeout.ts +1 -1
package/README.md
CHANGED
|
@@ -14,7 +14,7 @@ npm add @brianbuie/node-kit
|
|
|
14
14
|
import { Fetcher, Log } from '@brianbuie/node-kit';
|
|
15
15
|
```
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
## Extending Config
|
|
18
18
|
|
|
19
19
|
### tsconfig.json
|
|
20
20
|
|
|
@@ -43,6 +43,40 @@ const config = {
|
|
|
43
43
|
export default config;
|
|
44
44
|
```
|
|
45
45
|
|
|
46
|
+
# Changelog
|
|
47
|
+
|
|
48
|
+
## 0.16.1
|
|
49
|
+
|
|
50
|
+
- New `Cmd` utility for running shell commands, handling output and errors
|
|
51
|
+
- `Cmd.ffmpeg` and `Cmd.ffprobe` added with basic config for handling output (ffmpeg and ffprobe need to be installed separately)
|
|
52
|
+
- New `FileTypeImage` and `FileTypeVideo` extensions with methods to get media dimensions
|
|
53
|
+
|
|
54
|
+
## 0.16
|
|
55
|
+
|
|
56
|
+
- `Log` rewritten using [pino](https://github.com/pinojs/pino) under the hood. Will require updates in projects:
|
|
57
|
+
- `message` is still first argument, but second argument should include all details, instead of using an arbitrary number of args
|
|
58
|
+
- Errors should use the `err` key in the details object, instead of passing the error as an argument
|
|
59
|
+
- Dev defaults to `debug` level, prod defaults to `info`, use `LOG_LEVEL=info` env variable
|
|
60
|
+
- Removed `alert` and `notice` levels
|
|
61
|
+
- New `fatal` level above `error`
|
|
62
|
+
- New `trace` level, below `debug`
|
|
63
|
+
- `Dir.txtFiles` renamed from `Dir.textFiles`, for consistency with other file types
|
|
64
|
+
- `JsonFileType` & `NdJsonFileType` no longer user the `snapshot` hack to stringify special objects. Will remove `snapshot` in the future.
|
|
65
|
+
- `FileTypeCsv`
|
|
66
|
+
- Second argument changed from `keys` array to options object (`keys` can be provided as option)
|
|
67
|
+
- Automatic parsing of numbers, booleans, and nulls can be disabled (`parseNumbers`, `parseBooleans`, `parseNulls`)
|
|
68
|
+
- Removed `@types/node` as peer dependency
|
|
69
|
+
- Added explicit return types for everything
|
|
70
|
+
|
|
71
|
+
## 0.15.1
|
|
72
|
+
|
|
73
|
+
- `Dir` uses YYYYMMDD as default name
|
|
74
|
+
- `File` uses YYYYMMDD-HHmmss as default name
|
|
75
|
+
|
|
76
|
+
## 0.15
|
|
77
|
+
|
|
78
|
+
- `TypeWriter` option for `outFile`, defaults to `[moduleName].types.ts`
|
|
79
|
+
|
|
46
80
|
# API
|
|
47
81
|
|
|
48
82
|
<!--#region ts2md-api-merged-here-->
|
|
@@ -51,19 +85,15 @@ Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types
|
|
|
51
85
|
|
|
52
86
|
# Classes
|
|
53
87
|
|
|
54
|
-
| |
|
|
55
|
-
| --- |
|
|
56
|
-
| [Cache](#class-cache) |
|
|
57
|
-
| [
|
|
58
|
-
| [
|
|
59
|
-
| [
|
|
60
|
-
| [
|
|
61
|
-
| [
|
|
62
|
-
| [
|
|
63
|
-
| [FileTypeNdjson](#class-filetypendjson) |
|
|
64
|
-
| [Format](#class-format) |
|
|
65
|
-
| [Log](#class-log) |
|
|
66
|
-
| [TypeWriter](#class-typewriter) |
|
|
88
|
+
| | |
|
|
89
|
+
| --- | --- |
|
|
90
|
+
| [Cache](#class-cache) | [FileTypeImage](#class-filetypeimage) |
|
|
91
|
+
| [Cmd](#class-cmd) | [FileTypeJson](#class-filetypejson) |
|
|
92
|
+
| [Dir](#class-dir) | [FileTypeNdjson](#class-filetypendjson) |
|
|
93
|
+
| [Fetcher](#class-fetcher) | [FileTypeVideo](#class-filetypevideo) |
|
|
94
|
+
| [File](#class-file) | [Format](#class-format) |
|
|
95
|
+
| [FileType](#class-filetype) | [Log](#class-log) |
|
|
96
|
+
| [FileTypeCsv](#class-filetypecsv) | [TypeWriter](#class-typewriter) |
|
|
67
97
|
|
|
68
98
|
Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
|
|
69
99
|
|
|
@@ -77,8 +107,11 @@ so stale data can still be used if needed.
|
|
|
77
107
|
|
|
78
108
|
```ts
|
|
79
109
|
export class Cache<T> {
|
|
80
|
-
file
|
|
81
|
-
|
|
110
|
+
file: FileTypeJson<{
|
|
111
|
+
savedAt: string;
|
|
112
|
+
data: T;
|
|
113
|
+
}>;
|
|
114
|
+
ttl: Duration;
|
|
82
115
|
constructor(key: string, ttl: number | Duration, initialData?: T)
|
|
83
116
|
write(data: T)
|
|
84
117
|
read(): [
|
|
@@ -88,6 +121,64 @@ export class Cache<T> {
|
|
|
88
121
|
}
|
|
89
122
|
```
|
|
90
123
|
|
|
124
|
+
See also: [FileTypeJson](#class-filetypejson)
|
|
125
|
+
|
|
126
|
+
Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
|
|
127
|
+
|
|
128
|
+
---
|
|
129
|
+
## Class: Cmd
|
|
130
|
+
|
|
131
|
+
Spawn a child process for shell commands with `Cmd.run('command arg1=something arg2=2...")`
|
|
132
|
+
|
|
133
|
+
```ts
|
|
134
|
+
export class Cmd {
|
|
135
|
+
static bin = "/usr/local/bin";
|
|
136
|
+
static configure({ bin }: {
|
|
137
|
+
bin: string;
|
|
138
|
+
}): void
|
|
139
|
+
static args(args: Args): string[]
|
|
140
|
+
static async run(cmd: string, args: Args, opts?: SpawnOptionsWithoutStdio): Promise<string>
|
|
141
|
+
static async ffmpeg(args: Args)
|
|
142
|
+
static async ffprobe(args: Args)
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
See also: [Args](#type-args)
|
|
147
|
+
|
|
148
|
+
<details>
|
|
149
|
+
|
|
150
|
+
<summary>Class Cmd Details</summary>
|
|
151
|
+
|
|
152
|
+
### Method configure
|
|
153
|
+
|
|
154
|
+
configure global location of bin files
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
static configure({ bin }: {
|
|
158
|
+
bin: string;
|
|
159
|
+
}): void
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Method ffmpeg
|
|
163
|
+
|
|
164
|
+
Run ffmpeg for video processing (ffmpeg needs to be installed separately)
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
static async ffmpeg(args: Args)
|
|
168
|
+
```
|
|
169
|
+
See also: [Args](#type-args)
|
|
170
|
+
|
|
171
|
+
### Method ffprobe
|
|
172
|
+
|
|
173
|
+
Use ffprobe to get video stream dimensions (ffprobe needs to be installed separately)
|
|
174
|
+
|
|
175
|
+
```ts
|
|
176
|
+
static async ffprobe(args: Args)
|
|
177
|
+
```
|
|
178
|
+
See also: [Args](#type-args)
|
|
179
|
+
|
|
180
|
+
</details>
|
|
181
|
+
|
|
91
182
|
Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
|
|
92
183
|
|
|
93
184
|
---
|
|
@@ -97,28 +188,28 @@ Reference to a specific directory with methods to create and list files.
|
|
|
97
188
|
|
|
98
189
|
```ts
|
|
99
190
|
export class Dir {
|
|
100
|
-
#inputPath;
|
|
191
|
+
#inputPath: string;
|
|
101
192
|
#resolved?: string;
|
|
102
|
-
isTemp;
|
|
193
|
+
isTemp: boolean;
|
|
103
194
|
constructor(inputPath = Format.date("ymd"), options: DirOptions = {})
|
|
104
|
-
get pathUnsafe()
|
|
105
|
-
get path()
|
|
106
|
-
get name()
|
|
107
|
-
dir(subPath = Format.date("ymd"), options: DirOptions = { temp: this.isTemp })
|
|
108
|
-
tempDir(subPath?: string)
|
|
109
|
-
sanitize(filename: string)
|
|
110
|
-
filepath(base: string)
|
|
111
|
-
file(base = Format.date("ymd-hms"))
|
|
195
|
+
get pathUnsafe(): string
|
|
196
|
+
get path(): string
|
|
197
|
+
get name(): string
|
|
198
|
+
dir(subPath = Format.date("ymd"), options: DirOptions = { temp: this.isTemp }): Dir
|
|
199
|
+
tempDir(subPath?: string): Dir
|
|
200
|
+
sanitize(filename: string): string
|
|
201
|
+
filepath(base: string): string
|
|
202
|
+
file(base = Format.date("ymd-hms")): File
|
|
112
203
|
get contents(): (Dir | File)[]
|
|
113
|
-
get dirs()
|
|
114
|
-
get files()
|
|
115
|
-
get videos()
|
|
116
|
-
get images()
|
|
117
|
-
get jsonFiles()
|
|
118
|
-
get ndjsonFiles()
|
|
119
|
-
get csvFiles()
|
|
120
|
-
get
|
|
121
|
-
clear()
|
|
204
|
+
get dirs(): Dir[]
|
|
205
|
+
get files(): File[]
|
|
206
|
+
get videos(): File[]
|
|
207
|
+
get images(): File[]
|
|
208
|
+
get jsonFiles(): File[]
|
|
209
|
+
get ndjsonFiles(): File[]
|
|
210
|
+
get csvFiles(): File[]
|
|
211
|
+
get txtFiles(): File[]
|
|
212
|
+
clear(): void
|
|
122
213
|
}
|
|
123
214
|
```
|
|
124
215
|
|
|
@@ -145,7 +236,7 @@ Argument Details
|
|
|
145
236
|
Deletes the contents of the directory. Only allowed if created with `temp` option set to `true` (or created with `dir.tempDir` method).
|
|
146
237
|
|
|
147
238
|
```ts
|
|
148
|
-
clear()
|
|
239
|
+
clear(): void
|
|
149
240
|
```
|
|
150
241
|
|
|
151
242
|
### Method dir
|
|
@@ -153,9 +244,9 @@ clear()
|
|
|
153
244
|
Create a new Dir inside the current Dir
|
|
154
245
|
|
|
155
246
|
```ts
|
|
156
|
-
dir(subPath = Format.date("ymd"), options: DirOptions = { temp: this.isTemp })
|
|
247
|
+
dir(subPath = Format.date("ymd"), options: DirOptions = { temp: this.isTemp }): Dir
|
|
157
248
|
```
|
|
158
|
-
See also: [DirOptions](#type-diroptions), [Format](#class-format), [temp](#variable-temp)
|
|
249
|
+
See also: [Dir](#class-dir), [DirOptions](#type-diroptions), [Format](#class-format), [temp](#variable-temp)
|
|
159
250
|
|
|
160
251
|
Argument Details
|
|
161
252
|
|
|
@@ -178,14 +269,14 @@ const child = folder.dir('path/to/dir');
|
|
|
178
269
|
Create a new file in this directory. Filename defaults to `YYYYMMDD-HHMMSS` if not provided
|
|
179
270
|
|
|
180
271
|
```ts
|
|
181
|
-
file(base = Format.date("ymd-hms"))
|
|
272
|
+
file(base = Format.date("ymd-hms")): File
|
|
182
273
|
```
|
|
183
|
-
See also: [Format](#class-format)
|
|
274
|
+
See also: [File](#class-file), [Format](#class-format)
|
|
184
275
|
|
|
185
276
|
### Method filepath
|
|
186
277
|
|
|
187
278
|
```ts
|
|
188
|
-
filepath(base: string)
|
|
279
|
+
filepath(base: string): string
|
|
189
280
|
```
|
|
190
281
|
|
|
191
282
|
Argument Details
|
|
@@ -206,8 +297,9 @@ const filepath = folder.resolve('file.json');
|
|
|
206
297
|
Creates a new temp directory inside current Dir
|
|
207
298
|
|
|
208
299
|
```ts
|
|
209
|
-
tempDir(subPath?: string)
|
|
300
|
+
tempDir(subPath?: string): Dir
|
|
210
301
|
```
|
|
302
|
+
See also: [Dir](#class-dir)
|
|
211
303
|
|
|
212
304
|
Argument Details
|
|
213
305
|
|
|
@@ -227,13 +319,13 @@ Includes basic methods for requesting and parsing responses
|
|
|
227
319
|
|
|
228
320
|
```ts
|
|
229
321
|
export class Fetcher {
|
|
230
|
-
defaultOptions;
|
|
322
|
+
defaultOptions: FetchOptions;
|
|
231
323
|
constructor(opts: FetchOptions = {})
|
|
232
324
|
buildUrl(route: Route, opts: FetchOptions = {}): [
|
|
233
325
|
URL,
|
|
234
326
|
string
|
|
235
327
|
]
|
|
236
|
-
buildHeaders(route: Route, opts: FetchOptions = {})
|
|
328
|
+
buildHeaders(route: Route, opts: FetchOptions = {}): HeadersInit & Record<string, string>
|
|
237
329
|
buildRequest(route: Route, opts: FetchOptions = {}): [
|
|
238
330
|
Request,
|
|
239
331
|
FetchOptions,
|
|
@@ -267,7 +359,7 @@ See also: [FetchOptions](#type-fetchoptions), [Route](#type-route)
|
|
|
267
359
|
Merges options to get headers. Useful when extending the Fetcher class to add custom auth.
|
|
268
360
|
|
|
269
361
|
```ts
|
|
270
|
-
buildHeaders(route: Route, opts: FetchOptions = {})
|
|
362
|
+
buildHeaders(route: Route, opts: FetchOptions = {}): HeadersInit & Record<string, string>
|
|
271
363
|
```
|
|
272
364
|
See also: [FetchOptions](#type-fetchoptions), [Route](#type-route)
|
|
273
365
|
|
|
@@ -327,33 +419,39 @@ Represents a file on the file system. If the file doesn't exist, it is created t
|
|
|
327
419
|
|
|
328
420
|
```ts
|
|
329
421
|
export class File {
|
|
330
|
-
path;
|
|
331
|
-
root;
|
|
332
|
-
dir;
|
|
333
|
-
base;
|
|
334
|
-
name;
|
|
335
|
-
ext;
|
|
336
|
-
type;
|
|
422
|
+
path: string;
|
|
423
|
+
root: string;
|
|
424
|
+
dir: string;
|
|
425
|
+
base: string;
|
|
426
|
+
name: string;
|
|
427
|
+
ext: string;
|
|
428
|
+
type?: string;
|
|
337
429
|
constructor(filepath: string)
|
|
338
|
-
#resolve(filepath: string)
|
|
339
|
-
get exists()
|
|
430
|
+
#resolve(filepath: string): string
|
|
431
|
+
get exists(): boolean
|
|
340
432
|
get stats(): Partial<fs.Stats>
|
|
341
|
-
delete()
|
|
342
|
-
read()
|
|
343
|
-
lines()
|
|
344
|
-
get readStream()
|
|
345
|
-
get writeStream()
|
|
346
|
-
write(contents: string | ReadableStream)
|
|
347
|
-
append(lines: string | string[])
|
|
348
|
-
json<T>(contents?: T)
|
|
349
|
-
static get json()
|
|
350
|
-
ndjson<T extends object>(lines?: T | T[])
|
|
351
|
-
static get ndjson()
|
|
352
|
-
async csv<T extends object>(rows?: T[],
|
|
353
|
-
static get csv()
|
|
433
|
+
delete(): void
|
|
434
|
+
read(): string | undefined
|
|
435
|
+
lines(): string[]
|
|
436
|
+
get readStream(): fs.ReadStream | Readable
|
|
437
|
+
get writeStream(): fs.WriteStream
|
|
438
|
+
write(contents: string | ReadableStream): void | Promise<void>
|
|
439
|
+
append(lines: string | string[]): void
|
|
440
|
+
json<T>(contents?: T): FileTypeJson<T>
|
|
441
|
+
static get json(): typeof FileTypeJson
|
|
442
|
+
ndjson<T extends object>(lines?: T | T[]): FileTypeNdjson<T>
|
|
443
|
+
static get ndjson(): typeof FileTypeNdjson
|
|
444
|
+
async csv<T extends object>(rows?: T[], options?: FileTypeCsvOptions<T>): Promise<FileTypeCsv<T>>
|
|
445
|
+
static get csv(): typeof FileTypeCsv
|
|
446
|
+
async image(content?: ReadableStream)
|
|
447
|
+
static get image(): typeof FileTypeImage
|
|
448
|
+
async video(content?: ReadableStream)
|
|
449
|
+
static get video(): typeof FileTypeVideo
|
|
354
450
|
}
|
|
355
451
|
```
|
|
356
452
|
|
|
453
|
+
See also: [FileTypeCsv](#class-filetypecsv), [FileTypeImage](#class-filetypeimage), [FileTypeJson](#class-filetypejson), [FileTypeNdjson](#class-filetypendjson), [FileTypeVideo](#class-filetypevideo)
|
|
454
|
+
|
|
357
455
|
<details>
|
|
358
456
|
|
|
359
457
|
<summary>Class File Details</summary>
|
|
@@ -364,14 +462,15 @@ creates file if it doesn't exist, appends string or array of strings as new line
|
|
|
364
462
|
File always ends with '\n', so contents don't need to be read before appending
|
|
365
463
|
|
|
366
464
|
```ts
|
|
367
|
-
append(lines: string | string[])
|
|
465
|
+
append(lines: string | string[]): void
|
|
368
466
|
```
|
|
369
467
|
|
|
370
468
|
### Method csv
|
|
371
469
|
|
|
372
470
|
```ts
|
|
373
|
-
async csv<T extends object>(rows?: T[],
|
|
471
|
+
async csv<T extends object>(rows?: T[], options?: FileTypeCsvOptions<T>): Promise<FileTypeCsv<T>>
|
|
374
472
|
```
|
|
473
|
+
See also: [FileTypeCsv](#class-filetypecsv)
|
|
375
474
|
|
|
376
475
|
Returns
|
|
377
476
|
|
|
@@ -391,14 +490,15 @@ await file.write([{ col: 'val2' }, { col: 'val3' }]); // ✅ Writes multiple row
|
|
|
391
490
|
Deletes the file if it exists
|
|
392
491
|
|
|
393
492
|
```ts
|
|
394
|
-
delete()
|
|
493
|
+
delete(): void
|
|
395
494
|
```
|
|
396
495
|
|
|
397
496
|
### Method json
|
|
398
497
|
|
|
399
498
|
```ts
|
|
400
|
-
json<T>(contents?: T)
|
|
499
|
+
json<T>(contents?: T): FileTypeJson<T>
|
|
401
500
|
```
|
|
501
|
+
See also: [FileTypeJson](#class-filetypejson)
|
|
402
502
|
|
|
403
503
|
Returns
|
|
404
504
|
|
|
@@ -420,7 +520,7 @@ file.write({ something: 'else' }) // ✅ data is typed as object
|
|
|
420
520
|
### Method lines
|
|
421
521
|
|
|
422
522
|
```ts
|
|
423
|
-
lines()
|
|
523
|
+
lines(): string[]
|
|
424
524
|
```
|
|
425
525
|
|
|
426
526
|
Returns
|
|
@@ -430,8 +530,9 @@ lines as strings, removes trailing '\n'
|
|
|
430
530
|
### Method ndjson
|
|
431
531
|
|
|
432
532
|
```ts
|
|
433
|
-
ndjson<T extends object>(lines?: T | T[])
|
|
533
|
+
ndjson<T extends object>(lines?: T | T[]): FileTypeNdjson<T>
|
|
434
534
|
```
|
|
535
|
+
See also: [FileTypeNdjson](#class-filetypendjson)
|
|
435
536
|
|
|
436
537
|
Returns
|
|
437
538
|
|
|
@@ -440,7 +541,7 @@ FileTypeNdjson adaptor for current File, adds '.ndjson' extension if not present
|
|
|
440
541
|
### Method read
|
|
441
542
|
|
|
442
543
|
```ts
|
|
443
|
-
read()
|
|
544
|
+
read(): string | undefined
|
|
444
545
|
```
|
|
445
546
|
|
|
446
547
|
Returns
|
|
@@ -458,23 +559,25 @@ A generic file adaptor, extended by specific file type implementations
|
|
|
458
559
|
|
|
459
560
|
```ts
|
|
460
561
|
export class FileType {
|
|
461
|
-
file;
|
|
562
|
+
file: File;
|
|
462
563
|
constructor(filepath: string, contents?: string)
|
|
463
|
-
get path()
|
|
464
|
-
get root()
|
|
465
|
-
get dir()
|
|
466
|
-
get base()
|
|
467
|
-
get name()
|
|
468
|
-
get ext()
|
|
469
|
-
get type()
|
|
470
|
-
get exists()
|
|
471
|
-
get stats()
|
|
472
|
-
delete()
|
|
473
|
-
get readStream()
|
|
474
|
-
get writeStream()
|
|
564
|
+
get path(): string
|
|
565
|
+
get root(): string
|
|
566
|
+
get dir(): string
|
|
567
|
+
get base(): string
|
|
568
|
+
get name(): string
|
|
569
|
+
get ext(): string
|
|
570
|
+
get type(): string | undefined
|
|
571
|
+
get exists(): boolean
|
|
572
|
+
get stats(): Partial<fs.Stats>
|
|
573
|
+
delete(): void
|
|
574
|
+
get readStream(): fs.ReadStream | Readable
|
|
575
|
+
get writeStream(): fs.WriteStream
|
|
475
576
|
}
|
|
476
577
|
```
|
|
477
578
|
|
|
579
|
+
See also: [File](#class-file)
|
|
580
|
+
|
|
478
581
|
Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
|
|
479
582
|
|
|
480
583
|
---
|
|
@@ -485,10 +588,27 @@ Input rows as objects, keys are used as column headers
|
|
|
485
588
|
|
|
486
589
|
```ts
|
|
487
590
|
export class FileTypeCsv<Row extends object> extends FileType {
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
591
|
+
options: FileTypeCsvOptions<Row>;
|
|
592
|
+
constructor(filepath: string, options: FileTypeCsvOptions<Row> = {})
|
|
593
|
+
async write(rows: Row[]): Promise<void>
|
|
594
|
+
#parseVal(val: string): string | number | boolean | null
|
|
595
|
+
async read(): Promise<Row[]>
|
|
596
|
+
}
|
|
597
|
+
```
|
|
598
|
+
|
|
599
|
+
See also: [FileType](#class-filetype)
|
|
600
|
+
|
|
601
|
+
Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
|
|
602
|
+
|
|
603
|
+
---
|
|
604
|
+
## Class: FileTypeImage
|
|
605
|
+
|
|
606
|
+
```ts
|
|
607
|
+
export class FileTypeImage extends FileType {
|
|
608
|
+
async dimensions(): Promise<{
|
|
609
|
+
width: number;
|
|
610
|
+
height: number;
|
|
611
|
+
}>
|
|
492
612
|
}
|
|
493
613
|
```
|
|
494
614
|
|
|
@@ -500,7 +620,7 @@ Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types
|
|
|
500
620
|
## Class: FileTypeJson
|
|
501
621
|
|
|
502
622
|
A .json file that maintains data type when reading/writing.
|
|
503
|
-
> ⚠️ This is mildly unsafe,
|
|
623
|
+
> ⚠️ This is mildly unsafe, json files should be validated at runtime!
|
|
504
624
|
|
|
505
625
|
Examples
|
|
506
626
|
|
|
@@ -518,8 +638,8 @@ file.write({ something: 'else' }) // ✅ data is typed as object
|
|
|
518
638
|
```ts
|
|
519
639
|
export class FileTypeJson<T> extends FileType {
|
|
520
640
|
constructor(filepath: string, contents?: T)
|
|
521
|
-
read()
|
|
522
|
-
write(contents: T)
|
|
641
|
+
read(): T | undefined
|
|
642
|
+
write(contents: T): void
|
|
523
643
|
}
|
|
524
644
|
```
|
|
525
645
|
|
|
@@ -535,8 +655,25 @@ New-line delimited json file (.ndjson)
|
|
|
535
655
|
```ts
|
|
536
656
|
export class FileTypeNdjson<T extends object> extends FileType {
|
|
537
657
|
constructor(filepath: string, lines?: T | T[])
|
|
538
|
-
append(lines: T | T[])
|
|
539
|
-
lines()
|
|
658
|
+
append(lines: T | T[]): void
|
|
659
|
+
lines(): T[]
|
|
660
|
+
}
|
|
661
|
+
```
|
|
662
|
+
|
|
663
|
+
See also: [FileType](#class-filetype)
|
|
664
|
+
|
|
665
|
+
Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
|
|
666
|
+
|
|
667
|
+
---
|
|
668
|
+
## Class: FileTypeVideo
|
|
669
|
+
|
|
670
|
+
```ts
|
|
671
|
+
export class FileTypeVideo extends FileType {
|
|
672
|
+
async dimensions(): Promise<{
|
|
673
|
+
width: number;
|
|
674
|
+
height: number;
|
|
675
|
+
duration: number;
|
|
676
|
+
}>
|
|
540
677
|
}
|
|
541
678
|
```
|
|
542
679
|
|
|
@@ -551,11 +688,11 @@ Helpers for formatting dates, times, and numbers as strings
|
|
|
551
688
|
|
|
552
689
|
```ts
|
|
553
690
|
export class Format {
|
|
554
|
-
static date(formatStr: "iso" | "ymd" | "ymd-hm" | "ymd-hms" | "h:m:s" | string = "iso", d: DateArg<Date> = new Date())
|
|
555
|
-
static round(n: number, places = 0)
|
|
556
|
-
static plural(amount: number, singular: string, multiple?: string)
|
|
557
|
-
static ms(ms: number, style?: "digital")
|
|
558
|
-
static bytes(b: number)
|
|
691
|
+
static date(formatStr: "iso" | "ymd" | "ymd-hm" | "ymd-hms" | "h:m:s" | string = "iso", d: DateArg<Date> = new Date()): string
|
|
692
|
+
static round(n: number, places = 0): string
|
|
693
|
+
static plural(amount: number, singular: string, multiple?: string): string
|
|
694
|
+
static ms(ms: number, style?: "digital"): string
|
|
695
|
+
static bytes(b: number): string
|
|
559
696
|
}
|
|
560
697
|
```
|
|
561
698
|
|
|
@@ -568,7 +705,7 @@ export class Format {
|
|
|
568
705
|
date-fns format() with some shortcuts
|
|
569
706
|
|
|
570
707
|
```ts
|
|
571
|
-
static date(formatStr: "iso" | "ymd" | "ymd-hm" | "ymd-hms" | "h:m:s" | string = "iso", d: DateArg<Date> = new Date())
|
|
708
|
+
static date(formatStr: "iso" | "ymd" | "ymd-hm" | "ymd-hms" | "h:m:s" | string = "iso", d: DateArg<Date> = new Date()): string
|
|
572
709
|
```
|
|
573
710
|
|
|
574
711
|
Argument Details
|
|
@@ -593,7 +730,7 @@ Format.date('h:m:s') // '13:56:45'
|
|
|
593
730
|
Make millisecond durations actually readable (eg "123ms", "3.56s", "1m 34s", "3h 24m", "2d 4h")
|
|
594
731
|
|
|
595
732
|
```ts
|
|
596
|
-
static ms(ms: number, style?: "digital")
|
|
733
|
+
static ms(ms: number, style?: "digital"): string
|
|
597
734
|
```
|
|
598
735
|
|
|
599
736
|
Argument Details
|
|
@@ -608,7 +745,7 @@ Argument Details
|
|
|
608
745
|
Round a number to a specific set of places
|
|
609
746
|
|
|
610
747
|
```ts
|
|
611
|
-
static round(n: number, places = 0)
|
|
748
|
+
static round(n: number, places = 0): string
|
|
612
749
|
```
|
|
613
750
|
|
|
614
751
|
</details>
|
|
@@ -618,125 +755,60 @@ Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types
|
|
|
618
755
|
---
|
|
619
756
|
## Class: Log
|
|
620
757
|
|
|
758
|
+
Wrapper for [pino](https://github.com/pinojs/pino)
|
|
759
|
+
Levels: fatal, error, warn, info, debug, trace
|
|
760
|
+
Use `LOG_LEVL=info` to limit what's printed to console
|
|
761
|
+
Use `Log.configure` to customize the pino instance
|
|
762
|
+
|
|
621
763
|
```ts
|
|
622
764
|
export class Log {
|
|
623
|
-
static
|
|
624
|
-
static #
|
|
625
|
-
static
|
|
626
|
-
static
|
|
627
|
-
static
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
static
|
|
632
|
-
static
|
|
633
|
-
static
|
|
634
|
-
static
|
|
635
|
-
static info(...input: unknown[])
|
|
636
|
-
static debug(...input: unknown[])
|
|
765
|
+
static #logger?: Logger;
|
|
766
|
+
static #options: LogOptions;
|
|
767
|
+
static createLogger(): Logger
|
|
768
|
+
static configure(options: LogOptions = {}): void
|
|
769
|
+
static #getLogger(): Logger
|
|
770
|
+
static #write(level: LogLevel, message: string, details?: LogDetails): void
|
|
771
|
+
static trace(message: string, details?: LogDetails): void
|
|
772
|
+
static debug(message: string, details?: LogDetails): void
|
|
773
|
+
static info(message: string, details?: LogDetails): void
|
|
774
|
+
static warn(message: string, details?: LogDetails): void
|
|
775
|
+
static error(message: string, details?: LogDetails): void
|
|
776
|
+
static fatal(message: string, details?: LogDetails): void
|
|
637
777
|
}
|
|
638
778
|
```
|
|
639
779
|
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
<summary>Class Log Details</summary>
|
|
643
|
-
|
|
644
|
-
### Method
|
|
645
|
-
|
|
646
|
-
Gcloud parses JSON in stdout
|
|
647
|
-
|
|
648
|
-
```ts
|
|
649
|
-
static #toGcloud(entry: Entry)
|
|
650
|
-
```
|
|
780
|
+
See also: [LogDetails](#type-logdetails), [LogLevel](#type-loglevel), [LogOptions](#type-logoptions)
|
|
651
781
|
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
Includes colors and better inspection for logging during dev
|
|
655
|
-
|
|
656
|
-
```ts
|
|
657
|
-
static #toConsole(entry: Entry, color: ChalkInstance)
|
|
658
|
-
```
|
|
659
|
-
|
|
660
|
-
### Method alert
|
|
661
|
-
|
|
662
|
-
Events that require action or attention immediately
|
|
663
|
-
|
|
664
|
-
```ts
|
|
665
|
-
static alert(...input: unknown[])
|
|
666
|
-
```
|
|
667
|
-
|
|
668
|
-
### Method debug
|
|
669
|
-
|
|
670
|
-
Debug or trace information
|
|
671
|
-
|
|
672
|
-
```ts
|
|
673
|
-
static debug(...input: unknown[])
|
|
674
|
-
```
|
|
675
|
-
|
|
676
|
-
### Method error
|
|
677
|
-
|
|
678
|
-
Events that cause problems
|
|
679
|
-
|
|
680
|
-
```ts
|
|
681
|
-
static error(...input: unknown[])
|
|
682
|
-
```
|
|
683
|
-
|
|
684
|
-
### Method info
|
|
685
|
-
|
|
686
|
-
Routine information, such as ongoing status or performance
|
|
687
|
-
|
|
688
|
-
```ts
|
|
689
|
-
static info(...input: unknown[])
|
|
690
|
-
```
|
|
691
|
-
|
|
692
|
-
### Method notice
|
|
693
|
-
|
|
694
|
-
Normal but significant events, such as start up, shut down, or a configuration change
|
|
695
|
-
|
|
696
|
-
```ts
|
|
697
|
-
static notice(...input: unknown[])
|
|
698
|
-
```
|
|
699
|
-
|
|
700
|
-
### Method prepare
|
|
701
|
-
|
|
702
|
-
Handle first argument being a string or an object with a 'message' prop
|
|
782
|
+
Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
|
|
703
783
|
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
message?: string;
|
|
707
|
-
details: unknown[];
|
|
708
|
-
}
|
|
709
|
-
```
|
|
784
|
+
---
|
|
785
|
+
## Class: TypeWriter
|
|
710
786
|
|
|
711
|
-
|
|
787
|
+
Wrapper for [quicktype-core](https://github.com/glideapps/quicktype)
|
|
712
788
|
|
|
713
|
-
|
|
789
|
+
Example
|
|
714
790
|
|
|
715
791
|
```ts
|
|
716
|
-
|
|
792
|
+
const group = new TypeWriter('Group');
|
|
793
|
+
await types.addMember('Thing', [{ a: 1 }, { a: 2, b: 1 }]);
|
|
794
|
+
await types.toFile();
|
|
795
|
+
// type def for `Thing` saved in `types/Group.types.ts`
|
|
717
796
|
```
|
|
718
797
|
|
|
719
|
-
</details>
|
|
720
|
-
|
|
721
|
-
Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
|
|
722
|
-
|
|
723
|
-
---
|
|
724
|
-
## Class: TypeWriter
|
|
725
|
-
|
|
726
798
|
```ts
|
|
727
799
|
export class TypeWriter {
|
|
728
|
-
moduleName;
|
|
800
|
+
moduleName: string;
|
|
729
801
|
input = qt.jsonInputForTargetLanguage("typescript");
|
|
730
|
-
outDir;
|
|
731
|
-
outFile;
|
|
732
|
-
qtSettings
|
|
802
|
+
outDir: string;
|
|
803
|
+
outFile: string;
|
|
804
|
+
qtSettings: Partial<qt.Options>;
|
|
733
805
|
constructor(moduleName: string, settings: {
|
|
734
806
|
outDir?: string;
|
|
735
807
|
outFile?: string;
|
|
736
808
|
} & Partial<qt.Options> = {})
|
|
737
|
-
async addMember(name: string, _samples: any[])
|
|
738
|
-
async toString()
|
|
739
|
-
async toFile()
|
|
809
|
+
async addMember(name: string, _samples: any[]): Promise<void>
|
|
810
|
+
async toString(): Promise<string>
|
|
811
|
+
async toFile(): Promise<void>
|
|
740
812
|
}
|
|
741
813
|
```
|
|
742
814
|
|
|
@@ -749,7 +821,7 @@ export class TypeWriter {
|
|
|
749
821
|
function toString() { [native code] }
|
|
750
822
|
|
|
751
823
|
```ts
|
|
752
|
-
async toString()
|
|
824
|
+
async toString(): Promise<string>
|
|
753
825
|
```
|
|
754
826
|
|
|
755
827
|
</details>
|
|
@@ -770,9 +842,6 @@ Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types
|
|
|
770
842
|
|
|
771
843
|
## Function: snapshot
|
|
772
844
|
|
|
773
|
-
Allows special objects (Error, Headers, Set) to be included in JSON.stringify output.
|
|
774
|
-
Functions are removed
|
|
775
|
-
|
|
776
845
|
```ts
|
|
777
846
|
export function snapshot(i: unknown, max = 50, depth = 0): any
|
|
778
847
|
```
|
|
@@ -783,7 +852,7 @@ Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types
|
|
|
783
852
|
## Function: timeout
|
|
784
853
|
|
|
785
854
|
```ts
|
|
786
|
-
export async function timeout(ms: number)
|
|
855
|
+
export async function timeout(ms: number): Promise<void>
|
|
787
856
|
```
|
|
788
857
|
|
|
789
858
|
Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
|
|
@@ -793,8 +862,12 @@ Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types
|
|
|
793
862
|
|
|
794
863
|
| |
|
|
795
864
|
| --- |
|
|
865
|
+
| [Args](#type-args) |
|
|
796
866
|
| [DirOptions](#type-diroptions) |
|
|
797
867
|
| [FetchOptions](#type-fetchoptions) |
|
|
868
|
+
| [LogDetails](#type-logdetails) |
|
|
869
|
+
| [LogLevel](#type-loglevel) |
|
|
870
|
+
| [LogOptions](#type-logoptions) |
|
|
798
871
|
| [Query](#type-query) |
|
|
799
872
|
| [Route](#type-route) |
|
|
800
873
|
|
|
@@ -802,6 +875,15 @@ Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types
|
|
|
802
875
|
|
|
803
876
|
---
|
|
804
877
|
|
|
878
|
+
## Type: Args
|
|
879
|
+
|
|
880
|
+
```ts
|
|
881
|
+
export type Args = string | (string | number | null | undefined | Args)[]
|
|
882
|
+
```
|
|
883
|
+
|
|
884
|
+
Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
|
|
885
|
+
|
|
886
|
+
---
|
|
805
887
|
## Type: DirOptions
|
|
806
888
|
|
|
807
889
|
```ts
|
|
@@ -833,6 +915,35 @@ See also: [Query](#type-query), [timeout](#function-timeout)
|
|
|
833
915
|
|
|
834
916
|
Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
|
|
835
917
|
|
|
918
|
+
---
|
|
919
|
+
## Type: LogDetails
|
|
920
|
+
|
|
921
|
+
```ts
|
|
922
|
+
export type LogDetails = Record<string, unknown>
|
|
923
|
+
```
|
|
924
|
+
|
|
925
|
+
Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
|
|
926
|
+
|
|
927
|
+
---
|
|
928
|
+
## Type: LogLevel
|
|
929
|
+
|
|
930
|
+
```ts
|
|
931
|
+
export type LogLevel = "trace" | "debug" | "info" | "warn" | "error" | "fatal"
|
|
932
|
+
```
|
|
933
|
+
|
|
934
|
+
Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
|
|
935
|
+
|
|
936
|
+
---
|
|
937
|
+
## Type: LogOptions
|
|
938
|
+
|
|
939
|
+
```ts
|
|
940
|
+
export type LogOptions = pino.LoggerOptions & {
|
|
941
|
+
environment?: string;
|
|
942
|
+
}
|
|
943
|
+
```
|
|
944
|
+
|
|
945
|
+
Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types), [Variables](#variables)
|
|
946
|
+
|
|
836
947
|
---
|
|
837
948
|
## Type: Query
|
|
838
949
|
|
|
@@ -893,13 +1004,13 @@ Links: [API](#api), [Classes](#classes), [Functions](#functions), [Types](#types
|
|
|
893
1004
|
Typecheck and run all tests from `*.test.ts` files
|
|
894
1005
|
|
|
895
1006
|
```
|
|
896
|
-
|
|
1007
|
+
npm run test
|
|
897
1008
|
```
|
|
898
1009
|
|
|
899
1010
|
Format with Prettier, generate API docs for this Readme
|
|
900
1011
|
|
|
901
1012
|
```
|
|
902
|
-
|
|
1013
|
+
npm run build
|
|
903
1014
|
```
|
|
904
1015
|
|
|
905
1016
|
Release a new version
|