update-github-repo-descriptions.mjs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 dryRun = args.includes('--dry-run')
  9. const readJson = async path => JSON.parse(await readFile(path, 'utf8'))
  10. const ecosystemManifest = await readJson(join(sourceRoot, 'ecosystem', 'wrappers.json'))
  11. const branchRoles = await readJson(join(sourceRoot, 'ecosystem', 'branch-roles.json'))
  12. const repoSlugFromUrl = url => {
  13. const match = String(url).match(/github\.com[:/]([^/]+\/[^/]+?)(?:\.git)?$/)
  14. if (!match) {
  15. throw new Error(`Unable to parse GitHub repository URL: ${url}`)
  16. }
  17. return match[1]
  18. }
  19. const run = (command, commandArgs) => {
  20. const printable = `${command} ${commandArgs.join(' ')}`
  21. if (dryRun) {
  22. console.log(`[dry-run] ${printable}`)
  23. return
  24. }
  25. const result = spawnSync(command, commandArgs, {
  26. cwd: sourceRoot,
  27. encoding: 'utf8',
  28. stdio: 'inherit'
  29. })
  30. if (result.status !== 0) {
  31. throw new Error(`Command failed: ${printable}`)
  32. }
  33. }
  34. const records = [
  35. {
  36. github: branchRoles.publicMainRepository.github,
  37. description: branchRoles.publicMainRepository.description,
  38. homepage: 'https://file-viewer.app'
  39. },
  40. {
  41. github: ecosystemManifest.corePackage.github,
  42. description: ecosystemManifest.corePackage.description,
  43. homepage: 'https://doc.file-viewer.app'
  44. },
  45. ...(ecosystemManifest.renderers || []).map(renderer => ({
  46. github: renderer.github,
  47. description: renderer.description,
  48. homepage: 'https://doc.file-viewer.app'
  49. })),
  50. ...(ecosystemManifest.presets || []).map(preset => ({
  51. github: preset.github,
  52. description: preset.description,
  53. homepage: 'https://doc.file-viewer.app'
  54. })),
  55. ...ecosystemManifest.wrappers.map(component => ({
  56. github: component.github,
  57. description: component.description,
  58. homepage: 'https://doc.file-viewer.app'
  59. }))
  60. ]
  61. for (const record of records) {
  62. if (!record.description) {
  63. throw new Error(`Missing GitHub description for ${record.github}`)
  64. }
  65. const repo = repoSlugFromUrl(record.github)
  66. run('gh', [
  67. 'repo',
  68. 'edit',
  69. repo,
  70. '--description',
  71. record.description,
  72. '--homepage',
  73. record.homepage
  74. ])
  75. console.log(`Updated ${repo}: ${record.description}`)
  76. }
  77. console.log(`Updated ${records.length} GitHub repository description${records.length === 1 ? '' : 's'}.`)