I am attempting to write a npm script which will run ava, and if everything passed, will run another (deploy) command.
How can you get the result of an ava test either in js, or piped to a file or following command?
The desired functionality would look like this:
npm run safe-deploy
Which would run ava and if everything passed (determined by either exit code or parsing the output) would run another cli command.
Has anyone done this before and if so what's the most suitable way to tackle it?
The easiest thing you can do is to use bash &&
operator. Please check out this answer to get more details.
It does exactly what you want: runs the second command only in case the first one is executed successfully.
So for example if you have two npm test
and npm run deploy
you can combine them with &&
to make a safe-deploy
command.
The deploy
command is going to to be executed only if tests are passing.
Here is a minimal package.json you can have:
{
"name": "ava-safe-deploy",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "ava",
"deploy": "echo 'deployed successfully!'",
"safe-deploy": "npm test && npm run deploy"
},
"license": "ISC",
"devDependencies": {
"ava": "^0.25.0"
}
}
In case tests are failing you'll see the regular ava
error message: