From 82657026fcb7d32ab940c9effb56f8d1c1b29a69 Mon Sep 17 00:00:00 2001 From: Yash Pratap Singh Solanki Date: Wed, 29 Oct 2025 00:11:37 +0530 Subject: [PATCH 1/4] improved output Json structure for --as-json flag --- src/tree.js | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/src/tree.js b/src/tree.js index 5392e6d..7b79371 100644 --- a/src/tree.js +++ b/src/tree.js @@ -99,12 +99,10 @@ function printTree( * Recursively stores the folder structure in nested json object, ignoring items based on ignoreConfig. * If dirsOnly is true, files will be skipped. */ -function printTreeAsJson( +function treeAsJson( dirPath, ignoreConfig = { exactMatches: [], globPatterns: [] }, - dirsOnly = false, - jsonTree = {} - + dirsOnly = false ) { let items = fs .readdirSync(dirPath) @@ -116,16 +114,25 @@ function printTreeAsJson( return fs.statSync(fullPath).isDirectory(); }); } - + let currLevel=[] items.forEach((item, index) => { const fullPath = path.join(dirPath, item); if (fs.statSync(fullPath).isDirectory()) { - jsonTree[item]=printTreeAsJson(fullPath, ignoreConfig, dirsOnly,{}); + currLevel.push({ + "path":fullPath, + "name":item, + "type":"directory", + "children":treeAsJson(fullPath,ignoreConfig,dirsOnly) + }) }else{ - jsonTree[item]="FILE" + currLevel.push({ + "path":fullPath, + "name":item, + "type":"file" + }) } }); - return jsonTree; + return currLevel; } @@ -140,9 +147,16 @@ function runTree( asJson = false ) { const ignoreConfig = getIgnoreList(startPath, additionalFiles, additionalPatterns); - if (asJson){ - const tree = { [startPath]: printTreeAsJson(startPath, ignoreConfig, dirsOnly) }; - console.log(JSON.stringify(tree, null, 2)); + if (asJson){ + const tree = { + "path":startPath, + "name":startPath.split("/").at(-1), + "type":"directory", + "children":treeAsJson(startPath,ignoreConfig,dirsOnly) + } + + console.log(JSON.stringify(tree,null,2)) + // console.log(JSON.stringify( {[startPath]:printTreeAsJson(startPath, ignoreConfig, dirsOnly)} ,null , 4)) } else{ console.log(startPath); From e014cfd47f6955f2ed3e054b8433d2e45dec41d0 Mon Sep 17 00:00:00 2001 From: Yash Pratap Singh Solanki Date: Wed, 29 Oct 2025 00:16:41 +0530 Subject: [PATCH 2/4] Modified output Json structure in README as of current outputs for --as-json flag --- README.md | 50 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 753bb94..b8a76e1 100644 --- a/README.md +++ b/README.md @@ -111,16 +111,50 @@ treeview --help ```json { - "/Users/ved/projects/my-app": { - "public": {}, - "src": { - "components": {}, - "pages": {}, - "utils": {} + "path": "/Users/ved/projects/my-app", + "name": "my-app", + "type": "directory", + "children": [ + { + "path": "/Users/ved/projects/my-app/src", + "name": "src", + "type": "directory", + "children": [ + { + "path": "/Users/ved/projects/my-app/src/components", + "name": "components", + "type": "directory", + "children": [] + }, + { + "path": "/Users/ved/projects/my-app/src/pages", + "name": "pages", + "type": "directory", + "children": [] + }, + { + "path": "/Users/ved/projects/my-app/src/utils", + "name": "utils", + "type": "directory", + "children": [] + } + ] }, - "tests": {} - } + { + "path": "/Users/ved/projects/my-app/public", + "name": "public", + "type": "directory", + "children": [] + }, + { + "path": "/Users/ved/projects/my-app/tests", + "name": "tests", + "type": "directory", + "children": [] + } + ] } + ``` --- From da03592b12bb78e0cb5f26c5fa8a87b22ac88423 Mon Sep 17 00:00:00 2001 From: Yash Pratap Singh Solanki Date: Wed, 29 Oct 2025 00:40:48 +0530 Subject: [PATCH 3/4] Update src/tree.js Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- src/tree.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tree.js b/src/tree.js index 7b79371..6a082bd 100644 --- a/src/tree.js +++ b/src/tree.js @@ -150,7 +150,7 @@ function runTree( if (asJson){ const tree = { "path":startPath, - "name":startPath.split("/").at(-1), + "name": path.basename(startPath), "type":"directory", "children":treeAsJson(startPath,ignoreConfig,dirsOnly) } From 5083de8f5284eed479dca85c97a911ecf9d5f00c Mon Sep 17 00:00:00 2001 From: Yash Pratap Singh Solanki Date: Wed, 29 Oct 2025 00:47:22 +0530 Subject: [PATCH 4/4] Minute cleanups in src/tree.js --- src/tree.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/tree.js b/src/tree.js index 7b79371..f6b290a 100644 --- a/src/tree.js +++ b/src/tree.js @@ -114,21 +114,21 @@ function treeAsJson( return fs.statSync(fullPath).isDirectory(); }); } - let currLevel=[] + let currLevel = [] items.forEach((item, index) => { const fullPath = path.join(dirPath, item); if (fs.statSync(fullPath).isDirectory()) { currLevel.push({ - "path":fullPath, - "name":item, - "type":"directory", - "children":treeAsJson(fullPath,ignoreConfig,dirsOnly) + "path": fullPath, + "name": item, + "type": "directory", + "children": treeAsJson(fullPath, ignoreConfig, dirsOnly) }) }else{ currLevel.push({ - "path":fullPath, - "name":item, - "type":"file" + "path": fullPath, + "name": item, + "type": "file" }) } }); @@ -149,14 +149,14 @@ function runTree( const ignoreConfig = getIgnoreList(startPath, additionalFiles, additionalPatterns); if (asJson){ const tree = { - "path":startPath, - "name":startPath.split("/").at(-1), - "type":"directory", - "children":treeAsJson(startPath,ignoreConfig,dirsOnly) + "path": startPath, + "name": path.basename(startPath), + "type": "directory", + "children": treeAsJson(startPath, ignoreConfig, dirsOnly) } - console.log(JSON.stringify(tree,null,2)) - // console.log(JSON.stringify( {[startPath]:printTreeAsJson(startPath, ignoreConfig, dirsOnly)} ,null , 4)) + console.log(JSON.stringify(tree, null, 2)) + } else{ console.log(startPath);