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/5] 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/5] 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/5] 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/5] 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); From e084ef2865d346247dfbf00bb942bc46a18bbcda Mon Sep 17 00:00:00 2001 From: Yash Pratap Singh Solanki Date: Wed, 29 Oct 2025 13:48:06 +0530 Subject: [PATCH 5/5] Added functionality to create a json file with folder structure in it --- README.md | 7 +++++++ bin/index.js | 13 +++++++++---- src/tree.js | 20 +++++++++++++++++++- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index b8a76e1..871d79c 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ A simple CLI tool to print your project or folder structure in a clear tree view * `--ignore-pattern` → Ignore files matching glob patterns * `--dirs-only` → Show only directories * `--as-json` → Output as a nested JSON object + * `--as-json-file` → Create/overwrite current folder structure as a nested JSON object in a JSON file `treeview.json' --- @@ -85,6 +86,12 @@ treeview --dirs-only treeview --as-json ``` +### Create/Overwrite a file name `treeview.json` with folder structure as a JSON object + +```bash +treeview --as-json-file +``` + ### Show Help ```bash diff --git a/bin/index.js b/bin/index.js index f031bc5..a5ec7d7 100755 --- a/bin/index.js +++ b/bin/index.js @@ -9,7 +9,8 @@ function parseArgs() { const additionalFiles = []; const additionalPatterns = []; let dirsOnly = false; - let asJson=false; + let asJson = false; + let asJsonFile = false; for (let i = 0; i < args.length; i++) { if (args[i] === '--ignore-files') { @@ -32,6 +33,8 @@ function parseArgs() { dirsOnly = true; } else if (args[i] === '--as-json') { asJson = true; + } else if (args[i] === '--as-json-file') { + asJsonFile = true; } else if (args[i] === '--help' || args[i] === '-h') { console.log(` Treeview CLI - Print project folder structure @@ -44,6 +47,7 @@ Options: --help, -h Show this help message --dirs-only Show only directories, no files --as-json Show tree as a nested JSON object + --as-json-file Creates/Overwrites JSON folder structure in treeview.json Examples: treeview --ignore-files temp.txt build/ @@ -51,14 +55,15 @@ Examples: treeview --ignore-files temp.txt --ignore-pattern "*.log" "test-*" treeview --dirs-only treeview --as-json + treeview --as-json-file `); process.exit(0); } } - return { additionalFiles, additionalPatterns,dirsOnly,asJson }; + return { additionalFiles, additionalPatterns, dirsOnly, asJson, asJsonFile }; } // Parse arguments and run tree -const { additionalFiles, additionalPatterns,dirsOnly,asJson } = parseArgs(); -runTree(process.cwd(), additionalFiles, additionalPatterns,dirsOnly,asJson); +const { additionalFiles, additionalPatterns, dirsOnly, asJson, asJsonFile } = parseArgs(); +runTree(process.cwd(), additionalFiles, additionalPatterns, dirsOnly, asJson, asJsonFile); diff --git a/src/tree.js b/src/tree.js index f6b290a..05921ea 100644 --- a/src/tree.js +++ b/src/tree.js @@ -144,7 +144,8 @@ function runTree( additionalFiles = [], additionalPatterns = [], dirsOnly = false, - asJson = false + asJson = false, + asJsonFile = false ) { const ignoreConfig = getIgnoreList(startPath, additionalFiles, additionalPatterns); if (asJson){ @@ -158,6 +159,23 @@ function runTree( console.log(JSON.stringify(tree, null, 2)) } + else if (asJsonFile){ + const tree = { + "path": startPath, + "name": path.basename(startPath), + "type": "directory", + "children": treeAsJson(startPath, ignoreConfig, dirsOnly) + } + //logic for creating/overwriting folder structure as JSON object in treeview.js + fs.writeFile('treeview.json', JSON.stringify(tree, null, 2), (err) => { + if (err) { + console.error('Error writing file:', err); + return; + } + console.log('File written successfully'); + } + ); + } else{ console.log(startPath); printTree(startPath, "", ignoreConfig, dirsOnly);