directory-tree 3.0.0 → 3.2.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.
@@ -0,0 +1,4 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="Encoding" addBOMForNewFiles="with NO BOM" />
4
+ </project>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/node-directory-tree.iml" filepath="$PROJECT_DIR$/.idea/node-directory-tree.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="WEB_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$" />
5
+ <orderEntry type="inheritedJdk" />
6
+ <orderEntry type="sourceFolder" forTests="false" />
7
+ </component>
8
+ </module>
package/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="$PROJECT_DIR$" vcs="Git" />
5
+ </component>
6
+ </project>
package/README.md CHANGED
@@ -83,6 +83,8 @@ const tree = dirTree('./test/test_data', {extensions:/\.txt$/}, null, (item, PAT
83
83
 
84
84
  `normalizePath` : `Boolean` - If true, windows style paths will be normalized to unix style pathes (/ 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.
87
+
86
88
  ## Result
87
89
 
88
90
  Given a directory structured like this:
@@ -164,6 +166,33 @@ photos
164
166
  }
165
167
  ```
166
168
 
169
+ ## Adding custom fields
170
+ You can easily extend a `DirectoryTree` object with custom fields by adding them to the custom field.
171
+ For example add an `id` based on the path of a `DirectoryTree` object for each directory and file like so:
172
+ ```
173
+ import { createHash } from 'crypto';
174
+ import * as directoryTree from 'directory-tree';
175
+ import { DirectoryTree, DirectoryTreeOptions, DirectoryTreeCallback } from 'directory-tree';
176
+
177
+ const callback: DirectoryTreeCallback = (
178
+ item: DirectoryTree,
179
+ path: string
180
+ ) => {
181
+ item.custom.id = createHash('sha1').update(path).digest('base64');
182
+ };
183
+
184
+ const dirTree: DirectoryTree & { id?: string } = directoryTree(
185
+ "<your-directory-path>",
186
+ {},
187
+ callback,
188
+ callback
189
+ );
190
+
191
+ // to explore the object with the new custom fields
192
+ console.log(JSON.stringify(dirTree, null, 2));
193
+
194
+ ```
195
+
167
196
  ## Note
168
197
 
169
198
  Device, FIFO and socket files are ignored.
@@ -186,3 +215,22 @@ Make sure you have the dev dependencies installed (e.g. `npm install .`)
186
215
 
187
216
  This project requires at least Node v4.2.
188
217
  Check out version `0.1.1` if you need support for older versions of Node.
218
+
219
+ ## CLI usage
220
+
221
+ You can use script directly from command line for generating json data.
222
+
223
+ ```bash
224
+ $ npx directory-tree --help
225
+ ```
226
+ ```bash
227
+ $ npx directory-tree --path /Users/user/target --attributes type,extension --pretty -o ./xz.json --depth 1
228
+ ```
229
+
230
+ ### Available options
231
+ -p, --path string 🗂 The input folder to process. Required.
232
+ -e, --exclude string 🐒 Exclude some folders from processing by regexp string. Ex -e "test_data/some_dir$|js|.DS_Store"
233
+ -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
236
+ --pretty 💎 Json pretty print
package/bin/index.js ADDED
@@ -0,0 +1,102 @@
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require('fs');
4
+ const commandLineUsage = require('command-line-usage')
5
+ const commandLineArgs = require('command-line-args')
6
+ const directoryTree = require('../lib/directory-tree');
7
+
8
+ const optionList = [
9
+ {
10
+ name: 'path',
11
+ alias: 'p',
12
+ required: true,
13
+ defaultOption: true,
14
+ typeLabel: '{underline string}',
15
+ description: '🗂 The input folder to process. Required.'
16
+ },
17
+ {
18
+ name: 'exclude',
19
+ alias: 'e',
20
+ type: String,
21
+ description: '🐒 Exclude some folders from processing by regexp string. Ex -e "test_data/some_dir$|js|.DS_Store"'
22
+ },
23
+ {
24
+ name: 'output',
25
+ alias: 'o',
26
+ type: String,
27
+ description: '📝 Put result into file provided by this options. Overwrites if exists.'
28
+ },
29
+ {
30
+ name: 'depth',
31
+ alias: 'd',
32
+ type: Number,
33
+ description: '☞ Reads dirs in deep as specified. Usage of size attribute with depth option is prohibited.'
34
+ },
35
+ {
36
+ name: 'attributes',
37
+ type: String,
38
+ description: 'â„šī¸ Grab file attributes. Example: --attributes size,type,extension. Usage of size attribute with depth option is prohibited'
39
+ },
40
+ {
41
+ name: 'pretty',
42
+ type: Boolean,
43
+ description: '💎 Json pretty print'
44
+ },
45
+ {
46
+ name: 'help',
47
+ alias: 'h',
48
+ type: Boolean,
49
+ description: 'â‰ī¸ Print this usage guide.'
50
+ }
51
+ ]
52
+
53
+ const usageNotes = [
54
+ {
55
+ header: 'â›„ī¸ī¸ Folder-tree command line script',
56
+ content: 'Used for generates json representation of folder internals'
57
+ },
58
+ {
59
+ header: 'đŸ”Ĩ Options đŸ”Ĩ',
60
+ optionList: optionList
61
+ }
62
+ ]
63
+
64
+ const usage = commandLineUsage(usageNotes)
65
+ let options = null;
66
+ try {
67
+ options = commandLineArgs(optionList)
68
+ } catch(e) {
69
+ console.log(usage);
70
+ return;
71
+ }
72
+
73
+ if (Object.keys(options).length === 0 || options.help || !options.path) {
74
+ console.log(usage)
75
+ return;
76
+ }
77
+
78
+ if (!fs.existsSync(options.path)) {
79
+ console.log('-----------------------------------------------------------------------------------------------------')
80
+ console.log(`doesn't exist; please check your args`);
81
+ console.log('-----------------------------------------------------------------------------------------------------')
82
+ console.log(usage)
83
+ return;
84
+ }
85
+
86
+ try {
87
+ const result = directoryTree(options.path, {
88
+ depth: options.depth,
89
+ exclude: options.exclude ? [new RegExp(options.exclude)] : undefined,
90
+ attributes: options.attributes ? options.attributes.split(',') : undefined
91
+ })
92
+
93
+ const resultString = JSON.stringify(result, null, options.pretty ? ' ' : '');
94
+ if (options.output) {
95
+ fs.writeFileSync(options.output, resultString);
96
+ } else {
97
+ console.log(resultString);
98
+ }
99
+ } catch(e) {
100
+ console.log(e);
101
+ console.log(usage);
102
+ }
package/index.d.ts CHANGED
@@ -18,13 +18,15 @@ declare namespace directoryTree {
18
18
  children ? : DirectoryTree[];
19
19
  extension?: string;
20
20
  isSymbolicLink?: boolean;
21
+ custom: { [key: string]: any };
21
22
  }
22
23
  export interface DirectoryTreeOptions {
23
24
  normalizePath ? : boolean;
24
25
  exclude ? : RegExp | RegExp[];
25
- attributes ? : (keyof Stats)[];
26
+ attributes ? : (keyof Stats | "type" | "extension")[];
26
27
  extensions ? : RegExp;
27
28
  followSymlink ? : boolean;
29
+ depth ? : number;
28
30
  }
29
31
  export type DirectoryTreeCallback = (item: DirectoryTree, path: string, stats: Stats) => void;
30
32
  }
@@ -48,9 +48,14 @@ function isRegExp(regExp) {
48
48
  * @param {function} onEachDirectory
49
49
  * @return {Object}
50
50
  */
51
- function directoryTree (path, options, onEachFile, onEachDirectory) {
52
- const name = PATH.basename(path);
51
+ function directoryTree (path, options, onEachFile, onEachDirectory, currentDepth = 0) {
53
52
  options = options || {};
53
+
54
+ if (options.depth !== undefined && options.attributes.indexOf('size') !== -1) {
55
+ throw new Error('usage of size attribute with depth option is prohibited');
56
+ }
57
+
58
+ const name = PATH.basename(path);
54
59
  path = options.normalizePath ? normalizePath(path) : path;
55
60
  const item = { path, name };
56
61
  let stats;
@@ -119,9 +124,11 @@ function directoryTree (path, options, onEachFile, onEachDirectory) {
119
124
  let dirData = safeReadDirSync(path);
120
125
  if (dirData === null) return null;
121
126
 
122
- item.children = dirData
123
- .map(child => directoryTree(PATH.join(path, child), options, onEachFile, onEachDirectory))
124
- .filter(e => !!e);
127
+ if (options.depth === undefined || options.depth > currentDepth) {
128
+ item.children = dirData
129
+ .map(child => directoryTree(PATH.join(path, child), options, onEachFile, onEachDirectory, currentDepth + 1))
130
+ .filter(e => !!e);
131
+ }
125
132
 
126
133
  if (options.attributes) {
127
134
  options.attributes.forEach((attribute) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "directory-tree",
3
- "version": "3.0.0",
3
+ "version": "3.2.1",
4
4
  "description": "Convert a directory tree to a JS object.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -17,10 +17,15 @@
17
17
  "url": "https://github.com/mihneadb/node-directory-tree/issues"
18
18
  },
19
19
  "homepage": "https://github.com/mihneadb/node-directory-tree",
20
+ "dependencies": {
21
+ "command-line-args": "^5.2.0",
22
+ "command-line-usage": "^6.1.1"
23
+ },
20
24
  "devDependencies": {
21
25
  "chai": "^2.3.0",
22
26
  "mocha": "^8.3.2"
23
27
  },
28
+ "bin": "bin/index.js",
24
29
  "engines": {
25
30
  "node": ">=10.0"
26
31
  },