Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 49 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Copy link
Copy Markdown

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.json

Suggested change
* `--as-json-file` → Create/overwrite current folder structure as a nested JSON object in a JSON file `treeview.json'
* `--as-json-file` → Create/overwrite current folder structure as a nested JSON object in a JSON file `treeview.json`
Prompt To Fix With AI
This is a comment left during a code review.
Path: README.md
Line: 19:19

Comment:
**syntax:** missing closing backtick after `treeview.json`

```suggestion
  * `--as-json-file` → Create/overwrite current folder structure as a nested JSON object in a JSON file `treeview.json`
```

How can I resolve this? If you propose a fix, please make it concise.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

There's a typo at the end of this line. It uses a single quote (') instead of a backtick (` \).

Suggested change
* `--as-json-file` → Create/overwrite current folder structure as a nested JSON object in a JSON file `treeview.json'
* `--as-json-file` → Create/overwrite current folder structure as a nested JSON object in a JSON file `treeview.json`


---

Expand Down Expand Up @@ -85,6 +86,12 @@ treeview --dirs-only
treeview --as-json
```

### Create/Overwrite a file name `treeview.json` with folder structure as a JSON object

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: typo: 'name' should be 'named'

Suggested change
### Create/Overwrite a file name `treeview.json` with folder structure as a JSON object
### Create/Overwrite a file named `treeview.json` with folder structure as a JSON object
Prompt To Fix With AI
This 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
Expand All @@ -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": []
}
]
}

```

---
Expand Down
13 changes: 9 additions & 4 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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
Expand All @@ -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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 AI
This 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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
--as-json-file Creates/Overwrites JSON folder structure in treeview.json
--as-json-file Creates/Overwrites JSON folder structure in treeview.json


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);
56 changes: 44 additions & 12 deletions src/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
}


Expand All @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This block of code to create the tree object is identical to the one in the else if (asJsonFile) block (lines 163-168). To avoid duplication and improve maintainability, you should create the tree object once before the if/else if conditions.


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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 AI
This 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 fs.writeFileSync instead to ensure the file is written

Suggested change
fs.writeFile('treeview.json', JSON.stringify(tree, null, 2), (err) => {
if (err) {
console.error('Error writing file:', err);
return;
}
console.log('File written successfully');
}
);
fs.writeFileSync('treeview.json', JSON.stringify(tree, null, 2));
console.log('File written successfully');
Prompt To Fix With AI
This 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

There are a few issues in this block:

  • Asynchronous File I/O Bug: fs.writeFile is asynchronous. In a CLI script, the process can exit before the file write operation is complete, leading to an empty or incomplete file. This is a critical bug. You should use fs.writeFileSync instead.
  • Insufficient Error Handling: The callback for fs.writeFile logs an error but doesn't exit the process with a non-zero status code. This can be misleading for users or automated scripts, as they might assume the operation succeeded.
  • Inaccurate Comment: The comment on line 169 is inaccurate and can be removed.

I suggest using fs.writeFileSync inside a try...catch block to address these issues.

    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);
Expand Down