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

fix npx in winpty #644

Closed
wants to merge 1 commit 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
7 changes: 4 additions & 3 deletions src/installer/__tests__/__snapshots__/getScript.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ exports[`hookScript should match snapshot 1`] = `
# Hook created by Husky v4.0.7 (https://github.com/typicode/husky#readme)
# At: <locale date string>
# From: /home/typicode/projects/foo-package (https://github.com/foo/foo-package)
# With: npm
# With: npm (6.9.0)

gitRoot=\\"$(git rev-parse --show-toplevel)\\"
gitParams=\\"$*\\"
hookName=\`basename \\"$0\\"\`
packageManager=npm
packageManager=\\"npm\\"
packageManagerVersion=\\"6.9.0\\"

debug() {
if [ \\"$HUSKY_DEBUG\\" = \\"true\\" ] || [ \\"$HUSKY_DEBUG\\" = \\"1\\" ]; then
Expand Down Expand Up @@ -68,7 +69,7 @@ fi

cd \\".\\"

if command_exists winpty && test -t 1; then
if command_exists winpty && test -t 1 && [ \\"$packageManager\\" = \\"yarn\\" ] && [ \\"\${packageManagerVersion%%.*}\\" -lt 2 ]; then
exec < /dev/tty
fi

Expand Down
6 changes: 5 additions & 1 deletion src/installer/__tests__/getScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import getScript from '../getScript'

describe('hookScript', (): void => {
it('should match snapshot', (): void => {
const script = getScript({ relativeUserPkgDir: '.', pmName: 'npm' })
const script = getScript({
relativeUserPkgDir: '.',
pmName: 'npm',
pmVersion: '6.9.0'
})
expect(script).toMatchSnapshot()
})
})
1 change: 1 addition & 0 deletions src/installer/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ function install({
relativeUserPkgDir,
userPkgDir: path.join(tempDir, userPkgDir),
pmName: 'npm',
pmVersion: '6.9.0',
isCI
})
}
Expand Down
3 changes: 2 additions & 1 deletion src/installer/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ function run(): void {
const { absoluteGitCommonDir, relativeUserPkgDir } = getDirs(userPkgDir)

if (action === 'install') {
const { name: pmName } = whichPMRuns()
const { name: pmName, version: pmVersion } = whichPMRuns()
debug(`Package manager: ${pmName}`)
install({
absoluteGitCommonDir,
relativeUserPkgDir,
userPkgDir,
pmName,
pmVersion,
isCI
})
} else {
Expand Down
16 changes: 11 additions & 5 deletions src/installer/getScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface Context {
pkgDirectory?: string
pkgHomepage?: string
pmName: string
pmVersion: string
relativeUserPkgDir: string
}

Expand All @@ -26,19 +27,21 @@ const render = ({
pkgDirectory,
pkgHomepage,
pmName,
pmVersion,
relativeUserPkgDir
}: Context): string => `#!/bin/sh
${huskyIdentifier}

# Hook created by Husky v${huskyVersion} (${huskyHomepage})
# At: ${createdAt}
# From: ${pkgDirectory} (${pkgHomepage})
# With: ${pmName}
# With: ${pmName} (${pmVersion})

gitRoot="$(git rev-parse --show-toplevel)"
gitParams="$*"
hookName=\`basename "$0"\`
packageManager=${pmName}
packageManager="${pmName}"
packageManagerVersion="${pmVersion}"

debug() {
if [ "$HUSKY_DEBUG" = "true" ] || [ "$HUSKY_DEBUG" = "1" ]; then
Expand Down Expand Up @@ -94,7 +97,7 @@ fi

cd "${relativeUserPkgDir}"

if command_exists winpty && test -t 1; then
if command_exists winpty && test -t 1 && [ "$packageManager" = "yarn" ] && [ "\${packageManagerVersion%%.*}" -lt 2 ]; then
exec < /dev/tty
fi

Expand All @@ -113,10 +116,12 @@ esac
*/
export default function({
relativeUserPkgDir,
pmName
pmName,
pmVersion
}: {
relativeUserPkgDir: string
pmName: string
pmVersion: string
}): string {
const pkgHomepage = process.env.npm_package_homepage
const pkgDirectory = process.env.PWD
Expand All @@ -143,6 +148,7 @@ export default function({
relativeUserPkgDir: normalizedPath,
pkgDirectory,
pkgHomepage,
pmName
pmName,
pmVersion
})
}
4 changes: 3 additions & 1 deletion src/installer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,14 @@ export function install({
relativeUserPkgDir,
userPkgDir,
pmName, // package manager name
pmVersion, // package manager version
isCI // running in CI or not
}: {
absoluteGitCommonDir: string
relativeUserPkgDir: string
userPkgDir: string
pmName: string
pmVersion: string
isCI: boolean
}): void {
// Get conf from package.json or .huskyrc
Expand Down Expand Up @@ -144,7 +146,7 @@ export function install({
const hooks = getHooks(absoluteGitCommonDir)

// Prefix can be an empty string
const script = getScript({ relativeUserPkgDir, pmName })
const script = getScript({ relativeUserPkgDir, pmName, pmVersion })
createHooks(hooks, script)
}

Expand Down