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 | |
parent | 2f990e3408d00cad473d8dcf8a4e49155cc3cc33 (diff) | |
download | gitlab-ce-51226-check-yarn-lock.tar.gz |
check yarn.lock for duplicate entries51226-check-yarn-lock
-rw-r--r-- | package.json | 1 | ||||
-rw-r--r-- | scripts/frontend/postinstall.js | 52 | ||||
-rw-r--r-- | yarn.lock | 4 |
3 files changed, 57 insertions, 0 deletions
diff --git a/package.json b/package.json index f7b5e84548b..58ac914c0e0 100644 --- a/package.json +++ b/package.json @@ -109,6 +109,7 @@ "xterm": "^3.5.0" }, "devDependencies": { + "@yarnpkg/lockfile": "^1.0.2", "axios-mock-adapter": "^1.15.0", "babel-eslint": "^8.2.3", "babel-plugin-istanbul": "^4.1.6", 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.`); diff --git a/yarn.lock b/yarn.lock index 97aef77cb56..b78ff8c3531 100644 --- a/yarn.lock +++ b/yarn.lock @@ -272,6 +272,10 @@ "@webassemblyjs/wast-parser" "1.5.13" long "^3.2.0" +"@yarnpkg/lockfile@^1.0.2": + version "1.0.2" + resolved "https://registry.yarnpkg.com/@yarnpkg/lockfile/-/lockfile-1.0.2.tgz#833d163680a151d2441a2489f5fe5fa87ac87726" + abbrev@1, abbrev@1.0.x: version "1.0.9" resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.0.9.tgz#91b4792588a7738c25f35dd6f63752a2f8776135" |