Skip to content

Commit

Permalink
feat: 🎸 implement toTreeSync() method
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jun 25, 2023
1 parent 2ce01b7 commit 09c9770
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/print/__tests__/index.test.ts
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/ (...)"
`);
});
35 changes: 35 additions & 0 deletions src/print/index.ts
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?: '/' | '\\';
}
1 change: 1 addition & 0 deletions src/volume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1880,6 +1880,7 @@ export class Volume implements FsCallbackApi {
public statfsSync: FsSynchronousApi['statfsSync'] = notImplemented;
public writevSync: FsSynchronousApi['writevSync'] = notImplemented;
public readvSync: FsSynchronousApi['readvSync'] = notImplemented;
public opendirSync: FsSynchronousApi['opendirSync'] = notImplemented;

public cp: FsCallbackApi['cp'] = notImplemented;
public lutimes: FsCallbackApi['lutimes'] = notImplemented;
Expand Down

0 comments on commit 09c9770

Please sign in to comment.