Created
February 7, 2021 20:44
-
-
Save Oaphi/bf725b37467b10b56f29edd7e17e4190 to your computer and use it in GitHub Desktop.
Recursive append Node.js utility
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { BaseEncodingOptions } from "fs"; | |
import { appendFile, mkdir } from "fs/promises"; | |
import { parse, sep } from "path"; | |
const recursiveAppend = async ( | |
path: string, | |
encoding: BaseEncodingOptions["encoding"] = "utf-8" | |
) => { | |
const parts = path.split(sep); | |
const lastPart = parts.pop(); | |
if (!lastPart) return; | |
const onlyDirs = parse(lastPart).ext === ""; | |
if (onlyDirs) parts.push(lastPart); | |
await mkdir(parts.join(sep), { recursive: true }); | |
if (!onlyDirs) { | |
await appendFile(path, "", { encoding }); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment