directory-tree 3.0.1 â 3.1.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.
- package/README.md +21 -0
- package/bin/index.js +102 -0
- package/index.d.ts +1 -0
- package/lib/directory-tree.js +10 -4
- package/package.json +6 -1
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:
|
|
@@ -186,3 +188,22 @@ Make sure you have the dev dependencies installed (e.g. `npm install .`)
|
|
|
186
188
|
|
|
187
189
|
This project requires at least Node v4.2.
|
|
188
190
|
Check out version `0.1.1` if you need support for older versions of Node.
|
|
191
|
+
|
|
192
|
+
## CLI usage
|
|
193
|
+
|
|
194
|
+
You can use script directly from command line for generating json data.
|
|
195
|
+
|
|
196
|
+
```bash
|
|
197
|
+
$ npx directory-tree --help
|
|
198
|
+
```
|
|
199
|
+
```bash
|
|
200
|
+
$ npx directory-tree --path /Users/user/target --attributes type,extension --pretty -o ./xz.json --depth 1
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Available options
|
|
204
|
+
-p, --path string đ The input folder to process. Required.
|
|
205
|
+
-e, --exclude string đ Exclude some folders from processing by regexp string. Ex -e "test_data/some_dir$|js|.DS_Store"
|
|
206
|
+
-o, --output string đ Put result into file provided by this options. Overwrites if exists.
|
|
207
|
+
-d, --depth number â Reads dirs in deep as specified. Usage of size attribute with depth option is prohibited.
|
|
208
|
+
--attributes string âšī¸ Grab file attributes. Example: --attributes size,type,extension. Usage of size attribute with depth option is prohibited
|
|
209
|
+
--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
|
@@ -25,6 +25,7 @@ declare namespace directoryTree {
|
|
|
25
25
|
attributes ? : (keyof Stats | "type" | "extension")[];
|
|
26
26
|
extensions ? : RegExp;
|
|
27
27
|
followSymlink ? : boolean;
|
|
28
|
+
depth ? : number;
|
|
28
29
|
}
|
|
29
30
|
export type DirectoryTreeCallback = (item: DirectoryTree, path: string, stats: Stats) => void;
|
|
30
31
|
}
|
package/lib/directory-tree.js
CHANGED
|
@@ -48,7 +48,11 @@ function isRegExp(regExp) {
|
|
|
48
48
|
* @param {function} onEachDirectory
|
|
49
49
|
* @return {Object}
|
|
50
50
|
*/
|
|
51
|
-
function directoryTree (path, options, onEachFile, onEachDirectory) {
|
|
51
|
+
function directoryTree (path, options, onEachFile, onEachDirectory, currentDepth = 0) {
|
|
52
|
+
if (options.depth !== undefined && options.attributes.indexOf('size') !== -1) {
|
|
53
|
+
throw new Error('usage of size attribute with depth option is prohibited');
|
|
54
|
+
}
|
|
55
|
+
|
|
52
56
|
const name = PATH.basename(path);
|
|
53
57
|
options = options || {};
|
|
54
58
|
path = options.normalizePath ? normalizePath(path) : path;
|
|
@@ -119,9 +123,11 @@ function directoryTree (path, options, onEachFile, onEachDirectory) {
|
|
|
119
123
|
let dirData = safeReadDirSync(path);
|
|
120
124
|
if (dirData === null) return null;
|
|
121
125
|
|
|
122
|
-
|
|
123
|
-
.
|
|
124
|
-
|
|
126
|
+
if (options.depth === undefined || options.depth > currentDepth) {
|
|
127
|
+
item.children = dirData
|
|
128
|
+
.map(child => directoryTree(PATH.join(path, child), options, onEachFile, onEachDirectory, currentDepth + 1))
|
|
129
|
+
.filter(e => !!e);
|
|
130
|
+
}
|
|
125
131
|
|
|
126
132
|
if (options.attributes) {
|
|
127
133
|
options.attributes.forEach((attribute) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "directory-tree",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0",
|
|
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
|
},
|