tree2dir
is a powerful Command Line Interface (CLI) tool designed to simplify the process of creating complex directory structures. It allows users to generate an entire file and folder structure from an ASCII tree representation. This can be incredibly useful for quickly setting up project structures, replicating folder structures, or even for educational purposes.
- Generate from ASCII Tree: Create a directory structure from a simple ASCII representation.
- File Input: Support for reading ASCII trees from
.txt
files. - GitHub Gist Support: Fetch and generate structures directly from GitHub gists.
- Interactive Mode: Paste your ASCII tree directly into the command line.
- Output Directory: Specify a custom output directory for generated structures.
- Dry Run Mode: Preview the structure without creating any files.
- Validation: Automatic validation of tree structure with helpful error messages.
- Special Character Support: Handles filenames with spaces, dashes, and underscores.
The fastest way to use tree2dir
without installation:
npx tree2dir generate
Follow the interactive prompts to paste your ASCII tree structure and generate the directories and files.
Install tree2dir
globally to use it from anywhere:
npm install -g tree2dir
For project-specific use:
npm install tree2dir --save-dev
To start tree2dir
in interactive mode, simply run:
tree2dir generate
-
-f, --file <path>
: Path to an ASCII tree file.tree2dir generate -f mytree.txt
-
-g, --gist <gistUrl>
: URL of a GitHub gist containing the ASCII tree.tree2dir generate -g https://gist.github.com/username/gistid
-
-o, --output <dir>
: Specify the output directory (default: current directory).tree2dir generate -f mytree.txt -o ./outdir
-
--dry-run
: Visualize the structure without creating files.tree2dir generate -f mytree.txt --dry-run
-
-v, --version
: Display the current version of tree2dir.tree2dir --version
-
-h, --help
: Show help and usage information.tree2dir --help
- Generate a structure from a file in a custom output directory:
tree2dir generate -f mytree.txt -o ./my-project
- Preview a structure without creating files:
tree2dir generate -f mytree.txt --dry-run
- Generate from a GitHub gist with dry run:
tree2dir generate -g <gist-url> --dry-run
- Quick one-time use with npx:
npx tree2dir generate -f mytree.txt -o ./my-project
To generate a structure from a file:
tree2dir generate --file <path-to-your-file>
tree2dir generate --file ./my-ascii-tree.txt
To generate a structure from a GitHub gist:
tree2dir generate --gist <gist-url>
tree2dir generate --gist https://gist.github.com/solancer/147dbff070d5424283f2f69be23bd8d6
myProject/
βββ README.md
βββ src/
β βββ main.js
β βββ components/
β β βββ Header.js
β β βββ Footer.js
β β βββ shared/
β β βββ Button.js
β β βββ Slider.js
β βββ utils/
β βββ helpers.js
β βββ test/
β βββ helper.test.js
β βββ mockData.js
βββ lib/
β βββ middleware.js
β βββ database.js
βββ tests/
β βββ main.test.js
β βββ components/
β βββ Header.test.js
β βββ Footer.test.js
βββ package.json
The tool performs several validations on the ASCII tree:
- Duplicate Paths: Detects and prevents duplicate file/directory paths.
- Invalid Characters: Checks for invalid characters in filenames (e.g.,
:
). - Empty Directories: Warns about empty directories in the structure.
- Path Validation: Ensures all paths are valid for the target operating system.
The tool supports various special characters in filenames:
- Spaces:
file with spaces.js
- Dashes:
file-with-dashes.js
- Underscores:
file_with_underscores.js
tree2dir
can be especially useful when combined with LLMs for generating directory structures. LLMs can be prompted to create an ASCII representation of a project structure, which tree2dir can then turn into an actual directory setup.
- Rapid Prototyping: Quickly create boilerplate structures for new projects.
- Educational Tools: Teach file system structures in a visual and interactive way.
- Project Templates: Easily replicate complex project structures for consistency across multiple projects.
- AI-assisted Project Setup: Generate project structures using AI tools like ChatGPT or Claude.
While tree2dir
is primarily a CLI tool, it can also be used programmatically within your Node.js applications. This allows you to generate directory structures dynamically based on your needs.
First, install tree2dir
as a dependency in your project:
npm install tree2dir --save
Or, if you are using Yarn:
yarn add tree2dir
Here's how to use tree2dir to generate directory structures within your application:
const { generate } = require('tree2dir');
// Define your ASCII tree as a string
const asciiTree = `
myProject/
βββ README.md
βββ src/
β βββ main.js
β βββ utils/
β βββ helpers.js
βββ package.json
`;
// Use the `generate` function to create the structure
generate(asciiTree, './output', { dryRun: false })
.then(() => {
console.log('Directory structure has been successfully created!');
})
.catch(error => {
console.error('An error occurred:', error);
});
The library exports the following functions:
asciiTree
: The ASCII tree structure as a stringbasePath
: The base directory where the structure will be createdoptions
: Optional configuration
- Save Time: Quickly scaffold project structures without manually creating each file and folder.
- Consistency: Ensure consistent project structures across teams or multiple projects.
- Visualization: The ASCII tree format makes it easy to visualize and understand directory structures.
- Integration: Works well with other tools and workflows, including AI assistants and code generators.
Contributions are welcome! Here's how you can contribute to the project:
- Fork the repository: Create your own fork of the project.
- Create a branch: Create a branch for your feature or fix.
git checkout -b feature/your-feature-name
- Make your changes: Implement your changes, following the code style of the project.
- Run the tests: Make sure all tests pass with your changes.
npm test
- Run linting: Ensure your code meets our linting standards.
npm run lint
- Submit a pull request: Push your changes to your fork and submit a pull request to the main repository.
See the CHANGELOG.md file for details on the latest changes.
This project is licensed under the MIT License - see the LICENSE file for details.
- Special thanks to all contributors who have helped make this project better.
- ASCII tree parsing was inspired by various text-based tree visualization techniques.
Made with β€οΈ by Srinivas Gowda