@thi.ng/file-io
Advanced tools
Comparing version 0.1.0 to 0.2.0
# Change Log | ||
- **Last updated**: 2022-05-18T12:59:20Z | ||
- **Last updated**: 2022-05-19T13:17:54Z | ||
- **Generator**: [thi.ng/monopub](https://thi.ng/monopub) | ||
@@ -12,2 +12,8 @@ | ||
## [0.2.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/file-io@0.2.0) (2022-05-19) | ||
#### 🚀 Features | ||
- add dirs() iterator, add writeFile(), dry-run handling ([0279db8](https://github.com/thi-ng/umbrella/commit/0279db8)) | ||
## [0.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/file-io@0.1.0) (2022-05-18) | ||
@@ -14,0 +20,0 @@ |
import type { ILogger } from "@thi.ng/logger"; | ||
export declare const deleteFile: (path: string, logger?: ILogger | undefined) => void; | ||
/** | ||
* Deletes file at given path. If `dryRun` is true (default: false), the file | ||
* WON'T be deleted, however if a `logger` is provided then at least a dry-run | ||
* log message will be emitted. | ||
* | ||
* @param path | ||
* @param logger | ||
* @param dryRun | ||
*/ | ||
export declare const deleteFile: (path: string, logger?: ILogger | undefined, dryRun?: boolean) => void; | ||
//# sourceMappingURL=delete.d.ts.map |
import { unlinkSync } from "fs"; | ||
export const deleteFile = (path, logger) => { | ||
logger && logger.debug("deleting file:", path); | ||
/** | ||
* Deletes file at given path. If `dryRun` is true (default: false), the file | ||
* WON'T be deleted, however if a `logger` is provided then at least a dry-run | ||
* log message will be emitted. | ||
* | ||
* @param path | ||
* @param logger | ||
* @param dryRun | ||
*/ | ||
export const deleteFile = (path, logger, dryRun = false) => { | ||
logger && logger.info(`${dryRun ? "[dryrun] " : ""}deleting file: ${path}`); | ||
if (dryRun) | ||
return; | ||
unlinkSync(path); | ||
}; |
@@ -6,2 +6,9 @@ import type { ILogger } from "@thi.ng/logger"; | ||
* | ||
* @remarks | ||
* If NO `match` is given, all files will be matched. Directory names will not | ||
* be tested and are always traversed (up to given `maxDepth`). | ||
* | ||
* The optional `logger` is only used to log errors for files which couldn't be | ||
* accessed. | ||
* | ||
* @param dir | ||
@@ -12,3 +19,17 @@ * @param match | ||
*/ | ||
export declare const files: (dir: string, match: string | RegExp, maxDepth?: number, logger?: ILogger | undefined) => IterableIterator<string>; | ||
export declare const files: (dir: string, match?: string | RegExp, maxDepth?: number, logger?: ILogger | undefined) => IterableIterator<string>; | ||
/** | ||
* Similar to {@link files}, however yields iterator of only matching | ||
* sub-directories in given `dir`. Normal files are being ignored. | ||
* | ||
* @remarks | ||
* Unlike the regex matching in {@link files}, here the regex will be applied to | ||
* the _full_ sub-path (starting with `dir`) in order to determine a match. | ||
* | ||
* @param dir | ||
* @param match | ||
* @param maxDepth | ||
* @param logger | ||
*/ | ||
export declare const dirs: (dir: string, match?: string | RegExp, maxDepth?: number, logger?: ILogger | undefined) => IterableIterator<string>; | ||
//# sourceMappingURL=files.d.ts.map |
58
files.js
@@ -8,2 +8,9 @@ import { isString } from "@thi.ng/checks/is-string"; | ||
* | ||
* @remarks | ||
* If NO `match` is given, all files will be matched. Directory names will not | ||
* be tested and are always traversed (up to given `maxDepth`). | ||
* | ||
* The optional `logger` is only used to log errors for files which couldn't be | ||
* accessed. | ||
* | ||
* @param dir | ||
@@ -14,18 +21,16 @@ * @param match | ||
*/ | ||
export const files = (dir, match, maxDepth = Infinity, logger) => __files(dir, match, logger, maxDepth, 0); | ||
function* __files(dir, match, logger, maxDepth = Infinity, depth = 0) { | ||
export const files = (dir, match = "", maxDepth = Infinity, logger) => __files(dir, match, logger, maxDepth, 0); | ||
function* __files(dir, match = "", logger, maxDepth = Infinity, depth = 0) { | ||
if (depth >= maxDepth) | ||
return; | ||
const re = isString(match) | ||
? new RegExp(`${match.replace(/\./g, "\\.")}$`) | ||
: match; | ||
const re = __ensureRegEx(match); | ||
for (let f of readdirSync(dir)) { | ||
const curr = dir + sep + f; | ||
try { | ||
if (re.test(f)) { | ||
if (statSync(curr).isDirectory()) { | ||
yield* __files(curr, match, logger, maxDepth, depth + 1); | ||
} | ||
else if (re.test(f)) { | ||
yield curr; | ||
} | ||
else if (statSync(curr).isDirectory()) { | ||
yield* __files(curr, match, logger, maxDepth, depth + 1); | ||
} | ||
} | ||
@@ -38,1 +43,36 @@ catch (e) { | ||
} | ||
/** | ||
* Similar to {@link files}, however yields iterator of only matching | ||
* sub-directories in given `dir`. Normal files are being ignored. | ||
* | ||
* @remarks | ||
* Unlike the regex matching in {@link files}, here the regex will be applied to | ||
* the _full_ sub-path (starting with `dir`) in order to determine a match. | ||
* | ||
* @param dir | ||
* @param match | ||
* @param maxDepth | ||
* @param logger | ||
*/ | ||
export const dirs = (dir, match = "", maxDepth = Infinity, logger) => __dirs(dir, match, logger, maxDepth, 0); | ||
function* __dirs(dir, match = "", logger, maxDepth = Infinity, depth = 0) { | ||
if (depth >= maxDepth) | ||
return; | ||
const re = __ensureRegEx(match); | ||
for (let f of readdirSync(dir)) { | ||
const curr = dir + sep + f; | ||
try { | ||
if (statSync(curr).isDirectory()) { | ||
if (re.test(curr)) | ||
yield curr; | ||
yield* __dirs(curr, match, logger, maxDepth, depth + 1); | ||
} | ||
} | ||
catch (e) { | ||
logger && | ||
logger.warn(`ignoring file/dir: ${f} (${e.message})`); | ||
} | ||
} | ||
} | ||
/** @internal */ | ||
const __ensureRegEx = (match) => isString(match) ? new RegExp(`${match.replace(/\./g, "\\.")}$`) : match; |
@@ -7,2 +7,3 @@ export * from "./delete.js"; | ||
export * from "./text.js"; | ||
export * from "./write.js"; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -7,1 +7,2 @@ export * from "./delete.js"; | ||
export * from "./text.js"; | ||
export * from "./write.js"; |
import type { Fn3, NumOrString } from "@thi.ng/api"; | ||
import type { ILogger } from "@thi.ng/logger"; | ||
export declare const readJSON: (path: string, logger?: ILogger | undefined) => any; | ||
export declare const writeJSON: (path: string, obj: any, replacer?: Fn3<any, string, any, any> | NumOrString[] | null | undefined, space?: NumOrString | undefined, logger?: ILogger | undefined) => void; | ||
/** | ||
* Serializes `obj` to JSON and writes result to UTF-8 file `path`. See | ||
* {@link writeText} for more details. | ||
* | ||
* @remarks | ||
* The `replacer` and `space` args are the same as supported by | ||
* `JSON.stringify()`. | ||
* | ||
* @param path | ||
* @param obj | ||
* @param replacer | ||
* @param space | ||
* @param logger | ||
* @param dryRun | ||
*/ | ||
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; | ||
//# sourceMappingURL=json.d.ts.map |
17
json.js
import { readText, writeText } from "./text.js"; | ||
export const readJSON = (path, logger) => JSON.parse(readText(path, logger)); | ||
export const writeJSON = (path, obj, replacer, space, logger) => writeText(path, JSON.stringify(obj, replacer, space), logger); | ||
/** | ||
* Serializes `obj` to JSON and writes result to UTF-8 file `path`. See | ||
* {@link writeText} for more details. | ||
* | ||
* @remarks | ||
* The `replacer` and `space` args are the same as supported by | ||
* `JSON.stringify()`. | ||
* | ||
* @param path | ||
* @param obj | ||
* @param replacer | ||
* @param space | ||
* @param logger | ||
* @param dryRun | ||
*/ | ||
export const writeJSON = (path, obj, replacer, space, logger, dryRun = false) => writeText(path, JSON.stringify(obj, replacer, space) + "\n", logger, dryRun); |
{ | ||
"name": "@thi.ng/file-io", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Assorted file I/O utils (with logging support) for NodeJS", | ||
@@ -90,2 +90,5 @@ "type": "module", | ||
"default": "./text.js" | ||
}, | ||
"./write": { | ||
"default": "./write.js" | ||
} | ||
@@ -97,3 +100,3 @@ }, | ||
}, | ||
"gitHead": "f696c94665a3adef4fa475d99dd2e18ceb15863f\n" | ||
"gitHead": "ec1685021284dc0e98bbfe67abefc3d1f6c0f7dd\n" | ||
} |
import type { ILogger } from "@thi.ng/logger"; | ||
export declare const readText: (path: string, logger?: ILogger | undefined) => string; | ||
export declare const writeText: (path: string, body: string | string[], logger?: ILogger | undefined) => void; | ||
/** | ||
* Writes `body` as UTF-8 file to given `path`. If `dryRun` is true (default: | ||
* false), the file WON'T be written, however if a `logger` is provided then at | ||
* least a dry-run log message will be emitted. | ||
* | ||
* @param path | ||
* @param body | ||
* @param logger | ||
* @param dryRun | ||
*/ | ||
export declare const writeText: (path: string, body: string | string[], logger?: ILogger | undefined, dryRun?: boolean) => void; | ||
//# sourceMappingURL=text.d.ts.map |
20
text.js
import { isArray } from "@thi.ng/checks/is-array"; | ||
import { readFileSync, writeFileSync } from "fs"; | ||
import { ensureDirForFile } from "./ensure-dir.js"; | ||
import { readFileSync } from "fs"; | ||
import { writeFile } from "./write.js"; | ||
export const readText = (path, logger) => { | ||
@@ -8,6 +8,12 @@ logger && logger.debug("reading file:", path); | ||
}; | ||
export const writeText = (path, body, logger) => { | ||
logger && logger.debug("writing file:", path); | ||
ensureDirForFile(path); | ||
writeFileSync(path, isArray(body) ? body.join("\n") : body, "utf-8"); | ||
}; | ||
/** | ||
* Writes `body` as UTF-8 file to given `path`. If `dryRun` is true (default: | ||
* false), the file WON'T be written, however if a `logger` is provided then at | ||
* least a dry-run log message will be emitted. | ||
* | ||
* @param path | ||
* @param body | ||
* @param logger | ||
* @param dryRun | ||
*/ | ||
export const writeText = (path, body, logger, dryRun = false) => writeFile(path, isArray(body) ? body.join("\n") : body, "utf-8", logger, dryRun); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27493
20
292
6
1