diff --git a/README.md b/README.md index 753bb94..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 @@ -111,16 +118,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": [] + } + ] + }, + { + "path": "/Users/ved/projects/my-app/public", + "name": "public", + "type": "directory", + "children": [] }, - "tests": {} - } + { + "path": "/Users/ved/projects/my-app/tests", + "name": "tests", + "type": "directory", + "children": [] + } + ] } + ``` --- 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 5392e6d..05921ea 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; } @@ -137,12 +144,37 @@ function runTree( additionalFiles = [], additionalPatterns = [], dirsOnly = false, - asJson = false + asJson = false, + asJsonFile = 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": path.basename(startPath), + "type": "directory", + "children": treeAsJson(startPath, ignoreConfig, dirsOnly) + } + + 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);