@jsenv/filesystem 2.7.2 → 3.2.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.
Files changed (37) hide show
  1. package/README.md +1 -1
  2. package/main.js +14 -12
  3. package/package.json +3 -4
  4. package/src/assertDirectoryPresence.js +2 -2
  5. package/src/assertFilePresence.js +2 -2
  6. package/src/collectDirectoryMatchReport.js +2 -2
  7. package/src/collectFiles.js +2 -2
  8. package/src/copyDirectoryContent.js +6 -6
  9. package/src/{copyFileSystemNode.js → copyEntry.js} +15 -18
  10. package/src/ensureEmptyDirectory.js +4 -4
  11. package/src/ensureParentDirectories.js +2 -1
  12. package/src/fileSystemRootUrl.js +2 -0
  13. package/src/grantPermissionsOnEntry.js +20 -0
  14. package/src/internal/createWatcher.js +1 -1
  15. package/src/internal/guard_second_call.js +20 -0
  16. package/src/internal/read_entry_info.js +20 -0
  17. package/src/moveDirectoryContent.js +6 -6
  18. package/src/{moveFileSystemNode.js → moveEntry.js} +10 -10
  19. package/src/moveUrl.js +10 -0
  20. package/src/readEntryModificationTime.js +6 -0
  21. package/src/{readFileSystemNodePermissions.js → readEntryPermissions.js} +1 -1
  22. package/src/{readFileSystemNodeStat.js → readEntryStat.js} +3 -3
  23. package/src/readFile.js +8 -7
  24. package/src/readFileSync.js +20 -0
  25. package/src/registerDirectoryLifecycle.js +201 -151
  26. package/src/registerFileLifecycle.js +39 -6
  27. package/src/{removeFileSystemNode.js → removeEntry.js} +4 -4
  28. package/src/{testFileSystemNodePermissions.js → testEntryPermissions.js} +3 -2
  29. package/src/writeDirectory.js +4 -3
  30. package/src/{writeFileSystemNodeModificationTime.js → writeEntryModificationTime.js} +3 -2
  31. package/src/{writeFileSystemNodePermissions.js → writeEntryPermissions.js} +3 -2
  32. package/src/writeFile.js +5 -6
  33. package/src/writeFileSync.js +20 -0
  34. package/src/writeSymbolicLink.js +4 -4
  35. package/src/grantPermissionsOnFileSystemNode.js +0 -20
  36. package/src/internal/fileSystemNodeToTypeOrNull.js +0 -18
  37. package/src/readFileSystemNodeModificationTime.js +0 -6
package/src/writeFile.js CHANGED
@@ -1,6 +1,6 @@
1
- import { promises } from "fs"
1
+ import { promises } from "node:fs"
2
+
2
3
  import { assertAndNormalizeFileUrl } from "./assertAndNormalizeFileUrl.js"
3
- import { urlToFileSystemPath } from "./urlToFileSystemPath.js"
4
4
  import { ensureParentDirectories } from "./ensureParentDirectories.js"
5
5
 
6
6
  // https://nodejs.org/dist/latest-v13.x/docs/api/fs.html#fs_fspromises_writefile_file_data_options
@@ -8,14 +8,13 @@ const { writeFile: writeFileNode } = promises
8
8
 
9
9
  export const writeFile = async (destination, content = "") => {
10
10
  const destinationUrl = assertAndNormalizeFileUrl(destination)
11
-
12
- const destinationPath = urlToFileSystemPath(destinationUrl)
11
+ const destinationUrlObject = new URL(destinationUrl)
13
12
  try {
14
- await writeFileNode(destinationPath, content)
13
+ await writeFileNode(destinationUrlObject, content)
15
14
  } catch (error) {
16
15
  if (error.code === "ENOENT") {
17
16
  await ensureParentDirectories(destinationUrl)
18
- await writeFileNode(destinationPath, content)
17
+ await writeFileNode(destinationUrlObject, content)
19
18
  return
20
19
  }
21
20
  throw error
@@ -0,0 +1,20 @@
1
+ import { writeFileSync as writeFileSyncNode, mkdirSync } from "node:fs"
2
+
3
+ import { assertAndNormalizeFileUrl } from "./assertAndNormalizeFileUrl.js"
4
+
5
+ export const writeFileSync = (destination, content = "") => {
6
+ const destinationUrl = assertAndNormalizeFileUrl(destination)
7
+ const destinationUrlObject = new URL(destinationUrl)
8
+ try {
9
+ writeFileSyncNode(destinationUrlObject, content)
10
+ } catch (error) {
11
+ if (error.code === "ENOENT") {
12
+ mkdirSync(new URL("./", destinationUrlObject), {
13
+ recursive: true,
14
+ })
15
+ writeFileSyncNode(destinationUrlObject, content)
16
+ return
17
+ }
18
+ throw error
19
+ }
20
+ }
@@ -6,9 +6,9 @@ import { urlToFileSystemPath } from "./urlToFileSystemPath.js"
6
6
  import { resolveUrl } from "./resolveUrl.js"
7
7
  import { isFileSystemPath } from "./isFileSystemPath.js"
8
8
  import { ensureParentDirectories } from "./ensureParentDirectories.js"
9
- import { readFileSystemNodeStat } from "./readFileSystemNodeStat.js"
9
+ import { readEntryStat } from "./readEntryStat.js"
10
10
  import { readSymbolicLink } from "./readSymbolicLink.js"
11
- import { removeFileSystemNode } from "./removeFileSystemNode.js"
11
+ import { removeEntry } from "./removeEntry.js"
12
12
 
13
13
  // https://nodejs.org/dist/latest-v13.x/docs/api/fs.html#fs_fspromises_symlink_target_path_type
14
14
  const { symlink } = promises
@@ -38,7 +38,7 @@ export const writeSymbolicLink = async ({
38
38
  // In practice, if code don't specify "type" on windows, you later get EPERM errors
39
39
  // when doing operation on the symlink such as "fs.stat"
40
40
  if (isWindows && typeof type === "undefined") {
41
- const toStats = await readFileSystemNodeStat(toInfo.url, {
41
+ const toStats = await readEntryStat(toInfo.url, {
42
42
  nullIfNotFound: true,
43
43
  })
44
44
  type = toStats && toStats.isDirectory() ? "dir" : "file"
@@ -60,7 +60,7 @@ export const writeSymbolicLink = async ({
60
60
  }
61
61
  }
62
62
  if (allowOverwrite) {
63
- await removeFileSystemNode(fromUrl)
63
+ await removeEntry(fromUrl)
64
64
  await symlink(toInfo.value, symbolicLinkPath, type)
65
65
  return
66
66
  }
@@ -1,20 +0,0 @@
1
- import { assertAndNormalizeFileUrl } from "./assertAndNormalizeFileUrl.js"
2
- import { readFileSystemNodePermissions } from "./readFileSystemNodePermissions.js"
3
- import { writeFileSystemNodePermissions } from "./writeFileSystemNodePermissions.js"
4
-
5
- export const grantPermissionsOnFileSystemNode = async (
6
- source,
7
- { read = false, write = false, execute = false },
8
- ) => {
9
- const sourceUrl = assertAndNormalizeFileUrl(source)
10
-
11
- const filePermissions = await readFileSystemNodePermissions(sourceUrl)
12
- await writeFileSystemNodePermissions(sourceUrl, {
13
- owner: { read, write, execute },
14
- group: { read, write, execute },
15
- others: { read, write, execute },
16
- })
17
- return async () => {
18
- await writeFileSystemNodePermissions(sourceUrl, filePermissions)
19
- }
20
- }
@@ -1,18 +0,0 @@
1
- import { statSync } from "node:fs"
2
-
3
- import { urlToFileSystemPath } from "../urlToFileSystemPath.js"
4
-
5
- import { statsToType } from "./statsToType.js"
6
-
7
- export const fileSystemNodeToTypeOrNull = (url) => {
8
- const path = urlToFileSystemPath(url)
9
- try {
10
- const stats = statSync(path)
11
- return statsToType(stats)
12
- } catch (e) {
13
- if (e.code === "ENOENT") {
14
- return null
15
- }
16
- throw e
17
- }
18
- }
@@ -1,6 +0,0 @@
1
- import { readFileSystemNodeStat } from "./readFileSystemNodeStat.js"
2
-
3
- export const readFileSystemNodeModificationTime = async (source) => {
4
- const stats = await readFileSystemNodeStat(source)
5
- return Math.floor(stats.mtimeMs)
6
- }