11cb0ef41Sopenharmony_ci'use strict'
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ciconst vendors = require('./vendors.json')
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ciconst env = process.env
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_ci// Used for testing only
81cb0ef41Sopenharmony_ciObject.defineProperty(exports, '_vendors', {
91cb0ef41Sopenharmony_ci  value: vendors.map(function (v) {
101cb0ef41Sopenharmony_ci    return v.constant
111cb0ef41Sopenharmony_ci  })
121cb0ef41Sopenharmony_ci})
131cb0ef41Sopenharmony_ci
141cb0ef41Sopenharmony_ciexports.name = null
151cb0ef41Sopenharmony_ciexports.isPR = null
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_civendors.forEach(function (vendor) {
181cb0ef41Sopenharmony_ci  const envs = Array.isArray(vendor.env) ? vendor.env : [vendor.env]
191cb0ef41Sopenharmony_ci  const isCI = envs.every(function (obj) {
201cb0ef41Sopenharmony_ci    return checkEnv(obj)
211cb0ef41Sopenharmony_ci  })
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ci  exports[vendor.constant] = isCI
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_ci  if (!isCI) {
261cb0ef41Sopenharmony_ci    return
271cb0ef41Sopenharmony_ci  }
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci  exports.name = vendor.name
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci  switch (typeof vendor.pr) {
321cb0ef41Sopenharmony_ci    case 'string':
331cb0ef41Sopenharmony_ci      // "pr": "CIRRUS_PR"
341cb0ef41Sopenharmony_ci      exports.isPR = !!env[vendor.pr]
351cb0ef41Sopenharmony_ci      break
361cb0ef41Sopenharmony_ci    case 'object':
371cb0ef41Sopenharmony_ci      if ('env' in vendor.pr) {
381cb0ef41Sopenharmony_ci        // "pr": { "env": "BUILDKITE_PULL_REQUEST", "ne": "false" }
391cb0ef41Sopenharmony_ci        exports.isPR = vendor.pr.env in env && env[vendor.pr.env] !== vendor.pr.ne
401cb0ef41Sopenharmony_ci      } else if ('any' in vendor.pr) {
411cb0ef41Sopenharmony_ci        // "pr": { "any": ["ghprbPullId", "CHANGE_ID"] }
421cb0ef41Sopenharmony_ci        exports.isPR = vendor.pr.any.some(function (key) {
431cb0ef41Sopenharmony_ci          return !!env[key]
441cb0ef41Sopenharmony_ci        })
451cb0ef41Sopenharmony_ci      } else {
461cb0ef41Sopenharmony_ci        // "pr": { "DRONE_BUILD_EVENT": "pull_request" }
471cb0ef41Sopenharmony_ci        exports.isPR = checkEnv(vendor.pr)
481cb0ef41Sopenharmony_ci      }
491cb0ef41Sopenharmony_ci      break
501cb0ef41Sopenharmony_ci    default:
511cb0ef41Sopenharmony_ci      // PR detection not supported for this vendor
521cb0ef41Sopenharmony_ci      exports.isPR = null
531cb0ef41Sopenharmony_ci  }
541cb0ef41Sopenharmony_ci})
551cb0ef41Sopenharmony_ci
561cb0ef41Sopenharmony_ciexports.isCI = !!(
571cb0ef41Sopenharmony_ci  env.CI !== 'false' && // Bypass all checks if CI env is explicitly set to 'false'
581cb0ef41Sopenharmony_ci  (env.BUILD_ID || // Jenkins, Cloudbees
591cb0ef41Sopenharmony_ci  env.BUILD_NUMBER || // Jenkins, TeamCity
601cb0ef41Sopenharmony_ci  env.CI || // Travis CI, CircleCI, Cirrus CI, Gitlab CI, Appveyor, CodeShip, dsari
611cb0ef41Sopenharmony_ci  env.CI_APP_ID || // Appflow
621cb0ef41Sopenharmony_ci  env.CI_BUILD_ID || // Appflow
631cb0ef41Sopenharmony_ci  env.CI_BUILD_NUMBER || // Appflow
641cb0ef41Sopenharmony_ci  env.CI_NAME || // Codeship and others
651cb0ef41Sopenharmony_ci  env.CONTINUOUS_INTEGRATION || // Travis CI, Cirrus CI
661cb0ef41Sopenharmony_ci  env.RUN_ID || // TaskCluster, dsari
671cb0ef41Sopenharmony_ci  exports.name ||
681cb0ef41Sopenharmony_ci  false)
691cb0ef41Sopenharmony_ci)
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_cifunction checkEnv (obj) {
721cb0ef41Sopenharmony_ci  // "env": "CIRRUS"
731cb0ef41Sopenharmony_ci  if (typeof obj === 'string') return !!env[obj]
741cb0ef41Sopenharmony_ci
751cb0ef41Sopenharmony_ci  // "env": { "env": "NODE", "includes": "/app/.heroku/node/bin/node" }
761cb0ef41Sopenharmony_ci  if ('env' in obj) {
771cb0ef41Sopenharmony_ci    // Currently there are no other types, uncomment when there are
781cb0ef41Sopenharmony_ci    // if ('includes' in obj) {
791cb0ef41Sopenharmony_ci    return env[obj.env] && env[obj.env].includes(obj.includes)
801cb0ef41Sopenharmony_ci    // }
811cb0ef41Sopenharmony_ci  }
821cb0ef41Sopenharmony_ci  if ('any' in obj) {
831cb0ef41Sopenharmony_ci    return obj.any.some(function (k) {
841cb0ef41Sopenharmony_ci      return !!env[k]
851cb0ef41Sopenharmony_ci    })
861cb0ef41Sopenharmony_ci  }
871cb0ef41Sopenharmony_ci  return Object.keys(obj).every(function (k) {
881cb0ef41Sopenharmony_ci    return env[k] === obj[k]
891cb0ef41Sopenharmony_ci  })
901cb0ef41Sopenharmony_ci}
91