|
| 1 | +const execa = require("execa"); |
| 2 | +const logger = require("./log"); |
| 3 | + |
| 4 | +const DEBUG_FLAG = "--debug"; |
| 5 | +const usingDebugFlag = process.argv.includes(DEBUG_FLAG); |
| 6 | + |
| 7 | +const SKIP_WEBINY_VERSIONS_CHECK_FLAG = "--no-package-versions-check"; |
| 8 | +const skippingWebinyVersionsCheck = process.argv.includes(SKIP_WEBINY_VERSIONS_CHECK_FLAG); |
| 9 | + |
| 10 | +function listWebinyPackageVersions() { |
| 11 | + const { stdout } = execa.sync("yarn", ["info", "@webiny/*", "--name-only", "--all", "--json"], { |
| 12 | + encoding: "utf-8" |
| 13 | + }); |
| 14 | + |
| 15 | + // Each line is a JSON string, so parse them individually |
| 16 | + const lines = stdout |
| 17 | + .trim() |
| 18 | + .split("\n") |
| 19 | + .map(line => JSON.parse(line)); |
| 20 | + |
| 21 | + const versionMap = new Map(); |
| 22 | + |
| 23 | + for (const entry of lines) { |
| 24 | + // An example entry: "@webiny/cli@npm:5.42.3" |
| 25 | + const match = entry.match(/^(@webiny\/[^@]+)@npm:(.+)$/); |
| 26 | + if (!match) { |
| 27 | + continue; |
| 28 | + } |
| 29 | + |
| 30 | + const [, pkg, version] = match; |
| 31 | + if (!versionMap.has(pkg)) { |
| 32 | + versionMap.set(pkg, new Set()); |
| 33 | + } |
| 34 | + |
| 35 | + versionMap.get(pkg).add(version); |
| 36 | + } |
| 37 | + |
| 38 | + return versionMap; |
| 39 | +} |
| 40 | + |
| 41 | +function ensureSameWebinyPackageVersions() { |
| 42 | + // Just in case, we want to allow users to skip the check. |
| 43 | + if (skippingWebinyVersionsCheck) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + let webinyVersions; |
| 48 | + try { |
| 49 | + webinyVersions = listWebinyPackageVersions(); |
| 50 | + } catch (e) { |
| 51 | + const message = ["Failed to inspect Webiny package versions."]; |
| 52 | + |
| 53 | + if (!usingDebugFlag) { |
| 54 | + message.push( |
| 55 | + `For more information, try running with ${logger.warning.hl(DEBUG_FLAG)}.` |
| 56 | + ); |
| 57 | + } |
| 58 | + |
| 59 | + message.push("Learn more: https://webiny.link/webiny-package-versions-check"); |
| 60 | + |
| 61 | + logger.warning(message.join(" ")); |
| 62 | + if (usingDebugFlag) { |
| 63 | + logger.debug(e); |
| 64 | + } |
| 65 | + |
| 66 | + console.log(); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + let hasMismatch = false; |
| 71 | + const mismatchedPackages = []; |
| 72 | + for (const [pkg, versions] of webinyVersions.entries()) { |
| 73 | + if (versions.size > 1) { |
| 74 | + hasMismatch = true; |
| 75 | + mismatchedPackages.push([pkg, versions]); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + if (hasMismatch) { |
| 80 | + const message = [ |
| 81 | + "The following Webiny packages have mismatched versions:", |
| 82 | + "", |
| 83 | + ...mismatchedPackages.map(([pkg, versions]) => { |
| 84 | + return `‣ ${pkg}: ${Array.from(versions).join(", ")}`; |
| 85 | + }), |
| 86 | + "", |
| 87 | + `Please ensure all Webiny packages are using the same version. If you think this is a mistake, you can also skip this check by appending the ${logger.error.hl( |
| 88 | + SKIP_WEBINY_VERSIONS_CHECK_FLAG |
| 89 | + )} flag. Learn more: https://webiny.link/webiny-package-versions-check` |
| 90 | + ]; |
| 91 | + |
| 92 | + logger.error(message.join("\n")); |
| 93 | + process.exit(1); |
| 94 | + } |
| 95 | + |
| 96 | + process.exit(0); |
| 97 | +} |
| 98 | + |
| 99 | +module.exports = { |
| 100 | + ensureSameWebinyPackageVersions |
| 101 | +}; |
0 commit comments