Skip to content

Commit

Permalink
Add support for PNPM for installation commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Kocal committed Aug 22, 2024
1 parent 30dc220 commit 9a03029
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 5 deletions.
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

0 comments on commit 9a03029

Please sign in to comment.