Skip to content

Commit

Permalink
fix(core): migrate using yarn when nxJson has another package manager…
Browse files Browse the repository at this point in the history
… configured (#16844)

(cherry picked from commit 0654776)
  • Loading branch information
johnhwhite authored and FrozenPandaz committed May 8, 2023
1 parent 4233eff commit 0defcad
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
43 changes: 43 additions & 0 deletions packages/nx/src/utils/package-manager.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
jest.mock('fs');
import * as fs from 'fs';
import * as configModule from 'nx/src/config/configuration';
import { detectPackageManager } from 'nx/src/utils/package-manager';

describe('package-manager', () => {
it('should detect package manager in nxJson', () => {
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({
cli: {
packageManager: 'pnpm',
},
});
const packageManager = detectPackageManager();
expect(packageManager).toEqual('pnpm');
expect(fs.existsSync).not.toHaveBeenCalled();
});

it('should detect yarn package manager from yarn.lock', () => {
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({});
(fs.existsSync as jest.Mock).mockReturnValueOnce(true);
const packageManager = detectPackageManager();
expect(packageManager).toEqual('yarn');
expect(fs.existsSync).toHaveBeenNthCalledWith(1, 'yarn.lock');
});

it('should detect pnpm package manager from pnpm-lock.yaml', () => {
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({});
(fs.existsSync as jest.Mock).mockImplementation((path) => {
return path === 'pnpm-lock.yaml';
});
const packageManager = detectPackageManager();
expect(packageManager).toEqual('pnpm');
expect(fs.existsSync).toHaveBeenCalledTimes(3);
});

it('should use npm package manager as default', () => {
jest.spyOn(configModule, 'readNxJson').mockReturnValueOnce({});
(fs.existsSync as jest.Mock).mockReturnValue(false);
const packageManager = detectPackageManager();
expect(packageManager).toEqual('npm');
expect(fs.existsSync).toHaveBeenCalledTimes(5);
});
});
13 changes: 8 additions & 5 deletions packages/nx/src/utils/package-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,14 @@ export interface PackageManagerCommands {
*/
export function detectPackageManager(dir: string = ''): PackageManager {
const nxJson = readNxJson();
return nxJson.cli?.packageManager ?? existsSync(join(dir, 'yarn.lock'))
? 'yarn'
: existsSync(join(dir, 'pnpm-lock.yaml'))
? 'pnpm'
: 'npm';
return (
nxJson.cli?.packageManager ??
(existsSync(join(dir, 'yarn.lock'))
? 'yarn'
: existsSync(join(dir, 'pnpm-lock.yaml'))
? 'pnpm'
: 'npm')
);
}

/**
Expand Down

0 comments on commit 0defcad

Please sign in to comment.