JavaScript everywhere one language to rule them all
We have JavaScript on the Browser, Mobile devices, Server, and now in the shell ;) what else can we ask for. JavaScript in our dreams? Maybe not but I really do love having one language to do all my work in. Not to long ago, I had to master a minimum of four languages to be productive. I would need to write my code in C#, JavaScript, PowerShell, SQL or PHP, JavaScript, BASH, SQL you get the point. Now with NodeJS I just need JavaScript to do it all. A few days ago, I needed to write a script that would create an account in the database, encrypt the password and link up all the permissions for that user across a few different tables. Then it would update some HTML based on rows added by the newly created user and push the code to the production server. It was easy as pie and came with all the benefits that the same code could run on my web server, command line, or cron job because its just a nodeJS package. In other words, its all JavaScript. I did not have to struggle to remember the crazy commands to do the work in BASH looking up man pages on sed/awk that I only use 4 times a year. It just is great to be able to use all the same tools you use every day. The same SQL client libraries, parsing tools, etc.
Writing a node shell script is easy it is no different from a Bash, Perl, or Python script. You just start the script with #!/usr/bin/env node and away you go! One hard thing with writing shell scripts is handling argument parsing and doing all the cool term outputs like progress bars, etc. You can just use packages like CLI or other NodeJS argument parsers to do all the heavy lifting so you can focus on writing code. I have started creating node shell script to do little tasks that I did manually. I am in love with my new workflow.
#!/usr/bin/env node
var cli = require('cli')
, crypto = require("crypto")
, dbclient = require('mysql').Client
, db = new dbclient()
, clId;
var options = cli.parse({
host: ['host', 'DB Host', 'string', '0.0.0.0']
, db: ['db', 'DB Database', 'string', 'database']
, dbuser: ['dbu', 'DB username', 'string', 'admin']
, dbpass: ['dbp', 'DB password', 'string', 'hummmmmm']
, username: ['u', 'New Username', 'string']
, password: ['p', 'Password', 'string']
});
var md5 = crypto.createHash('md5')
.update(options.password).digest('hex');
db.user = options.dbuser;
db.password = options.dbpass;
db.host = options.host;
db.database = options.db;
db.connect();
db.query('INSERT INTO tableA (foo) VALUES(?)',
[options.username], function(err, r) {
if(err) return cli.fatal(err.message);
cli.ok('INSERT INTO tableA >> ' +
(clId = r.insertId));
db.query('INSERT INTO login (uid,pwd,clId) VALUES(?, ?, ?)',
[options.username, md5, clId], function(err, r) {
if(err) return cli.fatal(err.message);
cli.ok('INSERT INTO login >> ' + options.username +
','+ md5 +','+ clId);
// do other crazy work
db.end();
});
});
JavaScript feels very good in a shell script with node supporting a REPL (Read Eval Print Loop) and its interpreted language it is a perfect fit. C# has PowerShell and Java has BeanShell but I just like the feel of nodeJS because the language is just a better fit.
1 Notes/ Hide
-
demetriusj posted this