Skip to content

Commit 4ec37da

Browse files
authored
Merge pull request #1215 from ext/master
feat: sort files and folder when using `toTreeSync()`
2 parents 5094071 + 673cb7a commit 4ec37da

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

docs/print/index.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ console.log(toTreeSync(fs, { dir: process.cwd() + '/src/fsa-to-node' }));
1313

1414
// Output:
1515
// src/
16-
// ├─ Dirent.ts
17-
// ├─ Stats.ts
1816
// ├─ __tests__/
1917
// │ ├─ hasBigInt.js
2018
// │ ├─ index.test.ts
2119
// │ ├─ node.test.ts
2220
// │ ├─ process.test.ts
23-
// │ ├─ promises.test.ts
21+
// │ └─ promises.test.ts
22+
// ├─ Dirent.ts
23+
// ├─ Stats.ts
2424
// ...
2525
```
2626

@@ -48,3 +48,21 @@ console.log(toTreeSync(fs));
4848
// ├─ package.json
4949
// ├─ tsconfig.json
5050
```
51+
52+
By default the output is sorted alphabetically with folders first, to disable sorting the `sort` option can be disabled:
53+
54+
```
55+
console.log(toTreeSync(fs, { sort: false }));
56+
57+
// Output:
58+
// src/
59+
// ├─ Dirent.ts
60+
// ├─ Stats.ts
61+
// ├─ __tests__/
62+
// │ ├─ hasBigInt.js
63+
// │ ├─ index.test.ts
64+
// │ ├─ node.test.ts
65+
// │ ├─ process.test.ts
66+
// │ ├─ promises.test.ts
67+
// ...
68+
```

src/print/__tests__/index.test.ts

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ test('can a one level deep directory tree', () => {
1919
});
2020
expect(toTreeSync(fs, { dir: '/' })).toMatchInlineSnapshot(`
2121
"/
22-
├─ file.txt
23-
└─ foo/
24-
├─ bar.txt
25-
└─ index.php"
22+
├─ foo/
23+
│ ├─ bar.txt
24+
│ └─ index.php
25+
└─ file.txt"
2626
`);
2727
});
2828

@@ -78,3 +78,39 @@ test('can print starting from subfolder', () => {
7878
└─ main.rb"
7979
`);
8080
});
81+
82+
test('can print folders sorted first', () => {
83+
const { fs } = memfs({
84+
'/a.txt': '...',
85+
'/b/file.txt': '...',
86+
'/c.txt': '...',
87+
});
88+
expect(toTreeSync(fs)).toMatchInlineSnapshot(`
89+
"/
90+
├─ b/
91+
│ └─ file.txt
92+
├─ a.txt
93+
└─ c.txt"
94+
`);
95+
});
96+
97+
test('can print files and folders sorted alphabetically', () => {
98+
const { fs } = memfs({
99+
'/a.txt': '...',
100+
'/c.txt': '...',
101+
'/b.txt': '...',
102+
'/src/a.txt': '...',
103+
'/src/c.txt': '...',
104+
'/src/b.txt': '...',
105+
});
106+
expect(toTreeSync(fs)).toMatchInlineSnapshot(`
107+
"/
108+
├─ src/
109+
│ ├─ a.txt
110+
│ ├─ b.txt
111+
│ └─ c.txt
112+
├─ a.txt
113+
├─ b.txt
114+
└─ c.txt"
115+
`);
116+
});

src/print/index.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,23 @@ export const toTreeSync = (fs: FsSynchronousApi, opts: ToTreeOptions = {}) => {
99
if (dir[dir.length - 1] !== separator) dir += separator;
1010
const tab = opts.tab || '';
1111
const depth = opts.depth ?? 10;
12+
const sort = opts.sort ?? true;
1213
let subtree = ' (...)';
1314
if (depth > 0) {
1415
const list = fs.readdirSync(dir, { withFileTypes: true }) as IDirent[];
16+
if (sort) {
17+
list.sort((a, b) => {
18+
if (a.isDirectory() && b.isDirectory()) {
19+
return a.name.toString().localeCompare(b.name.toString());
20+
} else if (a.isDirectory()) {
21+
return -1;
22+
} else if (b.isDirectory()) {
23+
return 1;
24+
} else {
25+
return a.name.toString().localeCompare(b.name.toString());
26+
}
27+
});
28+
}
1529
subtree = printTree(
1630
tab,
1731
list.map(entry => tab => {
@@ -34,4 +48,5 @@ export interface ToTreeOptions {
3448
tab?: string;
3549
depth?: number;
3650
separator?: '/' | '\\';
51+
sort?: boolean;
3752
}

0 commit comments

Comments
 (0)