Skip to content

Commit

Permalink
Replace execa with git bash
Browse files Browse the repository at this point in the history
  • Loading branch information
gucong3000 committed May 16, 2018
1 parent ff0652a commit 8fee8a6
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 65 deletions.
58 changes: 25 additions & 33 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"homepage": "https://github.com/typicode/husky#readme",
"dependencies": {
"cosmiconfig": "^4.0.0",
"execa": "^0.9.0",
"find-up": "^2.1.0",
"is-ci": "^1.1.0",
"pkg-dir": "^2.0.0",
Expand All @@ -56,7 +55,6 @@
"slash": "^2.0.0"
},
"devDependencies": {
"@types/execa": "^0.9.0",
"@types/find-up": "^2.1.1",
"@types/jest": "^22.2.3",
"@types/node": "^10.0.6",
Expand Down
1 change: 0 additions & 1 deletion src/installer/bin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as execa from 'execa'
import * as fs from 'fs'
import * as isCI from 'is-ci'
import * as path from 'path'
Expand Down
22 changes: 22 additions & 0 deletions src/runner/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,28 @@ describe('run', () => {
expect(status).toBe(1)
})

it('should support POSIX shell command on Windows', () => {
const dir = tempy.directory()

fs.writeFileSync(
path.join(dir, 'package.json'),
JSON.stringify({
husky: {
hooks: {
'pre-commit': 'MSG=success npm run test --script-shell=$SHELL'
}
},
scripts: {
test: 'echo "$MSG"'
}
})
)

const status = index([, getScriptPath(dir), 'pre-commit'])

expect(status).toBe(0)
})

it('should support old scripts but show a deprecated message', () => {
const dir = tempy.directory()

Expand Down
66 changes: 37 additions & 29 deletions src/runner/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as execa from 'execa'
import { spawnSync } from 'child_process'
import * as readPkg from 'read-pkg'
import getConf from '../getConf'

Expand All @@ -17,40 +17,48 @@ export default function([, scriptPath, hookName = '']: string[]): number {
const oldCommand: string | undefined =
pkg && pkg.scripts && pkg.scripts[hookName.replace('-', '')]

if (command) {
console.log(`husky > ${hookName} (node ${process.version})`)
return shell(cwd, hookName, command)
}

if (oldCommand) {
console.log()
console.log(
`Warning: Setting ${hookName} script in package.json > scripts will be deprecated in v1.0`
)
console.log(
`Please move it to husky.hooks in package.json, a .huskyrc file, or a husky.config.js file`
)
console.log(`Or run ./node_modules/.bin/husky-upgrade for automatic update`)
console.log()
console.log(`See https://github.com/typicode/husky for usage`)
console.log()
console.log(`husky > ${hookName} (node ${process.version})`)
return shell(cwd, hookName, oldCommand)
}

return 0
}

function shell(cwd: string, hookName: string, cmds: string) {
const shellPath = process.env.SHELL || 'sh'
let status
try {
if (command) {
console.log(`husky > ${hookName} (node ${process.version})`)
execa.shellSync(command, { cwd, stdio: 'inherit' })
return 0
}

if (oldCommand) {
console.log()
console.log(
`Warning: Setting ${hookName} script in package.json > scripts will be deprecated in v1.0`
)
console.log(
`Please move it to husky.hooks in package.json, a .huskyrc file, or a husky.config.js file`
)
console.log(
`Or run ./node_modules/.bin/husky-upgrade for automatic update`
)
console.log()
console.log(`See https://github.com/typicode/husky for usage`)
console.log()
console.log(`husky > ${hookName} (node ${process.version})`)
execa.shellSync(oldCommand, { cwd, stdio: 'inherit' })
return 0
}

return 0
} catch (e) {
status = spawnSync(shellPath as string, ['-c', cmds], {
cwd,
stdio: 'inherit'
}).status
} catch (ex) {
status = 1
}
if (status) {
const noVerifyMessage =
hookName === 'prepare-commit-msg'
? '(cannot be bypassed with --no-verify due to Git specs)'
: '(add --no-verify to bypass)'

console.log(`husky > ${hookName} hook failed ${noVerifyMessage}`)
return 1
}
return status || 0
}

0 comments on commit 8fee8a6

Please sign in to comment.