Skip to content

Commit b47fa42

Browse files
committed
fix and simplify project root detection in pnpm
1 parent 173ca0b commit b47fa42

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

simple-git-hooks.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ const fs = require('fs')
22
const os = require("os");
33
const path = require('path');
44

5-
const { packageVersion } = require('./package.json');
6-
75
const VALID_GIT_HOOKS = [
86
'applypatch-msg',
97
'pre-applypatch',
@@ -83,14 +81,9 @@ function getProjectRootDirectoryFromNodeModules(projectPath) {
8381

8482
const projDir = projectPath.split(/[\\/]/) // <- would split both on '/' and '\'
8583

86-
if (projDir.includes('.pnpm')
87-
&& projDir.length > 3
88-
&& _arraysAreEqual(projDir.slice(projDir.length - 3), [
89-
'node_modules',
90-
'.pnpm',
91-
`simple-git-hooks@${packageVersion}`,
92-
])) {
93-
return projDir.slice(0, projDir.length - 3).join('/');
84+
const indexOfPnpmDir = projDir.indexOf('.pnpm')
85+
if (indexOfPnpmDir > -1) {
86+
return projDir.slice(0, indexOfPnpmDir - 1).join('/');
9487
}
9588

9689
// A yarn2 STAB

simple-git-hooks.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const os = require('os')
33
const spc = require("./simple-git-hooks");
44
const path = require("path")
55

6-
const { packageVersion } = require('./package.json');
6+
const { version: packageVersion } = require('./package.json');
77

88

99
// Get project root directory
@@ -22,6 +22,7 @@ test('getProjectRootDirectory falls back to undefined when we are not in node_mo
2222

2323
test('getProjectRootDirectory return correct dir when installed using pnpm:', () => {
2424
expect(spc.getProjectRootDirectoryFromNodeModules(`var/my-project/node_modules/.pnpm/simple-git-hooks@${packageVersion}`)).toBe('var/my-project')
25+
expect(spc.getProjectRootDirectoryFromNodeModules(`var/my-project/node_modules/.pnpm/simple-git-hooks@${packageVersion}/node_modules/simple-git-hooks`)).toBe('var/my-project')
2526
})
2627

2728

0 commit comments

Comments
 (0)