import { exec } from 'child_process';
function runCommand(command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject(new Error(`Command failed: ${command}\n${stderr}`));
} else {
resolve(stdout.trim());
}
});
});
}
(async () => {
try {
console.log('Installing Newman globally...');
const installOutput = await runCommand('npm install -g newman');
console.log('Installation output:', installOutput);
console.log('Checking Newman version...');
const versionOutput = await runCommand('newman -v');
console.log('Newman version:', versionOutput);
const versionRegex = /^\d+\.\d+\.\d+$/;
if (!versionRegex.test(versionOutput)) {
throw new Error('Newman version output is not a valid version string');
}
console.log('Test passed: Newman installed and version verified successfully.');
} catch (error) {
console.error('Test failed:', error.message);
process.exit(1);
}
})();This script uses Node.js child_process.exec to run shell commands asynchronously.
First, it runs npm install -g newman to install Newman globally. It waits for the command to finish and checks for errors.
Next, it runs newman -v to get the installed version. It verifies the output matches a version pattern like 1.2.3.
If any command fails or the version output is invalid, the script throws an error and exits with failure.
This approach ensures the installation and version check are automated and validated properly.