Coder Social home page Coder Social logo

prevent-forbidden-code's Introduction

prevent-forbidden-code

This module is intended as a ghooks or husky compatible plugin that prevents unwanted code from being committed into your repository.

NPM

NPM Version Build Status npm semantic-release David David

Prerequisites

  • git
  • Node >= 4.4.x
  • ghooks or husky

Make sure you have a git repository (git init) BEFORE installing ghooks, otherwise you have to take extra steps if you install ghooks before running git init.

Installing

npm install prevent-forbidden-code ghooks --save-dev

Basic Configuration

// inside package.json
...
  "config": {
    "ghooks": {
      "pre-commit": "prevent-forbidden-code",
    }
  }
...

How it works, and default settings

By default, prevent-forbidden-code screens for the following array of commands in your code:

[
    "console.log(",
    "console.info(",
    "console.error(",
    "console.warn(",
    "debugger",
    "var_dump",
    "print_r",
    "fdescribe(",
    "fit(",
    "ddescribe(",
    "iit("
]

With each commit, the committed files are scanned for any lines containing these commands. If found, prevent-forbidden-code alerts you as to the type of offending command found, and where they were found:

[ >>> BEGIN PRE-COMMIT FORBIDDEN CODE CHECK ]
FAILURE: You left a console.log( in README.md
[ >>> COMMIT REJECTED ]
If you absolutely need to commit this use git commit --no-verify (-n)

Note: You can prevent the scan entirely by using the --no-verify flag on your commit, as mentioned in the output sample above.

Configuration Options

Exclusions

Adding exclusions to your configuration allows prevent-forbidden-code to skip one or more matching files from the scan. Under the hood, prevent-forbidden-code uses the excellent minimatch module to convert glob expressions in the array into regular expressions for file name matching. In the following configuration sample, all usages of the normally prevented commands in any markdown file would would be allowed.

// inside package.json
...
  "config": {
    "ghooks": {
      "pre-commit": "prevent-forbidden-code",
    },
    "prevent-forbidden-code": {
      "exclude": ["*.md"]
    }
  }
...

Rejecting Custom Lists of Commands

To override the default list of forbidden commands, just add your own forbid parameter to the config as an array of commands strings. The following example would scan files only for describe.only( and it.only( usages.

// inside package.json
...
  "config": {
    "ghooks": {
      "pre-commit": "prevent-forbidden-code",
    },
    "prevent-forbidden-code": {
      "forbid": ["describe.only(", "it.only("]  
      "exclude": ["*.md"]
    }
  }
...

License

This software is licensed under the MIT license.

prevent-forbidden-code's People

Contributors

keithpepin-mw avatar keithpepin avatar keithpepin-cyg avatar

Stargazers

Ian Chouinard avatar  avatar

Watchers

 avatar

prevent-forbidden-code's Issues

Deleting a file prior to running prevent-forbidden-code results in error

Deleted a file from a project and upon running the prevent-forbidden-code check, received the following error:

[ >>> BEGIN PRE-COMMIT FORBIDDEN CODE CHECK ]
fs.js:549
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^

Error: ENOENT: no such file or directory, open 'config/protractor.conf.js'
    at Error (native)
    at Object.fs.openSync (fs.js:549:18)
    at Object.fs.readFileSync (fs.js:397:15)
    at /Users/keithpepin/gyda/git-repo/gaf-protractor-config/node_modules/prevent-forbidden-code/index.js:62:31
    at Array.forEach (native)
    at /Users/keithpepin/gyda/git-repo/gaf-protractor-config/node_modules/prevent-forbidden-code/index.js:61:11
    at Git.<anonymous> (/Users/keithpepin/gyda/git-repo/gaf-protractor-config/node_modules/prevent-forbidden-code/node_modules/simple-git/src/git.js:929:18)
    at Git.<anonymous> (/Users/keithpepin/gyda/git-repo/gaf-protractor-config/node_modules/prevent-forbidden-code/node_modules/simple-git/src/git.js:1199:21)
    at emitTwo (events.js:87:13)
    at ChildProcess.emit (events.js:172:7)

Error: ENOENT: no such file or directory, open ''

When running checks in a separate project, getting the following error:

[ >>> BEGIN PRE-COMMIT FORBIDDEN CODE CHECK ]
fs.js:549
  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
                 ^

Error: ENOENT: no such file or directory, open ''
    at Error (native)
    at Object.fs.openSync (fs.js:549:18)
    at Object.fs.readFileSync (fs.js:397:15)
    at /Users/keithpepin/gyda/git-repo/gaf-protractor-config/node_modules/prevent-forbidden-code/index.js:62:31
    at Array.forEach (native)
    at /Users/keithpepin/gyda/git-repo/gaf-protractor-config/node_modules/prevent-forbidden-code/index.js:61:11
    at Git.<anonymous> (/Users/keithpepin/gyda/git-repo/gaf-protractor-config/node_modules/prevent-forbidden-code/node_modules/simple-git/src/git.js:929:18)
    at Git.<anonymous> (/Users/keithpepin/gyda/git-repo/gaf-protractor-config/node_modules/prevent-forbidden-code/node_modules/simple-git/src/git.js:1199:21)
    at emitTwo (events.js:87:13)
    at ChildProcess.emit (events.js:172:7)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.