Skip to content

Add support for PNPM for installation commands #1285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions fixtures/package-helper/pnpm-yarn-npm/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions fixtures/package-helper/pnpm-yarn-npm/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions fixtures/package-helper/pnpm-yarn-npm/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
5 changes: 5 additions & 0 deletions fixtures/package-helper/pnpm/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions lib/package-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ ${missingPackagesRecommendation.message}
}

function getInstallCommand(packageConfigs) {
const hasPnpmLockfile = fs.existsSync('pnpm-lock.yaml');
const hasYarnLockfile = fs.existsSync('yarn.lock');
const hasNpmLockfile = fs.existsSync('package-lock.json');
const packageInstallStrings = packageConfigs.map((packageConfig) => {
const firstPackage = packageConfig[0];

Expand All @@ -51,11 +51,15 @@ function getInstallCommand(packageConfigs) {
return `${firstPackage.name}@${recommendedVersion}`;
});

if (hasNpmLockfile && !hasYarnLockfile) {
return chalk.yellow(`npm install ${packageInstallStrings.join(' ')} --save-dev`);
if (hasPnpmLockfile) {
return chalk.yellow(`pnpm add ${packageInstallStrings.join(' ')} --save-dev`);
}

return chalk.yellow(`yarn add ${packageInstallStrings.join(' ')} --dev`);
if (hasYarnLockfile) {
return chalk.yellow(`yarn add ${packageInstallStrings.join(' ')} --dev`);
}

return chalk.yellow(`npm install ${packageInstallStrings.join(' ')} --save-dev`);
}

function isPackageInstalled(packageConfig) {
Expand Down
20 changes: 19 additions & 1 deletion test/package-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe('package-helper', () => {
const packageRecommendations = packageHelper.getMissingPackageRecommendations([
{ name: 'foo' }, { name: 'webpack' }, { name: 'bar' }
]);
expect(packageRecommendations.installCommand).to.contain('yarn add foo bar');
expect(packageRecommendations.installCommand).to.contain('npm install foo bar');
expect(stripAnsi(packageRecommendations.message)).to.contain('foo & bar');
});

Expand All @@ -51,6 +51,15 @@ describe('package-helper', () => {
expect(stripAnsi(packageRecommendations.message)).to.contain('foo & bar');
});

it('missing packages with pnpm-lock.yaml only', () => {
process.chdir(path.join(__dirname, '../fixtures/package-helper/pnpm'));
const packageRecommendations = packageHelper.getMissingPackageRecommendations([
{ name: 'foo' }, { name: 'webpack' }, { name: 'bar' }
]);
expect(packageRecommendations.installCommand).to.contain('pnpm add foo bar');
expect(stripAnsi(packageRecommendations.message)).to.contain('foo & bar');
});

it('missing packages with both package-lock.json and yarn.lock', () => {
process.chdir(path.join(__dirname, '../fixtures/package-helper/yarn-npm'));
const packageRecommendations = packageHelper.getMissingPackageRecommendations([
Expand All @@ -60,6 +69,15 @@ describe('package-helper', () => {
expect(stripAnsi(packageRecommendations.message)).to.contain('foo & bar');
});

it('missing packages with package-lock.json, yarn.lock and pnpm-lock.yaml', () => {
process.chdir(path.join(__dirname, '../fixtures/package-helper/pnpm-yarn-npm'));
const packageRecommendations = packageHelper.getMissingPackageRecommendations([
{ name: 'foo' }, { name: 'webpack' }, { name: 'bar' }
]);
expect(packageRecommendations.installCommand).to.contain('pnpm add foo bar');
expect(stripAnsi(packageRecommendations.message)).to.contain('foo & bar');
});

it('missing packages with alternative packages', () => {
process.chdir(path.join(__dirname, '../fixtures/package-helper/yarn'));
const packageRecommendations = packageHelper.getMissingPackageRecommendations([
Expand Down
Loading