@jsenv/filesystem 4.14.1 → 4.14.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/filesystem",
3
- "version": "4.14.1",
3
+ "version": "4.14.2",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -30,7 +30,7 @@
30
30
  ],
31
31
  "sideEffects": false,
32
32
  "dependencies": {
33
- "@jsenv/urls": "2.6.2",
33
+ "@jsenv/urls": "2.6.3",
34
34
  "@jsenv/url-meta": "8.5.4",
35
35
  "@jsenv/abort": "4.3.1",
36
36
  "@jsenv/utils": "2.2.1"
package/src/main.js CHANGED
@@ -22,7 +22,10 @@ export { findAncestorDirectoryUrl } from "./path_and_url/find_ancestor_directory
22
22
  // All other exports are agnostic and focused on the filesystem only
23
23
  // "lookupPackageDirectory" is an exception because it related to node module resolution
24
24
  // but it's very useful and it's not a problem to have it here (and useful to share this logic)
25
- export { lookupPackageDirectory } from "./path_and_url/lookup_package_directory.js";
25
+ export {
26
+ lookupPackageDirectory,
27
+ readPackageAtOrNull,
28
+ } from "./path_and_url/lookup_package_directory.js";
26
29
 
27
30
  // list
28
31
  export { collectDirectoryMatchReport } from "./list/collect_directory_match_report.js";
@@ -1,4 +1,4 @@
1
- import { existsSync } from "node:fs";
1
+ import { existsSync, readFileSync } from "node:fs";
2
2
  import { findAncestorDirectoryUrl } from "./find_ancestor_directory_url.js";
3
3
 
4
4
  export const lookupPackageDirectory = (currentUrl) => {
@@ -7,3 +7,16 @@ export const lookupPackageDirectory = (currentUrl) => {
7
7
  return existsSync(new URL(potentialPackageJsonFileUrl));
8
8
  });
9
9
  };
10
+
11
+ export const readPackageAtOrNull = (packageDirectoryUrl) => {
12
+ try {
13
+ const packageFileContent = readFileSync(
14
+ new URL("./package.json", packageDirectoryUrl),
15
+ "utf8",
16
+ );
17
+ const packageJSON = JSON.parse(packageFileContent);
18
+ return packageJSON;
19
+ } catch {
20
+ return null;
21
+ }
22
+ };
@@ -3,26 +3,59 @@ import { readdirSync } from "node:fs";
3
3
  import { readEntryStatSync } from "../read_write/stat/read_entry_stat_sync.js";
4
4
  import { removeEntrySync } from "./remove_entry_sync.js";
5
5
 
6
- export const clearDirectorySync = (initialDirectoryUrl, pattern = "**/*") => {
6
+ export const clearDirectorySync = (
7
+ initialDirectoryUrl,
8
+ secondArg,
9
+ thirdArg,
10
+ ) => {
11
+ let removePatterns = {};
12
+ if (secondArg && typeof secondArg === "object") {
13
+ removePatterns = secondArg;
14
+ } else {
15
+ removePatterns = {};
16
+ let clearPatterns = secondArg || "**/*";
17
+ let keepPatterns = thirdArg || [];
18
+
19
+ if (typeof keepPatterns === "string") {
20
+ keepPatterns = [keepPatterns];
21
+ }
22
+ if (Array.isArray(keepPatterns)) {
23
+ for (const keepPattern of keepPatterns) {
24
+ Object.assign(removePatterns, {
25
+ [keepPattern]: false,
26
+ });
27
+ }
28
+ }
29
+ if (typeof clearPatterns === "string") {
30
+ clearPatterns = [clearPatterns];
31
+ }
32
+ if (Array.isArray(clearPatterns)) {
33
+ let someClearPatternHandleNodeModules = false;
34
+ for (const clearPattern of clearPatterns) {
35
+ Object.assign(removePatterns, {
36
+ [clearPatterns]: true,
37
+ });
38
+ if (
39
+ !someClearPatternHandleNodeModules &&
40
+ clearPattern.includes("node_modules")
41
+ ) {
42
+ someClearPatternHandleNodeModules = true;
43
+ }
44
+ }
45
+ Object.assign(removePatterns, {
46
+ "**/.*": false,
47
+ "**/.*/": false,
48
+ });
49
+ if (!someClearPatternHandleNodeModules) {
50
+ Object.assign(removePatterns, {
51
+ "**/node_modules/": false,
52
+ });
53
+ }
54
+ }
55
+ }
56
+
7
57
  const associations = URL_META.resolveAssociations(
8
- {
9
- clear:
10
- typeof pattern === "string"
11
- ? {
12
- [pattern]: true,
13
- "**/.*": false,
14
- "**/.*/": false,
15
- ...(pattern.includes("node_modules")
16
- ? {}
17
- : { "**/node_modules/": false }),
18
- }
19
- : {
20
- "**/.*": false,
21
- "**/.*/": false,
22
- "**/node_modules/": false,
23
- ...pattern,
24
- },
25
- },
58
+ { remove: removePatterns },
26
59
  initialDirectoryUrl,
27
60
  );
28
61
  const visitDirectory = (directoryUrl) => {
@@ -54,7 +87,7 @@ export const clearDirectorySync = (initialDirectoryUrl, pattern = "**/*") => {
54
87
  url: subDirectoryUrl,
55
88
  associations,
56
89
  });
57
- if (meta.clear) {
90
+ if (meta.remove) {
58
91
  removeEntrySync(subDirectoryUrl, {
59
92
  recursive: true,
60
93
  allowUseless: true,
@@ -65,19 +98,20 @@ export const clearDirectorySync = (initialDirectoryUrl, pattern = "**/*") => {
65
98
  !URL_META.urlChildMayMatch({
66
99
  url: subDirectoryUrl,
67
100
  associations,
68
- predicate: ({ clear }) => clear,
101
+ predicate: ({ remove }) => remove,
69
102
  })
70
103
  ) {
71
104
  continue;
72
105
  }
73
106
  visitDirectory(subDirectoryUrl);
107
+ continue;
74
108
  }
75
109
 
76
110
  const meta = URL_META.applyAssociations({
77
111
  url: entryUrl,
78
112
  associations,
79
113
  });
80
- if (meta.clear) {
114
+ if (meta.remove) {
81
115
  removeEntrySync(entryUrl, { allowUseless: true });
82
116
  continue;
83
117
  }