Skip to content

Commit 4554977

Browse files
committed
Fixes: #10. Use 'directoryFirst' to make sure folders first in the report.
1 parent 0961146 commit 4554977

5 files changed

Lines changed: 56 additions & 23 deletions

File tree

__test__/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ describe('use with require', function () {
2222
f: true,
2323
base: testBase,
2424
noreport: true,
25+
directoryFirst: true,
2526
}).then((res) => {
2627
result = res;
2728
done();

__test__/o.data

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/Users/rdp/www/rdp/github/MrRaindrop/tree-cli/__test__/__data__
2+
├── node_modules/
3+
| ├── bfolder/
4+
| └── a.txt
25
├── a.ts
3-
├── b.data
4-
└── node_modules/
5-
├── a.txt
6-
└── bfolder/
6+
└── b.data
77

88
directory: 2 file: 3
99

bin/tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ var cli = meow({
4848
' (current directory) and \'..\' (previous directory).',
4949
'-d',
5050
' list directories only.',
51+
'--directoryFirst',
52+
' list directories first.',
5153
'-f',
5254
' append a \'/\' for directories, a \'=\' for socket files',
5355
' and a \'|\' for FIFOs',

tree.js

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ var Promise = require('bluebird'),
5454
// append a '/' for directories, a '=' for socket files
5555
// and a '|' for FIFOs
5656
f: false,
57+
directoryFirst: false,
5758
},
5859

5960
_ignores = [],
@@ -221,6 +222,16 @@ var Promise = require('bluebird'),
221222

222223
},
223224

225+
// calc priority when the flag directoryFirst is true.
226+
_calcPriority = function (node) {
227+
return node.type === 'directory' ? 1 : 0;
228+
},
229+
230+
// compare priority when the flag directoryFirst is true.
231+
_comparePriority = function (nodeA, nodeB) {
232+
return _calcPriority(nodeB) - _calcPriority(nodeA);
233+
},
234+
224235
appendChildNodes = function (parent) {
225236

226237
_debug('appendTreeNode:', parent);
@@ -278,8 +289,39 @@ var Promise = require('bluebird'),
278289
if (_flags.d && type !== 'directory') {
279290
return
280291
}
292+
if (_testIgnore(child, _ignores)) {
293+
child.ignore = true;
294+
}
295+
parent.children.push(child);
296+
// for statistics.
297+
if (!child.ignore) {
298+
_stats.all.push(child);
299+
_stats[type].push(child);
300+
}
301+
else {
302+
if (!_stats.ignore[type]) {
303+
_stats.ignore[type] = [];
304+
}
305+
_stats.ignore[type].push(child);
306+
}
307+
})
308+
.catch(function (err) {
309+
_debug(filePath, ' is invalid to access.');
310+
})
311+
});
312+
})
313+
.then(function () {
314+
if (_flags.directoryFirst) {
315+
parent.children.sort(_comparePriority);
316+
}
317+
})
318+
.then(function () {
319+
return Promise.resolve(parent.children)
320+
.each(function (child, index) {
321+
return Promise.resolve()
322+
.then(function () {
281323
// otherwise every type of file counts.
282-
var isLast = index === files.length - 1;
324+
var isLast = index === parent.children.length - 1;
283325
isLast && (child.lasts[child.level - 1] = true);
284326
if (_testIgnore(child, _ignores)) {
285327
child.ignore = true;
@@ -297,25 +339,10 @@ var Promise = require('bluebird'),
297339
}
298340
}
299341
}
300-
parent.children.push(child);
301-
// for statistics.
302-
if (!child.ignore) {
303-
_stats.all.push(child);
304-
_stats[type].push(child);
305-
}
306-
else {
307-
if (!_stats.ignore[type]) {
308-
_stats.ignore[type] = [];
309-
}
310-
_stats.ignore[type].push(child);
311-
}
312-
if (type === 'directory') {
342+
if (child.type === 'directory') {
313343
return appendChildNodes(child);
314344
}
315-
})
316-
.catch(function (err) {
317-
_debug(filePath, ' is invalid to access.');
318-
})
345+
});
319346
});
320347
})
321348
.catch(function (err) {
@@ -327,7 +354,6 @@ var Promise = require('bluebird'),
327354
process.exit(-1);
328355
}
329356
});
330-
331357
},
332358

333359
genTree = function (rootPath) {

types/index.d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ namespace Tree {
2828
// append a '/' for directories, a '=' for socket files
2929
// and a '|' for FIFOs
3030
f: boolean;
31+
// list directories only.
32+
d: boolean;
33+
// list directories first.
34+
directoryFirst: boolean;
3135
}
3236

3337
interface ITreeRoot {

0 commit comments

Comments
 (0)