Skip to content

Commit 6bd76e2

Browse files
fix: add --prefix to npm install to target correct project directory (#88)
1 parent f733e0a commit 6bd76e2

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

src/utils.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,15 @@ export async function setUpNpmConfig(
6868
export async function installNpmDependencies(
6969
nodeCtx: NodeContext,
7070
packageList: { [key: string]: string },
71+
projectPath?: string,
7172
) {
7273
const packages = Object.entries(packageList).map(([k, v]) => `${k}@${v}`);
7374
console.log(`\nInstalling packages: ${packages.join(' ')}`);
74-
await npm.install(nodeCtx, packages);
75+
if (projectPath) {
76+
await npm.install(nodeCtx, ['--prefix', projectPath, ...packages]);
77+
} else {
78+
await npm.install(nodeCtx, packages);
79+
}
7580
}
7681

7782
export async function rebuildNpmDependencies(
@@ -218,8 +223,9 @@ export async function prepareNpmEnv(
218223
}
219224

220225
// install npm packages
226+
const projectPath = path.dirname(runCfg.path);
221227
startTime = new Date().getTime();
222-
await installNpmDependencies(nodeCtx, fixedPackageList);
228+
await installNpmDependencies(nodeCtx, fixedPackageList, projectPath);
223229
endTime = new Date().getTime();
224230

225231
if (runCfg.npm?.usePackageLock !== true) {

tests/unit/src/__snapshots__/utils.spec.ts.snap

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,21 @@ exports[`utils .prepareNpmEnv should call npm install 1`] = `
7272
]
7373
`;
7474

75+
exports[`utils .prepareNpmEnv should call npm install with --prefix when projectPath is provided 1`] = `
76+
[
77+
{
78+
"nodePath": "node-bin",
79+
"npmPath": "npm-bin",
80+
"useGlobals": false,
81+
},
82+
[
83+
"--prefix",
84+
"/fake/project",
85+
"mypackage@1.2.3",
86+
],
87+
]
88+
`;
89+
7590
exports[`utils .prepareNpmEnv should set right registry for npm 1`] = `
7691
[
7792
{

tests/unit/src/utils.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,17 @@ describe('utils', function () {
152152
installSpyOn.mock.calls[installSpyOn.mock.calls.length - 1],
153153
).toMatchSnapshot();
154154
});
155+
it('should call npm install with --prefix when projectPath is provided', async function () {
156+
const installSpyOn = jest.spyOn(npm, 'install');
157+
await installNpmDependencies(
158+
nodeCtx,
159+
{ mypackage: '1.2.3' },
160+
'/fake/project',
161+
);
162+
expect(
163+
installSpyOn.mock.calls[installSpyOn.mock.calls.length - 1],
164+
).toMatchSnapshot();
165+
});
155166
it('should use env var for registry', async function () {
156167
process.env.SAUCE_NPM_CACHE = 'npmland.io';
157168
const loadSpyOn = jest.spyOn(npm, 'configure');

0 commit comments

Comments
 (0)