Socket
Socket
Sign inDemoInstall

simple-git-hooks

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple-git-hooks - npm Package Compare versions

Comparing version 2.5.1 to 2.6.1

6

package.json
{
"name": "simple-git-hooks",
"version": "2.5.1",
"version": "2.6.1",
"description": "A simple, zero dependency tool for setting up git hooks for small projects",

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

"scripts": {
"postinstall": "node ./postinstall.js",
"uninstall": "node ./uninstall.js"
"uninstall": "node ./uninstall.js",
"postinstall": "node ./postinstall.js"
},

@@ -13,0 +13,0 @@ "keywords": [

#!/usr/bin/env node
const {checkSimpleGitHooksInDependencies, getProjectRootDirectoryFromNodeModules, setHooksFromConfig} = require("./simple-git-hooks");
function postinstall() {

@@ -21,3 +20,2 @@ let projectDirectory;

}
postinstall()

@@ -8,4 +8,2 @@ # simple-git-hooks

> The package was recently renamed from `simple-pre-commit`. See **Releases** for the `simple-pre-commit` documentation.
- Zero dependency

@@ -15,2 +13,42 @@ - Small configuration (1 object in package.json)

Visit [github page](https://github.com/toplenboren/simple-git-hooks) for documentation
## Usage
### Add simple-git-hooks to the project
1. Install simple-git-hooks as a dev dependency:
```sh
npm install simple-git-hooks --save-dev
```
2. Add `simple-git-hooks` to your `package.json`. Fill it with git hooks and the corresponding commands.
For example:
```jsonc
{
"simple-git-hooks": {
"pre-commit": "npx lint-staged",
"pre-push": "cd ../../ && npm run format",
// All unused hooks will be removed automatically by default
// but you can use the `preserveUnused` option like following to prevent this behavior
// if you'd prefer preserve all unused hooks
"preserveUnused": true,
// if you'd prefer preserve specific unused hooks
"preserveUnused": ["commit-msg"]
}
}
```
This configuration is going to run all linters on every `commit` and formatter on `push`.
3. Run the CLI script to update the git hooks with the commands from the config:
```sh
npx simple-git-hooks
```
Now all the git hooks are created.
const fs = require('fs')
const path = require('path');
const VALID_GIT_HOOKS = ['applypatch-msg', 'pre-applypatch', 'post-applypatch', 'pre-commit', 'pre-merge-commit', 'prepare-commit-msg', 'commit-msg', 'post-commit', 'pre-rebase', 'post-checkout', 'post-merge', 'pre-push', 'pre-receive', 'update', 'proc-receive', 'post-receive', 'post-update', 'reference-transaction', 'push-to-checkout', 'pre-auto-gc', 'post-rewrite', 'sendemail-validate', 'fsmonitor-watchman', 'p4-changelist', 'p4-prepare-changelist', 'p4-post-changelist', 'p4-pre-submit', 'post-index-change',]
const VALID_GIT_HOOKS = [
'applypatch-msg',
'pre-applypatch',
'post-applypatch',
'pre-commit',
'pre-merge-commit',
'prepare-commit-msg',
'commit-msg',
'post-commit',
'pre-rebase',
'post-checkout',
'post-merge',
'pre-push',
'pre-receive',
'update',
'proc-receive',
'post-receive',
'post-update',
'reference-transaction',
'push-to-checkout',
'pre-auto-gc',
'post-rewrite',
'sendemail-validate',
'fsmonitor-watchman',
'p4-changelist',
'p4-prepare-changelist',
'p4-post-changelist',
'p4-pre-submit',
'post-index-change',
]
const VALID_OPTIONS = ['preserveUnused']

@@ -37,2 +67,3 @@

}
function getProjectRootDirectoryFromNodeModules(projectPath) {

@@ -50,3 +81,2 @@ function _arraysAreEqual(a1, a2) {

// A yarn2 STAB
if (projDir.includes('.yarn') && projDir.includes('unplugged')) {

@@ -67,2 +97,3 @@ return undefined

}
function checkSimpleGitHooksInDependencies(projectRootPath) {

@@ -72,3 +103,5 @@ if (typeof projectRootPath !== 'string') {

}
const {packageJsonContent} = _getPackageJson(projectRootPath)
if ('dependencies' in packageJsonContent && 'simple-git-hooks' in packageJsonContent.dependencies) {

@@ -83,8 +116,12 @@ console.log('[WARN] You should move simple-git-hooks to the devDependencies!')

}
function setHooksFromConfig(projectRootPath=process.cwd()) {
const config = _getConfig(projectRootPath)
if (!config) {
throw('[ERROR] Config was not found! Please add `.simple-git-hooks.js` or `simple-git-hooks.js` or `.simple-git-hooks.json` or `simple-git-hooks.json` or `simple-git-hooks` entry in package.json.\r\nCheck README for details')
}
const preserveUnused = Array.isArray(config.preserveUnused) ? config.preserveUnused : config.preserveUnused ? VALID_GIT_HOOKS: []
for (let hook of VALID_GIT_HOOKS) {

@@ -98,7 +135,10 @@ if (Object.prototype.hasOwnProperty.call(config, hook)) {

}
function _setHook(hook, command, projectRoot=process.cwd()) {
const gitRoot = getGitProjectRoot(projectRoot)
const hookCommand = "#!/bin/sh\n" + command
const hookDirectory = gitRoot + '/hooks/'
const hookPath = path.normalize(hookDirectory + hook)
const normalizedHookDirectory = path.normalize(hookDirectory)

@@ -108,6 +148,9 @@ if (!fs.existsSync(normalizedHookDirectory)) {

}
fs.writeFileSync(hookPath, hookCommand)
fs.chmodSync(hookPath, 0o0755)
console.log(`[INFO] Successfully set the ${hook} with command: ${command}`)
}
function removeHooks(projectRoot=process.cwd()) {

@@ -118,5 +161,7 @@ for (let configEntry of VALID_GIT_HOOKS) {

}
function _removeHook(hook, projectRoot=process.cwd()) {
const gitRoot = getGitProjectRoot(projectRoot)
const hookPath = path.normalize(gitRoot + '/hooks/' + hook)
if (fs.existsSync(hookPath)) {

@@ -126,2 +171,3 @@ fs.unlinkSync(hookPath)

}
function _getPackageJson(projectPath = process.cwd()) {

@@ -131,9 +177,13 @@ if (typeof projectPath !== "string") {

}
const targetPackageJson = path.normalize(projectPath + '/package.json')
if (!fs.statSync(targetPackageJson).isFile()) {
throw Error("Package.json doesn't exist")
}
const packageJsonDataRaw = fs.readFileSync(targetPackageJson)
return { packageJsonContent: JSON.parse(packageJsonDataRaw), packageJsonPath: targetPackageJson }
}
function _getConfig(projectRootPath) {

@@ -143,4 +193,8 @@ if (typeof projectRootPath !== 'string') {

}
// every function here should accept projectRootPath as first argument and return object
const sources = [
() => _getConfigFromFile(projectRootPath, '.simple-git-hooks.cjs'),
() => _getConfigFromFile(projectRootPath, '.simple-git-hooks.js'),
() => _getConfigFromFile(projectRootPath, 'simple-git-hooks.cjs'),
() => _getConfigFromFile(projectRootPath, 'simple-git-hooks.js'),

@@ -151,2 +205,3 @@ () => _getConfigFromFile(projectRootPath, '.simple-git-hooks.json'),

]
for (let executeSource of sources) {

@@ -168,2 +223,3 @@ let config = executeSource()

}
function _getConfigFromFile(projectRootPath, fileName) {

@@ -173,5 +229,7 @@ if (typeof projectRootPath !== "string") {

}
if (typeof fileName !== "string") {
throw TypeError("fileName is not a string")
}
try {

@@ -182,3 +240,3 @@ const filePath = path.normalize(projectRootPath + '/' + fileName)

}
return require(filePath)
return require(filePath) // handle `.js` and `.json`
} catch (err) {

@@ -188,3 +246,5 @@ return undefined

}
function _validateHooks(config) {
for (let hookOrOption in config) {

@@ -195,4 +255,6 @@ if (!VALID_GIT_HOOKS.includes(hookOrOption) && !VALID_OPTIONS.includes(hookOrOption)) {

}
return true
}
module.exports = {

@@ -199,0 +261,0 @@ checkSimpleGitHooksInDependencies,

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