npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tree2dir

v1.1.0

Published

<div align="center"> <br> <a href="https://tree2dir.solancer.com"> <img src="Stuff/tree2dir-logo.png" width="200" height="200"> </a> <h1>tree2dir</h1> <p> <b>Directory And File Structure Generator</b> </p> <a href="https://github.com/solance

Downloads

8

Readme


Overview

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.

Features

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

Installation

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

Usage

General Command

To start tree2dir in interactive mode, simply run:

tree2dir generate

Then paste your ASCII tree and press Ctrl+D when finished.

From a File

To generate a structure from a file:

tree2dir generate --file <path-to-your-file>

For example:

tree2dir generate --file ./my-ascii-tree.txt

From a GitHub Gist

To generate a structure from a GitHub gist:

tree2dir generate --gist <gist-url>

For example:

tree2dir generate --gist https://gist.github.com/solancer/147dbff070d5424283f2f69be23bd8d6

ASCII Tree Format

The ASCII tree should follow a simple format, for example:

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

Large Language Models (LLM) Integration

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.

Scenarios Of Use

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

Using tree2dir as a Library in Node.js Applications

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.

Installation

First, install tree2dir as a dependency in your project:

npm install tree2dir --save

Or, if you are using Yarn:

yarn add tree2dir

Usage

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);
  });

Async/Await

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();

TypeScript

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();

Advanced Usage: Accepting ASCII Trees from Streams

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

Using with TypeScript

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.

Contributing

Contributions to tree2dir are welcome. Please fork the repository, make your changes, and submit a pull request.

License

tree2dir is open-source software licensed under the MIT License.