verify-compatibility-readmes.mjs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { readFile } from 'node:fs/promises'
  2. import { dirname, join, resolve } from 'node:path'
  3. import { fileURLToPath } from 'node:url'
  4. import { loadEcosystemReleaseContext } from './lib/ecosystem-packages.mjs'
  5. const scriptDir = dirname(fileURLToPath(import.meta.url))
  6. const sourceRoot = resolve(scriptDir, '..')
  7. const { wrapperManifest, entries } = await loadEcosystemReleaseContext(sourceRoot)
  8. const releaseEntryByName = new Map(entries.map(entry => [entry.packageName, entry]))
  9. const compatibilityTargets = new Map()
  10. for (const wrapper of wrapperManifest.wrappers) {
  11. for (const historicalPackage of wrapper.historicalPackages) {
  12. compatibilityTargets.set(historicalPackage, wrapper.packageName)
  13. }
  14. }
  15. function assert(condition, message) {
  16. if (!condition) {
  17. throw new Error(message)
  18. }
  19. }
  20. function hasChineseMigrationHint(content, standardPackageName) {
  21. return (
  22. content.includes(standardPackageName) &&
  23. (content.includes('标准包名') || content.includes('新项目') || content.includes('优先使用'))
  24. )
  25. }
  26. function hasEnglishMigrationHint(content, standardPackageName) {
  27. const normalized = content.toLowerCase()
  28. return (
  29. content.includes(standardPackageName) &&
  30. (normalized.includes('standard package name') ||
  31. normalized.includes('new integrations') ||
  32. normalized.includes('prefer'))
  33. )
  34. }
  35. let checked = 0
  36. for (const [historicalPackage, standardPackage] of compatibilityTargets) {
  37. const entry = releaseEntryByName.get(historicalPackage)
  38. if (!entry) {
  39. continue
  40. }
  41. const readmePath = join(entry.absoluteDir, 'README.md')
  42. const readmeEnPath = join(entry.absoluteDir, 'README.en.md')
  43. const readme = await readFile(readmePath, 'utf8')
  44. const readmeEn = await readFile(readmeEnPath, 'utf8')
  45. assert(
  46. hasChineseMigrationHint(readme, standardPackage),
  47. `${historicalPackage} README.md must recommend the standard package ${standardPackage}`
  48. )
  49. assert(
  50. hasEnglishMigrationHint(readmeEn, standardPackage),
  51. `${historicalPackage} README.en.md must recommend the standard package ${standardPackage}`
  52. )
  53. checked += 1
  54. }
  55. console.log(`Verified ${checked} compatibility package README migration hints.`)