From b47fa429f7d525267dcc4c8d5341ebf29d69499b Mon Sep 17 00:00:00 2001 From: Kirill Romanov Date: Fri, 2 Apr 2021 23:03:10 +0300 Subject: [PATCH] fix and simplify project root detection in pnpm --- simple-git-hooks.js | 13 +++---------- simple-git-hooks.test.js | 3 ++- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/simple-git-hooks.js b/simple-git-hooks.js index 9f6e0ee..f994a57 100644 --- a/simple-git-hooks.js +++ b/simple-git-hooks.js @@ -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', @@ -83,14 +81,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 diff --git a/simple-git-hooks.test.js b/simple-git-hooks.test.js index 8e155c6..9d7d2e0 100644 --- a/simple-git-hooks.test.js +++ b/simple-git-hooks.test.js @@ -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 @@ -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') })