update-github-repo-topics.mjs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { readFile } from 'node:fs/promises'
  2. import { dirname, join, resolve } from 'node:path'
  3. import { spawnSync } from 'node:child_process'
  4. import { fileURLToPath } from 'node:url'
  5. const scriptDir = dirname(fileURLToPath(import.meta.url))
  6. const sourceRoot = resolve(scriptDir, '..')
  7. const args = process.argv.slice(2)
  8. const apply = args.includes('--apply')
  9. const keepExisting = args.includes('--keep-existing')
  10. const offline = args.includes('--offline')
  11. if (apply && offline) {
  12. throw new Error('Cannot apply topic updates with --offline because current GitHub topics must be read first.')
  13. }
  14. const desiredTopics = [
  15. 'file-viewer',
  16. 'document-viewer',
  17. 'document-preview',
  18. 'file-preview',
  19. 'office-viewer',
  20. 'pdf-viewer',
  21. 'docx',
  22. 'pptx',
  23. 'xlsx',
  24. 'cad-viewer',
  25. 'dwg',
  26. 'dxf',
  27. 'vue',
  28. 'react',
  29. 'typescript',
  30. 'web-components',
  31. 'wasm',
  32. 'offline-first',
  33. 'self-hosted',
  34. 'private-deployment'
  35. ]
  36. const readJson = async path => JSON.parse(await readFile(path, 'utf8'))
  37. const branchRoles = await readJson(join(sourceRoot, 'ecosystem', 'branch-roles.json'))
  38. const repoSlugFromUrl = url => {
  39. const match = String(url).match(/github\.com[:/]([^/]+\/[^/]+?)(?:\.git)?$/)
  40. if (!match) {
  41. throw new Error(`Unable to parse GitHub repository URL: ${url}`)
  42. }
  43. return match[1]
  44. }
  45. const repoArg = args.find(arg => arg.startsWith('--repo='))
  46. const repo = repoArg
  47. ? repoArg.slice('--repo='.length)
  48. : repoSlugFromUrl(branchRoles.publicMainRepository.github)
  49. const runGh = (commandArgs, { capture = false } = {}) => {
  50. const printable = `gh ${commandArgs.join(' ')}`
  51. const result = spawnSync('gh', commandArgs, {
  52. cwd: sourceRoot,
  53. encoding: 'utf8',
  54. stdio: capture ? 'pipe' : 'inherit'
  55. })
  56. if (result.status !== 0) {
  57. const details = [result.stderr, result.stdout].filter(Boolean).join('\n')
  58. throw new Error(`Command failed: ${printable}${details ? `\n${details}` : ''}`)
  59. }
  60. return result.stdout || ''
  61. }
  62. const readCurrentTopics = () => {
  63. if (offline) {
  64. return null
  65. }
  66. try {
  67. const currentOutput = runGh(
  68. ['repo', 'view', repo, '--json', 'repositoryTopics', '--jq', '.repositoryTopics[].name'],
  69. { capture: true }
  70. )
  71. return currentOutput
  72. .split('\n')
  73. .map(topic => topic.trim())
  74. .filter(Boolean)
  75. } catch (error) {
  76. if (apply) {
  77. throw error
  78. }
  79. console.warn(`Could not read current GitHub topics: ${error.message}`)
  80. return null
  81. }
  82. }
  83. const printPlan = currentTopics => {
  84. console.log(`Repository: ${repo}`)
  85. console.log(`Desired topics (${desiredTopics.length}): ${desiredTopics.join(', ')}`)
  86. if (currentTopics) {
  87. const currentSet = new Set(currentTopics)
  88. const desiredSet = new Set(desiredTopics)
  89. const toAdd = desiredTopics.filter(topic => !currentSet.has(topic))
  90. const toRemove = keepExisting ? [] : currentTopics.filter(topic => !desiredSet.has(topic))
  91. console.log(`Current topics (${currentTopics.length}): ${currentTopics.join(', ') || '(none)'}`)
  92. console.log(`Add: ${toAdd.join(', ') || '(none)'}`)
  93. console.log(`Remove: ${toRemove.join(', ') || '(none)'}`)
  94. }
  95. console.log('Run with --apply to update GitHub topics.')
  96. }
  97. if (!apply) {
  98. printPlan(readCurrentTopics())
  99. process.exit(0)
  100. }
  101. const currentTopics = readCurrentTopics()
  102. const currentSet = new Set(currentTopics)
  103. const desiredSet = new Set(desiredTopics)
  104. const topicsToAdd = desiredTopics.filter(topic => !currentSet.has(topic))
  105. const topicsToRemove = keepExisting ? [] : currentTopics.filter(topic => !desiredSet.has(topic))
  106. printPlan(currentTopics)
  107. if (!topicsToAdd.length && !topicsToRemove.length) {
  108. console.log('GitHub topics already match the desired discovery set.')
  109. process.exit(0)
  110. }
  111. const editArgs = ['repo', 'edit', repo]
  112. for (const topic of topicsToAdd) {
  113. editArgs.push('--add-topic', topic)
  114. }
  115. for (const topic of topicsToRemove) {
  116. editArgs.push('--remove-topic', topic)
  117. }
  118. runGh(editArgs)
  119. console.log(`Updated GitHub topics for ${repo}.`)