Skip to content

Commit

Permalink
fix(init): update package.json postinstall
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Feb 22, 2021
1 parent 7387773 commit 3e8baaa
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@ import { PackageJson } from 'type-fest'
import { add } from './add'
import { install } from './install'

const regex = /^[ ]+|\t+/m

export function init(): void {
// Read package.json
const pkg = JSON.parse(
fs.readFileSync('package.json', 'utf-8')
) as PackageJson
const str = fs.readFileSync('package.json', 'utf-8')
const pkg = JSON.parse(str) as PackageJson

// Add postinstall script
if (pkg.scripts !== undefined) {
pkg.scripts.postinstall = 'husky install'
}
pkg.scripts ||= {}
pkg.scripts.postinstall = 'husky install'

// Write package.json
const indent = regex.exec(str)?.[0]
fs.writeFileSync('package.json', JSON.stringify(pkg, null, indent))

// Install husky
install()

// Add pre-commit sample
Expand Down
4 changes: 4 additions & 0 deletions test/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ init_git
npx --no husky init
npm set-script test "echo \"msg from pre-commit hook\" && exit 1"


# Debug
# cat .husky/*

# Test package.json scripts
grep '"postinstall": "husky install"' package.json || echo -e "\e[0;32mOK\e[m"

# Test core.hooksPath
test_hooksPath ".husky"

Expand Down

0 comments on commit 3e8baaa

Please sign in to comment.