update-contributors.mjs 1.2 KB

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