examples.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const pathResolve = require('path').resolve
  2. // load environment variables into process.env.*
  3. require('dotenv').config()
  4. const glob = require('glob')
  5. const proxyquire = require('proxyquire').noCallThru()
  6. const GitHubApi = require('../')
  7. const examplesPaths = glob.sync('*.js', {
  8. cwd: pathResolve(process.cwd(), 'examples')
  9. })
  10. examplesPaths.forEach(runExample)
  11. function runExample (name, i) {
  12. proxyquire(`../examples/${name}`, {
  13. '@octokit/rest': function (options) {
  14. if (!options) options = {}
  15. options.debug = false
  16. const github = new GitHubApi(options)
  17. // set a EXAMPLES_GITHUB_TOKEN environment variable to avoid
  18. // running against GitHub's rate limiting
  19. if (process.env.EXAMPLES_GITHUB_TOKEN) {
  20. github.authenticate({
  21. type: 'token',
  22. token: process.env.EXAMPLES_GITHUB_TOKEN
  23. })
  24. }
  25. return github
  26. }
  27. })
  28. }
  29. process.on('unhandledRejection', (error) => {
  30. if (error.code === 401) {
  31. // this is due to our invalid authentication token, so we ignore it
  32. return
  33. }
  34. if (error.code === 403) {
  35. // when API rate limit is reached 403 Forbidden is thrown
  36. return
  37. }
  38. if (/getaddrinfo ENOTFOUND github.my-ghe-enabled-company.com/.test(error.message)) {
  39. // expected error from enterpriseUploadAsset, ignore
  40. return
  41. }
  42. console.log(error)
  43. process.exit(1)
  44. })