index.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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.BUILD_ID || // Jenkins, Cloudbees
  48. env.BUILD_NUMBER || // Jenkins, TeamCity
  49. env.CI || // Travis CI, CircleCI, Cirrus CI, Gitlab CI, Appveyor, CodeShip, dsari
  50. env.CI_APP_ID || // Appflow
  51. env.CI_BUILD_ID || // Appflow
  52. env.CI_BUILD_NUMBER || // Appflow
  53. env.CI_NAME || // Codeship and others
  54. env.CONTINUOUS_INTEGRATION || // Travis CI, Cirrus CI
  55. env.RUN_ID || // TaskCluster, dsari
  56. exports.name ||
  57. false
  58. )
  59. function checkEnv (obj) {
  60. // "env": "CIRRUS"
  61. if (typeof obj === 'string') return !!env[obj]
  62. // "env": { "env": "NODE", "includes": "/app/.heroku/node/bin/node" }
  63. if ('env' in obj) {
  64. // Currently there are no other types, uncomment when there are
  65. // if ('includes' in obj) {
  66. return env[obj.env] && env[obj.env].includes(obj.includes)
  67. // }
  68. }
  69. if ('any' in obj) {
  70. return obj.any.some(function (k) {
  71. return !!env[k]
  72. })
  73. }
  74. return Object.keys(obj).every(function (k) {
  75. return env[k] === obj[k]
  76. })
  77. }