tdpw
Version:
CLI tool for uploading Playwright test reports to TestDino platform with Azure storage support
218 lines • 7.23 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
NODE_ENV: zod_1.z.enum(['development', 'staging', 'production', 'test']).optional(),
// API configuration
TESTDINO_API_URL: zod_1.z.string().url().optional(),
TESTDINO_TOKEN: zod_1.z.string().optional(),
// Upload options
TESTDINO_UPLOAD_HTML: zod_1.z.string().optional(),
TESTDINO_UPLOAD_TRACES: zod_1.z.string().optional(),
TESTDINO_VERBOSE: zod_1.z.string().optional(),
// Logging configuration
LOG_LEVEL: zod_1.z.enum(['error', 'warn', 'info', 'debug', 'silent']).optional(),
// CI/CD detection variables
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(),
// Runtime configuration
TESTDINO_TIMEOUT: zod_1.z.string().optional(),
TESTDINO_RETRY_COUNT: zod_1.z.string().optional(),
TESTDINO_MAX_FILE_SIZE: 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.NODE_ENV) {
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 boolean value from environment variable
*/
static getBooleanEnv(key, defaultValue = false) {
const env = this.getEnvironment();
const value = env[key];
return value ? (0, types_1.stringToBoolean)(value) : defaultValue;
}
/**
* Get numeric value from environment variable
*/
static getNumberEnv(key, defaultValue) {
const env = this.getEnvironment();
const value = env[key];
if (!value)
return defaultValue;
const parsed = parseInt(value, 10);
if (isNaN(parsed)) {
throw new types_1.ConfigurationError(`Environment variable ${key} must be a valid number, got: ${value}`);
}
return parsed;
}
/**
* Get string value from environment variable
*/
static getStringEnv(key, defaultValue) {
const env = this.getEnvironment();
return env[key] || defaultValue;
}
/**
* Validate required environment variables
*/
static validateRequired(requiredVars) {
const env = this.getEnvironment();
const missing = [];
for (const varName of requiredVars) {
if (!env[varName]) {
missing.push(varName);
}
}
if (missing.length > 0) {
throw new types_1.ConfigurationError(`Missing required environment variables: ${missing.join(', ')}`);
}
}
/**
* Get environment summary for debugging
*/
static getEnvironmentSummary() {
const env = this.getEnvironment();
return {
type: this.detectEnvironmentType(),
ci: this.isCI(),
provider: this.detectCIProvider(),
nodeEnv: env.NODE_ENV,
logLevel: env.LOG_LEVEL || 'info',
hasToken: !!env.TESTDINO_TOKEN,
hasApiUrl: !!env.TESTDINO_API_URL,
uploadHtml: this.getBooleanEnv('TESTDINO_UPLOAD_HTML'),
uploadTraces: this.getBooleanEnv('TESTDINO_UPLOAD_TRACES'),
verbose: this.getBooleanEnv('TESTDINO_VERBOSE'),
};
}
}
exports.EnvironmentUtils = EnvironmentUtils;
//# sourceMappingURL=env.js.map