mern-forge-core v1.1.32
Core Code Generator
This repository contains the code generator for the core of the project. The code generator is used to generate the core code from the templates. The code generator is written in TypeScript and uses Handlebars for templating.
Table of Contents
Main Classes:
Following are the main classes that are used in the code generator.
File
This is the base class for all files that can be generated.
Properties
content
- The content of the filename
- The name of the file
Methods
generate(path: string): Promise<void>
- Generates the file in the given path
Usage
This class used to be extended by other classes to generate files.
StaticFile
This is a file that is copied from a given path. Extended from the File
class.
Usage
This class should be used for such files that are not generated but copied from a given path.
TemplateFile
This is a file that is generated from a template. Extended from the File
class.
The template should be a .hbs
file and the data should be an object.
Usage
This class should be used for such files that are generated from a template.
Folder
This is a folder that can contain other folders and files. It can be generated at a given path where it will create a folder with the given name and generate all files and folders within that folder.
Properties
name
- The name of the folder
Methods
addFile(file: File | string): this
- Adds a file to the folder. If a string is given, a newFile
object is created with the given name.addFolder(folder: Folder | string): this
- Adds a folder to the folder. If a string is given, a newFolder
object is created with the given name.addFiles(files: (File | string)[]): this
- Adds multiple files to the folder. If a string is given, a newFile
object is created with the given name.addFolders(folders: (Folder | string)[]): this
- Adds multiple folders to the folder. If a string is given, a newFolder
object is created with the given name.findFile(name: string): File | null
- Finds a file with the given name in the folder. If the file is not found,null
is returned.findFileOrCreate(name: string): File
- Finds a file with the given name in the folder. If the file is not found, a newFile
object is created with the given name and returned.findFolder(name: string): Folder | null
- Finds a folder with the given name in the folder. If the folder is not found,null
is returned.findFolderOrCreate(name: string): Folder
- Finds a folder with the given name in the folder. If the folder is not found, a newFolder
object is created with the given name and returned.find(name: string): File | Folder | null
- Finds a file or folder with the given name in the folder. If the file or folder is not found,null
is returned.generate(path: string): Promise<void>
- Generates the folder at the given path.
Usage
This class is a container for files and folders. It can be used to create a folder structure and generate it at a given path.
Project
This is an abstract class that can be used to create a project. It contains a root folder that can be used to create a folder structure and generate it at a given path.
All projects must implement the process
method which is used to process the data and create the folder structure.
Properties
root
- The root folder of the project
Methods
generate(path: string): Promise<void>
- Generates the project at the given path.process(data: T): this
- Processes the data and creates the folder structure.
Usage
This class is an abstract class that can be used to create a project. It contains a root folder that can be used to create a folder structure and generate it at a given path.
Project Structure
For every new project template, a new folder is created in the src/projects
folder. The folder name should be the same as the template name. The folder should contain the following files: (Taking express as an example)
Express.ts
- The project class that extends theProject
class.IEpress.ts
- The interface that defines the data that is used to create the project.
There should be following folder structure in the project folder:
sample
- A folder that contains a sample project that is generated using the template.types
- A folder that contains the types that are used in the project.enums
- A folder that contains the enums that are used in the project.utils
- A folder that contains the utility functions that are used in the project.files
- A folder that contains the files that are used in the project. This will contain the following sub-folders:template
- A folder that contains the template files that are used to generate the project files. It will contain sub-folders for each file type with following files: (Takingapp.js
as an example)app.js.hbs
- The handlebars template file.index.ts
- TheAppJS
class that extends theFile
class that uses the template file to generate theapp.js
file.IAppJS.ts
- The interface that defines the data that is used to generate theapp.js
file.
static
- A folder that contains the static files that are used in the project. It will contain all static files that are used in the project in their respective sub-folders.
composers
- A folder that contains the composer functions that will generate multiple files and folders for a specific case: (Taking example ofcomposeRoutes
function as an example)import Folder from 'base/Folder'; import RouteJs from '../files/template/RouteJs'; import IExpressBasic from '../IExpressBasic'; const composeRoutes = (data: IExpressBasic, rootFolder: Folder) => { const routesFolder = new Folder('routes'); data.entities.forEach((entity) => { routesFolder.addFile(new RouteJs(entity.name, { entity })); }); rootFolder.addFolder(routesFolder); }; export default composeRoutes;
Adding a new project template
To add a new project template, follow the steps below:
Create a new folder in the
src/projects
folder with the name of the template.Create a new file in the project folder with the name of the template and the extension
.ts
. This will be the project class that extends theProject
class.Create a new file in the project folder with the name of the template and the extension
.ts
. This will be the interface that defines the data that is used to create the project.Create a new folder in the project folder with the name
sample
. This will contain a sample project that is generated using the template.Create template files in the
files/template
folder. These files will be used to generate the project files. It will contain sub-folders for each file type with following files: (Takingapp.js
as an example)app.js.hbs
- The handlebars template file.index.ts
- TheAppJS
class that extends theFile
class that uses the template file to generate theapp.js
file.IAppJS.ts
- The interface that defines the data that is used to generate theapp.js
file.
Create static files in the
files/static
folder. These files will be copied as it is to the generated project. It will contain all static files that are used in the project in their respective sub-folders.Create composer functions in the
composers
folder. These functions will generate multiple files and folders for a specific case: (Taking example ofcomposeRoutes
function as an example)Call all composer functions in the
process
method of the project class. And add the generated files and folders to the root folder.Use this class to generate the project in the
src/app.ts
file.
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago