Skip to content

Commit

Permalink
Merge pull request #48 from Djaler/fix-pnpm
Browse files Browse the repository at this point in the history
fix and simplify project root detection in pnpm
  • Loading branch information
toplenboren committed Apr 24, 2021
2 parents ab996b9 + b47fa42 commit 1f06c3b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
13 changes: 3 additions & 10 deletions simple-git-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ const fs = require('fs')
const os = require("os");
const path = require('path');

const { packageVersion } = require('./package.json');

const VALID_GIT_HOOKS = [
'applypatch-msg',
'pre-applypatch',
Expand Down Expand Up @@ -85,14 +83,9 @@ function getProjectRootDirectoryFromNodeModules(projectPath) {

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

if (projDir.includes('.pnpm')
&& projDir.length > 3
&& _arraysAreEqual(projDir.slice(projDir.length - 3), [
'node_modules',
'.pnpm',
`simple-git-hooks@${packageVersion}`,
])) {
return projDir.slice(0, projDir.length - 3).join('/');
const indexOfPnpmDir = projDir.indexOf('.pnpm')
if (indexOfPnpmDir > -1) {
return projDir.slice(0, indexOfPnpmDir - 1).join('/');
}

// A yarn2 STAB
Expand Down
3 changes: 2 additions & 1 deletion simple-git-hooks.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const os = require('os')
const spc = require("./simple-git-hooks");
const path = require("path")

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


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

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


Expand Down

0 comments on commit 1f06c3b

Please sign in to comment.