@jsenv/filesystem 2.5.1 → 2.7.1

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/README.md CHANGED
@@ -18,10 +18,13 @@ console.log(relativeUrl) // "src/file.js"
18
18
  ```js
19
19
  import { listFilesMatching } from "@jsenv/filesystem"
20
20
 
21
- const jsFiles = await listFilesMatching(
22
- "./**/*.js",
23
- new URL("./", import.meta.url),
24
- )
21
+ const jsFiles = await listFilesMatching({
22
+ directoryUrl: new URL("./", import.meta.url),
23
+ patterns: {
24
+ "./**/*.js": true,
25
+ "./**/*.test.js": false,
26
+ },
27
+ })
25
28
  ```
26
29
 
27
30
  ```console
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.5.1",
3
+ "version": "2.7.1",
4
4
  "description": "Collection of functions to interact with filesystem in Node.js",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -13,57 +13,46 @@
13
13
  "url": "https://github.com/jsenv/filesystem"
14
14
  },
15
15
  "engines": {
16
- "node": ">=14.17.0"
16
+ "node": ">=16.13.0"
17
17
  },
18
18
  "publishConfig": {
19
- "access": "public",
20
- "registry": "https://registry.npmjs.org"
19
+ "access": "public"
21
20
  },
22
21
  "type": "module",
23
22
  "exports": {
24
23
  ".": {
25
- "import": "./main.js",
26
- "require": "./dist/commonjs/jsenv_filesystem.cjs"
24
+ "import": "./main.js"
27
25
  },
28
26
  "./*": "./*"
29
27
  },
28
+ "main": "./main.js",
30
29
  "files": [
31
- "/dist/",
32
30
  "/src/",
33
31
  "/main.js"
34
32
  ],
35
33
  "scripts": {
36
- "eslint-check": "node ./node_modules/eslint/bin/eslint.js .",
37
- "generate-importmap": "node ./script/importmap/generate_importmap.mjs",
38
- "measure-performances": "node ./script/performance/generate_performance_report.mjs --local",
39
- "dist": "npm run build",
40
- "build": "node --experimental-import-meta-resolve ./script/build/build.mjs",
34
+ "eslint": "node ./node_modules/eslint/bin/eslint.js .",
35
+ "importmap": "node ./script/importmap/importmap.mjs",
36
+ "performance": "node --expose-gc ./script/performance/performance.mjs --log",
41
37
  "test": "node ./script/test/test.mjs",
42
38
  "test-with-coverage": "npm run test -- --coverage",
43
- "prettier-format": "node ./script/prettier/prettier_format.mjs",
44
- "prettier-format-stage": "npm run prettier-format -- --staged",
45
- "prettier-check": "npm run prettier-format -- --dry-run",
46
- "prepublishOnly": "node ./script/publish/remove_postinstall.mjs && npm run dist",
47
- "postpublish": "node ./script/publish/restore_postinstall.mjs"
39
+ "prettier": "prettier --write ."
48
40
  },
49
41
  "dependencies": {
50
- "@jsenv/abort": "3.1.1",
51
- "@jsenv/url-meta": "6.0.1"
42
+ "@jsenv/abort": "4.1.2",
43
+ "@jsenv/url-meta": "6.0.3"
52
44
  },
53
45
  "devDependencies": {
54
- "@babel/core": "7.16.0",
55
- "@babel/eslint-parser": "7.16.0",
56
- "@jsenv/assert": "2.3.2",
57
- "@jsenv/core": "23.2.2",
58
- "@jsenv/eslint-config": "16.0.8",
46
+ "@jsenv/assert": "2.4.1",
47
+ "@jsenv/core": "25.2.1",
48
+ "@jsenv/eslint-config": "16.0.9",
59
49
  "@jsenv/github-release-package": "1.2.3",
60
- "@jsenv/importmap-eslint-resolver": "5.1.2",
61
- "@jsenv/importmap-node-module": "2.4.1",
50
+ "@jsenv/importmap-eslint-resolver": "5.2.2",
51
+ "@jsenv/importmap-node-module": "5.1.0",
62
52
  "@jsenv/package-publish": "1.6.2",
63
53
  "@jsenv/performance-impact": "2.2.1",
64
- "@jsenv/prettier-check-project": "5.6.1",
65
- "eslint": "7.32.0",
66
- "eslint-plugin-import": "2.25.2",
67
- "prettier": "2.4.1"
54
+ "eslint": "8.7.0",
55
+ "eslint-plugin-import": "2.25.4",
56
+ "prettier": "2.5.1"
68
57
  }
69
- }
58
+ }
@@ -0,0 +1,59 @@
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
+ let namesOnFileSystem
23
+ try {
24
+ namesOnFileSystem = readdirSync(
25
+ // When Node.js receives "C:/" on windows it returns
26
+ // the process.cwd() directory content...
27
+ // This can be fixed by passing "file:///C:/" directly but as a url object
28
+ new URL(reconstructedFileUrl),
29
+ )
30
+ } catch (e) {
31
+ if (e && e.code === "ENOENT") {
32
+ return null
33
+ }
34
+ throw e
35
+ }
36
+ const foundOnFilesystem = namesOnFileSystem.includes(name)
37
+ if (foundOnFilesystem) {
38
+ reconstructedFileUrl += name
39
+ } else {
40
+ const nameOnFileSystem = namesOnFileSystem.find(
41
+ (nameCandidate) => nameCandidate.toLowerCase() === name.toLowerCase(),
42
+ )
43
+ if (!nameOnFileSystem) {
44
+ return null
45
+ }
46
+ reconstructedFileUrl += nameOnFileSystem
47
+ }
48
+ if (i === parts.length) {
49
+ if (followLink) {
50
+ const realPath = realpathSync.native(
51
+ urlToFileSystemPath(reconstructedFileUrl),
52
+ )
53
+ return fileSystemPathToUrl(realPath)
54
+ }
55
+ return reconstructedFileUrl
56
+ }
57
+ reconstructedFileUrl += "/"
58
+ }
59
+ }
@@ -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) => {
@@ -1,33 +1,19 @@
1
1
  import { collectFiles } from "./collectFiles.js"
2
2
 
3
- export const listFilesMatching = async (
4
- patterns,
3
+ export const listFilesMatching = async ({
5
4
  directoryUrl = process.cwd(),
5
+ patterns,
6
6
  signal,
7
- ) => {
8
- if (typeof patterns === "string") {
9
- patterns = [patterns]
10
- } else if (Array.isArray(patterns)) {
11
- throw new TypeError(
12
- `patterns must be a string or an array of strings, got ${patterns}`,
13
- )
7
+ }) => {
8
+ if (typeof patterns !== "object" || patterns === null) {
9
+ throw new TypeError(`patterns must be an object, got ${patterns}`)
14
10
  }
15
11
 
16
- const patternMap = {}
17
- patterns.forEach((pattern, index) => {
18
- if (typeof pattern !== "string") {
19
- throw new TypeError(
20
- `every pattern must be a string, found ${pattern} at index ${index}`,
21
- )
22
- }
23
- patternMap[pattern] = true
24
- })
25
-
26
12
  const fileDatas = await collectFiles({
27
13
  signal,
28
14
  directoryUrl,
29
15
  structuredMetaMap: {
30
- matches: patternMap,
16
+ matches: patterns,
31
17
  },
32
18
  predicate: ({ matches }) => matches,
33
19
  })
@@ -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)