directory-tree 3.5.2 → 3.6.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.
@@ -16,16 +16,15 @@ jobs:
16
16
 
17
17
  strategy:
18
18
  matrix:
19
- node-version: [12.x, 14.x, 16.x]
19
+ node-version: [22.x]
20
20
  # See supported Node.js release schedule at https://nodejs.org/en/about/releases/
21
21
 
22
22
  steps:
23
23
  - uses: actions/checkout@v2
24
24
  - name: Use Node.js ${{ matrix.node-version }}
25
- uses: actions/setup-node@v2
25
+ uses: actions/setup-node@v4
26
26
  with:
27
27
  node-version: ${{ matrix.node-version }}
28
- cache: 'npm'
29
28
  - run: npm ci
30
29
  - run: npm run build --if-present
31
30
  - run: npm test
package/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Creates a JavaScript object representing a directory tree.
4
4
 
5
- This library gets [~100k downloads per week](http://npm-stats.org/#/directory-tree). If you find it useful, feel free to <a href="https://www.buymeacoffee.com/pXhXnSXSbN"><img src="https://img.buymeacoffee.com/button-api/?text=Buy me a coffee.&emoji=&slug=pXhXnSXSbN&button_colour=FFDD00&font_colour=000000&font_family=Cookie&outline_colour=000000&coffee_colour=ffffff" /></a>
5
+ This library gets [~100k downloads per week](http://npm-stats.org/#/directory-tree). If you find it useful, feel free to <a href="https://www.buymeacoffee.com/pxhxnsxsbn"><img height="40" src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" /></a> .
6
6
 
7
7
  ## Install
8
8
 
@@ -81,9 +81,9 @@ const tree = dirTree('./test/test_data', {extensions:/\.txt$/}, null, (item, PAT
81
81
 
82
82
  `attributes` : `string[]` - Array of [FS.stats](https://nodejs.org/api/fs.html#fs_class_fs_stats) attributes.
83
83
 
84
- `normalizePath` : `Boolean` - If true, windows style paths will be normalized to unix style pathes (/ instead of \\).
84
+ `normalizePath` : `Boolean` - If true, Windows style paths will be normalized to UNIX style paths (/ instead of \\).
85
85
 
86
- `depth` : `number` - If presented, reads so many nested dirs as specified in argument. Usage of size attribute with depth option is prohibited.
86
+ `depth` : `number` - Limits directory traversal to the specified depth level. When combined with `size`, directories at the depth limit have `size: undefined` (see "Using size with depth" below).
87
87
 
88
88
  ## Result
89
89
 
@@ -166,6 +166,68 @@ photos
166
166
  }
167
167
  ```
168
168
 
169
+ ## Using size with depth
170
+
171
+ The `size` attribute can be used together with the `depth` option, but with an important caveat:
172
+
173
+ **When depth limiting is active:**
174
+ - **Files** always have accurate sizes
175
+ - **Directories at the depth limit** (whose children are not traversed) will have `size: undefined`
176
+ - **Parent directories** containing depth-limited directories will also have `size: undefined`
177
+
178
+ This is because directory sizes are calculated by recursively summing all child sizes. When depth limits prevent full traversal, the size would be incomplete and potentially misleading, so `undefined` is returned instead.
179
+
180
+ **Example:**
181
+
182
+ ```js
183
+ const dirTree = require('directory-tree');
184
+ const tree = dirTree('/some/path', {
185
+ depth: 1,
186
+ attributes: ['size', 'type']
187
+ });
188
+ ```
189
+
190
+ Given this structure:
191
+ ```
192
+ folder/
193
+ ├── file1.txt (100 bytes)
194
+ ├── file2.txt (200 bytes)
195
+ └── subfolder/
196
+ └── file3.txt (300 bytes)
197
+ ```
198
+
199
+ The result will be:
200
+ ```js
201
+ {
202
+ "path": "folder",
203
+ "name": "folder",
204
+ "size": undefined,
205
+ "type": "directory",
206
+ "children": [
207
+ {
208
+ "path": "folder/file1.txt",
209
+ "name": "file1.txt",
210
+ "size": 100,
211
+ "type": "file"
212
+ },
213
+ {
214
+ "path": "folder/file2.txt",
215
+ "name": "file2.txt",
216
+ "size": 200,
217
+ "type": "file"
218
+ },
219
+ {
220
+ "path": "folder/subfolder",
221
+ "name": "subfolder",
222
+ "size": undefined,
223
+ "type": "directory"
224
+ }
225
+ ]
226
+ }
227
+ ```
228
+
229
+ **Note:** When serializing to JSON (e.g., `JSON.stringify(tree)`), `undefined` values are omitted. Properties with `undefined` will not appear in the JSON string.
230
+
169
231
  ## Adding custom fields
170
232
  You can easily extend a `DirectoryTree` object with custom fields by adding them to the custom field.
171
233
  For example add an `id` based on the path of a `DirectoryTree` object for each directory and file like so:
@@ -211,11 +273,6 @@ $ npm test
211
273
 
212
274
  Make sure you have the dev dependencies installed (e.g. `npm install .`)
213
275
 
214
- ## Node version
215
-
216
- This project requires at least Node v4.2.
217
- Check out version `0.1.1` if you need support for older versions of Node.
218
-
219
276
  ## CLI usage
220
277
 
221
278
  You can use script directly from command line for generating json data.
@@ -231,6 +288,6 @@ $ npx directory-tree --path /Users/user/target --attributes type,extension --pre
231
288
  -p, --path string 🗂 The input folder to process. Required.
232
289
  -e, --exclude string 🐒 Exclude some folders from processing by regexp string. Ex -e "test_data/some_dir$|js|.DS_Store"
233
290
  -o, --output string 📝 Put result into file provided by this options. Overwrites if exists.
234
- -d, --depth number ☞ Reads dirs in deep as specified. Usage of size attribute with depth option is prohibited.
235
- --attributes string ℹ️ Grab file attributes. Example: --attributes size,type,extension. Usage of size attribute with depth option is prohibited
291
+ -d, --depth number ☞ Limits directory traversal to specified depth. Combined with size, directories at depth limit have size: undefined.
292
+ --attributes string ℹ️ Grab file attributes. Example: --attributes size,type,extension. With depth, directories at limit have size: undefined.
236
293
  --pretty 💎 Json pretty print
package/bin/index.js CHANGED
@@ -30,12 +30,12 @@ const optionList = [
30
30
  name: 'depth',
31
31
  alias: 'd',
32
32
  type: Number,
33
- description: '☞ Reads dirs in deep as specified. Usage of size attribute with depth option is prohibited.'
33
+ description: '☞ Limits directory traversal to specified depth. Combined with size, directories at depth limit have size: undefined.'
34
34
  },
35
35
  {
36
36
  name: 'attributes',
37
37
  type: String,
38
- description: 'ℹ️ Grab file attributes. Example: --attributes size,type,extension. Usage of size attribute with depth option is prohibited'
38
+ description: 'ℹ️ Grab file attributes. Example: --attributes size,type,extension. With depth, directories at limit have size: undefined.'
39
39
  },
40
40
  {
41
41
  name: 'pretty',
package/index.d.ts CHANGED
@@ -17,12 +17,12 @@ declare namespace directoryTree {
17
17
  export interface DirectoryTree<C extends Record<string, any> = Record<string, any>> {
18
18
  path: string;
19
19
  name: string;
20
- size: number;
20
+ size?: number;
21
21
  type: "directory" | "file";
22
22
  children?: DirectoryTree<C>[];
23
23
  extension?: string;
24
24
  isSymbolicLink?: boolean;
25
- custom: C;
25
+ custom?: C;
26
26
  }
27
27
 
28
28
  export interface DirectoryTreeOptions {
@@ -22,7 +22,7 @@ function safeReadDirSync (path) {
22
22
  }
23
23
 
24
24
  /**
25
- * Normalizes windows style paths by replacing double backslahes with single forward slahes (unix style).
25
+ * Normalizes Windows style paths by replacing double backslashes with single forward slashes (UNIX style).
26
26
  * @param {string} path
27
27
  * @return {string}
28
28
  */
@@ -51,10 +51,6 @@ function isRegExp(regExp) {
51
51
  function directoryTree (path, options, onEachFile, onEachDirectory, currentDepth = 0) {
52
52
  options = options || {};
53
53
 
54
- if (options.depth !== undefined && options.attributes && options.attributes.indexOf('size') !== -1) {
55
- throw new Error('usage of size attribute with depth option is prohibited');
56
- }
57
-
58
54
  const name = PATH.basename(path);
59
55
  path = options.normalizePath ? normalizePath(path) : path;
60
56
  const item = { path, name };
@@ -134,7 +130,15 @@ function directoryTree (path, options, onEachFile, onEachDirectory, currentDepth
134
130
  options.attributes.forEach((attribute) => {
135
131
  switch (attribute) {
136
132
  case 'size':
137
- item.size = item.children.reduce((prev, cur) => prev + cur.size, 0);
133
+ // If children don't exist (depth limit reached), set to undefined
134
+ if (!item.children) {
135
+ item.size = undefined;
136
+ } else {
137
+ // If any child has undefined size, parent size is also undefined
138
+ // Otherwise, calculate recursive size
139
+ const hasUndefinedSize = item.children.some(child => child.size === undefined);
140
+ item.size = hasUndefinedSize ? undefined : item.children.reduce((prev, cur) => prev + cur.size, 0);
141
+ }
138
142
  break;
139
143
  case 'type':
140
144
  item.type = constants.DIRECTORY;
@@ -145,7 +149,7 @@ function directoryTree (path, options, onEachFile, onEachDirectory, currentDepth
145
149
  item[attribute] = stats[attribute];
146
150
  break;
147
151
  }
148
-
152
+
149
153
  });
150
154
  }
151
155
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "directory-tree",
3
- "version": "3.5.2",
3
+ "version": "3.6.0",
4
4
  "description": "Convert a directory tree to a JS object.",
5
5
  "repository": {
6
6
  "type": "git",