tdpw
Version:
CLI tool for uploading Playwright test reports to TestDino platform with TestDino storage support
170 lines • 5.39 kB
JavaScript
;
/**
* Environment detection and validation utilities
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.EnvironmentUtils = exports.CIProvider = exports.EnvironmentType = exports.EnvVarSchema = void 0;
const zod_1 = require("zod");
const types_1 = require("../types");
/**
* Environment variable schema with validation
*/
exports.EnvVarSchema = zod_1.z.object({
// Core configuration - Only 3 supported environment variables
TESTDINO_RUNTIME: zod_1.z
.enum(['development', 'staging', 'production', 'test'])
.optional(),
TESTDINO_API_URL: zod_1.z.string().url().optional(),
TESTDINO_TOKEN: zod_1.z.string().optional(),
// CI/CD detection variables (auto-detected, not user-configurable)
CI: zod_1.z.string().optional(),
GITHUB_ACTIONS: zod_1.z.string().optional(),
GITLAB_CI: zod_1.z.string().optional(),
JENKINS_URL: zod_1.z.string().optional(),
AZURE_HTTP_USER_AGENT: zod_1.z.string().optional(),
CIRCLECI: zod_1.z.string().optional(),
});
/**
* Environment type detection
*/
var EnvironmentType;
(function (EnvironmentType) {
EnvironmentType["DEVELOPMENT"] = "development";
EnvironmentType["STAGING"] = "staging";
EnvironmentType["PRODUCTION"] = "production";
EnvironmentType["TEST"] = "test";
})(EnvironmentType || (exports.EnvironmentType = EnvironmentType = {}));
/**
* CI/CD provider detection
*/
var CIProvider;
(function (CIProvider) {
CIProvider["GITHUB_ACTIONS"] = "github-actions";
CIProvider["GITLAB_CI"] = "gitlab-ci";
CIProvider["JENKINS"] = "jenkins";
CIProvider["AZURE_DEVOPS"] = "azure-devops";
CIProvider["CIRCLECI"] = "circleci";
CIProvider["UNKNOWN"] = "unknown";
})(CIProvider || (exports.CIProvider = CIProvider = {}));
/**
* Environment utilities class
*/
class EnvironmentUtils {
static envCache = null;
/**
* Get and validate environment variables with caching
*/
static getEnvironment() {
if (this.envCache) {
return this.envCache;
}
try {
this.envCache = exports.EnvVarSchema.parse(process.env);
return this.envCache;
}
catch (error) {
if (error instanceof zod_1.z.ZodError) {
const issues = error.issues
.map(issue => `${issue.path.join('.')}: ${issue.message}`)
.join(', ');
throw new types_1.ConfigurationError(`Invalid environment variables: ${issues}`, error);
}
throw new types_1.ConfigurationError('Failed to validate environment', error);
}
}
/**
* Clear environment cache (useful for testing)
*/
static clearCache() {
this.envCache = null;
}
/**
* Detect current environment type
*/
static detectEnvironmentType() {
const env = this.getEnvironment();
switch (env.TESTDINO_RUNTIME) {
case 'development':
return EnvironmentType.DEVELOPMENT;
case 'staging':
return EnvironmentType.STAGING;
case 'production':
return EnvironmentType.PRODUCTION;
case 'test':
return EnvironmentType.TEST;
default:
// Default to production for safety
return EnvironmentType.PRODUCTION;
}
}
/**
* Detect CI/CD provider
*/
static detectCIProvider() {
const env = this.getEnvironment();
if (env.GITHUB_ACTIONS) {
return CIProvider.GITHUB_ACTIONS;
}
if (env.GITLAB_CI) {
return CIProvider.GITLAB_CI;
}
if (env.JENKINS_URL) {
return CIProvider.JENKINS;
}
if (env.AZURE_HTTP_USER_AGENT) {
return CIProvider.AZURE_DEVOPS;
}
if (env.CIRCLECI) {
return CIProvider.CIRCLECI;
}
return CIProvider.UNKNOWN;
}
/**
* Check if running in CI environment
*/
static isCI() {
const env = this.getEnvironment();
return ((0, types_1.stringToBoolean)(env.CI) || this.detectCIProvider() !== CIProvider.UNKNOWN);
}
/**
* Check if in development mode
*/
static isDevelopment() {
return this.detectEnvironmentType() === EnvironmentType.DEVELOPMENT;
}
/**
* Check if in production mode
*/
static isProduction() {
return this.detectEnvironmentType() === EnvironmentType.PRODUCTION;
}
/**
* Check if in test mode
*/
static isTest() {
return this.detectEnvironmentType() === EnvironmentType.TEST;
}
/**
* Get string value from environment variable
*/
static getStringEnv(key, defaultValue) {
const env = this.getEnvironment();
return env[key] || defaultValue;
}
/**
* Get environment summary for debugging
*/
static getEnvironmentSummary() {
const env = this.getEnvironment();
return {
type: this.detectEnvironmentType(),
ci: this.isCI(),
provider: this.detectCIProvider(),
runtime: env.TESTDINO_RUNTIME,
hasToken: !!env.TESTDINO_TOKEN,
hasApiUrl: !!env.TESTDINO_API_URL,
};
}
}
exports.EnvironmentUtils = EnvironmentUtils;
//# sourceMappingURL=env.js.map