diff options
author | Lukas Eipert <leipert@gitlab.com> | 2018-09-07 13:20:06 +0200 |
---|---|---|
committer | Lukas Eipert <leipert@gitlab.com> | 2018-09-11 01:25:10 +0200 |
commit | fe487fe2f2e6f07b95cf92f8b0fa786425e62dae (patch) | |
tree | bd42448548ffc8ff91e727f3f662a645773487d1 /scripts/frontend/postinstall.js | |
parent | 2f990e3408d00cad473d8dcf8a4e49155cc3cc33 (diff) | |
download | gitlab-ce-51226-check-yarn-lock.tar.gz |
check yarn.lock for duplicate entries51226-check-yarn-lock
Diffstat (limited to 'scripts/frontend/postinstall.js')
-rw-r--r-- | scripts/frontend/postinstall.js | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/scripts/frontend/postinstall.js b/scripts/frontend/postinstall.js index 682039a41b3..7f0abd88a15 100644 --- a/scripts/frontend/postinstall.js +++ b/scripts/frontend/postinstall.js @@ -1,3 +1,5 @@ +const fs = require('fs'); +const path = require('path'); const chalk = require('chalk'); // check that fsevents is available if we're on macOS @@ -19,4 +21,54 @@ if (process.platform === 'darwin') { } } +if (process.env.NODE_ENV !== 'production') { + const lockfileParser = require('@yarnpkg/lockfile'); + + // check that certain packages are only listed once in the yarn.lock + const packagesToCheck = ['@gitlab-org/gitlab-svgs', 'bootstrap']; + const file = fs.readFileSync(path.join(__dirname, '../../yarn.lock'), 'utf8'); + const packages = lockfileParser.parse(file); + + if (packages.type !== 'success') { + console.error(`${chalk.red('error')} Could not parse 'yarn.lock'. Please make sure it exists`); + process.exit(1); + } + + function checkUniqueYarnLockEntry(hasError, packageName) { + const resolved = Object.entries(packages.object).reduce((result, [name, value]) => { + if (name.replace(/"/g, '').startsWith(`${packageName}@`)) { + const resolvedUrl = value.resolved; + + if (!result.includes(resolvedUrl)) { + result.push(resolvedUrl); + } + } + return result; + }, []); + + if (resolved.length !== 1) { + console.error( + chalk.red(` + The dependency ${packageName} has ${resolved.length} entries in 'yarn.lock'. + It is supposed to have exactly one entry, please fix that by: + + 1. Delete all lines starting with '${packageName}@' from the yarn.lock + 2. Re-run \`${chalk.cyan('yarn install')}\` + 3. Commit changes to yarn.lock + `) + ); + return true; + } + + return hasError; + } + + const hasError = packagesToCheck.reduce(checkUniqueYarnLockEntry, false); + + if (hasError) { + console.error(`${chalk.red('error')} Dependency postinstall check failed.`); + process.exit(1); + } +} + console.log(`${chalk.green('success')} Dependency postinstall check passed.`); |