component-file-generator

0.5.2 • Public • Published

Component File Generator

npm license Build Status npm npm

This package helps me and our team to generate complex components structues with ease

Installation

you can install it using npm :

npm i component-file-generator

Setup

Basic

you need to install node on your machine to use this package,

also create a simple file in your app root :

└── projectName
    └── ...
    └── generator.js

with the following code :

const generator = require('component-file-generator');
generator.exec(
  {
    service: {
      root:'./app/services',
      structure: {
        name: "[name]",
        children:[
          {
            type: "file",
            name: "README.md",
            content: "# [name] Service\n description"
          },
          {
            type: "file",
            name: "[name]Service.js",
            content: `import Service from '../Service';\n\nexport default class [name] {\n\t// instruction\n\t}\n}\n`
          },
          {
            type: "file",
            name: "package.json",
            content: "{\n\t\"main\": \"./[name]Service.js\"\n}"
          }
        ]
      }
    }
  },
);

for the above example we will get the following folder structure :

├─ ...
└─ app
  ├─ services
    ├─ [ComponentNameA]
    │  ├─ [ComponentNameA]Service.js
    │  ├─ README.md
       └─ package.json

to Execut it from root project using CLI :

node generator

it will prompt a question : in our case service name ?

service name ?
_

Multiple items

Easy as the first example:

with the following code :

const generator = require('component-file-generator');
generator.exec({
	service: {
		root:'./app/services',
		structure: {
			name: "[name]",
			children:[
				...
			]
		}
	},
	model: {
		root:'./app/models',
		structure: {
			name: "[name]",
			children:[
				...
			]
		}
	},
	controller: {
		root:'./app/controllers',
		structure: {
			name: "[name]",
			children:[
				...
			]
		}
	},
	...
});

you'll get a prompt with select options:

Custom Structure

You can add one or multiple custom component types, you pass an Object with a key that represent component name and value with an object with root and structure to define where and what should generate:

  • root folder where to generate structure
  • structure that containe your component file architecture

Example :

// in this case our component name is `service`
generator.exec({
	[key]: { // component/module nane
		root:'path/to/folder',
		structure: { ... },
	}
});

Name Modificators

We only implimented two lowerCase and capitalize for the moment,

you can use them like :

...
{
  type: "file",
  name: "[name:lowerCase].md",
  content: "# [name:capitalize] Service\n description"
}
...

Next

  • For structure proprety content will be able accept path for file templates too
  • Write proper documentation

Changelog 0.5.0

  • add name modificators: ['lowerCase', 'capitalize']
  • optimize workflow
  • update readme.md

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 0.5.2
    41
    • latest

Version History

Package Sidebar

Install

npm i component-file-generator

Weekly Downloads

61

Version

0.5.2

License

MIT

Unpacked Size

24.6 kB

Total Files

15

Last publish

Collaborators

  • khofaai
  • cephalon_lars