<file_sep>const Component = require('./component-files');


const fs = require('fs');
const YAML = require('yaml');
const path = require('path');


class Structure {
    folder = "./src"

    static isArray(obj) {
        return obj.length !== undefined
    }

    constructor(configFile, folder) {
        const file = fs.readFileSync(configFile, 'utf8')
        this.structureConfig = YAML.parse(file);
        this.folder = folder;
    }
    
    _getComponentsPaths(structure, currentPath = this.folder) {
        let paths = [];

        if (Structure.isArray(structure)) {
            paths.push(...structure.map( item => path.join(currentPath, item) ));
        } else {
            Object.entries(structure).forEach( ([itemName, nextStructure]) => {
                paths.push(...this._getComponentsPaths(nextStructure, path.join(currentPath, itemName)));
            } )
        }

        return paths;
    }

    createComponents() {
        const paths = this._getComponentsPaths(this.structureConfig);
        paths.map(this.createComponentFiles);
    }

    createComponentFiles(path) {
        fs.mkdirSync(path, {recursive: true});
        const component = new Component(path);
        component.extrude();
    }
}

module.exports = Structure;
<file_sep># Create React Components

This package created to simplify React components structure creation in new React project.

### Warning
Don't use this module in your old React projects, module can cause damage.
You are free to use this in new React project.

### How to use it?
1. Create new React project. Read a [official CRA docs](https://github.com/facebook/create-react-app#creating-an-app) to know how to do this. 
2. Create config with name `components.yml`. It can looks like:
    ```yaml
   # components.yml
    app: # it is the folder
      components: # it is the folder
        - App # it is the component
    client:
      Header:
        components:
          - Header
          - Navbar
          - NavbarMenu
    shared:
      components:
        - Button
        - Logo
    ```
3. Run `npx cr-components create-all` to parse and create components from a config.

Every component`s folder will be created with next files: _Component.jsx_, _Component.css_, _index.js_.

Example folder tree:
```
├───app
│   └───components
│       └───App
├───client
│   └───Header
│       └───components
│           ├───Header
│           ├───Navbar
│           └───NavbarMenu
└───shared
    └───components
        ├───Button
        └───Logo

```

### Advice
Run ```cr-components create-all -h``` to see more options.

##### To be continued...<file_sep>#!/usr/bin/env node

const {Command} = require('commander');
const Structure = require("./components-structure")

const program = new Command();

program
    .version(require('./package.json').version)
    .command('create-all')
    .description('clone a repository into a newly created directory')
    .option('-c, --config <config>', 'components config file', './components.yml')
    .option('-f, --folder <folder>', 'output src folder', "./src")
    .action((source) => {
        const {config, folder} = source;
        const structure = new Structure(config, folder);
        structure.createComponents();
        structure.createComponents();
    });


program.parse(process.argv);
