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.4.1 to 2.5.1

4

cli.js
#!/usr/bin/env node
/**
* A CLI tool to change the git hooks to commands from config
*/
const {setHooksFromConfig} = require('./simple-git-hooks')

@@ -8,0 +4,0 @@

4

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

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

"license": "MIT",
"repository": "https://github.com/toplenboren/simple-git-hooks"
"repository": "toplenboren/simple-git-hooks"
}

@@ -13,3 +13,2 @@ #!/usr/bin/env node

}
if (checkSimpleGitHooksInDependencies(projectDirectory)) {

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

@@ -10,54 +10,6 @@ # simple-git-hooks

## Usage
- Zero dependency
- Small configuration (1 object in package.json)
- Lightweight:
### 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`.
> There are more ways to configure the package. Check out [Additional configuration options](#additional-configuration-options).
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.
### Update git hooks command
1. Change the configuration.
2. Run `npx simple-git-hooks` **from the root of your project**.
Note for **yarn2** users: Please run `yarn dlx simple-git-hooks` instead of the command above. More info on [dlx](https://yarnpkg.com/cli/dlx)
Note that you should manually run `npx simple-git-hooks` **every time you change a command**.
Visit [github page](https://github.com/toplenboren/simple-git-hooks) for documentation
const fs = require('fs')
const os = require("os");
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']

@@ -68,3 +37,2 @@

}
function getProjectRootDirectoryFromNodeModules(projectPath) {

@@ -98,3 +66,2 @@ function _arraysAreEqual(a1, a2) {

}
function checkSimpleGitHooksInDependencies(projectRootPath) {

@@ -104,9 +71,6 @@ if (typeof projectRootPath !== 'string') {

}
const {packageJsonContent} = _getPackageJson(projectRootPath)
// if simple-git-hooks in dependencies -> note user that he should remove move it to devDeps!
if ('dependencies' in packageJsonContent && 'simple-git-hooks' in packageJsonContent.dependencies) {
console.log('[WARN] You should move simple-git-hooks to the devDependencies!')
return true // We only check that we are in the correct package, e.g not in a dependency of a dependency
return true
}

@@ -118,16 +82,8 @@ if (!('devDependencies' in packageJsonContent)) {

}
/**
* Parses the config and sets git hooks
* @param {string} projectRootPath
*/
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) {

@@ -141,15 +97,15 @@ if (Object.prototype.hasOwnProperty.call(config, hook)) {

}
function _setHook(hook, command, projectRoot=process.cwd()) {
const gitRoot = getGitProjectRoot(projectRoot)
const hookCommand = "#!/bin/sh" + os.EOL + command
const hookPath = path.normalize(gitRoot + '/hooks/' + hook)
const hookCommand = "#!/bin/sh\n" + command
const hookDirectory = gitRoot + '/hooks/'
const hookPath = path.normalize(hookDirectory + hook)
const normalizedHookDirectory = path.normalize(hookDirectory)
if (!fs.existsSync(normalizedHookDirectory)) {
fs.mkdirSync(normalizedHookDirectory)
}
fs.writeFileSync(hookPath, hookCommand)
fs.chmodSync(hookPath, 0o0755)
console.log(`[INFO] Successfully set the ${hook} with command: ${command}`)
}
function removeHooks(projectRoot=process.cwd()) {

@@ -160,7 +116,5 @@ 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)) {

@@ -170,3 +124,2 @@ fs.unlinkSync(hookPath)

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

@@ -176,13 +129,9 @@ 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) {

@@ -192,4 +141,2 @@ if (typeof projectRootPath !== 'string') {

}
// every function here should accept projectRootPath as first argument and return object
const sources = [

@@ -202,3 +149,2 @@ () => _getConfigFromFile(projectRootPath, '.simple-git-hooks.js'),

]
for (let executeSource of sources) {

@@ -216,3 +162,2 @@ let config = executeSource()

}
function _getConfigFromPackageJson(projectRootPath = process.cwd()) {

@@ -222,3 +167,2 @@ const {packageJsonContent} = _getPackageJson(projectRootPath)

}
function _getConfigFromFile(projectRootPath, fileName) {

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

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

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

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

@@ -245,5 +187,3 @@ return undefined

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

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

}
return true
}
module.exports = {

@@ -260,0 +198,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