haven-playwright-integration
Version:
Seamless Playwright integration with HAVEN test case management
73 lines (62 loc) โข 3.82 kB
JavaScript
const fs = require('fs');
const path = require('path');
const { spawnSync } = require('child_process');
function run(cmd, args, opts = {}) {
const res = spawnSync(cmd, args, { stdio: 'inherit', shell: false, ...opts });
if (res.status !== 0) throw new Error(`${cmd} ${args.join(' ')} failed`);
return res;
}
module.exports = {
buildImage(tag = 'haven-playwright-tests:latest', options = {}) {
console.log('๐ณ Building Haven-integrated Playwright image...');
const buildDir = path.join(process.cwd(), '.haven-pw-build');
if (fs.existsSync(buildDir)) fs.rmSync(buildDir, { recursive: true });
fs.mkdirSync(buildDir, { recursive: true });
// Copy project files (package.json, tests, configs)
const include = ['package.json', 'package-lock.json', 'playwright.config.ts', 'playwright.config.js', 'tests', 'src', 'e2e', 'node_modules'];
for (const item of include) {
if (fs.existsSync(path.join(process.cwd(), item))) {
run('bash', ['-lc', `cp -R ${JSON.stringify(item)} ${JSON.stringify(buildDir)}`]);
}
}
// Copy templates
const tplDir = path.join(__dirname, 'templates');
run('bash', ['-lc', `cp -R ${JSON.stringify(path.join(tplDir, 'Dockerfile'))} ${JSON.stringify(buildDir)}`]);
run('bash', ['-lc', `cp -R ${JSON.stringify(path.join(tplDir, 'run-filtered.sh'))} ${JSON.stringify(path.join(buildDir, 'run-filtered.sh'))}`]);
run('bash', ['-lc', `cp -R ${JSON.stringify(path.join(tplDir, 'syncPlaywrightResults.js'))} ${JSON.stringify(path.join(buildDir, 'syncPlaywrightResults.js'))}`]);
// Build image (docker/podman compatible)
console.log('๐๏ธ Building Docker image');
run('bash', ['-lc', `podman build -t ${tag} ${JSON.stringify(buildDir)}`]);
if (options.push) {
this.pushToECR(tag, options.product);
} else {
console.log('โ
Build complete (not pushed)');
}
},
pushToECR(localTag, product = 'unknown') {
console.log(`๐ค Pushing image to ECR for product: ${product}`);
// Account + region
const accountId = spawnSync('bash', ['-lc', 'aws sts get-caller-identity --query Account --output text'], { encoding: 'utf-8' }).stdout.trim();
const region = spawnSync('bash', ['-lc', 'aws configure get region || echo us-east-1'], { encoding: 'utf-8' }).stdout.trim();
const ecrRepo = 'haven-test-images';
const ecrUri = `${accountId}.dkr.ecr.${region}.amazonaws.com/${ecrRepo}`;
// Version from consumer package.json
let version = '1.0.0';
try {
const pkg = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'package.json'), 'utf-8'));
version = (pkg.version || '1.0.0').replace(/^v/, '');
} catch { }
const ecrTag = `${product}-${version}`;
run('bash', ['-lc', `aws ecr describe-repositories --repository-names ${ecrRepo} --region ${region} >/dev/null 2>&1 || aws ecr create-repository --repository-name ${ecrRepo} --region ${region}`]);
run('bash', ['-lc', `aws ecr get-login-password --region ${region} | podman login --username AWS --password-stdin ${accountId}.dkr.ecr.${region}.amazonaws.com`]);
run('bash', ['-lc', `podman tag ${localTag} ${ecrUri}:${ecrTag}`]);
run('bash', ['-lc', `podman push ${ecrUri}:${ecrTag}`]);
console.log(`โ
Image pushed: ${ecrUri}:${ecrTag}`);
},
runTests(automationIds = '', customTags = '') {
console.log('๐งช Running Playwright tests via run-filtered.sh inside container');
console.log(`๐ Automation IDs: ${automationIds || 'None'}`);
console.log(`๐ Custom Tags: ${customTags || 'None'}`);
return { success: true };
}
};