skip to Main Content

For my job, we create a lot of small web applications with NodeJS for the backend and Angular for the frontend

Said apps usually involve a lot of CRUDs

With Angular, I can run:

ng generate component 'component-name'

Which generates the ts, html, scss, and spec files

How would I go about creating a custom script to do something similar in NodeJS?

Currently, the project uses ExpressJS and Sequelize, and the structure as follows:

├── src
│   ├── controllers
│   |   ├── product.controller.js
│   ├── models
|   |   ├── product.model.js
│   ├── routes
|   |   ├── product.routes.js
├── index.js
├── package.json

Basically I want to create a script that generates all 3 files when given the name, something like

node generate client

Is it possible?

3

Answers


  1. I have been using express-generator for my express projects.

    1. Install generator as global package through npm

      $ npm install -g express-generator

    2. Create your express app

      $ express myapp

    This will creates an express app named myapp. The app will be created in a folder named myapp in the current working directory and the view engine will be set to jade by default.

    .
    ├── app.js
    ├── bin
    │   └── www
    ├── package.json
    ├── public
    │   ├── images
    │   ├── javascripts
    │   └── stylesheets
    │       └── style.css
    ├── routes
    │   ├── index.js
    │   └── users.js
    └── views
        ├── error.pug
        ├── index.pug
        └── layout.pug
    
    7 directories, 9 files
    
    

    If you don’t want any view engine simple remove it.

    1. Install dependencies and run the app

      $ npm i && npm start

    This is will the run server at http://localhost:3000/

    More on

    Login or Signup to reply.
  2. In node you can make use of fs library to create folder and write files into it.
    for example like this,

    const fs = require('fs');
    
    const html = `custom starter html script as per your requirement`;
    const css = `custom script as per your requirement`;
    const js = `custom script as per your requirement`;
    
    const folderName = process.argv[2];
    
    function createFiles() {
      try{
        if(folderName) {
          fs.mkdirSync(`${folderName}`); //creates the folder name with the argument you have provided.
          fs.writeFileSync(`${folderName}\index.html`,html); // writes to the folder you have created with the custom file contents.
      
    }
    }catch(e){
      console.log(`Error: ${e}`);
      }
      }
      
      
    if(folderName){
      createFiles();
    }else{
      console.log("Please enter the folder name");
     }
      
      
      
      
    Login or Signup to reply.
  3. You can simply do this by adding a bash script, and calling it in your package.json.

    Step 1: create a bash script generate-controller-model-route.sh in the route folder

    #!/bin/bash
    
    file_name=$1
    
    if [[ -z $file_name ]]; then
        echo "Error: file name not received."
        exit 1
    fi
    controller_folder='./src/controllers'
    model_folder='./src/models'
    route_folder='./src/routes'
    
    touch "$controller_folder/$file_name.controller.js"
    touch "$model_folder/$file_name.model.js"
    touch "$route_folder/$file_name.route.js"
    

    Step 2: add the bash script to the package.json (scripts property).

    "scripts": {
            "generate": "bash ./api/generate-controller-model-route.sh $1"
    }
    
    

    Step 3: call it with yarn/npm

    yarn generate client
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search