|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { fileURLToPath } from 'node:url'; |
| 4 | +import { describe, expect, it } from 'vitest'; |
| 5 | + |
| 6 | +const __filename = fileURLToPath(import.meta.url); |
| 7 | +const __dirname = path.dirname(__filename); |
| 8 | + |
| 9 | +const recommendedAddonsPath = path.resolve( |
| 10 | + __dirname, |
| 11 | + '../../..', |
| 12 | + 'packages/volto-light-theme/recommendedAddons.json', |
| 13 | +); |
| 14 | +const mrsDeveloperPath = path.resolve( |
| 15 | + __dirname, |
| 16 | + '../../..', |
| 17 | + 'mrs.developer.json', |
| 18 | +); |
| 19 | +const intranetPackagePath = path.resolve(__dirname, '..', 'package.json'); |
| 20 | + |
| 21 | +const readJSON = <T>(filePath: string): T => |
| 22 | + JSON.parse(fs.readFileSync(filePath, 'utf-8')); |
| 23 | + |
| 24 | +const normalizeVersion = (version: unknown) => |
| 25 | + typeof version === 'string' ? version.replace(/^[\\^~]/, '') : version; |
| 26 | + |
| 27 | +const excludedPackages = new Set([ |
| 28 | + '@eeacms/volto-accordion-block', |
| 29 | + '@plonegovbr/volto-social-media', |
| 30 | +]); |
| 31 | + |
| 32 | +describe('internal checks', () => { |
| 33 | + it('keeps recommended add-ons in sync with mrs.developer.json and intranet package.json', () => { |
| 34 | + const recommendedAddons = readJSON<Record<string, string>>( |
| 35 | + recommendedAddonsPath, |
| 36 | + ); |
| 37 | + const mrsDeveloper = readJSON<Record<string, unknown>>(mrsDeveloperPath); |
| 38 | + const intranetPackage = readJSON<{ |
| 39 | + dependencies?: Record<string, string>; |
| 40 | + addons?: string[]; |
| 41 | + }>(intranetPackagePath); |
| 42 | + |
| 43 | + const mrsByPackage = Object.entries(mrsDeveloper).reduce< |
| 44 | + Record< |
| 45 | + string, |
| 46 | + Record<string, unknown> & { configKey: string; package?: string } |
| 47 | + > |
| 48 | + >((acc, [configKey, configValue]) => { |
| 49 | + if ( |
| 50 | + configValue && |
| 51 | + typeof configValue === 'object' && |
| 52 | + 'package' in configValue |
| 53 | + ) { |
| 54 | + acc[String((configValue as Record<string, unknown>).package)] = { |
| 55 | + ...(configValue as Record<string, unknown>), |
| 56 | + configKey, |
| 57 | + }; |
| 58 | + } |
| 59 | + return acc; |
| 60 | + }, {}); |
| 61 | + |
| 62 | + const issues: string[] = []; |
| 63 | + const intranetDependencies = intranetPackage.dependencies || {}; |
| 64 | + const intranetAddons = intranetPackage.addons || []; |
| 65 | + |
| 66 | + Object.entries(recommendedAddons).forEach( |
| 67 | + ([packageName, recommendedVersion]) => { |
| 68 | + if (excludedPackages.has(packageName)) return; |
| 69 | + |
| 70 | + const mrsEntry = mrsByPackage[packageName]; |
| 71 | + |
| 72 | + if (!mrsEntry) { |
| 73 | + issues.push( |
| 74 | + `${packageName} is listed in recommendedAddons.json but missing from frontend/mrs.developer.json.`, |
| 75 | + ); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + const mrsVersion = |
| 80 | + (mrsEntry.tag as string | undefined) || |
| 81 | + (mrsEntry.branch as string | undefined) || |
| 82 | + (mrsEntry.version as string | undefined); |
| 83 | + |
| 84 | + if (!mrsVersion) { |
| 85 | + issues.push( |
| 86 | + `${packageName} (${mrsEntry.configKey}) has no tag, branch, or version specified in frontend/mrs.developer.json.`, |
| 87 | + ); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + const normalizedRecommended = normalizeVersion(recommendedVersion); |
| 92 | + const normalizedMrsVersion = normalizeVersion(mrsVersion); |
| 93 | + |
| 94 | + if ( |
| 95 | + recommendedVersion !== mrsVersion && |
| 96 | + normalizedRecommended !== normalizedMrsVersion |
| 97 | + ) { |
| 98 | + issues.push( |
| 99 | + `${packageName} differs: recommendedAddons.json=${recommendedVersion} vs frontend/mrs.developer.json(${mrsEntry.configKey})=${mrsVersion}.`, |
| 100 | + ); |
| 101 | + } |
| 102 | + }, |
| 103 | + ); |
| 104 | + |
| 105 | + Object.entries(recommendedAddons).forEach(([packageName]) => { |
| 106 | + if (excludedPackages.has(packageName)) return; |
| 107 | + |
| 108 | + const dependencyVersion = intranetDependencies[packageName]; |
| 109 | + |
| 110 | + if (!dependencyVersion) { |
| 111 | + issues.push( |
| 112 | + `${packageName} is listed in recommendedAddons.json but missing from frontend/packages/kitconcept-website/package.json dependencies.`, |
| 113 | + ); |
| 114 | + return; |
| 115 | + } |
| 116 | + |
| 117 | + if (dependencyVersion !== 'workspace:*') { |
| 118 | + issues.push( |
| 119 | + `${packageName} is pinned to a specific version in frontend/packages/kitconcept-website/package.json dependencies; expected 'workspace:*'.`, |
| 120 | + ); |
| 121 | + } |
| 122 | + }); |
| 123 | + |
| 124 | + Object.keys(recommendedAddons).forEach((packageName) => { |
| 125 | + if (excludedPackages.has(packageName)) return; |
| 126 | + |
| 127 | + if (!intranetAddons.includes(packageName)) { |
| 128 | + issues.push( |
| 129 | + `${packageName} is listed in recommendedAddons.json but missing from frontend/packages/kitconcept-website/package.json addons.`, |
| 130 | + ); |
| 131 | + } |
| 132 | + }); |
| 133 | + |
| 134 | + expect(issues).toEqual([]); |
| 135 | + }); |
| 136 | +}); |
0 commit comments