@thi.ng/file-io 0.2.0 → 0.3.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/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-05-20T09:18:30Z
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/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.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 | undefined, algo?: HashAlgo) => string;
4
+ export declare const stringHash: (src: string, logger?: ILogger | undefined, 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/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.0",
4
4
  "description": "Assorted file I/O utils (with logging support) for NodeJS",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "devDependencies": {
43
43
  "@microsoft/api-extractor": "^7.23.1",
44
- "@thi.ng/testament": "^0.2.6",
44
+ "@thi.ng/testament": "^0.2.7",
45
45
  "rimraf": "^3.0.2",
46
46
  "tools": "^0.0.1",
47
47
  "typedoc": "^0.22.15",
@@ -49,6 +49,9 @@
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": "e5c61ba561129c4a4b874aee1541b0c051ab6638\n"
100
112
  }
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/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
- };