1.0.3 • Published 8 months ago

@themrsami/folder-structure-generator v1.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
8 months ago

Folder Structure Generator

A powerful Node.js package that allows you to generate folder structures from JSON and create JSON from existing folder structures. Perfect for project templating, scaffolding, and backing up directory structures.

Features

  • Two-way conversion between folders and JSON
  • Create complex folder structures from JSON templates
  • Generate JSON from existing folder structures
  • Both CLI and programmatic interfaces
  • Async/await support
  • Preserves file contents
  • Supports nested directories
  • Easy to use
  • Progress tracking with beautiful progress bars
  • Colorful CLI output
  • Optimized for performance

Installation

npm install @themrsami/folder-structure-generator

Usage

Interactive CLI Usage

Simply run the command without any arguments to start the interactive mode:

npx @themrsami/folder-structure-generator

This will start an interactive session where you can: 1. Choose between generating JSON or creating folder structure 2. Select from available directories or JSON files in the current directory 3. Specify output names 4. See real-time progress with a beautiful progress bar

Interactive Features:

  • List of available directories and JSON files
  • Easy selection using arrow keys
  • Real-time progress tracking
  • Colorful and user-friendly interface
  • Guided workflow

Traditional CLI Usage

You can still use the traditional command-line arguments if preferred:

  1. Generate JSON from a folder structure:
npx @themrsami/folder-structure-generator generate-json ./my-directory -o structure.json
  1. Create folder structure from JSON:
npx @themrsami/folder-structure-generator create-structure ./structure.json -o generated-folder

Programmatic Usage

const { generateJson, createStructure } = require('@themrsami/folder-structure-generator');

// Example 1: Generate JSON from a directory
async function generateStructureJson() {
    try {
        const structure = await generateJson('./my-project');
        console.log(JSON.stringify(structure, null, 2));
    } catch (error) {
        console.error('Error:', error.message);
    }
}

// Example 2: Create folder structure from JSON
async function createProjectStructure() {
    const structure = {
        'src': {
            type: 'directory',
            children: {
                'components': {
                    type: 'directory',
                    children: {
                        'App.js': {
                            type: 'file',
                            content: 'function App() {\n  return <div>Hello World</div>;\n}'
                        }
                    }
                },
                'index.js': {
                    type: 'file',
                    content: 'console.log("Hello World!");'
                }
            }
        },
        'package.json': {
            type: 'file',
            content: '{\n  "name": "my-project",\n  "version": "1.0.0"\n}'
        }
    };
    
    try {
        await createStructure('./new-project', structure);
        console.log('Project structure created successfully!');
    } catch (error) {
        console.error('Error:', error.message);
    }
}

### Progress Tracking

The package now includes built-in progress tracking for both CLI and programmatic usage:

#### CLI Progress Bars
When using the CLI commands, you'll see a beautiful progress bar showing:
- Current progress percentage
- Number of processed files
- Currently processing file
- Visual progress bar

#### Programmatic Progress Tracking
```javascript
const { generateJson, createStructure, progress } = require('@themrsami/folder-structure-generator');

// Listen for progress events
progress.on('progress', ({ total, current, file }) => {
    console.log(`Processing: ${file} (${current}/${total})`);
});

// Use the functions as normal
await generateJson('./my-directory');

Performance Optimizations

The package now includes several optimizations:

  • Parallel file processing where possible
  • Efficient directory traversal
  • Minimal memory footprint
  • Progress tracking with negligible overhead

JSON Structure Format

The JSON structure follows this format:

{
    "directory-name": {
        "type": "directory",
        "children": {
            "file.txt": {
                "type": "file",
                "content": "File content here"
            },
            "nested-dir": {
                "type": "directory",
                "children": {
                    "another-file.js": {
                        "type": "file",
                        "content": "More content here"
                    }
                }
            }
        }
    }
}

Common Use Cases

  1. Project Templates: Save your project structures as JSON templates and quickly scaffold new projects.
const template = {
    'src': {
        type: 'directory',
        children: {
            'index.js': { type: 'file', content: '// Entry point' },
            'routes': {
                type: 'directory',
                children: {
                    'api.js': { type: 'file', content: '// API routes' }
                }
            }
        }
    }
};
await createStructure('./new-project', template);
  1. Backup Directory Structures: Generate JSON representations of your projects.
const backup = await generateJson('./my-project');
await fs.writeJson('./backup.json', backup);
  1. Project Scaffolding: Create standardized project structures.
const reactTemplate = {
    'src': {
        type: 'directory',
        children: {
            'components': { type: 'directory', children: {} },
            'styles': { type: 'directory', children: {} },
            'utils': { type: 'directory', children: {} },
            'App.js': {
                type: 'file',
                content: 'import React from "react";\n\nexport default function App() {\n  return <div>Hello World</div>;\n}'
            }
        }
    }
};

Error Handling

The package includes built-in error handling for common scenarios:

try {
    await createStructure('./output', structure);
} catch (error) {
    if (error.code === 'EEXIST') {
        console.error('Directory already exists');
    } else if (error.code === 'EACCES') {
        console.error('Permission denied');
    } else {
        console.error('Unexpected error:', error.message);
    }
}

CLI Options

generate-json command

npx @themrsami/folder-structure-generator generate-json [options] <directory>

Options:
  -o, --output <file>  Output JSON file (default: "structure.json")

create-structure command

npx @themrsami/folder-structure-generator create-structure [options] <jsonFile>

Options:
  -o, --output <directory>  Output directory (default: "generated-structure")

Author

License

MIT

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago