UNPKG

@cake-hub/cake-screenshot_diffs

Version:

A CAKE Screenshot diffing tool that includes a setup to comapre two given resources by screenshots taken from the available pages.

65 lines (54 loc) 2.03 kB
const { exec } = require ("child_process"); const path = require ("path"); class CommandLine { static get COMMAND_TIMEOUT_MS () { return 1000 * 30; // timeout when no output available for more than 30 sec } static initializeTimeout (resolveMethod, timeout = null) { if (timeout) { clearTimeout (timeout); } return setTimeout (() => { console.log ("CommandLine", "Command timed out!"); resolveMethod (); }, CommandLine.COMMAND_TIMEOUT_MS); } static execute (command, executionPath = path.resolve (__dirname, "../"), useTimeout = true) { return new Promise ((resolve, reject) => { const child = exec (command, { cwd: executionPath, stdio: ['pipe'], maxBuffer: 1024 * 1024 * 20, }); let timeout = null; if (useTimeout) { timeout = CommandLine.initializeTimeout (resolve); } console.log ("CommandLine", "Started executing " + executionPath, command); child.stdout.on('data', (data) => { if (!data.toString ('utf8')) { return; } console.log (executionPath, command, data.toString ('utf8')); if (useTimeout) { timeout = CommandLine.initializeTimeout (resolve, timeout); } }); child.on('exit', (code, signal) => { if (timeout) { clearTimeout (timeout); } console.log ("EXIT", command, code, signal); return resolve (code); }); child.on('error', (code) => { if (timeout) { clearTimeout (timeout); } console.log ("ERROR", command, code); return reject (code); }); }); } } module.exports = CommandLine;