index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. 'use strict'
  2. const vendors = require('./vendors.json')
  3. const env = process.env
  4. // Used for testing only
  5. Object.defineProperty(exports, '_vendors', {
  6. value: vendors.map(function (v) {
  7. return v.constant
  8. })
  9. })
  10. exports.name = null
  11. exports.isPR = null
  12. vendors.forEach(function (vendor) {
  13. const envs = Array.isArray(vendor.env) ? vendor.env : [vendor.env]
  14. const isCI = envs.every(function (obj) {
  15. return checkEnv(obj)
  16. })
  17. exports[vendor.constant] = isCI
  18. if (!isCI) {
  19. return
  20. }
  21. exports.name = vendor.name
  22. switch (typeof vendor.pr) {
  23. case 'string':
  24. // "pr": "CIRRUS_PR"
  25. exports.isPR = !!env[vendor.pr]
  26. break
  27. case 'object':
  28. if ('env' in vendor.pr) {
  29. // "pr": { "env": "BUILDKITE_PULL_REQUEST", "ne": "false" }
  30. exports.isPR = vendor.pr.env in env && env[vendor.pr.env] !== vendor.pr.ne
  31. } else if ('any' in vendor.pr) {
  32. // "pr": { "any": ["ghprbPullId", "CHANGE_ID"] }
  33. exports.isPR = vendor.pr.any.some(function (key) {
  34. return !!env[key]
  35. })
  36. } else {
  37. // "pr": { "DRONE_BUILD_EVENT": "pull_request" }
  38. exports.isPR = checkEnv(vendor.pr)
  39. }
  40. break
  41. default:
  42. // PR detection not supported for this vendor
  43. exports.isPR = null
  44. }
  45. })
  46. exports.isCI = !!(
  47. env.CI !== 'false' && // Bypass all checks if CI env is explicitly set to 'false'
  48. (env.BUILD_ID || // Jenkins, Cloudbees
  49. env.BUILD_NUMBER || // Jenkins, TeamCity
  50. env.CI || // Travis CI, CircleCI, Cirrus CI, Gitlab CI, Appveyor, CodeShip, dsari
  51. env.CI_APP_ID || // Appflow
  52. env.CI_BUILD_ID || // Appflow
  53. env.CI_BUILD_NUMBER || // Appflow
  54. env.CI_NAME || // Codeship and others
  55. env.CONTINUOUS_INTEGRATION || // Travis CI, Cirrus CI
  56. env.RUN_ID || // TaskCluster, dsari
  57. exports.name ||
  58. false)
  59. )
  60. function checkEnv (obj) {
  61. // "env": "CIRRUS"
  62. if (typeof obj === 'string') return !!env[obj]
  63. // "env": { "env": "NODE", "includes": "/app/.heroku/node/bin/node" }
  64. if ('env' in obj) {
  65. // Currently there are no other types, uncomment when there are
  66. // if ('includes' in obj) {
  67. return env[obj.env] && env[obj.env].includes(obj.includes)
  68. // }
  69. }
  70. if ('any' in obj) {
  71. return obj.any.some(function (k) {
  72. return !!env[k]
  73. })
  74. }
  75. return Object.keys(obj).every(function (k) {
  76. return env[k] === obj[k]
  77. })
  78. }