Skip to content

Commit 1f06c3b

Browse files
authored
Merge pull request #48 from Djaler/fix-pnpm
fix and simplify project root detection in pnpm
2 parents ab996b9 + b47fa42 commit 1f06c3b

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',
@@ -85,14 +83,9 @@ function getProjectRootDirectoryFromNodeModules(projectPath) {
8583

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

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

9891
// 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)