@jsenv/filesystem
Advanced tools
Comparing version 3.0.0 to 3.1.0
@@ -36,2 +36,3 @@ // we won't internalize @jsenv/url-meta | ||
export { readFile } from "./src/readFile.js" | ||
export { readFileSync } from "./src/readFileSync.js" | ||
export { readEntryModificationTime } from "./src/readEntryModificationTime.js" | ||
@@ -38,0 +39,0 @@ export { readEntryPermissions } from "./src/readEntryPermissions.js" |
{ | ||
"name": "@jsenv/filesystem", | ||
"version": "3.0.0", | ||
"version": "3.1.0", | ||
"description": "Collection of functions to interact with filesystem in Node.js", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -1,11 +0,10 @@ | ||
import { promisify } from "util" | ||
import { readFile as readFileNode } from "fs" | ||
import { promisify } from "node:util" | ||
import { readFile as readFileNode } from "node:fs" | ||
import { assertAndNormalizeFileUrl } from "./assertAndNormalizeFileUrl.js" | ||
import { urlToFileSystemPath } from "./urlToFileSystemPath.js" | ||
const readFilePromisified = promisify(readFileNode) | ||
export const readFile = async (value, { as = "string" } = {}) => { | ||
export const readFile = async (value, { as = "buffer" } = {}) => { | ||
const fileUrl = assertAndNormalizeFileUrl(value) | ||
const filePath = urlToFileSystemPath(fileUrl) | ||
const buffer = await readFilePromisified(filePath) | ||
const buffer = await readFilePromisified(new URL(fileUrl)) | ||
if (as === "buffer") { | ||
@@ -20,3 +19,5 @@ return buffer | ||
} | ||
throw new Error(`as must be one of buffer,string,json, received ${as}.`) | ||
throw new Error( | ||
`"as" must be one of "buffer","string","json" received "${as}"`, | ||
) | ||
} |
import { promises } from "node:fs" | ||
import { assertAndNormalizeFileUrl } from "./assertAndNormalizeFileUrl.js" | ||
import { urlToFileSystemPath } from "./urlToFileSystemPath.js" | ||
import { ensureParentDirectories } from "./ensureParentDirectories.js" | ||
@@ -12,10 +11,9 @@ | ||
const destinationUrl = assertAndNormalizeFileUrl(destination) | ||
const destinationPath = urlToFileSystemPath(destinationUrl) | ||
const destinationUrlObject = new URL(destinationUrl) | ||
try { | ||
await writeFileNode(destinationPath, content) | ||
await writeFileNode(destinationUrlObject, content) | ||
} catch (error) { | ||
if (error.code === "ENOENT") { | ||
await ensureParentDirectories(destinationUrl) | ||
await writeFileNode(destinationPath, content) | ||
await writeFileNode(destinationUrlObject, content) | ||
return | ||
@@ -22,0 +20,0 @@ } |
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
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
100190
69
2806
3