@jsenv/filesystem 4.6.9 → 4.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/filesystem",
3
- "version": "4.6.9",
3
+ "version": "4.7.1",
4
4
  "license": "MIT",
5
5
  "repository": {
6
6
  "type": "git",
@@ -33,7 +33,7 @@
33
33
  },
34
34
  "dependencies": {
35
35
  "@jsenv/urls": "2.2.7",
36
- "@jsenv/url-meta": "8.4.0",
36
+ "@jsenv/url-meta": "8.4.1",
37
37
  "@jsenv/abort": "4.3.0",
38
38
  "@jsenv/utils": "2.1.1"
39
39
  }
@@ -0,0 +1,99 @@
1
+ import { URL_META } from "@jsenv/url-meta";
2
+ import { urlToRelativeUrl } from "@jsenv/urls";
3
+
4
+ import { assertAndNormalizeDirectoryUrl } from "../path_and_url/directory_url_validation.js";
5
+ import { comparePathnames } from "../path_and_url/compare_pathnames.js";
6
+ import { readEntryStatSync } from "../read_write/stat/read_entry_stat_sync.js";
7
+ import { readDirectorySync } from "../read_write/read_directory_sync.js";
8
+
9
+ export const visitStructureSync = ({
10
+ directoryUrl,
11
+ associations,
12
+ predicate,
13
+ callback = () => {},
14
+ }) => {
15
+ const rootDirectoryUrl = assertAndNormalizeDirectoryUrl(directoryUrl);
16
+ if (typeof predicate !== "function") {
17
+ throw new TypeError(`predicate must be a function, got ${predicate}`);
18
+ }
19
+ associations = URL_META.resolveAssociations(associations, rootDirectoryUrl);
20
+ const matchingFileResultArray = [];
21
+ const visitDirectory = (directoryUrl) => {
22
+ const directoryItems = readDirectorySync(directoryUrl);
23
+ for (const directoryItem of directoryItems) {
24
+ const directoryChildNodeUrl = `${directoryUrl}${directoryItem}`;
25
+ let directoryChildNodeStats;
26
+ try {
27
+ directoryChildNodeStats = readEntryStatSync(directoryChildNodeUrl, {
28
+ // we ignore symlink because recursively traversed
29
+ // so symlinked file will be discovered.
30
+ // Moreover if they lead outside of directoryPath it can become a problem
31
+ // like infinite recursion of whatever.
32
+ // that we could handle using an object of pathname already seen but it will be useless
33
+ // because directoryPath is recursively traversed
34
+ followLink: false,
35
+ });
36
+ } catch (e) {
37
+ if (e && e.code === "ENOENT") {
38
+ continue;
39
+ }
40
+ throw e;
41
+ }
42
+ if (directoryChildNodeStats.isDirectory()) {
43
+ const subDirectoryUrl = `${directoryChildNodeUrl}/`;
44
+ if (
45
+ !URL_META.urlChildMayMatch({
46
+ url: subDirectoryUrl,
47
+ associations,
48
+ predicate,
49
+ })
50
+ ) {
51
+ continue;
52
+ }
53
+ callback({
54
+ url: subDirectoryUrl,
55
+ relativeUrl: urlToRelativeUrl(subDirectoryUrl, rootDirectoryUrl),
56
+ stats: directoryChildNodeStats,
57
+ isDirectory: true,
58
+ });
59
+ visitDirectory(subDirectoryUrl);
60
+ continue;
61
+ }
62
+ if (directoryChildNodeStats.isFile()) {
63
+ const meta = URL_META.applyAssociations({
64
+ url: directoryChildNodeUrl,
65
+ associations,
66
+ });
67
+ if (!predicate(meta)) {
68
+ continue;
69
+ }
70
+ const relativeUrl = urlToRelativeUrl(
71
+ directoryChildNodeUrl,
72
+ rootDirectoryUrl,
73
+ );
74
+ matchingFileResultArray.push({
75
+ url: new URL(relativeUrl, rootDirectoryUrl).href,
76
+ relativeUrl: decodeURIComponent(relativeUrl),
77
+ meta,
78
+ fileStats: directoryChildNodeStats,
79
+ });
80
+ callback({
81
+ url: directoryChildNodeUrl,
82
+ relativeUrl,
83
+ stats: directoryChildNodeStats,
84
+ isDirectory: false,
85
+ });
86
+ continue;
87
+ }
88
+ }
89
+ };
90
+ visitDirectory(rootDirectoryUrl);
91
+ // When we operate on thoose files later it feels more natural
92
+ // to perform operation in the same order they appear in the filesystem.
93
+ // It also allow to get a predictable return value.
94
+ // For that reason we sort matchingFileResultArray
95
+ matchingFileResultArray.sort((leftFile, rightFile) => {
96
+ return comparePathnames(leftFile.relativeUrl, rightFile.relativeUrl);
97
+ });
98
+ return matchingFileResultArray;
99
+ };
package/src/main.js CHANGED
@@ -20,6 +20,7 @@ export { ensureWindowsDriveLetter } from "./path_and_url/ensure_windows_drive_le
20
20
  export { collectDirectoryMatchReport } from "./list/collect_directory_match_report.js";
21
21
  export { collectFiles } from "./list/collect_files.js";
22
22
  export { listFilesMatching } from "./list/list_files_matching.js";
23
+ export { visitStructureSync } from "./list/visit_structure_sync.js";
23
24
 
24
25
  // read and write
25
26
  export { ensureParentDirectories } from "./read_write/ensure_parent_directories.js";
@@ -1,9 +1,13 @@
1
1
  import { ensureEmptyDirectorySync } from "../remove/ensure_empty_directory_sync.js";
2
+ import { readFileStructureSync } from "./read_file_structure_sync.js";
2
3
  import { writeFileSync } from "./write_file_sync.js";
3
4
 
4
5
  export const writeFileStructureSync = (directoryUrl, fileStructure) => {
5
6
  ensureEmptyDirectorySync(directoryUrl);
6
7
  if (fileStructure) {
8
+ if (typeof fileStructure === "string" || fileStructure instanceof URL) {
9
+ fileStructure = readFileStructureSync(fileStructure);
10
+ }
7
11
  Object.keys(fileStructure).forEach((relativeUrl) => {
8
12
  const fileUrl = new URL(relativeUrl, directoryUrl);
9
13
  const fileContent = fileStructure[relativeUrl];
@@ -6,14 +6,22 @@ import { removeEntrySync } from "./remove_entry_sync.js";
6
6
  export const clearDirectorySync = (initialDirectoryUrl, pattern = "**/*") => {
7
7
  const associations = URL_META.resolveAssociations(
8
8
  {
9
- clear: {
10
- [pattern]: true,
11
- "**/.*": false,
12
- "**/.*/": false,
13
- ...(pattern.includes("node_modules")
14
- ? {}
15
- : { "**/node_modules/": false }),
16
- },
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
+ },
17
25
  },
18
26
  initialDirectoryUrl,
19
27
  );