Skip to content

Comments

Added functionality to get treeview as JSON object for issue #8#27

Merged
vedpawar2254 merged 6 commits intovedpawar2254:mainfrom
yashpss01:feature/treeAsJson
Oct 27, 2025
Merged

Added functionality to get treeview as JSON object for issue #8#27
vedpawar2254 merged 6 commits intovedpawar2254:mainfrom
yashpss01:feature/treeAsJson

Conversation

@yashpss01
Copy link
Contributor

@yashpss01 yashpss01 commented Oct 25, 2025

Description

This PR adds the functionality to use the flag --as-json to get the treeview of the folder structure in nested JSON object.

Related Issue

Fixes issue #8

Testing

I have tested it on my local with different folder structures and its working fine.

Checklist

  • Code follows project style.
  • Self-reviewed the changes.
  • Updated docs if necessary.
  • Tests added or updated.
  • All tests pass.

Please review this and suggest if something not behaving as expected , or else merge it under hacktoberfest.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @yashpss01, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the treeview CLI tool by adding the capability to output the directory structure as a nested JSON object. This new functionality, accessible via the --as-json flag, provides a programmatic way to consume the folder hierarchy, making the tool more versatile for integration into other scripts or applications.

Highlights

  • New Feature: JSON Output: Introduced a new command-line flag, --as-json, which allows users to output the folder structure as a nested JSON object instead of the traditional visual tree.
  • Recursive JSON Generation: A new function, printTreeAsJson, has been implemented to recursively traverse the directory structure and build a corresponding JSON object. Files are represented by the string 'FILE' within the JSON.
  • Documentation Update: The README.md has been updated to include details about the new --as-json flag, its usage, and an example of the expected JSON output.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a valuable feature for outputting the directory structure as a JSON object. My review focuses on ensuring the output is valid, parseable JSON and on improving the performance and clarity of the new implementation. I've identified a critical issue where the output is not valid JSON, and I've provided suggestions to correct this. Additionally, there are opportunities to refactor the recursive function for better performance and to update the README with accurate information and examples. With these adjustments, this feature will be a robust and useful addition to the tool.

Comment on lines 143 to 145
if (asJson){
console.log(startPath, ":",printTreeAsJson(startPath, ignoreConfig, dirsOnly))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

high

The current implementation for the --as-json flag does not produce a valid JSON string. console.log() with multiple arguments will print a space-separated string representation of those arguments. To generate a valid JSON output that can be parsed by other tools, you should use JSON.stringify(). The root path should also be part of the JSON object itself for a valid structure.

  if (asJson){
    const tree = { [startPath]: printTreeAsJson(startPath, ignoreConfig, dirsOnly) };
    console.log(JSON.stringify(tree, null, 2));
  }

Comment on lines 102 to 129
function printTreeAsJson(
dirPath,
ignoreConfig = { exactMatches: [], globPatterns: [] },
dirsOnly = false,
jsonTree = {}

) {
let items = fs
.readdirSync(dirPath)
.filter(item => !shouldIgnoreItem(item, ignoreConfig));
// If dirsOnly, filter out files
if (dirsOnly) {
items = items.filter(item => {
const fullPath = path.join(dirPath, item);
return fs.statSync(fullPath).isDirectory();
});
}

items.forEach((item, index) => {
const fullPath = path.join(dirPath, item);
if (fs.statSync(fullPath).isDirectory()) {
jsonTree = {...jsonTree, [item]:printTreeAsJson(fullPath, ignoreConfig, dirsOnly,{})};
}else{
jsonTree = { ...jsonTree , [item] :"FILE"}
}
});
return jsonTree;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The printTreeAsJson function can be inefficient for large directory structures for a few reasons:

  1. It uses the spread syntax (...) inside a forEach loop to create a new jsonTree object on every iteration. This creates many intermediate objects, which can harm performance and increase memory usage.
  2. The jsonTree = {} parameter is used as an accumulator, but the recursive call printTreeAsJson(..., {}) passes a new empty object, which makes the parameter's purpose a bit confusing.
  3. When dirsOnly is true, fs.statSync() is called twice for each directory: once in the initial filter and again inside the forEach loop.

A more efficient and clearer approach is to create the object once, mutate it in the loop, and avoid redundant statSync calls.

function printTreeAsJson(
  dirPath,
  ignoreConfig = { exactMatches: [], globPatterns: [] },
  dirsOnly = false
) {
  const jsonTree = {};
  const items = fs
    .readdirSync(dirPath)
    .filter(item => !shouldIgnoreItem(item, ignoreConfig));

  for (const item of items) {
    const fullPath = path.join(dirPath, item);
    const stats = fs.statSync(fullPath);
    if (stats.isDirectory()) {
      jsonTree[item] = printTreeAsJson(fullPath, ignoreConfig, dirsOnly);
    } else if (!dirsOnly) {
      jsonTree[item] = "FILE";
    }
  }
  return jsonTree;
}

yashpss01 and others added 4 commits October 26, 2025 01:57
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Format JSON output with indentation for better readability.
@vedpawar2254 vedpawar2254 merged commit ead5587 into vedpawar2254:main Oct 27, 2025
@krushndayshmookh
Copy link
Contributor

@vedpawar2254 @yashpss01 the output does not seem to be complete. It does not show me files.

Ideally, the output should allow us to distinguish between folders and files and allow for extensibility.

I think it would be better if we have the output like so:

{
  "path": "/Users/krushn/project",
  "name": "project",
  "type": "directory",
  "children": [
    {
      "path": "/Users/krushn/project/dir1",
      "name": "dir1",
      "type": "directory",
      "children": [
        {
          "path": "/Users/krushn/project/dir1/file1.txt",
          "name": "file1.txt",
          "type": "file"
        }
      ]
    },
    {
      "path": "/Users/krushn/project/file2.txt",
      "name": "file2.txt",
      "type": "file"
    }
  ]
}

This should allow us to extend the model in the future.

Also, we should have a flag for outputting this stucture as a JSON file.

@vedpawar2254
Copy link
Owner

@yashpss01 will you work on it, or should I take it up

@yashpss01
Copy link
Contributor Author

@vedpawar2254 I will work upon this

@yashpss01
Copy link
Contributor Author

Raised PR #29 for this. Please review and provide feedback. @krushndayshmookh @vedpawar2254 .
Also will raise another issue for providing an output json file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants