1.1.32 • Published 1 year ago

mern-forge-core v1.1.32

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

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 file
    • name - 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 new File object is created with the given name.
    • addFolder(folder: Folder | string): this - Adds a folder to the folder. If a string is given, a new Folder object is created with the given name.
    • addFiles(files: (File | string)[]): this - Adds multiple files to the folder. If a string is given, a new File object is created with the given name.
    • addFolders(folders: (Folder | string)[]): this - Adds multiple folders to the folder. If a string is given, a new Folder 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 new File 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 new Folder 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 the Project 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: (Taking app.js as an example)
      • app.js.hbs - The handlebars template file.
      • index.ts - The AppJS class that extends the File class that uses the template file to generate the app.js file.
      • IAppJS.ts - The interface that defines the data that is used to generate the app.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 of composeRoutes 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:

  1. Create a new folder in the src/projects folder with the name of the template.

  2. 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 the Project class.

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

  4. Create a new folder in the project folder with the name sample. This will contain a sample project that is generated using the template.

  5. 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: (Taking app.js as an example)

    • app.js.hbs - The handlebars template file.
    • index.ts - The AppJS class that extends the File class that uses the template file to generate the app.js file.
    • IAppJS.ts - The interface that defines the data that is used to generate the app.js file.
  6. 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.

  7. Create composer functions in the composers folder. These functions will generate multiple files and folders for a specific case: (Taking example of composeRoutes function as an example)

  8. Call all composer functions in the process method of the project class. And add the generated files and folders to the root folder.

  9. Use this class to generate the project in the src/app.ts file.

1.1.32

1 year ago

1.1.31

1 year ago

1.1.30

1 year ago

1.1.29

1 year ago

1.1.28

1 year ago

1.1.27

1 year ago

1.1.26

1 year ago

1.1.25

1 year ago

1.1.24

1 year ago

1.1.23

1 year ago

1.1.22

1 year ago

1.1.21

1 year ago

1.1.20

1 year ago

1.1.19

1 year ago

1.1.18

1 year ago

1.1.17

1 year ago

1.1.16

1 year ago

1.1.15

1 year ago

1.1.13

1 year ago

1.1.12

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago