Skip to content

Recommend a different command for missing loaders based on the presence of yarn/npm lock files #291

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
Apr 12, 2018
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
Empty file.
5 changes: 5 additions & 0 deletions fixtures/package-helper/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/yarn-npm/package-lock.json

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/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
2 changes: 2 additions & 0 deletions fixtures/package-helper/yarn/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
2 changes: 1 addition & 1 deletion lib/friendly-errors/formatters/missing-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function formatErrors(errors) {
);

if (packageRecommendations) {
fixes.push(`${packageRecommendations.message}\n ${packageRecommendations.yarnInstall}`);
fixes.push(`${packageRecommendations.message}\n ${packageRecommendations.installCommand}`);
}
} else {
fixes.push('You may need to install and configure a special loader for this file type.');
Expand Down
18 changes: 15 additions & 3 deletions lib/package-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
'use strict';

const chalk = require('chalk');
const fs = require('fs');

function ensurePackagesExist(packageNames, requestedFeature) {
const recommendation = getPackageRecommendations(packageNames, requestedFeature);
Expand All @@ -20,10 +21,21 @@ function ensurePackagesExist(packageNames, requestedFeature) {

throw new Error(`
${recommendation.message}
${recommendation.yarnInstall}
${recommendation.installCommand}
`);
}

function getInstallCommand(packages) {
const hasYarnLockfile = fs.existsSync('yarn.lock');
const hasNpmLockfile = fs.existsSync('package-lock.json');

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

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

function getPackageRecommendations(packageNames, requestedFeature = null) {
let missingPackageNames = [];

Expand All @@ -48,11 +60,11 @@ function getPackageRecommendations(packageNames, requestedFeature = null) {
message += ` to use ${chalk.green(requestedFeature)}`;
}

let yarnInstall = chalk.yellow(`yarn add ${missingPackageNames.join(' ')} --dev`);
const installCommand = getInstallCommand(missingPackageNames);

return {
message,
yarnInstall
installCommand
};
}

Expand Down
49 changes: 49 additions & 0 deletions test/package-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* This file is part of the Symfony Webpack Encore package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

'use strict';

const expect = require('chai').expect;
const packageHelper = require('../lib/package-helper');
const path = require('path');
const process = require('process');

describe('package-helper', () => {
const baseCwd = process.cwd();

describe('recommended install command is based on the existing lock files', () => {
after(() => {
process.chdir(baseCwd);
});

it('missing packages without any lock file', () => {
process.chdir(path.join(__dirname , '../fixtures/package-helper/empty'));
const packageRecommendations = packageHelper.getPackageRecommendations(['foo', 'webpack', 'bar']);
expect(packageRecommendations.installCommand).to.contain('yarn add foo bar');
});

it('missing packages with package-lock.json only', () => {
process.chdir(path.join(__dirname, '../fixtures/package-helper/npm'));
const packageRecommendations = packageHelper.getPackageRecommendations(['foo', 'webpack', 'bar']);
expect(packageRecommendations.installCommand).to.contain('npm install foo bar');
});

it('missing packages with yarn.lock only', () => {
process.chdir(path.join(__dirname, '../fixtures/package-helper/yarn'));
const packageRecommendations = packageHelper.getPackageRecommendations(['foo', 'webpack', 'bar']);
expect(packageRecommendations.installCommand).to.contain('yarn add 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.getPackageRecommendations(['foo', 'webpack', 'bar']);
expect(packageRecommendations.installCommand).to.contain('yarn add foo bar');
});
});
});