-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: 🎸 implement toTreeSync() method
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { toTreeSync } from '..'; | ||
import { memfs } from '../..'; | ||
|
||
test('can print a single file', () => { | ||
const { fs } = memfs({ | ||
'/file.txt': '...', | ||
}); | ||
expect(toTreeSync(fs, { dir: '/' })).toMatchInlineSnapshot(` | ||
"/ | ||
└─ file.txt" | ||
`); | ||
}); | ||
|
||
test('can a one level deep directory tree', () => { | ||
const { fs } = memfs({ | ||
'/file.txt': '...', | ||
'/foo/bar.txt': '...', | ||
'/foo/index.php': '...', | ||
}); | ||
expect(toTreeSync(fs, { dir: '/' })).toMatchInlineSnapshot(` | ||
"/ | ||
├─ file.txt | ||
└─ foo/ | ||
├─ bar.txt | ||
└─ index.php" | ||
`); | ||
}); | ||
|
||
test('can print two-levels of folders', () => { | ||
const { fs } = memfs({ | ||
'/level1/level2/file.txt': '...', | ||
}); | ||
expect(toTreeSync(fs, { dir: '/' })).toMatchInlineSnapshot(` | ||
"/ | ||
└─ level1/ | ||
└─ level2/ | ||
└─ file.txt" | ||
`); | ||
}); | ||
|
||
test('can stop recursion at specified depth', () => { | ||
const { fs } = memfs({ | ||
'/dir1/dir2/dir3/file.txt': '...', | ||
}); | ||
expect(toTreeSync(fs, { dir: '/', depth: 2 })).toMatchInlineSnapshot(` | ||
"/ | ||
└─ dir1/ | ||
└─ dir2/ (...)" | ||
`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import {printTree} from 'json-joy/es6/util/print/printTree'; | ||
import {basename} from "../node-to-fsa/util"; | ||
import type {FsSynchronousApi} from "../node/types"; | ||
import type {IDirent} from "../node/types/misc"; | ||
|
||
export const toTreeSync = (fs: FsSynchronousApi, opts: ToTreeOptions = {}) => { | ||
const separator = opts.separator || '/'; | ||
let dir = opts.dir || separator; | ||
if (dir[dir.length - 1] !== separator) dir += separator; | ||
const tab = opts.tab || ''; | ||
const depth = opts.depth ?? 10; | ||
let subtree = ' (...)'; | ||
if (depth > 0) { | ||
const list = fs.readdirSync(dir, {withFileTypes: true}) as IDirent[]; | ||
subtree = printTree(tab, list.map( | ||
(entry) => (tab) => { | ||
const isDir = entry.isDirectory(); | ||
if (isDir) { | ||
return toTreeSync(fs, {dir: dir + entry.name, depth: depth - 1, tab}); | ||
} else { | ||
return '' + entry.name; | ||
} | ||
}, | ||
)) | ||
} | ||
const base = basename(dir, separator) + separator; | ||
return base + subtree; | ||
}; | ||
|
||
export interface ToTreeOptions { | ||
dir?: string; | ||
tab?: string; | ||
depth?: number; | ||
separator?: '/' | '\\'; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters