Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: clean-up code base #1230

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 13 additions & 15 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,27 @@ name: Node.js CI
on: [push]

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
node-version: [14, 16, 18]
os: [ubuntu-latest, macOS-latest]
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
- run: npm --version
node-version: 18
- run: npm ci --ignore-scripts
- run: npm test
- run: npm run lint

# Node 14 comes with npm 6 which has issues on Windows
# Skipping tests on Windows with Node 14
build-windows:
runs-on: ${{ matrix.os }}
test:
strategy:
matrix:
node-version: [16, 18]
os: [windows-latest]
node-version: [14, 16, 18]
os: [ubuntu-latest, macOS-latest, windows-latest]
# Node.js 14 comes with npm 6 which has issues on Windows
# therefore skipping tests on Windows with Node.js 14
exclude:
- os: windows-latest
node-version: 14
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/npm_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
types: [created]

jobs:
build:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -18,7 +18,7 @@ jobs:
- run: npm test

publish-npm:
needs: build
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npm run lint
npm test
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
By contributing, you agree to release your modifications under the MIT license (see the file LICENSE-MIT).
By contributing, you agree to release your modifications under the MIT license (see the file LICENSE).
4 changes: 3 additions & 1 deletion husky.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ if [ -z "$husky_skip_init" ]; then
fi
}

readonly hook_name="$(basename -- "$0")"
hook_name="$(basename -- "$0")"
readonly hook_name
debug "starting $hook_name..."

if [ "$HUSKY" = "0" ]; then
Expand All @@ -16,6 +17,7 @@ if [ -z "$husky_skip_init" ]; then

if [ -f ~/.huskyrc ]; then
debug "sourcing ~/.huskyrc"
# shellcheck source=/dev/null
. ~/.huskyrc
fi

Expand Down
15 changes: 8 additions & 7 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,27 @@
import p = require('path')
import h = require('./')

// Show usage and exit with code
/** Show usage and exit with given code */
function help(code: number) {
console.log(`Usage:
husky install [dir] (default: .husky)
husky uninstall
husky set|add <file> [cmd]`)
husky install [dir] (default: .husky)
husky uninstall
husky set|add <file> [cmd]`)
process.exit(code)
}

// Get CLI arguments
const [, , cmd, ...args] = process.argv
/** Lenght of arguments passed to husky */
const ln = args.length
const [x, y] = args

// Set or add command in hook
/** Set or add command in hook */
const hook = (fn: (a1: string, a2: string) => void) => (): void =>
// Show usage if no arguments are provided or more than 2
// Show usage if no or more than 2 arguments are provided
!ln || ln > 2 ? help(2) : fn(x, y)

// CLI commands
/** CLI commands */
const cmds: { [key: string]: () => void } = {
install: (): void => (ln > 1 ? help(2) : h.install(x)),
uninstall: h.uninstall,
Expand Down
16 changes: 10 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,29 @@ import cp = require('child_process')
import fs = require('fs')
import p = require('path')

// Logger
/** Logger */
const l = (msg: string): void => console.log(`husky - ${msg}`)

// Git command
/** Execute a git command */
const git = (args: string[]): cp.SpawnSyncReturns<Buffer> =>
cp.spawnSync('git', args, { stdio: 'inherit' })

/** Check prerequisites, install and configure husky */
export function install(dir = '.husky'): void {
if (process.env.HUSKY === '0') {
l('HUSKY env variable is set to 0, skipping install')
return
}

// Ensure that we're inside a git repository
// Ensure that we're inside a Git repository.
// If git command is not found, status is null and we should return.
// That's why status value needs to be checked explicitly.
// That's why the status value needs to be checked explicitly.
if (git(['rev-parse']).status !== 0) {
l(`git command not found, skipping install`)
return
}

// Custom dir help
/** Custom dir help */
const url = 'https://typicode.github.io/husky/#/?id=custom-directory'

// Ensure that we're not trying to install outside of cwd
Expand Down Expand Up @@ -59,11 +60,12 @@ export function install(dir = '.husky'): void {
l('Git hooks installed')
}

/** Add a new hook or override an existing hook */
export function set(file: string, cmd: string): void {
const dir = p.dirname(file)
if (!fs.existsSync(dir)) {
throw new Error(
`can't create hook, ${dir} directory doesn't exist (try running husky install)`,
`can't create hook, ${dir} directory doesn't exist (try running 'husky install')`,
)
}

Expand All @@ -80,6 +82,7 @@ ${cmd}
l(`created ${file}`)
}

/** Add a new hook or append a command to an existing hook */
export function add(file: string, cmd: string): void {
if (fs.existsSync(file)) {
fs.appendFileSync(file, `${cmd}\n`)
Expand All @@ -89,6 +92,7 @@ export function add(file: string, cmd: string): void {
}
}

/** Uninstall husky */
export function uninstall(): void {
git(['config', '--unset', 'core.hooksPath'])
}
1 change: 1 addition & 0 deletions test/1_default.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/functions.sh"
setup
install
Expand Down
1 change: 1 addition & 0 deletions test/2_in-sub-dir.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/functions.sh"
setup
install
Expand Down
1 change: 1 addition & 0 deletions test/3_from-sub-dir.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/functions.sh"
setup

Expand Down
1 change: 1 addition & 0 deletions test/4_not-git-dir.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/functions.sh"
setup
install
Expand Down
1 change: 1 addition & 0 deletions test/5_set-add.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/functions.sh"
setup
install
Expand Down
1 change: 1 addition & 0 deletions test/6_git_command_not_found.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/functions.sh"
setup
install
Expand Down
1 change: 1 addition & 0 deletions test/7_command_not_found.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/functions.sh"
setup
install
Expand Down
1 change: 1 addition & 0 deletions test/all.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env sh
set -e
npm run build
npm pack && mv husky-*.tgz /tmp/husky.tgz
Expand Down
16 changes: 10 additions & 6 deletions test/functions.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env sh
# Exit on error
set -eu

setup() {
name="$(basename -- $0)"
name="$(basename -- "$0")"
testDir="/tmp/husky-test-$name"
echo
echo "-------------------"
Expand All @@ -15,14 +16,16 @@ setup() {
mkdir -p "$testDir"
cd "$testDir"

# Unset env var when called via git hook
unset GIT_INDEX_FILE

# Init git
git init --quiet
git config user.email "test@test"
git config user.name "test"

# Init package.json
npm_config_loglevel="error"
npm init -y 1>/dev/null
npm_config_loglevel="error" npm init -y 1>/dev/null
}

install() {
Expand All @@ -40,17 +43,18 @@ expect() {
}

expect_hooksPath_to_be() {
readonly hooksPath=`git config core.hooksPath`
hooksPath="$(git config core.hooksPath)"
readonly hooksPath
if [ "$hooksPath" != "$1" ]; then
error "core.hooksPath should be $1, was $hooksPath"
fi
}

error() {
echo -e "\e[0;31mERROR:\e[m $1"
printf "\e[0;31mERROR:\e[m %s\n" "$1"
exit 1
}

ok() {
echo -e "\e[0;32mOK\e[m"
printf "\e[0;32mOK\e[m\n"
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "@tsconfig/node12/tsconfig.json",
"extends": "@tsconfig/node14/tsconfig.json",
"compilerOptions": {
"rootDir": "src",
"outDir": "lib",
Expand Down