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.
tree2dir
can be installed globally via npm:
npm install -g tree2dir
Or, for a one-time use without installation, you can use npx
:
npx tree2dir generate
To start tree2dir
in interactive mode, simply run:
tree2dir generate
Then paste your ASCII tree and press Ctrl+D when finished.
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
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.
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 tree2dir = 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
tree2dir.generate(asciiTree)
.then(() => {
console.log('Directory structure has been successfully created!');
})
.catch(error => {
console.error('An error occurred:', error);
});
If you're using modern JavaScript with async/await, the usage becomes even cleaner:
const tree2dir = require('tree2dir');
async function createProjectStructure() {
try {
const asciiTree = `
myProject/
βββ README.md
βββ src/
β βββ main.js
β βββ utils/
β βββ helpers.js
βββ package.json
`;
// Generate the directory structure
await tree2dir.generate(asciiTree);
console.log('Directory structure has been successfully created!');
} catch (error) {
console.error('An error occurred:', error);
}
}
createProjectStructure();
If you're using TypeScript, tree2dir provides type definitions out of the box. Here's how you might use it in a TypeScript application:
import { generate } from 'tree2dir';
async function createProjectStructure() {
const asciiTree: string = `
myProject/
βββ README.md
βββ src/
β βββ main.ts
β βββ utils/
β βββ helpers.ts
βββ package.json
`;
try {
// Generate the directory structure
await generate(asciiTree);
console.log('Directory structure has been successfully created!');
} catch (error) {
console.error('An error occurred:', error);
}
}
createProjectStructure();
In addition to passing a static ASCII tree string to tree2dir
, you can also process ASCII trees from streams. This is particularly useful when you're dealing with large directories or when you want to generate structures on-the-fly from an external source.
Here's an example of how you might accept an ASCII tree from a readable stream:
const tree2dir = require('tree2dir');
const { Readable } = require('stream');
function streamToTree2dir(stream) {
let asciiTree = '';
stream.on('data', chunk => {
asciiTree += chunk;
});
stream.on('end', async () => {
try {
await tree2dir.generate(asciiTree);
console.log('Directory structure has been successfully created!');
} catch (error) {
console.error('An error occurred while generating structure:', error);
}
});
}
// Example usage with a Readable stream
const treeStream = Readable.from(`
myProject/
βββ README.md
βββ src/
β βββ main.js
β βββ utils/
β βββ helpers.js
βββ package.json
`);
streamToTree2dir(treeStream);
If you're using TypeScript, the example would look similar, with the addition of types for better code reliability and developer experience:
import { generate } from 'tree2dir';
import { Readable } from 'stream';
function streamToTree2dir(stream: Readable) {
let asciiTree: string = '';
stream.on('data', (chunk: Buffer) => {
asciiTree += chunk.toString();
});
stream.on('end', async () => {
try {
await generate(asciiTree);
console.log('Directory structure has been successfully created!');
} catch (error) {
console.error('An error occurred while generating structure:', error);
}
});
}
// Example usage with a Readable stream
const treeStream: Readable = Readable.from(`
myProject/
βββ README.md
βββ src/
β βββ main.ts
β βββ utils/
β βββ helpers.ts
βββ package.json
`);
streamToTree2dir(treeStream);
By incorporating tree2dir into your Node.js applications, you gain the flexibility to programmatically create file structures, which can be particularly useful for scaffolding projects, generating reports, or organizing output data.
Contributions to tree2dir are welcome. Please fork the repository, make your changes, and submit a pull request.
tree2dir is open-source software licensed under the MIT License.