Skip to content

Commit

Permalink
refactor(cli): improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Mar 21, 2021
1 parent 3992c57 commit 6a7bc88
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/commands/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ import path from 'path'

import { l } from '../log'

const customDirHelp = 'https://typicode.github.io/husky/#/?id=custom-directory'

export function install(dir = '.husky'): void {
// Ensure that we're inside a git repository
if (cp.spawnSync('git', ['rev-parse']).status !== 0) {
// Do not fail to let projects downloaded as zip files have their dependencies installed
l('not a Git repository, skipping hooks installation')
return
}

// Ensure that we're not trying to install outside cwd
// Ensure that we're not trying to install outside of cwd
const absoluteHooksDir = path.resolve(process.cwd(), dir)
if (!absoluteHooksDir.startsWith(process.cwd())) {
throw new Error('.. not allowed')
throw new Error(`.. not allowed (see ${customDirHelp})`)
}

// Ensure that cwd is git top level
if (!fs.existsSync('.git')) {
throw new Error(".git can't be found")
throw new Error(`.git can't be found (see ${customDirHelp})`)
}

try {
Expand Down
4 changes: 3 additions & 1 deletion src/commands/set_add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ function format(file: string): string {
export function set(file: string, cmd: string): void {
const dir = path.dirname(file)
if (!fs.existsSync(dir)) {
throw new Error(`can't create hook, ${dir} directory doesn't exist`)
throw new Error(
`can't create hook, ${dir} directory doesn't exist (try running husky install)`,
)
}

fs.writeFileSync(file, data(cmd), { mode: 0o0755 })
Expand Down

0 comments on commit 6a7bc88

Please sign in to comment.