@jsenv/filesystem 2.6.1 → 2.7.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 CHANGED
@@ -24,6 +24,7 @@ export { ensureWindowsDriveLetter } from "./src/ensureWindowsDriveLetter.js"
24
24
  export { copyFileSystemNode } from "./src/copyFileSystemNode.js"
25
25
  export { copyDirectoryContent } from "./src/copyDirectoryContent.js"
26
26
  export { fileSystemPathToUrl } from "./src/fileSystemPathToUrl.js"
27
+ export { getRealFileSystemUrlSync } from "./src/getRealFileSystemUrlSync.js"
27
28
  export { grantPermissionsOnFileSystemNode } from "./src/grantPermissionsOnFileSystemNode.js"
28
29
  export { isFileSystemPath } from "./src/isFileSystemPath.js"
29
30
  export { listFilesMatching } from "./src/listFilesMatching.js"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/filesystem",
3
- "version": "2.6.1",
3
+ "version": "2.7.0",
4
4
  "description": "Collection of functions to interact with filesystem in Node.js",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -44,14 +44,14 @@
44
44
  },
45
45
  "devDependencies": {
46
46
  "@jsenv/assert": "2.4.1",
47
- "@jsenv/core": "25.1.0",
47
+ "@jsenv/core": "25.2.1",
48
48
  "@jsenv/eslint-config": "16.0.9",
49
49
  "@jsenv/github-release-package": "1.2.3",
50
50
  "@jsenv/importmap-eslint-resolver": "5.2.2",
51
51
  "@jsenv/importmap-node-module": "5.1.0",
52
52
  "@jsenv/package-publish": "1.6.2",
53
53
  "@jsenv/performance-impact": "2.2.1",
54
- "eslint": "8.6.0",
54
+ "eslint": "8.7.0",
55
55
  "eslint-plugin-import": "2.25.4",
56
56
  "prettier": "2.5.1"
57
57
  }
@@ -0,0 +1,48 @@
1
+ import { readdirSync, realpathSync } from "node:fs"
2
+
3
+ import { fileSystemPathToUrl } from "./fileSystemPathToUrl.js"
4
+ import { urlToFileSystemPath } from "./urlToFileSystemPath.js"
5
+
6
+ export const getRealFileSystemUrlSync = (
7
+ fileUrl,
8
+ { followLink = true } = {},
9
+ ) => {
10
+ const pathname = new URL(fileUrl).pathname
11
+ const parts = pathname.slice(1).split("/")
12
+ let reconstructedFileUrl = `file:///`
13
+ if (process.platform === "win32") {
14
+ const windowsDriveLetter = parts.shift()
15
+ reconstructedFileUrl += `${windowsDriveLetter}/`
16
+ }
17
+ let i = 0
18
+ // eslint-disable-next-line no-constant-condition
19
+ while (true) {
20
+ const name = parts[i]
21
+ i++
22
+ const namesOnFileSystem = readdirSync(
23
+ // When Node.js receives "C:/" on windows it returns
24
+ // the process.cwd() directory content...
25
+ // This can be fixed by passing "file:///C:/" directly but as a url object
26
+ new URL(reconstructedFileUrl),
27
+ )
28
+ const foundOnFilesystem = namesOnFileSystem.includes(name)
29
+ if (foundOnFilesystem) {
30
+ reconstructedFileUrl += name
31
+ } else {
32
+ const nameOnFileSystem = namesOnFileSystem.find(
33
+ (nameCandidate) => nameCandidate.toLowerCase() === name.toLowerCase(),
34
+ )
35
+ reconstructedFileUrl += nameOnFileSystem
36
+ }
37
+ if (i === parts.length) {
38
+ if (followLink) {
39
+ const realPath = realpathSync.native(
40
+ urlToFileSystemPath(reconstructedFileUrl),
41
+ )
42
+ return fileSystemPathToUrl(realPath)
43
+ }
44
+ return reconstructedFileUrl
45
+ }
46
+ reconstructedFileUrl += "/"
47
+ }
48
+ }
@@ -1,5 +1,7 @@
1
- import { statSync } from "fs"
1
+ import { statSync } from "node:fs"
2
+
2
3
  import { urlToFileSystemPath } from "../urlToFileSystemPath.js"
4
+
3
5
  import { statsToType } from "./statsToType.js"
4
6
 
5
7
  export const fileSystemNodeToTypeOrNull = (url) => {
@@ -31,16 +31,18 @@ export const writeSymbolicLink = async ({
31
31
  }) => {
32
32
  const fromUrl = assertAndNormalizeFileUrl(from)
33
33
  const toInfo = getToInfo(to, fromUrl)
34
-
34
+ // Node.js doc at https://nodejs.org/api/fs.html#fssymlinktarget-path-type-callback
35
+ // states the following:
36
+ // "If the type argument is not set, Node.js will autodetect
37
+ // target type and use 'file' or 'dir'"
38
+ // In practice, if code don't specify "type" on windows, you later get EPERM errors
39
+ // when doing operation on the symlink such as "fs.stat"
35
40
  if (isWindows && typeof type === "undefined") {
36
- // without this if you write a symbolic link without specifying the type on windows
37
- // you later get EPERM when doing stat on the symlink
38
41
  const toStats = await readFileSystemNodeStat(toInfo.url, {
39
42
  nullIfNotFound: true,
40
43
  })
41
44
  type = toStats && toStats.isDirectory() ? "dir" : "file"
42
45
  }
43
-
44
46
  const symbolicLinkPath = urlToFileSystemPath(fromUrl)
45
47
  try {
46
48
  await symlink(toInfo.value, symbolicLinkPath, type)