-
Notifications
You must be signed in to change notification settings - Fork 10
Added feature to make json file for directory structure relevant to Issue #31 #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
8265702
e014cfd
da03592
5083de8
78ee9f7
e084ef2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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' | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a typo at the end of this line. It uses a single quote (
Suggested change
|
||||||
|
|
||||||
| --- | ||||||
|
|
||||||
|
|
@@ -85,6 +86,12 @@ treeview --dirs-only | |||||
| treeview --as-json | ||||||
| ``` | ||||||
|
|
||||||
| ### Create/Overwrite a file name `treeview.json` with folder structure as a JSON object | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. syntax: typo: 'name' should be 'named'
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: README.md
Line: 89:89
Comment:
**syntax:** typo: 'name' should be 'named'
```suggestion
### Create/Overwrite a file named `treeview.json` with folder structure as a JSON object
```
How can I resolve this? If you propose a fix, please make it concise. |
||||||
|
|
||||||
| ```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": [] | ||||||
| } | ||||||
| ] | ||||||
| } | ||||||
|
|
||||||
| ``` | ||||||
|
|
||||||
| --- | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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,21 +47,23 @@ 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 | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: trailing whitespace after 'treeview.json'; consider removing for consistency Prompt To Fix With AIThis is a comment left during a code review.
Path: bin/index.js
Line: 50:50
Comment:
**style:** trailing whitespace after 'treeview.json'; consider removing for consistency
How can I resolve this? If you propose a fix, please make it concise.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The help text for this new option is not aligned with the others, which affects readability. Please adjust the spacing to align it with the other options.
Suggested change
|
||||||
|
|
||||||
| Examples: | ||||||
| treeview --ignore-files temp.txt build/ | ||||||
| treeview --ignore-pattern "*.log" "*.tmp" | ||||||
| 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); | ||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -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) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+152
to
+157
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||||||||||
|
|
||||||||||||||||||||||
| 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) | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
Comment on lines
+151
to
+168
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. style: identical tree construction duplicated in both branches; extract to a helper function or create once before the if/else check Prompt To Fix With AIThis is a comment left during a code review.
Path: src/tree.js
Line: 151:168
Comment:
**style:** identical tree construction duplicated in both branches; extract to a helper function or create once before the if/else check
How can I resolve this? If you propose a fix, please make it concise. |
||||||||||||||||||||||
| //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'); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
|
Comment on lines
+170
to
+177
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: async file write without waiting causes the process to exit before file creation completes; use
Suggested change
Prompt To Fix With AIThis is a comment left during a code review.
Path: src/tree.js
Line: 170:177
Comment:
**logic:** async file write without waiting causes the process to exit before file creation completes; use `fs.writeFileSync` instead to ensure the file is written
```suggestion
fs.writeFileSync('treeview.json', JSON.stringify(tree, null, 2));
console.log('File written successfully');
```
How can I resolve this? If you propose a fix, please make it concise.
Comment on lines
+169
to
+177
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are a few issues in this block:
I suggest using try {
fs.writeFileSync('treeview.json', JSON.stringify(tree, null, 2));
console.log('File "treeview.json" written successfully.');
} catch (err) {
console.error('Error writing file:', err);
process.exit(1);
} |
||||||||||||||||||||||
| } | ||||||||||||||||||||||
| else{ | ||||||||||||||||||||||
| console.log(startPath); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
syntax: missing closing backtick after
treeview.jsonPrompt To Fix With AI