@thi.ng/file-io 0.2.0 → 0.3.2

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2022-05-19T13:17:54Z
3
+ - **Last updated**: 2022-06-09T16:14:01Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,19 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [0.3.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/file-io@0.3.0) (2022-05-20)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add more fns (hashing, masking etc.) ([95b9e2b](https://github.com/thi-ng/umbrella/commit/95b9e2b))
17
+ - add fileHash(), stringHash()
18
+ - add maskHomeDir()
19
+ - add fileExt(), isDirectory()
20
+
21
+ #### 🩹 Bug fixes
22
+
23
+ - fix ensureDir() for local file paths ([4ae95c2](https://github.com/thi-ng/umbrella/commit/4ae95c2))
24
+
12
25
  ## [0.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/file-io@0.2.0) (2022-05-19)
13
26
 
14
27
  #### 🚀 Features
package/delete.d.ts CHANGED
@@ -8,5 +8,5 @@ import type { ILogger } from "@thi.ng/logger";
8
8
  * @param logger
9
9
  * @param dryRun
10
10
  */
11
- export declare const deleteFile: (path: string, logger?: ILogger | undefined, dryRun?: boolean) => void;
11
+ export declare const deleteFile: (path: string, logger?: ILogger, dryRun?: boolean) => void;
12
12
  //# sourceMappingURL=delete.d.ts.map
package/dir.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Checks if the directory for given file `path` already exists, and if not the
3
+ * case, creates it. Returns true if the latter case.
4
+ *
5
+ * @remarks
6
+ * If `path` only contains a filename (without any directory structure), the
7
+ * function does nothing.
8
+ *
9
+ * @param path
10
+ */
11
+ export declare const ensureDirForFile: (path: string) => boolean;
12
+ /**
13
+ * Returns true if `path` is a directory (assumes path exists).
14
+ *
15
+ * @param path
16
+ */
17
+ export declare const isDirectory: (path: string) => boolean;
18
+ //# sourceMappingURL=dir.d.ts.map
package/dir.js ADDED
@@ -0,0 +1,24 @@
1
+ import { existsSync, mkdirSync, statSync } from "fs";
2
+ import { sep } from "path";
3
+ /**
4
+ * Checks if the directory for given file `path` already exists, and if not the
5
+ * case, creates it. Returns true if the latter case.
6
+ *
7
+ * @remarks
8
+ * If `path` only contains a filename (without any directory structure), the
9
+ * function does nothing.
10
+ *
11
+ * @param path
12
+ */
13
+ export const ensureDirForFile = (path) => {
14
+ const dir = path.substring(0, path.lastIndexOf(sep));
15
+ return dir.length > 0 && !existsSync(dir)
16
+ ? (mkdirSync(dir, { recursive: true }), true)
17
+ : false;
18
+ };
19
+ /**
20
+ * Returns true if `path` is a directory (assumes path exists).
21
+ *
22
+ * @param path
23
+ */
24
+ export const isDirectory = (path) => statSync(path).isDirectory();
package/ext.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Returns file extension of given file `path`.
3
+ *
4
+ * @param path
5
+ */
6
+ export declare const fileExt: (path: string) => string;
7
+ //# sourceMappingURL=ext.d.ts.map
package/ext.js ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Returns file extension of given file `path`.
3
+ *
4
+ * @param path
5
+ */
6
+ export const fileExt = (path) => {
7
+ const match = /\.(\w+)$/.exec(path);
8
+ return match ? match[1].toLowerCase() : "";
9
+ };
package/files.d.ts CHANGED
@@ -15,7 +15,7 @@ import type { ILogger } from "@thi.ng/logger";
15
15
  * @param maxDepth
16
16
  * @param logger
17
17
  */
18
- export declare const files: (dir: string, match?: string | RegExp, maxDepth?: number, logger?: ILogger | undefined) => IterableIterator<string>;
18
+ export declare const files: (dir: string, match?: string | RegExp, maxDepth?: number, logger?: ILogger) => IterableIterator<string>;
19
19
  /**
20
20
  * Similar to {@link files}, however yields iterator of only matching
21
21
  * sub-directories in given `dir`. Normal files are being ignored.
@@ -29,5 +29,5 @@ export declare const files: (dir: string, match?: string | RegExp, maxDepth?: nu
29
29
  * @param maxDepth
30
30
  * @param logger
31
31
  */
32
- export declare const dirs: (dir: string, match?: string | RegExp, maxDepth?: number, logger?: ILogger | undefined) => IterableIterator<string>;
32
+ export declare const dirs: (dir: string, match?: string | RegExp, maxDepth?: number, logger?: ILogger) => IterableIterator<string>;
33
33
  //# sourceMappingURL=files.d.ts.map
package/files.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { isString } from "@thi.ng/checks/is-string";
2
2
  import { readdirSync, statSync } from "fs";
3
3
  import { sep } from "path";
4
+ import { isDirectory } from "./dir.js";
4
5
  /**
5
6
  * Recursively reads given directory (up to given max. depth, default: infinite)
6
7
  * and yields sequence of file names matching given extension (or regexp).
@@ -25,7 +26,7 @@ function* __files(dir, match = "", logger, maxDepth = Infinity, depth = 0) {
25
26
  for (let f of readdirSync(dir)) {
26
27
  const curr = dir + sep + f;
27
28
  try {
28
- if (statSync(curr).isDirectory()) {
29
+ if (isDirectory(curr)) {
29
30
  yield* __files(curr, match, logger, maxDepth, depth + 1);
30
31
  }
31
32
  else if (re.test(f)) {
package/hash.d.ts ADDED
@@ -0,0 +1,5 @@
1
+ import type { ILogger } from "@thi.ng/logger";
2
+ export declare type HashAlgo = "gost-mac" | "md4" | "md5" | "md_gost94" | "ripemd160" | "sha1" | "sha224" | "sha256" | "sha384" | "sha512" | "streebog256" | "streebog512" | "whirlpool";
3
+ export declare const fileHash: (path: string, logger?: ILogger, algo?: HashAlgo) => string;
4
+ export declare const stringHash: (src: string, logger?: ILogger, algo?: HashAlgo) => string;
5
+ //# sourceMappingURL=hash.d.ts.map
package/hash.js ADDED
@@ -0,0 +1,16 @@
1
+ import { createHash } from "crypto";
2
+ import { readFileSync } from "fs";
3
+ export const fileHash = (path, logger, algo = "sha256") => {
4
+ const sum = createHash(algo);
5
+ sum.update(readFileSync(path));
6
+ const hash = sum.digest("hex");
7
+ logger && logger.info(`${algo} hash for ${path}: ${hash}`);
8
+ return hash;
9
+ };
10
+ export const stringHash = (src, logger, algo = "sha256") => {
11
+ const sum = createHash(algo);
12
+ sum.update(src);
13
+ const hash = sum.digest("hex");
14
+ logger && logger.info(`${algo} hash for string: ${hash}`);
15
+ return hash;
16
+ };
package/index.d.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  export * from "./delete.js";
2
- export * from "./ensure-dir.js";
2
+ export * from "./dir.js";
3
+ export * from "./ext.js";
3
4
  export * from "./files.js";
5
+ export * from "./hash.js";
4
6
  export * from "./json.js";
7
+ export * from "./mask.js";
5
8
  export * from "./temp.js";
6
9
  export * from "./text.js";
7
10
  export * from "./write.js";
package/index.js CHANGED
@@ -1,7 +1,10 @@
1
1
  export * from "./delete.js";
2
- export * from "./ensure-dir.js";
2
+ export * from "./dir.js";
3
+ export * from "./ext.js";
3
4
  export * from "./files.js";
5
+ export * from "./hash.js";
4
6
  export * from "./json.js";
7
+ export * from "./mask.js";
5
8
  export * from "./temp.js";
6
9
  export * from "./text.js";
7
10
  export * from "./write.js";
package/json.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { Fn3, NumOrString } from "@thi.ng/api";
2
2
  import type { ILogger } from "@thi.ng/logger";
3
- export declare const readJSON: (path: string, logger?: ILogger | undefined) => any;
3
+ export declare const readJSON: (path: string, logger?: ILogger) => any;
4
4
  /**
5
5
  * Serializes `obj` to JSON and writes result to UTF-8 file `path`. See
6
6
  * {@link writeText} for more details.
@@ -16,5 +16,5 @@ export declare const readJSON: (path: string, logger?: ILogger | undefined) => a
16
16
  * @param logger
17
17
  * @param dryRun
18
18
  */
19
- export declare const writeJSON: (path: string, obj: any, replacer?: Fn3<any, string, any, any> | NumOrString[] | null | undefined, space?: NumOrString | undefined, logger?: ILogger | undefined, dryRun?: boolean) => void;
19
+ export declare const writeJSON: (path: string, obj: any, replacer?: Fn3<any, string, any, any> | NumOrString[] | null | undefined, space?: NumOrString | undefined, logger?: ILogger, dryRun?: boolean) => void;
20
20
  //# sourceMappingURL=json.d.ts.map
package/mask.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Replaces `home` (default: `process.env.HOME`) sub-path with given `mask`
3
+ * (default: `~`).
4
+ *
5
+ * @param path
6
+ * @param home
7
+ */
8
+ export declare const maskHomeDir: (path: string, home?: string | undefined, mask?: string) => string;
9
+ //# sourceMappingURL=mask.d.ts.map
package/mask.js ADDED
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Replaces `home` (default: `process.env.HOME`) sub-path with given `mask`
3
+ * (default: `~`).
4
+ *
5
+ * @param path
6
+ * @param home
7
+ */
8
+ export const maskHomeDir = (path, home = process.env.HOME, mask = "~") => (home ? path.replace(home, mask) : path);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/file-io",
3
- "version": "0.2.0",
3
+ "version": "0.3.2",
4
4
  "description": "Assorted file I/O utils (with logging support) for NodeJS",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -34,21 +34,24 @@
34
34
  "test": "testament test"
35
35
  },
36
36
  "dependencies": {
37
- "@thi.ng/api": "^8.3.6",
38
- "@thi.ng/checks": "^3.1.6",
39
- "@thi.ng/logger": "^1.1.6",
40
- "@thi.ng/random": "^3.2.6"
37
+ "@thi.ng/api": "^8.3.7",
38
+ "@thi.ng/checks": "^3.2.0",
39
+ "@thi.ng/logger": "^1.1.7",
40
+ "@thi.ng/random": "^3.3.1"
41
41
  },
42
42
  "devDependencies": {
43
- "@microsoft/api-extractor": "^7.23.1",
44
- "@thi.ng/testament": "^0.2.6",
43
+ "@microsoft/api-extractor": "^7.25.0",
44
+ "@thi.ng/testament": "^0.2.8",
45
45
  "rimraf": "^3.0.2",
46
46
  "tools": "^0.0.1",
47
- "typedoc": "^0.22.15",
48
- "typescript": "^4.6.4"
47
+ "typedoc": "^0.22.17",
48
+ "typescript": "^4.7.3"
49
49
  },
50
50
  "keywords": [
51
51
  "file",
52
+ "hash",
53
+ "json",
54
+ "logger",
52
55
  "node-only",
53
56
  "typescript"
54
57
  ],
@@ -73,15 +76,24 @@
73
76
  "./delete": {
74
77
  "default": "./delete.js"
75
78
  },
76
- "./ensure-dir": {
77
- "default": "./ensure-dir.js"
79
+ "./dir": {
80
+ "default": "./dir.js"
81
+ },
82
+ "./ext": {
83
+ "default": "./ext.js"
78
84
  },
79
85
  "./files": {
80
86
  "default": "./files.js"
81
87
  },
88
+ "./hash": {
89
+ "default": "./hash.js"
90
+ },
82
91
  "./json": {
83
92
  "default": "./json.js"
84
93
  },
94
+ "./mask": {
95
+ "default": "./mask.js"
96
+ },
85
97
  "./temp": {
86
98
  "default": "./temp.js"
87
99
  },
@@ -96,5 +108,5 @@
96
108
  "status": "stable",
97
109
  "year": 2022
98
110
  },
99
- "gitHead": "ec1685021284dc0e98bbfe67abefc3d1f6c0f7dd\n"
111
+ "gitHead": "9e516d30a1a537e027a6b3d78bf9121bc5831d31\n"
100
112
  }
package/temp.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { TypedArray } from "@thi.ng/api";
2
2
  import type { ILogger } from "@thi.ng/logger";
3
- export declare const createTempFile: (body: string | TypedArray, logger?: ILogger | undefined, name?: string | undefined) => string;
4
- export declare const tempFilePath: (name?: string | undefined) => string;
3
+ export declare const createTempFile: (body: string | TypedArray, logger?: ILogger, name?: string) => string;
4
+ export declare const tempFilePath: (name?: string) => string;
5
5
  //# sourceMappingURL=temp.d.ts.map
package/temp.js CHANGED
@@ -3,7 +3,7 @@ import { randomID } from "@thi.ng/random/random-id";
3
3
  import { realpathSync, writeFileSync } from "fs";
4
4
  import { tmpdir } from "os";
5
5
  import { sep } from "path";
6
- import { ensureDirForFile } from "./ensure-dir.js";
6
+ import { ensureDirForFile } from "./dir.js";
7
7
  export const createTempFile = (body, logger, name) => {
8
8
  const path = tempFilePath(name);
9
9
  logger && logger.debug("creating temp file:", path);
package/text.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { ILogger } from "@thi.ng/logger";
2
- export declare const readText: (path: string, logger?: ILogger | undefined) => string;
2
+ export declare const readText: (path: string, logger?: ILogger) => string;
3
3
  /**
4
4
  * Writes `body` as UTF-8 file to given `path`. If `dryRun` is true (default:
5
5
  * false), the file WON'T be written, however if a `logger` is provided then at
@@ -10,5 +10,5 @@ export declare const readText: (path: string, logger?: ILogger | undefined) => s
10
10
  * @param logger
11
11
  * @param dryRun
12
12
  */
13
- export declare const writeText: (path: string, body: string | string[], logger?: ILogger | undefined, dryRun?: boolean) => void;
13
+ export declare const writeText: (path: string, body: string | string[], logger?: ILogger, dryRun?: boolean) => void;
14
14
  //# sourceMappingURL=text.d.ts.map
package/write.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- /// <reference types="node" />
2
1
  import type { TypedArray } from "@thi.ng/api";
3
2
  import type { ILogger } from "@thi.ng/logger";
4
3
  import { WriteFileOptions } from "fs";
@@ -13,5 +12,5 @@ import { WriteFileOptions } from "fs";
13
12
  * @param logger
14
13
  * @param dryRun
15
14
  */
16
- export declare const writeFile: (path: string, body: string | TypedArray, opts?: WriteFileOptions | undefined, logger?: ILogger | undefined, dryRun?: boolean) => void;
15
+ export declare const writeFile: (path: string, body: string | TypedArray, opts?: WriteFileOptions, logger?: ILogger, dryRun?: boolean) => void;
17
16
  //# sourceMappingURL=write.d.ts.map
package/write.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { isString } from "@thi.ng/checks/is-string";
2
2
  import { writeFileSync } from "fs";
3
- import { ensureDirForFile } from "./ensure-dir.js";
3
+ import { ensureDirForFile } from "./dir.js";
4
4
  /**
5
5
  * Writes `body` as to given `path` (using optional `opts` to define encoding).
6
6
  * If `dryRun` is true (default: false), the file WON'T be written, however if a
package/ensure-dir.d.ts DELETED
@@ -1,8 +0,0 @@
1
- /**
2
- * Checks if the directory for given file path already exists, and if not the
3
- * case, creates it. Returns true if the latter case.
4
- *
5
- * @param path
6
- */
7
- export declare const ensureDirForFile: (path: string) => boolean;
8
- //# sourceMappingURL=ensure-dir.d.ts.map
package/ensure-dir.js DELETED
@@ -1,14 +0,0 @@
1
- import { existsSync, mkdirSync } from "fs";
2
- import { sep } from "path";
3
- /**
4
- * Checks if the directory for given file path already exists, and if not the
5
- * case, creates it. Returns true if the latter case.
6
- *
7
- * @param path
8
- */
9
- export const ensureDirForFile = (path) => {
10
- const dir = path.substring(0, path.lastIndexOf(sep));
11
- return !existsSync(dir)
12
- ? (mkdirSync(dir, { recursive: true }), true)
13
- : false;
14
- };