require('dotenv').config();
const commandLineArgs = require('command-line-args');
const options = commandLineArgs([{
name: "username"
}, {
name: "password"
}]);
const utils = require('../server/utils');
const bcrypt = require('bcrypt');
if (!options.username) {
throw "Not enough args (Username Required)";
}
if (!options.password) {
throw "Not enough args (Password Required)";
}
bcrypt.hash(options.password, 10).then(function(hashword) {
return utils.query(
"INSERT INTO users (username, password)" +
"VALUES (?, ?) ON DUPLICATE KEY UPDATE password=VALUES(password)",
[options.username, hashword]
);
}).then(function(result) {
/**
* For INSERT...ON DUPLICATE KEY UPDATE statements,
* the affectedRows value per row is:
* 1 if the row is inserted as a new row,
* 2 if an existing row is updated, and
* 0 if an existing row is set to its current values
*/
if (result.affectedRows === 1) {
console.log("Created new user", options.username);
} else {
console.log("Updated existing user");
}
process.exit(0);
}).catch(function(error) {
console.log("Error creating user", error);
process.exit(1);
});