how to run javascript function on linux terminal

rafaelrgl
rafaelrgl used Ask the Experts™
on
guys, here is the question. I have one function that optimize images using jimp package and I want to run this function on the command line of one server on digitalocean but i dont want to install lots of things on it. so I install npm and node, but I am very new to node and npm, so I dont know how to execute this javascript that will read the files and optimize images. I want to be able to do something like this on terminal:   execute optimize.js /var/www/html


here is the function I did:

function optimize(path){
    let Jimp     = require("jimp");
    let fs = Npm.require( 'fs' ),
    try { var files = fs.readdirSync(path); }
    catch(e) { console.log(e); }
    if (files.length > 0){
      for (var i = 0; i < files.length; i++) {
          //console.log(files[i]);
          var originalPath = path + '/original/' + files[i];
          var currentPath  = path + '/' + files[i];
          //copy img code here
          console.log('Processing 1. Copying bkp to folder: '+originalPath);
          fs.copyFileSync(currentPath, originalPath);
          //remove current file code here
          console.log('Processing 2. Removing current file from folder: '+currentPath);
          fs.unlinkSync(currentPath);
          //optimize image.
          Jimp.read(originalPath, function (err, lenna) {
                  console.log('Processing 3. Starting Optimizing from: '+originalPath);
                  console.log('Processing 4. Starting Optimizing to:   '+currentPath);
                  if (err) {
                      console.log('ERROR:'+err);
                  }else{
                      lenna.resize(828, Jimp.AUTO)    // resize width but keep height auto so we do not deforme image
                      .quality(55)                    // set JPEG quality
                      .write(currentPath);            // save
                      console.log('Processing 5. Finished Optimizing');
                  }
              });

      }
    }
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
nociSoftware Engineer
Distinguished Expert 2018

Commented:
There is a tool called node-js,  https://nodejs.org/
 that you can use.
It provides a complete environment to run javascript apps. npm can be used to install packages.

Commented:
This function should lie in a Javascript file, let us call it optimize.js. This Javascript file sould live in a directory, say optimize. Then you should install the required modules with npm. For "fs" you don't need to do anything, but for "jimp" you need to do "npm install jimp --save".
Then to be able to process the arguments from the command line you'll need to install a command line handler and write a function which calls that and then calls your routine. I suggest something like this

process.argv.forEach(function (val, index, array) {
  console.log(index + ': ' + val);
  if(!val.startsWith('-')) optimze(val);
});

so that you can use arguments starting with a minus sign as flags for various options.

The start the program with "node optimize.js /var/images"

HTH

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial