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": [] + } + ] } + ``` --- diff --git a/src/tree.js b/src/tree.js index 5392e6d..f6b290a 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": path.basename(startPath), + "type": "directory", + "children": treeAsJson(startPath, ignoreConfig, dirsOnly) + } + + console.log(JSON.stringify(tree, null, 2)) + } else{ console.log(startPath);