@jsenv/filesystem 4.2.2 → 4.2.4

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 (47) hide show
  1. package/README.md +17 -17
  2. package/package.json +4 -4
  3. package/src/assertDirectoryPresence.js +10 -10
  4. package/src/assertFilePresence.js +10 -10
  5. package/src/bufferToEtag.js +11 -11
  6. package/src/collectDirectoryMatchReport.js +38 -38
  7. package/src/collectFiles.js +35 -35
  8. package/src/comparePathnames.js +23 -23
  9. package/src/copyDirectoryContent.js +37 -37
  10. package/src/copyEntry.js +88 -88
  11. package/src/directory_url_validation.js +14 -14
  12. package/src/ensureEmptyDirectory.js +13 -13
  13. package/src/ensureParentDirectories.js +9 -9
  14. package/src/ensureWindowsDriveLetter.js +19 -19
  15. package/src/fileSystemRootUrl.js +1 -1
  16. package/src/file_url_validation.js +14 -14
  17. package/src/getRealFileSystemUrlSync.js +24 -24
  18. package/src/grantPermissionsOnEntry.js +9 -9
  19. package/src/internal/createWatcher.js +12 -12
  20. package/src/internal/getCommandArgument.js +7 -7
  21. package/src/internal/guard_second_call.js +20 -20
  22. package/src/internal/permissions.js +26 -26
  23. package/src/internal/read_entry_info.js +8 -8
  24. package/src/internal/statsToType.js +9 -9
  25. package/src/internal/track_resources.js +12 -12
  26. package/src/internal/urlTargetsSameFileSystemPath.js +4 -4
  27. package/src/listFilesMatching.js +5 -5
  28. package/src/main.js +35 -35
  29. package/src/moveDirectoryContent.js +37 -37
  30. package/src/moveEntry.js +44 -44
  31. package/src/readDirectory.js +21 -21
  32. package/src/readEntryModificationTime.js +4 -4
  33. package/src/readEntryPermissions.js +10 -10
  34. package/src/readEntryStat.js +27 -27
  35. package/src/readFile.js +12 -12
  36. package/src/readFileSync.js +9 -9
  37. package/src/readSymbolicLink.js +9 -9
  38. package/src/registerDirectoryLifecycle.js +118 -118
  39. package/src/registerFileLifecycle.js +66 -66
  40. package/src/removeEntry.js +81 -81
  41. package/src/testEntryPermissions.js +15 -15
  42. package/src/writeDirectory.js +17 -17
  43. package/src/writeEntryModificationTime.js +10 -10
  44. package/src/writeEntryPermissions.js +26 -26
  45. package/src/writeFile.js +16 -16
  46. package/src/writeFileSync.js +10 -10
  47. package/src/writeSymbolicLink.js +39 -39
@@ -1,22 +1,22 @@
1
- import { utimes } from "node:fs"
1
+ import { utimes } from "node:fs";
2
2
 
3
- import { assertAndNormalizeFileUrl } from "./file_url_validation.js"
3
+ import { assertAndNormalizeFileUrl } from "./file_url_validation.js";
4
4
 
5
5
  export const writeEntryModificationTime = (source, mtime) => {
6
- const sourceUrl = assertAndNormalizeFileUrl(source)
6
+ const sourceUrl = assertAndNormalizeFileUrl(source);
7
7
  const mtimeValue =
8
- typeof mtime === "number" ? new Date(Math.floor(mtime)) : mtime
8
+ typeof mtime === "number" ? new Date(Math.floor(mtime)) : mtime;
9
9
  // reading atime mutates its value so there is no use case I can think of
10
10
  // where we want to modify it
11
- const atimeValue = mtimeValue
11
+ const atimeValue = mtimeValue;
12
12
 
13
13
  return new Promise((resolve, reject) => {
14
14
  utimes(new URL(sourceUrl), atimeValue, mtimeValue, (error) => {
15
15
  if (error) {
16
- reject(error)
16
+ reject(error);
17
17
  } else {
18
- resolve()
18
+ resolve();
19
19
  }
20
- })
21
- })
22
- }
20
+ });
21
+ });
22
+ };
@@ -1,12 +1,12 @@
1
- import { chmod } from "node:fs"
1
+ import { chmod } from "node:fs";
2
2
 
3
- import { permissionsToBinaryFlags } from "./internal/permissions.js"
4
- import { assertAndNormalizeFileUrl } from "./file_url_validation.js"
3
+ import { permissionsToBinaryFlags } from "./internal/permissions.js";
4
+ import { assertAndNormalizeFileUrl } from "./file_url_validation.js";
5
5
 
6
6
  export const writeEntryPermissions = async (source, permissions) => {
7
- const sourceUrl = assertAndNormalizeFileUrl(source)
7
+ const sourceUrl = assertAndNormalizeFileUrl(source);
8
8
 
9
- let binaryFlags
9
+ let binaryFlags;
10
10
  if (typeof permissions === "object") {
11
11
  permissions = {
12
12
  owner: {
@@ -28,58 +28,58 @@ export const writeEntryPermissions = async (source, permissions) => {
28
28
  permissions,
29
29
  ),
30
30
  },
31
- }
32
- binaryFlags = permissionsToBinaryFlags(permissions)
31
+ };
32
+ binaryFlags = permissionsToBinaryFlags(permissions);
33
33
  } else {
34
- binaryFlags = permissions
34
+ binaryFlags = permissions;
35
35
  }
36
36
 
37
37
  return new Promise((resolve, reject) => {
38
38
  chmod(new URL(sourceUrl), binaryFlags, (error) => {
39
39
  if (error) {
40
- reject(error)
40
+ reject(error);
41
41
  } else {
42
- resolve()
42
+ resolve();
43
43
  }
44
- })
45
- })
46
- }
44
+ });
45
+ });
46
+ };
47
47
 
48
- const actionLevels = { read: 0, write: 1, execute: 2 }
49
- const subjectLevels = { others: 0, group: 1, owner: 2 }
48
+ const actionLevels = { read: 0, write: 1, execute: 2 };
49
+ const subjectLevels = { others: 0, group: 1, owner: 2 };
50
50
 
51
51
  const getPermissionOrComputeDefault = (action, subject, permissions) => {
52
52
  if (subject in permissions) {
53
- const subjectPermissions = permissions[subject]
53
+ const subjectPermissions = permissions[subject];
54
54
  if (action in subjectPermissions) {
55
- return subjectPermissions[action]
55
+ return subjectPermissions[action];
56
56
  }
57
57
 
58
- const actionLevel = actionLevels[action]
58
+ const actionLevel = actionLevels[action];
59
59
  const actionFallback = Object.keys(actionLevels).find(
60
60
  (actionFallbackCandidate) =>
61
61
  actionLevels[actionFallbackCandidate] > actionLevel &&
62
62
  actionFallbackCandidate in subjectPermissions,
63
- )
63
+ );
64
64
  if (actionFallback) {
65
- return subjectPermissions[actionFallback]
65
+ return subjectPermissions[actionFallback];
66
66
  }
67
67
  }
68
68
 
69
- const subjectLevel = subjectLevels[subject]
69
+ const subjectLevel = subjectLevels[subject];
70
70
  // do we have a subject with a stronger level (group or owner)
71
71
  // where we could read the action permission ?
72
72
  const subjectFallback = Object.keys(subjectLevels).find(
73
73
  (subjectFallbackCandidate) =>
74
74
  subjectLevels[subjectFallbackCandidate] > subjectLevel &&
75
75
  subjectFallbackCandidate in permissions,
76
- )
76
+ );
77
77
  if (subjectFallback) {
78
- const subjectPermissions = permissions[subjectFallback]
78
+ const subjectPermissions = permissions[subjectFallback];
79
79
  return action in subjectPermissions
80
80
  ? subjectPermissions[action]
81
- : getPermissionOrComputeDefault(action, subjectFallback, permissions)
81
+ : getPermissionOrComputeDefault(action, subjectFallback, permissions);
82
82
  }
83
83
 
84
- return false
85
- }
84
+ return false;
85
+ };
package/src/writeFile.js CHANGED
@@ -1,31 +1,31 @@
1
- import { writeFile as writeFileNode } from "node:fs"
1
+ import { writeFile as writeFileNode } from "node:fs";
2
2
 
3
- import { assertAndNormalizeFileUrl } from "./file_url_validation.js"
4
- import { ensureParentDirectories } from "./ensureParentDirectories.js"
3
+ import { assertAndNormalizeFileUrl } from "./file_url_validation.js";
4
+ import { ensureParentDirectories } from "./ensureParentDirectories.js";
5
5
 
6
6
  export const writeFile = async (destination, content = "") => {
7
- const destinationUrl = assertAndNormalizeFileUrl(destination)
8
- const destinationUrlObject = new URL(destinationUrl)
7
+ const destinationUrl = assertAndNormalizeFileUrl(destination);
8
+ const destinationUrlObject = new URL(destinationUrl);
9
9
  try {
10
- await writeFileNaive(destinationUrlObject, content)
10
+ await writeFileNaive(destinationUrlObject, content);
11
11
  } catch (error) {
12
12
  if (error.code === "ENOENT") {
13
- await ensureParentDirectories(destinationUrl)
14
- await writeFileNaive(destinationUrlObject, content)
15
- return
13
+ await ensureParentDirectories(destinationUrl);
14
+ await writeFileNaive(destinationUrlObject, content);
15
+ return;
16
16
  }
17
- throw error
17
+ throw error;
18
18
  }
19
- }
19
+ };
20
20
 
21
21
  const writeFileNaive = (urlObject, content) => {
22
22
  return new Promise((resolve, reject) => {
23
23
  writeFileNode(urlObject, content, (error) => {
24
24
  if (error) {
25
- reject(error)
25
+ reject(error);
26
26
  } else {
27
- resolve()
27
+ resolve();
28
28
  }
29
- })
30
- })
31
- }
29
+ });
30
+ });
31
+ };
@@ -1,20 +1,20 @@
1
- import { writeFileSync as writeFileSyncNode, mkdirSync } from "node:fs"
1
+ import { writeFileSync as writeFileSyncNode, mkdirSync } from "node:fs";
2
2
 
3
- import { assertAndNormalizeFileUrl } from "./file_url_validation.js"
3
+ import { assertAndNormalizeFileUrl } from "./file_url_validation.js";
4
4
 
5
5
  export const writeFileSync = (destination, content = "") => {
6
- const destinationUrl = assertAndNormalizeFileUrl(destination)
7
- const destinationUrlObject = new URL(destinationUrl)
6
+ const destinationUrl = assertAndNormalizeFileUrl(destination);
7
+ const destinationUrlObject = new URL(destinationUrl);
8
8
  try {
9
- writeFileSyncNode(destinationUrlObject, content)
9
+ writeFileSyncNode(destinationUrlObject, content);
10
10
  } catch (error) {
11
11
  if (error.code === "ENOENT") {
12
12
  mkdirSync(new URL("./", destinationUrlObject), {
13
13
  recursive: true,
14
- })
15
- writeFileSyncNode(destinationUrlObject, content)
16
- return
14
+ });
15
+ writeFileSyncNode(destinationUrlObject, content);
16
+ return;
17
17
  }
18
- throw error
18
+ throw error;
19
19
  }
20
- }
20
+ };
@@ -1,20 +1,20 @@
1
- import { promises } from "node:fs"
1
+ import { promises } from "node:fs";
2
2
  import {
3
3
  fileSystemPathToUrl,
4
4
  urlToFileSystemPath,
5
5
  resolveUrl,
6
6
  isFileSystemPath,
7
- } from "@jsenv/urls"
7
+ } from "@jsenv/urls";
8
8
 
9
- import { assertAndNormalizeFileUrl } from "./file_url_validation.js"
10
- import { ensureParentDirectories } from "./ensureParentDirectories.js"
11
- import { readEntryStat } from "./readEntryStat.js"
12
- import { readSymbolicLink } from "./readSymbolicLink.js"
13
- import { removeEntry } from "./removeEntry.js"
9
+ import { assertAndNormalizeFileUrl } from "./file_url_validation.js";
10
+ import { ensureParentDirectories } from "./ensureParentDirectories.js";
11
+ import { readEntryStat } from "./readEntryStat.js";
12
+ import { readSymbolicLink } from "./readSymbolicLink.js";
13
+ import { removeEntry } from "./removeEntry.js";
14
14
 
15
15
  // https://nodejs.org/dist/latest-v13.x/docs/api/fs.html#fs_fspromises_symlink_target_path_type
16
- const { symlink } = promises
17
- const isWindows = process.platform === "win32"
16
+ const { symlink } = promises;
17
+ const isWindows = process.platform === "win32";
18
18
 
19
19
  /**
20
20
  * Writes a symbolic link pointing from a filesystem node to an other
@@ -31,8 +31,8 @@ export const writeSymbolicLink = async ({
31
31
  allowUseless = false,
32
32
  allowOverwrite = false,
33
33
  }) => {
34
- const fromUrl = assertAndNormalizeFileUrl(from)
35
- const toInfo = getToInfo(to, fromUrl)
34
+ const fromUrl = assertAndNormalizeFileUrl(from);
35
+ const toInfo = getToInfo(to, fromUrl);
36
36
  // Node.js doc at https://nodejs.org/api/fs.html#fssymlinktarget-path-type-callback
37
37
  // states the following:
38
38
  // "If the type argument is not set, Node.js will autodetect
@@ -42,76 +42,76 @@ export const writeSymbolicLink = async ({
42
42
  if (isWindows && typeof type === "undefined") {
43
43
  const toStats = await readEntryStat(toInfo.url, {
44
44
  nullIfNotFound: true,
45
- })
46
- type = toStats && toStats.isDirectory() ? "dir" : "file"
45
+ });
46
+ type = toStats && toStats.isDirectory() ? "dir" : "file";
47
47
  }
48
- const symbolicLinkPath = urlToFileSystemPath(fromUrl)
48
+ const symbolicLinkPath = urlToFileSystemPath(fromUrl);
49
49
  try {
50
- await symlink(toInfo.value, symbolicLinkPath, type)
50
+ await symlink(toInfo.value, symbolicLinkPath, type);
51
51
  } catch (error) {
52
52
  if (error.code === "ENOENT") {
53
- await ensureParentDirectories(fromUrl)
54
- await symlink(toInfo.value, symbolicLinkPath, type)
55
- return
53
+ await ensureParentDirectories(fromUrl);
54
+ await symlink(toInfo.value, symbolicLinkPath, type);
55
+ return;
56
56
  }
57
57
  if (error.code === "EEXIST") {
58
58
  if (allowUseless) {
59
- const existingSymbolicLinkUrl = await readSymbolicLink(fromUrl)
59
+ const existingSymbolicLinkUrl = await readSymbolicLink(fromUrl);
60
60
  if (existingSymbolicLinkUrl === toInfo.url) {
61
- return
61
+ return;
62
62
  }
63
63
  }
64
64
  if (allowOverwrite) {
65
- await removeEntry(fromUrl)
66
- await symlink(toInfo.value, symbolicLinkPath, type)
67
- return
65
+ await removeEntry(fromUrl);
66
+ await symlink(toInfo.value, symbolicLinkPath, type);
67
+ return;
68
68
  }
69
69
  }
70
- throw error
70
+ throw error;
71
71
  }
72
- }
72
+ };
73
73
 
74
74
  const getToInfo = (to, fromUrl) => {
75
75
  if (typeof to === "string") {
76
76
  // absolute filesystem path
77
77
  if (isFileSystemPath(to)) {
78
- const url = fileSystemPathToUrl(to)
79
- const value = to
78
+ const url = fileSystemPathToUrl(to);
79
+ const value = to;
80
80
  return {
81
81
  url,
82
82
  value,
83
- }
83
+ };
84
84
  }
85
85
 
86
86
  // relative url
87
87
  if (to.startsWith("./") || to.startsWith("../")) {
88
- const url = resolveUrl(to, fromUrl)
89
- const value = to
88
+ const url = resolveUrl(to, fromUrl);
89
+ const value = to;
90
90
  return {
91
91
  url,
92
92
  value,
93
- }
93
+ };
94
94
  }
95
95
 
96
96
  // absolute url
97
- const url = resolveUrl(to, fromUrl)
98
- const value = urlToFileSystemPath(url)
97
+ const url = resolveUrl(to, fromUrl);
98
+ const value = urlToFileSystemPath(url);
99
99
  return {
100
100
  url,
101
101
  value,
102
- }
102
+ };
103
103
  }
104
104
 
105
105
  if (to instanceof URL) {
106
- const url = String(to)
107
- const value = urlToFileSystemPath(url)
106
+ const url = String(to);
107
+ const value = urlToFileSystemPath(url);
108
108
  return {
109
109
  url,
110
110
  value,
111
- }
111
+ };
112
112
  }
113
113
 
114
114
  throw new TypeError(
115
115
  `symbolic link to must be a string or an url, received ${to}`,
116
- )
117
- }
116
+ );
117
+ };