brat / services /run-playwright.js
emiogiwara's picture
update
41d0a4b
const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');
const { randomUUID } = require('crypto');
const executePlaywright = async (userCode) => {
return new Promise((resolve) => {
const scriptName = `playwright_script_${randomUUID()}.mjs`;
const scriptPath = path.join(__dirname, scriptName);
// Simpan kode ke file sementara
fs.writeFileSync(scriptPath, userCode);
// Jalankan kode dengan node
exec(`node ${scriptPath}`, { timeout: 30000 }, (error, stdout, stderr) => {
// Hapus file setelah eksekusi
fs.unlinkSync(scriptPath);
if (error) {
return resolve({ success: false, error: stderr || error.message });
}
resolve({ success: true, output: stdout });
});
});
};
module.exports = { executePlaywright };