Skip to content

Commit

Permalink
feat(repo): initial 0.1.0 (#1)
Browse files Browse the repository at this point in the history
* feat(repo): initial

* feat(repo): adding husky config and running itself

* cleanup(repo): minor
  • Loading branch information
jeliasson authored May 25, 2022
1 parent 89181ba commit d502b2d
Show file tree
Hide file tree
Showing 24 changed files with 4,090 additions and 2 deletions.
24 changes: 24 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module.exports = {
root: true,
parser: '@typescript-eslint/parser',
env: {
node: true,
},
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
],
plugins: ['eslint-plugin-import-helpers'],
rules: {
'import-helpers/order-imports': [
'warn',
{
newlinesBetween: 'always',
groups: ['absolute', 'module', '/^@/', ['parent', 'sibling', 'index']],
alphabetize: { order: 'asc', ignoreCase: true },
},
],
},
ignorePatterns: ['templates/**/*.tsx'],
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
lib
node_modules
husky-hooks.config.js
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx @jeliasson/husky-hooks pre-commit
4 changes: 4 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx @jeliasson/husky-hooks pre-push
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
21 changes: 21 additions & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 jeliasson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
158 changes: 156 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,156 @@
# husky-hooks
This is a playground for creating a npm package for some husky hooks
# `@jeliasson/husky-hooks`

This npm package aims to increase the developer experience and consistancy by providing a set of hooks that can be opted-in the development lifecycle. It depends on [husky](https://www.npmjs.com/package/husky) for `pre-commit` and `pre-push` hooks, and a few other zero/low dependency packages.

> :warning: **Note**: This package is in development so please move with caution.
- [Setup](#setup)
- [Hooks](#hooks)
- [`check-branch`](#check-branch)
- [`check-lock-files`](#check-lock-files)
- [Other](#other)
- [Development](#development)
- [Prerequsites](#prerequsites)
- [Todo](#todo)
- [Contributing](#contributing)

## Setup

First, make sure you have [husky](https://www.npmjs.com/package/husky) installed. See installation [here](https://typicode.github.io/husky/#/?id=manual) or refer to below quick setup.

```bash
# Install husky dependency
yarn add --dev husky

# Install husky
yarn husky install
```

Once you have husky installed, let's proceed with setting up `@jeliasson/husky-hooks` and connect it with husky's `pre-commit` and `pre-push` hooks.

```bash
# Install dependency @jeliasson/husky-hooks
yarn add --dev @jeliasson/husky-hooks

# Add package pre-commit hook
npx husky add .husky/pre-commit "npx @jeliasson/husky-hooks pre-commit"

# Add package pre-push hook
npx husky add .husky/pre-push "npx @jeliasson/husky-hooks pre-push"

# Create configuration file (husky-hooks.config.js)
npx @jeliasson/husky-hooks generate-config

# To test;
# Make a new branch, create a test file, git add and commit
git checkout -b testing/jeliasson-husky-hooks
touch test.tmp
git add test.tmp
git commit -m "test(repo): keep calm and commit"
```

## Hooks

Hooks to run is defined in the configuration file `husky-hooks.config.js` that was created as part of the setup. Below is a table of available built-in hooks, followed by it's respective specific configuration. Future hooks may be created that require additional dev dependencies, not included in this package, but those will be marked.

| Name | Description |
| --------------------------------------- | ------------------------------------------------------------------------------------------------ |
| [`check-branch`](#check-branch) | Check which git branch we're currently on, and abort if it's a protected branch. |
| [`check-lock-files`](#check-lock-files) | Check for package manager lock files, and abort if any are present lock file that we don't want. |

#### `check-branch`

Check which git branch we're currently on, and abort if it's a protected branch. This can be useful to make sure that commits stay away from branches that only being used for Pull Requests or CI/CD.

**Setup**

Add `check-branch` hook to `pre-commit` and/or `pre-push`.

**Config**

```js
{
settings: {
'check-branch': {
// Git branches that should be protected from accidental commit or push
protectedBranches: ['main', 'dev'],
},
}
}
```

#### `check-lock-files`

Check for package manager lock files, and abort if any are present lock file that we don't want. This is useful to ensure that e.g. only`yarn.lock` is present in the repository.

**Setup**

Add `check-lock-files` hook to `pre-commit` and/or `pre-push`.

**Config**

```js
{
settings: {
'check-lock-files': {
// Package manager lock file that should be present in the repository
allowLockFile: 'yarn.lock',

// Package manager lock files that should yield a abort
denyLockFiles: ['package-lock.json', 'pnpm-lock.yaml'],
},
}
}
```

## Other

**Print output**

Sometimes you may want to print the actual output of a hook, and passing `--stdout` will print the stdout of all hooks.

```bash
npx @jeliasson/husky-hooks pre-commit --stdout
```

## Development

### Prerequsites

- NodeJS >= 14
- yarn

From the package directory, run

```bash
yarn link
```

Start tsc watch

```bash
yarn dev
```

From the test project directory, run

```bash
yarn link husky-hooks
```

### Todo

- [ ] Move this to GitHub Issues
- [x] Use this package in the development
- [ ] Make stable
- [ ] Write tests
- [ ] CI/CD for testing
- [ ] Use [zod](https://www.npmjs.com/package/zod) for configuration parsing
- [ ] Add [cz-cli](https://github.com/commitizen/cz-cli) as 3rd party
- [ ] Replace yargs with [clipanion](https://www.npmjs.com/package/clipanion)
- [ ] Add `stdout` as optional to all settings
- [ ] 🚀

## Contributing

See [CONTRIBUTING](CONTRIBUTING.md) ❤️
59 changes: 59 additions & 0 deletions husky-hooks.config.default.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const config = {
hooks: {
'pre-commit': [
'test-sleep', // This is a dummy hook for demo purposes
'test-check-ip', // This is a dummy hook for demo purposes
'check-branch',
'check-lock-files',
],

'pre-push': [
'test-sleep', // This is a dummy hook for demo purposes
'test-check-ip', // This is a dummy hook for demo purposes
'check-branch',
'check-lock-files',
],
},

settings: {
/**
* check-branch
*
* Check which git branch we're currently on, and abort if it's a
* protected branch. This can be useful to make sure that commits stay
* away from branches that only being used for Pull Requests or CI/CD.
*/
'check-branch': {
// Git branches that should be protected from accidental commit or push
protectedBranches: ['main', 'dev'],
},

/**
* check-lock-files
*
* Check for package manager lock files, and abort if any are present
* lock file that we don't want. This is useful to ensure that e.g.
* only yarn.lock is present in the repository.
*/
'check-lock-files': {
// Package manager lock file that should be present in the repository
allowLockFile: 'yarn.lock',

// Package manager lock files that should yield a abort
denyLockFiles: ['package-lock.json', 'pnpm-lock.yaml'],
},

/**
* test-sleep
*
* This is a dummy hook for demo purposes. It sleeps for 1000 seconds
* before continuing to next hook.
*/
'test-sleep': {
// How long should the hook sleep for
delay: 1000,
},
},
}

module.exports = config
7 changes: 7 additions & 0 deletions jestconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"transform": {
"^.+\\.(t|j)sx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$",
"moduleFileExtensions": ["ts", "tsx", "js", "jsx", "json", "node"]
}
64 changes: 64 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
{
"name": "@jeliasson/husky-hooks",
"version": "0.1.0",
"license": "MIT",
"description": "Increase the developer experience and consistancy by providing a set of hooks that can be opted-in the development lifecycle.",
"author": "jeliasson",
"homepage": "https://github.com/jeliasson/npm-husky-hooks",
"keywords": [
"husky",
"hooks"
],
"bugs": {
"url": "https://github.com/jeliasson/npm-husky-hooks/issues"
},
"repository": {
"type": "git",
"url": "git+https://github.com/jeliasson/npm-husky-hooks.git"
},
"dependencies": {
"husky": "^8.0.1",
"node-fetch": "^2.6.7",
"yargs": "^17.5.1",
"zod": "^3.17.3"
},
"devDependencies": {
"@types/jest": "^27.5.1",
"@types/node": "^17.0.35",
"@types/node-fetch": "^2.6.1",
"@typescript-eslint/eslint-plugin": "^5.26.0",
"@typescript-eslint/parser": "^5.26.0",
"eslint": "^8.16.0",
"eslint-config-prettier": "^8.5.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-import-helpers": "^1.2.1",
"jest": "^28.1.0",
"prettier": "2.6.2",
"ts-jest": "^28.0.2",
"tsc-watch": "^5.0.3",
"typescript": "^4.6.4"
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
"lib/**/*"
],
"bin": {
"husky-hooks": "lib/bin.js"
},
"scripts": {
"dev": "tsc-watch",
"build": "tsc",
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
"lint": "eslint --ignore-path .gitignore --ext .js,.ts,.tsx --fix .",
"test": "jest --config jestconfig.json",
"preinstall": "echo \"Error: no preinstall specified\" && exit 0",
"install": "echo \"Error: no install specified\" && exit 0",
"postinstall": "echo \"Error: no postinstall specified\" && exit 0",
"prepare": "npm run build",
"prepublishOnly": "npm test && npm run lint",
"preversion": "npm run lint",
"version": "npm run format && git add -A src",
"postversion": "git push && git push --tags"
}
}
11 changes: 11 additions & 0 deletions prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// https://prettier.io/docs/en/options.html
/** @type {import('prettier').RequiredOptions} */
module.exports = {
trailingComma: "es5",
bracketSpacing: true,
tabWidth: 2,
semi: false,
singleQuote: true,
arrowParens: "always",
overrides: [],
}
5 changes: 5 additions & 0 deletions src/__tests__/Default.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// import pkg from '../index';

// test('My Greeter', () => {
// expect(pkg()).toBe('Hello Carl');
// });
Loading

0 comments on commit d502b2d

Please sign in to comment.