Reputation: 866
I have got a script which replaces data in the database and I want to run this script with npm command
package.json
"scripts": {
"database": "node devData/database.js --delete & node devData/database.js --import"
},
database.js
if (process.argv[2] === '--import') {
importData();
} else if (process.env[2] === '--delete') {
deleteData();
}
The key here is the &
I guess. With this solution only the first part of npm command
is recognized which is --delete
.
I changed npm command in package.json
to "node devData/database.js --delete ; node devData/database.js --import"
and then inside database.js
I'm running forEach
to check whether I've got --import
or --delete
process.argv.forEach(part => {
part === '--delete' ? deleteData() : part === '--import' ? importData() : '';
})
Solution works but I have to run it sometimes twice due to uploading same content to db
which gives me duplicate error.
Thanks for any advice.
Upvotes: 0
Views: 107
Reputation: 70075
You want to run one command and then, after it succeeds, run the second command, right? For that, use &&
which is the shell "and" operator. (Using a single &
instructs the shell to put the first command in the background.)
"scripts": {
"database": "node devData/database.js --delete && node devData/database.js --import"
},
With the &&
operator, the second command runs unless the first command exits with an error code.
Upvotes: 1