From 12b3d9ec9fcbe37fe398d017a4534cd9982ef997 Mon Sep 17 00:00:00 2001 From: Felipe Plets Date: Wed, 27 Dec 2023 16:31:06 -0500 Subject: [PATCH 1/4] fix: add root node_modules to import commands when running pnpm --- packages/cli/src/commands.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/commands.js b/packages/cli/src/commands.js index 8cdb972e4..586c2f2af 100644 --- a/packages/cli/src/commands.js +++ b/packages/cli/src/commands.js @@ -100,6 +100,23 @@ function formatFilepath(filepath) { return filepath; } +function getSiblings(root) { + const siblings = [path.join(root, '..')]; + + // if we're in a pnpm package, add the root of the workspace node_modules to the list of siblings + if (root.includes('.pnpm')) { + // Find the index of the first occurrence of '.pnpm' + const nodeModulesIndex = root.indexOf('.pnpm'); + + // If 'node_modules' is found, extract the substring up to that point + if (nodeModulesIndex !== -1) { + siblings.push(path.join(root.substring(0, nodeModulesIndex), '@percy')); + } + } + + return siblings; +} + // Imports and returns compatibile CLI commands from various sources export async function importCommands() { let root = path.resolve(url.fileURLToPath(import.meta.url), '../..'); @@ -109,7 +126,7 @@ export async function importCommands() { // find included dependencies root, // find potential sibling packages - path.join(root, '..'), + ...getSiblings(root), // find any current project dependencies process.cwd() ]), async (roots, dir) => { From acaab4ab5743eb4dec4c672aaa58f25ff7a4a392 Mon Sep 17 00:00:00 2001 From: Felipe Plets Date: Thu, 27 Jun 2024 11:04:58 -0400 Subject: [PATCH 2/4] chore: add unit tests --- packages/cli/src/commands.js | 2 +- packages/cli/test/commands.test.js | 29 ++++++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/packages/cli/src/commands.js b/packages/cli/src/commands.js index 586c2f2af..0a038ad3b 100644 --- a/packages/cli/src/commands.js +++ b/packages/cli/src/commands.js @@ -100,7 +100,7 @@ function formatFilepath(filepath) { return filepath; } -function getSiblings(root) { +export function getSiblings(root) { const siblings = [path.join(root, '..')]; // if we're in a pnpm package, add the root of the workspace node_modules to the list of siblings diff --git a/packages/cli/test/commands.test.js b/packages/cli/test/commands.test.js index f3d66b3ae..6c60b2ef5 100644 --- a/packages/cli/test/commands.test.js +++ b/packages/cli/test/commands.test.js @@ -1,7 +1,7 @@ import path from 'path'; import { logger, mockfs, fs } from '@percy/cli-command/test/helpers'; import { mockModuleCommands, mockPnpCommands, mockLegacyCommands } from './helpers.js'; -import { importCommands } from '../src/commands.js'; +import { importCommands, getSiblings } from '../src/commands.js'; describe('CLI commands', () => { beforeEach(async () => { @@ -213,3 +213,30 @@ describe('CLI commands', () => { }); }); }); + +describe('getSiblings', () => { + it('returns the parent directory for a path without .pnpm', () => { + const root = '/path/to/project'; + const expected = [path.join(root, '..')]; + expect(getSiblings(root)).toEqual(expected); + }); + + it('returns the parent directory and @percy path for a path with .pnpm', () => { + const root = '/path/to/.pnpm/project'; + const expected = [ + path.join(root, '..'), + path.join('/path/to', '@percy') + ]; + expect(getSiblings(root)).toEqual(expected); + }); + + it('handles paths with .pnpm but no node_modules correctly', () => { + // This test ensures that the function's logic for manipulating the path works even without a direct node_modules relationship. + const root = '/path/with/.pnpm/and/no/node_modules'; + const expected = [ + path.join(root, '..'), + path.join('/path/with', '@percy') + ]; + expect(getSiblings(root)).toEqual(expected); + }); +}); \ No newline at end of file From 713f5b7e5030e1aed61b18468ae258c08cf16db9 Mon Sep 17 00:00:00 2001 From: Felipe Plets Date: Wed, 3 Jul 2024 08:39:29 -0400 Subject: [PATCH 3/4] chore(cli): add trailing new line by end of file --- packages/cli/test/commands.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/cli/test/commands.test.js b/packages/cli/test/commands.test.js index 6c60b2ef5..8190f90d5 100644 --- a/packages/cli/test/commands.test.js +++ b/packages/cli/test/commands.test.js @@ -239,4 +239,4 @@ describe('getSiblings', () => { ]; expect(getSiblings(root)).toEqual(expected); }); -}); \ No newline at end of file +}); From 54282a80968bd1206efd14642d0440f461be5a9f Mon Sep 17 00:00:00 2001 From: Felipe Plets Date: Wed, 3 Jul 2024 08:54:25 -0400 Subject: [PATCH 4/4] chore(cli): slightly improve code and test coverage --- packages/cli/src/commands.js | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/packages/cli/src/commands.js b/packages/cli/src/commands.js index 0a038ad3b..a26f304e7 100644 --- a/packages/cli/src/commands.js +++ b/packages/cli/src/commands.js @@ -103,15 +103,12 @@ function formatFilepath(filepath) { export function getSiblings(root) { const siblings = [path.join(root, '..')]; - // if we're in a pnpm package, add the root of the workspace node_modules to the list of siblings - if (root.includes('.pnpm')) { - // Find the index of the first occurrence of '.pnpm' - const nodeModulesIndex = root.indexOf('.pnpm'); - - // If 'node_modules' is found, extract the substring up to that point - if (nodeModulesIndex !== -1) { - siblings.push(path.join(root.substring(0, nodeModulesIndex), '@percy')); - } + // Check if Percy CLI is installed using '.pnpm' by searching + // ffor the .pnpm folder in the root path + const nodeModulesIndex = root.indexOf('.pnpm'); + if (nodeModulesIndex !== -1) { + // add the parent directory of the .pnpm and append /@percy + siblings.push(path.join(root.substring(0, nodeModulesIndex), '@percy')); } return siblings;