index.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. 'use strict'
  2. var vendors = require('./vendors.json')
  3. var env = process.env
  4. // Used for testing only
  5. Object.defineProperty(exports, '_vendors', {
  6. value: vendors.map(function (v) { return v.constant })
  7. })
  8. exports.name = null
  9. exports.isPR = null
  10. vendors.forEach(function (vendor) {
  11. var envs = Array.isArray(vendor.env) ? vendor.env : [vendor.env]
  12. var isCI = envs.every(function (obj) {
  13. return checkEnv(obj)
  14. })
  15. exports[vendor.constant] = isCI
  16. if (isCI) {
  17. exports.name = vendor.name
  18. switch (typeof vendor.pr) {
  19. case 'string':
  20. // "pr": "CIRRUS_PR"
  21. exports.isPR = !!env[vendor.pr]
  22. break
  23. case 'object':
  24. if ('env' in vendor.pr) {
  25. // "pr": { "env": "BUILDKITE_PULL_REQUEST", "ne": "false" }
  26. exports.isPR = vendor.pr.env in env && env[vendor.pr.env] !== vendor.pr.ne
  27. } else if ('any' in vendor.pr) {
  28. // "pr": { "any": ["ghprbPullId", "CHANGE_ID"] }
  29. exports.isPR = vendor.pr.any.some(function (key) {
  30. return !!env[key]
  31. })
  32. } else {
  33. // "pr": { "DRONE_BUILD_EVENT": "pull_request" }
  34. exports.isPR = checkEnv(vendor.pr)
  35. }
  36. break
  37. default:
  38. // PR detection not supported for this vendor
  39. exports.isPR = null
  40. }
  41. }
  42. })
  43. exports.isCI = !!(
  44. env.CI || // Travis CI, CircleCI, Cirrus CI, Gitlab CI, Appveyor, CodeShip, dsari
  45. env.CONTINUOUS_INTEGRATION || // Travis CI, Cirrus CI
  46. env.BUILD_NUMBER || // Jenkins, TeamCity
  47. env.RUN_ID || // TaskCluster, dsari
  48. exports.name ||
  49. false
  50. )
  51. function checkEnv (obj) {
  52. if (typeof obj === 'string') return !!env[obj]
  53. return Object.keys(obj).every(function (k) {
  54. return env[k] === obj[k]
  55. })
  56. }