@thi.ng/file-io 2.1.1 → 2.1.3
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/CHANGELOG.md +1 -1
- package/README.md +2 -2
- package/delete.js +2 -4
- package/file-chunks.d.ts +0 -1
- package/file-chunks.js +2 -4
- package/files.js +5 -8
- package/hash.d.ts +1 -3
- package/hash.js +1 -2
- package/package.json +12 -12
- package/text.d.ts +0 -1
- package/watch.d.ts +8 -1
- package/watch.js +5 -10
- package/write.d.ts +0 -1
- package/write.js +1 -2
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
[](https://mastodon.thi.ng/@toxi)
|
|
8
8
|
|
|
9
9
|
> [!NOTE]
|
|
10
|
-
> This is one of
|
|
10
|
+
> This is one of 193 standalone projects, maintained as part
|
|
11
11
|
> of the [@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo
|
|
12
12
|
> and anti-framework.
|
|
13
13
|
>
|
|
@@ -54,7 +54,7 @@ For Node.js REPL:
|
|
|
54
54
|
const fio = await import("@thi.ng/file-io");
|
|
55
55
|
```
|
|
56
56
|
|
|
57
|
-
Package sizes (brotli'd, pre-treeshake): ESM: 2.
|
|
57
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 2.05 KB
|
|
58
58
|
|
|
59
59
|
## Dependencies
|
|
60
60
|
|
package/delete.js
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import { rmSync, unlinkSync } from "node:fs";
|
|
2
2
|
const deleteFile = (path, logger, dryRun = false) => {
|
|
3
3
|
logger && logger.info(`${dryRun ? "[dryrun] " : ""}deleting file: ${path}`);
|
|
4
|
-
if (dryRun)
|
|
5
|
-
return;
|
|
4
|
+
if (dryRun) return;
|
|
6
5
|
unlinkSync(path);
|
|
7
6
|
};
|
|
8
7
|
const deleteDir = (path, logger, dryRun = false) => {
|
|
9
8
|
logger && logger.info(`${dryRun ? "[dryrun] " : ""}deleting file: ${path}`);
|
|
10
|
-
if (dryRun)
|
|
11
|
-
return;
|
|
9
|
+
if (dryRun) return;
|
|
12
10
|
rmSync(path, { recursive: true, force: true });
|
|
13
11
|
};
|
|
14
12
|
export {
|
package/file-chunks.d.ts
CHANGED
package/file-chunks.js
CHANGED
|
@@ -17,11 +17,9 @@ async function* fileChunks(path, opts = {}) {
|
|
|
17
17
|
length: size,
|
|
18
18
|
position: start
|
|
19
19
|
});
|
|
20
|
-
if (bytesRead === 0)
|
|
21
|
-
break;
|
|
20
|
+
if (bytesRead === 0) break;
|
|
22
21
|
yield buffer;
|
|
23
|
-
if (bytesRead < size)
|
|
24
|
-
break;
|
|
22
|
+
if (bytesRead < size) break;
|
|
25
23
|
start += bytesRead;
|
|
26
24
|
}
|
|
27
25
|
} finally {
|
package/files.js
CHANGED
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import { readdirSync
|
|
1
|
+
import { readdirSync } from "node:fs";
|
|
2
2
|
import { sep } from "node:path";
|
|
3
3
|
import { isDirectory } from "./dir.js";
|
|
4
4
|
import { __ensurePred } from "./internal/ensure.js";
|
|
5
5
|
const files = (dir, match = "", maxDepth = Infinity, logger) => __files(dir, match, logger, maxDepth, 0);
|
|
6
6
|
function* __files(dir, match = "", logger, maxDepth = Infinity, depth = 0) {
|
|
7
|
-
if (depth >= maxDepth)
|
|
8
|
-
return;
|
|
7
|
+
if (depth >= maxDepth) return;
|
|
9
8
|
const pred = __ensurePred(match);
|
|
10
9
|
for (let f of readdirSync(dir).sort()) {
|
|
11
10
|
const curr = dir + sep + f;
|
|
@@ -22,15 +21,13 @@ function* __files(dir, match = "", logger, maxDepth = Infinity, depth = 0) {
|
|
|
22
21
|
}
|
|
23
22
|
const dirs = (dir, match = "", maxDepth = Infinity, logger) => __dirs(dir, match, logger, maxDepth, 0);
|
|
24
23
|
function* __dirs(dir, match = "", logger, maxDepth = Infinity, depth = 0) {
|
|
25
|
-
if (depth >= maxDepth)
|
|
26
|
-
return;
|
|
24
|
+
if (depth >= maxDepth) return;
|
|
27
25
|
const pred = __ensurePred(match);
|
|
28
26
|
for (let f of readdirSync(dir).sort()) {
|
|
29
27
|
const curr = dir + sep + f;
|
|
30
28
|
try {
|
|
31
|
-
if (
|
|
32
|
-
if (pred(curr))
|
|
33
|
-
yield curr;
|
|
29
|
+
if (isDirectory(curr)) {
|
|
30
|
+
if (pred(curr)) yield curr;
|
|
34
31
|
yield* __dirs(curr, match, logger, maxDepth, depth + 1);
|
|
35
32
|
}
|
|
36
33
|
} catch (e) {
|
package/hash.d.ts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
/// <reference types="node" />
|
|
3
1
|
import type { TypedArray } from "@thi.ng/api";
|
|
4
2
|
import type { ILogger } from "@thi.ng/logger";
|
|
5
3
|
import type { Readable } from "node:stream";
|
|
@@ -18,7 +16,7 @@ export declare const fileHash: (path: string, logger?: ILogger, algo?: HashAlgo)
|
|
|
18
16
|
* "sha256"). If `logger` is given, the hash will be logged too.
|
|
19
17
|
*
|
|
20
18
|
* @remarks
|
|
21
|
-
* Also see {@link fileHash} and {@link
|
|
19
|
+
* Also see {@link fileHash} and {@link bufferHash}.
|
|
22
20
|
*
|
|
23
21
|
* @param src
|
|
24
22
|
* @param logger
|
package/hash.js
CHANGED
|
@@ -6,8 +6,7 @@ const fileHash = async (path, logger, algo = "sha256") => {
|
|
|
6
6
|
};
|
|
7
7
|
const streamHash = async (src, logger, algo = "sha256") => {
|
|
8
8
|
const sum = createHash(algo);
|
|
9
|
-
for await (let chunk of src)
|
|
10
|
-
sum.update(chunk);
|
|
9
|
+
for await (let chunk of src) sum.update(chunk);
|
|
11
10
|
const hash = sum.digest("hex");
|
|
12
11
|
logger && logger.info(`${algo} hash: ${hash}`);
|
|
13
12
|
return hash;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/file-io",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.3",
|
|
4
4
|
"description": "Assorted file I/O utils (with logging support) for NodeJS/Bun",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"type": "git",
|
|
11
11
|
"url": "https://github.com/thi-ng/umbrella.git"
|
|
12
12
|
},
|
|
13
|
-
"homepage": "https://
|
|
13
|
+
"homepage": "https://thi.ng/file-io",
|
|
14
14
|
"funding": [
|
|
15
15
|
{
|
|
16
16
|
"type": "github",
|
|
@@ -36,17 +36,17 @@
|
|
|
36
36
|
"tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
|
|
37
37
|
},
|
|
38
38
|
"dependencies": {
|
|
39
|
-
"@thi.ng/api": "^8.11.
|
|
40
|
-
"@thi.ng/checks": "^3.6.
|
|
41
|
-
"@thi.ng/hex": "^2.3.
|
|
42
|
-
"@thi.ng/logger": "^3.0.
|
|
43
|
-
"@thi.ng/random": "^3.
|
|
39
|
+
"@thi.ng/api": "^8.11.3",
|
|
40
|
+
"@thi.ng/checks": "^3.6.5",
|
|
41
|
+
"@thi.ng/hex": "^2.3.47",
|
|
42
|
+
"@thi.ng/logger": "^3.0.13",
|
|
43
|
+
"@thi.ng/random": "^3.8.1"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@microsoft/api-extractor": "^7.
|
|
47
|
-
"esbuild": "^0.
|
|
48
|
-
"typedoc": "^0.25.
|
|
49
|
-
"typescript": "^5.
|
|
46
|
+
"@microsoft/api-extractor": "^7.47.0",
|
|
47
|
+
"esbuild": "^0.21.5",
|
|
48
|
+
"typedoc": "^0.25.13",
|
|
49
|
+
"typescript": "^5.5.2"
|
|
50
50
|
},
|
|
51
51
|
"keywords": [
|
|
52
52
|
"async",
|
|
@@ -123,5 +123,5 @@
|
|
|
123
123
|
"status": "stable",
|
|
124
124
|
"year": 2022
|
|
125
125
|
},
|
|
126
|
-
"gitHead": "
|
|
126
|
+
"gitHead": "154c95cf9d6bab32174498ec3b5b5d87e42be7f9\n"
|
|
127
127
|
}
|
package/text.d.ts
CHANGED
package/watch.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import { type Event, type IClear, type INotify, type IObjectOf, type Listener, type Predicate } from "@thi.ng/api";
|
|
3
2
|
import type { ILogger } from "@thi.ng/logger";
|
|
4
3
|
import { type FSWatcher } from "node:fs";
|
|
@@ -16,7 +15,15 @@ export interface WatcherOpts {
|
|
|
16
15
|
logger: ILogger;
|
|
17
16
|
}
|
|
18
17
|
export interface PathWatchOpts {
|
|
18
|
+
/**
|
|
19
|
+
* If enabled (default) and if the path refers to a directory, any file
|
|
20
|
+
* changes within that dir (or any subdir) will trigger an event.
|
|
21
|
+
*/
|
|
19
22
|
recursive: boolean;
|
|
23
|
+
/**
|
|
24
|
+
* File extension filter. If given, events are only triggered for paths
|
|
25
|
+
* matching this filter.
|
|
26
|
+
*/
|
|
20
27
|
ext: string | RegExp | Predicate<string>;
|
|
21
28
|
}
|
|
22
29
|
export declare const EVENT_ADDED = "added";
|
package/watch.js
CHANGED
|
@@ -5,8 +5,7 @@ var __decorateClass = (decorators, target, key, kind) => {
|
|
|
5
5
|
for (var i = decorators.length - 1, decorator; i >= 0; i--)
|
|
6
6
|
if (decorator = decorators[i])
|
|
7
7
|
result = (kind ? decorator(target, key, result) : decorator(result)) || result;
|
|
8
|
-
if (kind && result)
|
|
9
|
-
__defProp(target, key, result);
|
|
8
|
+
if (kind && result) __defProp(target, key, result);
|
|
10
9
|
return result;
|
|
11
10
|
};
|
|
12
11
|
import {
|
|
@@ -38,8 +37,7 @@ let Watcher = class {
|
|
|
38
37
|
}
|
|
39
38
|
}
|
|
40
39
|
add(path, opts) {
|
|
41
|
-
if (this.watchers[path])
|
|
42
|
-
return false;
|
|
40
|
+
if (this.watchers[path]) return false;
|
|
43
41
|
const isDir = isDirectory(path);
|
|
44
42
|
const pred = __ensurePred(opts?.ext || "");
|
|
45
43
|
this.opts.logger.debug(`adding watcher for: ${path}`);
|
|
@@ -47,11 +45,9 @@ let Watcher = class {
|
|
|
47
45
|
path,
|
|
48
46
|
{ recursive: opts?.recursive !== false },
|
|
49
47
|
(event, currPath) => {
|
|
50
|
-
if (!currPath)
|
|
51
|
-
return;
|
|
48
|
+
if (!currPath) return;
|
|
52
49
|
currPath = isDir ? join(path, currPath) : path;
|
|
53
|
-
if (!pred(currPath))
|
|
54
|
-
return;
|
|
50
|
+
if (!pred(currPath)) return;
|
|
55
51
|
setTimeout(() => {
|
|
56
52
|
if (event === "change") {
|
|
57
53
|
this.opts.logger.info(`file changed: ${currPath}`);
|
|
@@ -75,8 +71,7 @@ let Watcher = class {
|
|
|
75
71
|
}
|
|
76
72
|
remove(path) {
|
|
77
73
|
const watcher = this.watchers[path];
|
|
78
|
-
if (!watcher)
|
|
79
|
-
return false;
|
|
74
|
+
if (!watcher) return false;
|
|
80
75
|
this.opts.logger.debug(`removing watcher for: ${path}`);
|
|
81
76
|
watcher.close();
|
|
82
77
|
delete this.watchers[path];
|
package/write.d.ts
CHANGED
package/write.js
CHANGED
|
@@ -3,8 +3,7 @@ import { writeFileSync } from "node:fs";
|
|
|
3
3
|
import { ensureDirForFile } from "./dir.js";
|
|
4
4
|
const writeFile = (path, body, opts, logger, dryRun = false) => {
|
|
5
5
|
logger && logger.info(`${dryRun ? "[dryrun] " : ""}writing file: ${path}`);
|
|
6
|
-
if (dryRun)
|
|
7
|
-
return;
|
|
6
|
+
if (dryRun) return;
|
|
8
7
|
ensureDirForFile(path);
|
|
9
8
|
writeFileSync(path, body, !opts && isString(body) ? "utf-8" : opts);
|
|
10
9
|
};
|