Socket
Socket
Sign inDemoInstall

simple-pre-commit

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-pre-commit - npm Package Compare versions

Comparing version 1.1.2 to 1.1.3

9

cli.js
#!/usr/bin/env node
const os = require('os')
/** A CLI tool to change the pre-commit command from package.json
* It really has only one function — to set new pre commit hook.
* Checks the package.json for simple-pre-commit hook command and sets the found command as hook
*/
const {getCommandFromConfig, setPreCommitHook} = require('./simple-pre-commit')
const command = getCommandFromConfig(process.cwd())
if (!command) {

@@ -17,5 +10,3 @@ console.log(`Couldn't parse command! Please add command to package.json or .simple-pre-commit.json. See README.md for details`)

}
setPreCommitHook(command)
console.log('Set pre commit hook: ' + command)

4

package.json
{
"name": "simple-pre-commit",
"version": "1.1.2",
"version": "1.1.3",
"description": "A simple, zero dependency tool for setting up git pre-commit hook for small projects",

@@ -9,3 +9,3 @@ "author": "Mikhail Gorbunov <toplenboren@gmail.com> (toplenboren.gituhb.io)",

"scripts": {
"publish": "clean-publish",
"publish": "clean-publish --without-publish",
"postinstall": "node ./postinstall.js"

@@ -12,0 +12,0 @@ },

@@ -5,12 +5,4 @@ #!/usr/bin/env node

/**
* Creates the pre-commit from command in config by default
*/
function postinstall() {
let projectDirectory;
/* When script is run after install, the process.cwd() would be like <project_folder>/node_modules/simple-pre-commit
Here we try to get the original project directory by going upwards by 2 levels
If we were not able to get new directory we assume, we are already in the project root */
const parsedProjectDirectory = getProjectRootDirectoryFromNodeModules(process.cwd())

@@ -22,3 +14,2 @@ if (parsedProjectDirectory !== undefined) {

}
if (checkSimplePreCommitInDependencies(projectDirectory)) {

@@ -25,0 +16,0 @@ try {

# simple-pre-commit
![](https://img.shields.io/npm/v/simple-pre-commit) ![](https://img.shields.io/badge/dependencies-zero-green)
A tool, that let you set any command from `package.json` as a pre-commit hook.
- Zero dependency
- Lightweight
- Easy to install
- Dead simple to use
- Easy to configure (one line in `package.json`)
- Lightweight*:
## Add pre-commit hook to the project:
| Package name | Unpacked size |
| ------------- | ------------- |
| husky v4 `4.3.8` | `53.5 kB` |
| husky v5 `5.0.9` | `24.5 kB` |
| pre-commit `1.2.2` | `~80kB` |
| **simple-pre-commit** `1.1.3` | `9.0 kB` |
## Usage
### Add pre-commit hook to the project:
1. Install the simple-pre-commit as dev dependency

@@ -18,3 +29,3 @@

2. Add the `simple-pre-commit` to your `package.json`. Feed it with any command you would like to run as a pre-commit hook.
2. Add the `simple-pre-commit` to your `package.json`. Feed it with any command you would like to run as a pre-commit hook.

@@ -25,2 +36,4 @@ ```json

> There are more ways to configure the package. Check out [additional configuration](#Additional configuration options)
3. Run the CLI script to update the git hook with command from `package.json`

@@ -33,18 +46,17 @@

Now the command from `package.json` is set up as executable git pre-commit hook.
You can look up about git hooks [here](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks)
## Updating a pre-commit hook command
### Update a pre-commit hook command
Run `npx simple-pre-commit` **from root of your project**
Note that you should manually run `npx simple-pre-commit` every time you change the command
Note that you should manually run `npx simple-pre-commit` **every time you change the command**
## Additional configuration options
### Additional configuration options
You can also add the `.simple-pre-commit.json` to the project and write the command inside it, if you do not want to put command inside `package.json`
You can also add the `.simple-pre-commit.json` or `simple-pre-commit.json` to the project and write the command inside it, if you do not want to put command inside `package.json`
That way, `.simple-pre-commit.json` should look like this and `package.json` may not have `simple-pre-commit` configuration inside it
That way, `.simple-pre-commit.json` or `simple-pre-commit.json` should look like this and `package.json` may not have `simple-pre-commit` configuration inside it
```(json)
```json
{

@@ -51,0 +63,0 @@ "simple-pre-commit":"npx lint staged"

@@ -5,7 +5,2 @@ const fs = require('fs')

/**
* Recursively gets the .git folder path from provided directory
* @param {string} directory
* @return {string | undefined} .git folder path or undefined if it was not found
*/
function getGitProjectRoot(directory=module.parent.filename) {

@@ -41,8 +36,2 @@ let start = directory

}
/**
* Transforms the <project>/node_modules/simple-pre-commit to <project>
* @param projectPath - path to the simple-pre-commit in node modules
* @return {string | undefined} - an absolute path to the project or undefined if projectPath is not in node_modules
*/
function getProjectRootDirectoryFromNodeModules(projectPath) {

@@ -66,9 +55,2 @@ function _arraysAreEqual(a1, a2) {

}
/**
* Checks the 'simple-pre-commit' in dependencies of the project
* @param {string} projectRootPath
* @throws TypeError if packageJsonData not an object
* @return {Boolean}
*/
function checkSimplePreCommitInDependencies(projectRootPath) {

@@ -91,11 +73,2 @@ if (typeof projectRootPath !== 'string') {

}
/**
* Gets user-set command either from sources
* First try to get command from .simple-pre-commit.json
* If not found -> try to get command from package.json
* @param {string} projectRootPath
* @throws TypeError if projectRootPath is not string
* @return {string | undefined}
*/
function getCommandFromConfig(projectRootPath) {

@@ -108,8 +81,9 @@ if (typeof projectRootPath !== 'string') {

const sources = [
_getCommandFromSimplePreCommitJson,
_getCommandFromPackageJson,
() => _getCommandFromFile(projectRootPath, '.simple-pre-commit.json'),
() => _getCommandFromFile(projectRootPath, 'simple-pre-commit.json'),
() => _getCommandFromPackageJson(projectRootPath),
]
for (let i = 0; i < sources.length; ++i) {
let command = sources[i](projectRootPath)
let command = sources[i]()
if (command) {

@@ -122,7 +96,2 @@ return command

}
/**
* Creates or replaces an existing executable script in .git/hooks/pre-commit with provided command
* @param {string} command
*/
function setPreCommitHook(command) {

@@ -137,10 +106,2 @@ const gitRoot = getGitProjectRoot(process.cwd())

}
/** Reads package.json file, returns package.json content and path
* @param {string} projectPath - a path to the project, defaults to process.cwd
* @return {{packageJsonContent: any, packageJsonPath: string}}
* @throws TypeError if projectPath is not a string
* @throws Error if cant read package.json
* @private
*/
function _getPackageJson(projectPath = process.cwd()) {

@@ -160,10 +121,2 @@ if (typeof projectPath !== "string") {

}
/**
* Gets current command from package.json[simple-pre-commit]
* @param {string} projectRootPath
* @throws TypeError if packageJsonPath is not a string
* @throws Error if package.json couldn't be read
* @return {undefined | string}
*/
function _getCommandFromPackageJson(projectRootPath = process.cwd()) {

@@ -173,10 +126,3 @@ const {packageJsonContent} = _getPackageJson(projectRootPath)

}
/**
* Gets user-set command from simple-pre-commit.json
* Since the file is not required in node.js projects it returns undefined if something is off
* @param {string} projectRootPath
* @return {string | undefined}
*/
function _getCommandFromSimplePreCommitJson(projectRootPath) {
function _getCommandFromFile(projectRootPath, fileName) {
if (typeof projectRootPath !== "string") {

@@ -186,4 +132,8 @@ throw TypeError("projectRootPath is not a string")

if (typeof fileName !== "string") {
throw TypeError("fileName is not a string")
}
try {
const simplePreCommitJsonPath = path.normalize(projectRootPath + '/simple-pre-commit.json')
const simplePreCommitJsonPath = path.normalize(projectRootPath + '/' + fileName)
const simplePreCommitJsonRaw = fs.readFileSync(simplePreCommitJsonPath)

@@ -190,0 +140,0 @@ const simplePreCommitJson = JSON.parse(simplePreCommitJsonRaw)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc