update-contributors.mjs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env node
  2. import { spawn } from 'node:child_process'
  3. import fs from 'node:fs/promises'
  4. const README_FILE_NAME = new URL('../README.md', import.meta.url)
  5. const readme = await fs.open(README_FILE_NAME, 'r+')
  6. const readmeContent = await readme.readFile()
  7. const githubcontrib = spawn('npx', [
  8. 'githubcontrib',
  9. '--owner', 'transloadit',
  10. '--repo', 'uppy',
  11. '--cols', '6',
  12. '--format', 'md',
  13. '--showlogin', 'true',
  14. '--sortOrder', 'desc',
  15. ], {
  16. stdio: ['ignore', 'pipe', 'inherit'],
  17. })
  18. githubcontrib.on('error', console.error)
  19. // Detect start of contributors section.
  20. const START_TAG = Buffer.from('<!--contributors-->\n')
  21. const START_TAG_POSITION = readmeContent.indexOf(START_TAG) + START_TAG.byteLength
  22. let cursor = START_TAG_POSITION
  23. for await (const data of githubcontrib.stdout) {
  24. const { bytesWritten } = await readme.write(data.toString('utf-8'), cursor, 'utf-8')
  25. cursor += bytesWritten
  26. }
  27. if (cursor === START_TAG_POSITION) {
  28. console.log('Empty response from githubcontrib. GitHub’s rate limit?')
  29. await readme.close()
  30. process.exit(1)
  31. }
  32. // Write the end of the file.
  33. await readme.write(
  34. readmeContent,
  35. readmeContent.indexOf('<!--/contributors-->'),
  36. undefined,
  37. cursor,
  38. )
  39. await readme.close()