update-contributors.mjs 1.3 KB

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