@jsenv/filesystem 3.0.0 → 3.1.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/main.js +1 -0
- package/package.json +1 -1
- package/src/readFile.js +8 -7
- package/src/readFileSync.js +20 -0
- package/src/writeFile.js +3 -5
package/main.js
CHANGED
|
@@ -33,6 +33,7 @@ export { moveEntry } from "./src/moveEntry.js"
|
|
|
33
33
|
export { moveUrl } from "./src/moveUrl.js"
|
|
34
34
|
export { readDirectory } from "./src/readDirectory.js"
|
|
35
35
|
export { readFile } from "./src/readFile.js"
|
|
36
|
+
export { readFileSync } from "./src/readFileSync.js"
|
|
36
37
|
export { readEntryModificationTime } from "./src/readEntryModificationTime.js"
|
|
37
38
|
export { readEntryPermissions } from "./src/readEntryPermissions.js"
|
|
38
39
|
export { readEntryStat } from "./src/readEntryStat.js"
|
package/package.json
CHANGED
package/src/readFile.js
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
import { promisify } from "util"
|
|
2
|
-
import { readFile as readFileNode } from "fs"
|
|
1
|
+
import { promisify } from "node:util"
|
|
2
|
+
import { readFile as readFileNode } from "node:fs"
|
|
3
|
+
|
|
3
4
|
import { assertAndNormalizeFileUrl } from "./assertAndNormalizeFileUrl.js"
|
|
4
|
-
import { urlToFileSystemPath } from "./urlToFileSystemPath.js"
|
|
5
5
|
|
|
6
6
|
const readFilePromisified = promisify(readFileNode)
|
|
7
|
-
export const readFile = async (value, { as = "
|
|
7
|
+
export const readFile = async (value, { as = "buffer" } = {}) => {
|
|
8
8
|
const fileUrl = assertAndNormalizeFileUrl(value)
|
|
9
|
-
const
|
|
10
|
-
const buffer = await readFilePromisified(filePath)
|
|
9
|
+
const buffer = await readFilePromisified(new URL(fileUrl))
|
|
11
10
|
if (as === "buffer") {
|
|
12
11
|
return buffer
|
|
13
12
|
}
|
|
@@ -17,5 +16,7 @@ export const readFile = async (value, { as = "string" } = {}) => {
|
|
|
17
16
|
if (as === "json") {
|
|
18
17
|
return JSON.parse(buffer.toString())
|
|
19
18
|
}
|
|
20
|
-
throw new Error(
|
|
19
|
+
throw new Error(
|
|
20
|
+
`"as" must be one of "buffer","string","json" received "${as}"`,
|
|
21
|
+
)
|
|
21
22
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { readFileSync as readFileSyncNode } from "node:fs"
|
|
2
|
+
|
|
3
|
+
import { assertAndNormalizeFileUrl } from "./assertAndNormalizeFileUrl.js"
|
|
4
|
+
|
|
5
|
+
export const readFileSync = (value, { as = "buffer" } = {}) => {
|
|
6
|
+
const fileUrl = assertAndNormalizeFileUrl(value)
|
|
7
|
+
const buffer = readFileSyncNode(new URL(fileUrl))
|
|
8
|
+
if (as === "buffer") {
|
|
9
|
+
return buffer
|
|
10
|
+
}
|
|
11
|
+
if (as === "string") {
|
|
12
|
+
return buffer.toString()
|
|
13
|
+
}
|
|
14
|
+
if (as === "json") {
|
|
15
|
+
return JSON.parse(buffer.toString())
|
|
16
|
+
}
|
|
17
|
+
throw new Error(
|
|
18
|
+
`"as" must be one of "buffer","string","json" received "${as}"`,
|
|
19
|
+
)
|
|
20
|
+
}
|
package/src/writeFile.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { promises } from "node:fs"
|
|
2
2
|
|
|
3
3
|
import { assertAndNormalizeFileUrl } from "./assertAndNormalizeFileUrl.js"
|
|
4
|
-
import { urlToFileSystemPath } from "./urlToFileSystemPath.js"
|
|
5
4
|
import { ensureParentDirectories } from "./ensureParentDirectories.js"
|
|
6
5
|
|
|
7
6
|
// https://nodejs.org/dist/latest-v13.x/docs/api/fs.html#fs_fspromises_writefile_file_data_options
|
|
@@ -9,14 +8,13 @@ const { writeFile: writeFileNode } = promises
|
|
|
9
8
|
|
|
10
9
|
export const writeFile = async (destination, content = "") => {
|
|
11
10
|
const destinationUrl = assertAndNormalizeFileUrl(destination)
|
|
12
|
-
|
|
13
|
-
const destinationPath = urlToFileSystemPath(destinationUrl)
|
|
11
|
+
const destinationUrlObject = new URL(destinationUrl)
|
|
14
12
|
try {
|
|
15
|
-
await writeFileNode(
|
|
13
|
+
await writeFileNode(destinationUrlObject, content)
|
|
16
14
|
} catch (error) {
|
|
17
15
|
if (error.code === "ENOENT") {
|
|
18
16
|
await ensureParentDirectories(destinationUrl)
|
|
19
|
-
await writeFileNode(
|
|
17
|
+
await writeFileNode(destinationUrlObject, content)
|
|
20
18
|
return
|
|
21
19
|
}
|
|
22
20
|
throw error
|