Search code examples
githubgithub-actionsgithub-pages

Git Hub Action remove console.log()


I am learning the GitHub action now I have my project which consists of Angular as the front end and WordPress as the backend. For testing purposes, I have used a lot of console.log() functions for testing purposes. Now I want when I deploy or commit to the Git Hub it automatically removes all the logs so it won't be visible on deployed version.


Solution

  • You can use the Find and Replace action to replace all the console.log() occurrences as an intermediate step before deployment.

    For example:

    name: Deploy
    on:
      push:
        tags:
          - '*'
    
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
          - uses: actions/checkout@v2
    
          - name: Find and Replace
            uses: jacobtomlinson/gha-find-replace@v2
            with:
              find: "console\.log\(([^)]+)\)"
              replace: ""
              regex: true
    
          # deployment steps
    

    Also, I would recommend disabling console.log()s in production as an alternative. Disabling console.log() in production.

    P.S. I'm not sure if regex is correct, it should be tested well. Took it here.