Skip to content

Commit

Permalink
Check if yarn.lock is ignored before throwing error (#582)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
dopecodez and sindresorhus committed Dec 31, 2020
1 parent 8f812e0 commit c76987a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
15 changes: 15 additions & 0 deletions source/git-util.js
Expand Up @@ -226,3 +226,18 @@ exports.verifyRecentGitVersion = async () => {

verifyRequirementSatisfied('git', installedVersion);
};

exports.checkIfFileGitIgnored = async pathToFile => {
try {
const {stdout} = await execa('git', ['check-ignore', pathToFile]);
return Boolean(stdout);
} catch (error) {
// If file is not ignored, `git check-ignore` throws an empty error and exits.
// Check that and return false so as not to throw an unwanted error.
if (error.stdout === '' && error.stderr === '') {
return false;
}

throw error;
}
};
22 changes: 14 additions & 8 deletions source/index.js
Expand Up @@ -128,15 +128,21 @@ module.exports = async (input = 'patch', options) => {
{
title: 'Installing dependencies using Yarn',
enabled: () => options.yarn === true,
task: () => exec('yarn', ['install', '--frozen-lockfile', '--production=false']).pipe(
catchError(error => {
if (error.stderr.startsWith('error Your lockfile needs to be updated')) {
return throwError(new Error('yarn.lock file is outdated. Run yarn, commit the updated lockfile and try again.'));
}
task: () => {
return exec('yarn', ['install', '--frozen-lockfile', '--production=false']).pipe(
catchError(async error => {
if ((!error.stderr.startsWith('error Your lockfile needs to be updated'))) {
return;
}

return throwError(error);
})
)
if (await git.checkIfFileGitIgnored('yarn.lock')) {
return;
}

throw new Error('yarn.lock file is outdated. Run yarn, commit the updated lockfile and try again.');
})
);
}
},
{
title: 'Installing dependencies using npm',
Expand Down

0 comments on commit c76987a

Please sign in to comment.