You Got A Question? Ask    GNOME Community!


Using NodeJS Scripting In GNOME

Node is good for doing Web Applications, Server/Client Applications, Standalone Applications ..Node is pretty much good for any kind of applications. The way I see it, Node in a year from now will be part of a default Linux distro installation, since there is a huge amount of applications written on top of Node.

So if everyone has Node installed, what if we use Node also for Linux scripting? Because there is a bit of a flame between Python and Node communities, I won’t go into whats better, I will just show how you can scripting with Node.


Installation

First we need to install Node. In Fedora

$ sudo yum install nodejs npm

However I strongly reckon to build it from sources.


Hello World

The typical “hello world” would be as simple as

1. Create a file

 $ gedit hello.js
console.log("Hello World!");

2. Run it

$ node hello.js
Hello World!

Lets make it more like a script.

1. Add +x

Add execution permission to hello.js

$ chmod +x hello.js

2. Set Env
Set the environment runtime.

#!/usr/bin/env node
console.log("Hello World!");

3. Run It

./hello.js
Hello World!

You can run external commands and grab the output, you can have keyboard inputs, well you can have everything. There is even a module that mimic Linux Shell in pure NodeJS. Two of the must have modules are FS-Extra (extra file system methods) and Async that adds control flow in NodeJS async nature.

What none can disagrees, is the fact that there is nothing so actively developed and used as NodeJS right now. If you are wondering about performance.. I’ve see, things from Node Community that work faster than C/C++. That doesn’t mean is faster, but it performs really good.


Reading A File

Lets read the PRETTY_NAME value in /etc/os-release file.

NAME=Fedora
VERSION="20 (Heisenbug)"
ID=fedora
VERSION_ID=20
PRETTY_NAME="Fedora 20 (Heisenbug)"
ANSI_COLOR="0;34"
CPE_NAME="cpe:/o:fedoraproject:fedora:20"
HOME_URL="https://fedoraproject.org/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_BUGZILLA_PRODUCT="Fedora"
REDHAT_BUGZILLA_PRODUCT_VERSION=20
REDHAT_SUPPORT_PRODUCT="Fedora"
REDHAT_SUPPORT_PRODUCT_VERSION=20

A simple script to extract the PRETTY_NAME would be:

#!/usr/bin/env node

var fs = require('fs'),
    prettyName ='PRETTY_NAME';

fs.readFile('/etc/os-release', 'utf8', function (e,data) {
  if (e) {
    return console.err(e);
  }
  var array = data.toString().split("\n");
    for(i in array) {
       if ( (array[i].indexOf(prettyName)) ===0)
        console.log(array[i].substring(12));     
    }
});

Output:

"Fedora 20 (Heisenbug)"

Help ;)

I want to add in Goops a function that will read user’s OS and GNOME info and will send the data to an online server. To get the OS version, I can read OS-RELEASE file, so if you can verify that PRETTY_NAME, NAME & VERSION_ID of your distro is similar to Fedora one, it would be very helpful.

To read the GNOME version we can parse /usr/share/gnome/gnome-version.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<gnome-version>
<platform>3</platform>
<minor>10</minor>
<micro>2</micro>
<distributor>GNOME.Org</distributor>
<date>2013-11-26</date>
</gnome-version>

Again if you can verify that all distros have this file, it would be much appreciated!


 
  We can't watch comments unless G+ provides an API or if you send a notification, e.g +World Of Gnome
     Sometimes is better to place your questions on GNOME Community